Skip to content

fix: align folder popup to physical pixels for sharp 1px borders#749

Merged
Ivy233 merged 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/pixel-snapping-for-folder-popup
Apr 16, 2026
Merged

fix: align folder popup to physical pixels for sharp 1px borders#749
Ivy233 merged 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/pixel-snapping-for-folder-popup

Conversation

@Ivy233
Copy link
Copy Markdown
Contributor

@Ivy233 Ivy233 commented Apr 16, 2026

Apply pixel snapping to FolderGridViewPopup's position (x, y) and dimensions (width, height) to ensure edges align with physical pixel boundaries. This fixes blurry/anti-aliased 1px borders at fractional scaling factors like 125% or 150%.

修复:应用组弹窗物理像素对齐以获得锐利的1px描边

对 FolderGridViewPopup 的位置 (x, y) 和尺寸 (width, height) 应用 物理像素对齐,确保边缘与物理像素边界对齐。这修复了在 125% 或
150% 等非整数缩放比例下 1px 描边模糊/抗锯齿的问题。

PMS: BUG-306847

Summary by Sourcery

Bug Fixes:

  • Snap FolderGridViewPopup position and size to physical pixels to fix blurred 1px borders on non-integer scale factors.

Apply pixel snapping to FolderGridViewPopup's position (x, y) and
dimensions (width, height) to ensure edges align with physical pixel
boundaries. This fixes blurry/anti-aliased 1px borders at fractional
scaling factors like 125% or 150%.

修复:应用组弹窗物理像素对齐以获得锐利的1px描边

对 FolderGridViewPopup 的位置 (x, y) 和尺寸 (width, height) 应用
物理像素对齐,确保边缘与物理像素边界对齐。这修复了在 125% 或
150% 等非整数缩放比例下 1px 描边模糊/抗锯齿的问题。

PMS: BUG-306847
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 16, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR snaps the FolderGridViewPopup’s position and fixed dimensions to physical pixels by using the screen device pixel ratio, preventing blurry 1px borders at fractional display scaling factors.

File-Level Changes

Change Details Files
Apply device-pixel snapping to FolderGridViewPopup’s position so the popup is centered on whole physical pixels.
  • Import QtQuick.Window to access Screen.devicePixelRatio.
  • Introduce a dpr property on the popup bound to Screen.devicePixelRatio.
  • Replace raw center-based x/y calculations with Math.round-based expressions that snap the computed coordinates to the nearest device-pixel boundary.
qml/FolderGridViewPopup.qml
Ensure the popup’s fixed width and height are aligned to physical pixels in the windowed frame context.
  • Replace hard-coded numeric width/height with expressions that multiply by Screen.devicePixelRatio, round, then divide back to logical pixels.
  • Document via comments that this is to keep edges aligned to integer physical pixels across scaling factors.
qml/windowed/WindowedFrame.qml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In WindowedFrame.qml, overriding FolderGridViewPopup’s height with a hard-coded value means the existing logic that forces an even height to avoid the extra outer pixel is no longer used; consider either keeping that logic or incorporating the even-height constraint into the new DPR-aligned calculation.
  • The DPR-based rounding logic is duplicated (local dpr property in FolderGridViewPopup vs direct Screen.devicePixelRatio usage in WindowedFrame); consider extracting a shared helper or using a consistent pattern to avoid divergence if the snapping logic needs to change later.
  • Using Screen.devicePixelRatio assumes a single screen and may not reflect the actual window/screen the popup is on in multi-monitor setups; consider using the window’s screen DPR (or reacting to screen changes) if this component can be shown on different screens.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `WindowedFrame.qml`, overriding `FolderGridViewPopup`’s `height` with a hard-coded value means the existing logic that forces an even height to avoid the extra outer pixel is no longer used; consider either keeping that logic or incorporating the even-height constraint into the new DPR-aligned calculation.
- The DPR-based rounding logic is duplicated (local `dpr` property in `FolderGridViewPopup` vs direct `Screen.devicePixelRatio` usage in `WindowedFrame`); consider extracting a shared helper or using a consistent pattern to avoid divergence if the snapping logic needs to change later.
- Using `Screen.devicePixelRatio` assumes a single screen and may not reflect the actual window/screen the popup is on in multi-monitor setups; consider using the window’s screen DPR (or reacting to screen changes) if this component can be shown on different screens.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码的修改主要是为了解决在高DPI(设备像素比)屏幕下,Qt Quick窗口或Popup显示模糊、边缘不对齐的问题。这是一个常见的图形界面适配问题。以下是对这段代码的审查意见:

1. 语法逻辑

  • 逻辑正确性
    • 代码逻辑是正确的。通过 Screen.devicePixelRatio 获取当前的设备像素比(DPR),将逻辑像素转换为物理像素,进行四舍五入取整(Math.round),然后再转换回逻辑像素。
    • 公式 Math.round(value * dpr) / dpr 能够确保最终的逻辑像素值对应的物理像素是整数,从而避免亚像素渲染导致的模糊或半像素边框问题。
  • 依赖导入
    • FolderGridViewPopup.qml 中正确添加了 import QtQuick.Window 2.15,这是使用 Screen.devicePixelRatio 所必需的,没有遗漏。

2. 代码质量

  • 可读性
    • 添加了清晰的中文注释("获取当前屏幕的 DPR..." 和 "物理像素对齐..."),这有助于后续维护者理解为什么要进行这样的数学运算,代码质量较好。
  • 魔法数字
    • FolderGridViewPopup.qml 中,height 的计算公式里包含 130 这个魔法数字(/* title height*/)。虽然这不是本次修改引入的,但在代码审查中应指出,建议将其提取为一个具名属性(如 titleBarHeight),以便统一管理和修改。
  • 属性绑定
    • WindowedFrame.qml 中,widthheight 现在是绑定表达式。这意味着如果 Screen.devicePixelRatio 在运行时发生变化(例如用户拖动窗口到不同DPI的显示器,或者系统缩放比例改变),Popup的尺寸会自动更新。这是一个好的特性,符合QML的响应式编程模型。

3. 代码性能

  • 计算开销
    • Math.round 和基本的乘除法运算开销非常小,对性能几乎无影响。
    • Screen.devicePixelRatio 的读取通常也是高效的。
  • 绑定频繁触发
    • 潜在风险:在 WindowedFrame.qml 中,widthheight 直接绑定到了 Screen.devicePixelRatio。如果 Screen 对象频繁发出 devicePixelRatio 变化的信号(虽然在桌面端通常不频繁,但在某些特殊窗口管理器或拖拽场景下可能发生),会导致Popup频繁重新计算布局和重绘。
    • 建议:虽然通常不需要优化,但如果发现性能瓶颈,可以考虑在组件初始化时(Component.onCompleted)计算一次并赋值,而不是保持绑定。但对于这种静态尺寸的Popup,目前的写法是可以接受的。

4. 代码安全

  • 除零风险
    • dpr (即 Screen.devicePixelRatio) 在正常操作系统环境下通常大于等于 1.0。但在极端理论情况下或某些嵌入式特殊环境下,如果 dpr 为 0,会导致除以零的错误。
    • 建议:虽然概率极低,为了代码的健壮性,可以添加一个保护性检查:
      property real dpr: Screen.devicePixelRatio > 0 ? Screen.devicePixelRatio : 1.0
  • 初始化顺序
    • QML属性绑定依赖于组件的创建顺序。确保 FolderGridViewPopup 使用 Screen 时,Screen 已经可用。由于 Screen 是Qt Quick Window模块的单例对象,通常在导入后立即可用,此处风险较低。

总结与改进建议

这段代码修改有效地解决了高DPI下的显示对齐问题,逻辑清晰,注释到位。

改进建议:

  1. 增加健壮性检查
    FolderGridViewPopup.qml 中,建议对 dpr 做非零保护:

    property real dpr: Screen.devicePixelRatio > 0 ? Screen.devicePixelRatio : 1.0
  2. 消除魔法数字
    针对 FolderGridViewPopup.qml 中的 130,建议优化为:

    property int titleBarHeight: 130
    height: ((cs * 3) % 2 === 0 ? (cs * 3) : (cs * 3 + 1)) + titleBarHeight
  3. 考虑性能优化(可选)
    如果 WindowedFrame.qml 中的 Popup 尺寸在运行时不需要动态响应 DPI 变化(即用户不会在打开菜单时切换屏幕缩放),可以将绑定改为初始化时赋值,减少不必要的信号监听:

    // 在 WindowedFrame.qml 中
    Component.onCompleted: {
        let dpr = Screen.devicePixelRatio || 1.0;
        folderGridViewPopup.width = Math.round(370 * dpr) / dpr;
        folderGridViewPopup.height = Math.round(330 * dpr) / dpr;
    }

    注:保持原样(绑定方式)通常更符合QML风格,且能正确处理多显示器不同缩放比例的场景,因此这一点仅作为极端性能敏感场景下的备选方案。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, BLumia, Ivy233

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Ivy233 Ivy233 merged commit ed13ec8 into linuxdeepin:master Apr 16, 2026
11 checks passed
@Ivy233 Ivy233 deleted the fix/pixel-snapping-for-folder-popup branch April 16, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants