Skip to content

fix(shot): align size tips with actual saved image dimensions#826

Merged
lzwind merged 1 commit intolinuxdeepin:masterfrom
dengzhongyuan365-dev:bug-fix-4-16
Apr 16, 2026
Merged

fix(shot): align size tips with actual saved image dimensions#826
lzwind merged 1 commit intolinuxdeepin:masterfrom
dengzhongyuan365-dev:bug-fix-4-16

Conversation

@dengzhongyuan365-dev
Copy link
Copy Markdown
Contributor

The selection size tooltip displayed recordWidth×DPR × recordHeight×DPR, but the actual crop applies -2 width and -1 height adjustments to avoid capturing the dashed selection border, causing a few pixels discrepancy between the displayed size and the saved image file info.

bug: https://pms.uniontech.com/bug-view-353039.html

The selection size tooltip displayed recordWidth×DPR × recordHeight×DPR,
but the actual crop applies -2 width and -1 height adjustments to avoid
capturing the dashed selection border, causing a few pixels discrepancy
between the displayed size and the saved image file info.

bug: https://pms.uniontech.com/bug-view-353039.html
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码修改主要涉及了在处理高DPI(设备像素比)屏幕时,对提示框尺寸计算的调整。以下是对该代码修改的审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的分析:

1. 语法逻辑分析

优点:

  • 修改后的逻辑引入了 qMax(..., 0),这是一个很好的防御性编程实践。它防止了当 size.width() - 2size.height() - 1 为负数时(即原始 size 极小),导致 static_cast<int> 转换为负数尺寸,从而引发潜在的 UI 渲染错误或崩溃。
  • 注释清晰地解释了 -2-1 的来源(避开选区虚线边框),这有助于代码维护。

潜在问题与建议:

  • 类型转换风险pixelRatioqreal(即 double),size.width()int。在 size.width() - 2 后再乘以 pixelRatio,如果 pixelRatio 非整数(如 1.25 或 1.5),结果可能会产生浮点数精度误差。虽然 static_cast<int> 会截断小数部分,但建议确认业务逻辑是否需要四舍五入(例如使用 qRound)而非直接截断。

    • 改进建议:如果业务需要更精确的像素对齐,可以考虑使用 qRound 替代 static_cast<int>
      int tipWidth = qMax(qRound((size.width() - 2) * pixelRatio), 0);
      int tipHeight = qMax(qRound((size.height() - 1) * pixelRatio), 0);
  • 边界条件未完全覆盖:虽然使用了 qMax(..., 0) 防止负数,但没有检查 size.width()size.height() 本身是否小于 2 或 1。如果 size.width() < 2size.width() - 2 会变成负数,乘以正数 pixelRatio 后仍为负数,然后被 qMax 修正为 0。虽然结果是安全的,但逻辑上可能不够直观。

    • 改进建议:如果业务允许,可以提前检查 size 的有效性:
       if (size.width() <= 2 || size.height() <= 1) {
           // 处理无效尺寸的情况,例如直接返回或设置默认尺寸
           return;
       }

2. 代码质量分析

优点:

  • 代码可读性较好,变量命名(tipWidthtipHeight)清晰。
  • 注释解释了修改的意图,符合良好的文档实践。

改进建议:

  • 魔法数字-2-1 是硬编码的"魔法数字",虽然注释解释了用途,但如果这些值在代码中多处使用,建议定义为常量:
    constexpr int BORDER_MARGIN_X = 2;
    constexpr int BORDER_MARGIN_Y = 1;
    int tipWidth = qMax(static_cast<int>((size.width() - BORDER_MARGIN_X) * pixelRatio), 0);
    int tipHeight = qMax(static_cast<int>((size.height() - BORDER_MARGIN_Y) * pixelRatio), 0);
  • 重复计算pixelRatio 是通过 qApp->primaryScreen()->devicePixelRatio() 获取的,如果此函数在短时间内频繁调用(例如在鼠标移动时),可以考虑缓存 pixelRatio 的值(假设屏幕设置不会在运行时频繁变化)。

3. 代码性能分析

  • 性能影响:修改后的代码引入了额外的减法运算和 qMax 调用,但这些操作的性能开销可以忽略不计。
  • 潜在优化:如果 updateTips 被高频调用(例如在拖动选区时),可以缓存 pixelRatio 的值,避免重复调用 devicePixelRatio()
    static qreal lastPixelRatio = -1.0;
    if (lastPixelRatio < 0) {
        lastPixelRatio = qApp->primaryScreen()->devicePixelRatio();
    }
    qreal pixelRatio = lastPixelRatio;
    (注意:此优化仅在屏幕设置不会动态变化时有效。)

4. 代码安全分析

  • 安全性:修改后的代码通过 qMax(..., 0) 防止了负数尺寸,避免了潜在的 UI 渲染错误或崩溃,安全性有所提升。
  • 潜在风险:如果 pixelRatio 异常(例如为 0 或负数,虽然理论上不可能),仍可能导致问题。可以增加对 pixelRatio 的检查:
    if (pixelRatio <= 0) {
        pixelRatio = 1.0; // 设置默认值
    }

5. 综合改进后的代码示例

结合以上分析,改进后的代码可能如下:

void TopTips::updateTips(QPoint pos, const QSize &size)
{
    // 检查尺寸有效性
    if (size.width() <= 2 || size.height() <= 1) {
        return; // 或设置默认尺寸
    }

    QPoint startPoint = pos;
    qreal pixelRatio = qApp->primaryScreen()->devicePixelRatio();
    
    // 检查 pixelRatio 有效性
    if (pixelRatio <= 0) {
        pixelRatio = 1.0;
    }

    // 定义边距常量
    constexpr int BORDER_MARGIN_X = 2;
    constexpr int BORDER_MARGIN_Y = 1;

    // 计算尺寸,使用 qRound 四舍五入
    int tipWidth = qMax(qRound((size.width() - BORDER_MARGIN_X) * pixelRatio), 0);
    int tipHeight = qMax(qRound((size.height() - BORDER_MARGIN_Y) * pixelRatio), 0);
    QSize s = QSize(tipWidth, tipHeight);

    setContent(s);
    startPoint.setX(pos.x());
}

总结

该代码修改在逻辑上是合理的,主要是为了与截图裁剪逻辑保持一致,并通过 qMax 提高了安全性。建议的改进点包括:

  1. 使用 qRound 替代 static_cast<int> 以更精确地处理浮点数。
  2. 提前检查 size 的有效性。
  3. 将魔法数字定义为常量。
  4. 考虑缓存 pixelRatio 以优化性能(如果适用)。
  5. 增加 pixelRatio 的有效性检查。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

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

@lzwind lzwind merged commit e24358f into linuxdeepin:master Apr 16, 2026
7 of 8 checks passed
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.

3 participants