Skip to content

fix(shot): dynamically calculate size tips dimensions based on font size#828

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:bug-fix-4-16
Apr 20, 2026
Merged

fix(shot): dynamically calculate size tips dimensions based on font size#828
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:bug-fix-4-16

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member

Use fontMetrics() to compute width and height instead of hardcoded setFixedSize(90, 30), ensuring size tips display correctly on different font sizes and screen scaling ratios (e.g., 125% scale with font size 20).

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

Use fontMetrics() to compute width and height instead of hardcoded
setFixedSize(90, 30), ensuring size tips display correctly on different
font sizes and screen scaling ratios (e.g., 125% scale with font size 20).

bug: https://pms.uniontech.com/bug-view-346697.html
@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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

这段代码的修改主要是为了让 TopTips 控件的大小能够根据文本内容动态调整,替代了之前硬编码的固定尺寸(90x30 和 500x30)。以下是对该 diff 的详细审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面:

1. 语法逻辑

  • 基本正确:代码逻辑上没有明显的语法错误。使用 fontMetrics() 计算文本宽度和高度是 Qt 中实现自适应大小的标准做法。
  • 计算逻辑wh 的计算公式(基于面积和比例)在逻辑上是为了保持 1920x1080 的比例,但在数学推导上略显复杂且可能存在精度问题。如果目标是保持 16:9 的比例,直接使用 size.width()size.height() 进行比例缩放会更直观。

2. 代码质量

  • 可读性:代码可读性良好,变量命名清晰。
  • 硬编码消除:移除了 setFixedSize(90, 30)setFixedSize(500, 30),这是一个很好的改进,增加了组件的灵活性。
  • 魔法数字:代码中仍然存在魔法数字 19201080。建议将其定义为类常量或宏,例如 const int kStandardWidth = 1920;,以提高可维护性。
  • 边界情况fontMetrics().boundingRect(text).height() 返回的是字体的 bounding box 高度,这通常比实际显示的文本高度要大(包含 ascent 和 descent)。如果 UI 要求非常精确的行高,可能需要调整(例如使用 height()leading())。

3. 代码性能

  • 计算开销fontMetrics().horizontalAdvance()fontMetrics().boundingRect() 涉及字体渲染计算,在 setContent 函数中被调用。如果此函数被极高频率地调用(例如在鼠标移动的 MouseMoveEvent 中),可能会造成轻微的性能开销。但在大多数 UI 更新场景下,这种开销是可以接受的。
  • 重复计算:在 ifelse 分支中,代码逻辑非常相似。可以考虑提取公共逻辑,减少重复代码。

4. 代码安全

  • 类型转换:使用了 static_cast<int> 进行浮点数到整数的转换,这是安全的,但要注意截断带来的精度损失。
  • 除零风险:计算 hw 时使用了除法 / size.width()/ size.height()。虽然 QSize 通常不为 0,但为了防御性编程,建议在除法前检查 size.width()size.height() 是否为 0,以防止潜在的除零崩溃。
  • 空指针/引用fontMetrics() 依赖于控件已正确构造并有字体设置,这在 setContent 被调用时通常是安全的。

改进建议代码

// 在类定义中添加常量
// static constexpr int kStandardWidth = 1920;
// static constexpr int kStandardHeight = 1080;

void TopTips::setContent(const QSize &size)
{
    // 防御性编程:检查尺寸有效性
    if (size.width() <= 0 || size.height() <= 0) {
        qCWarning(dsrApp) << "Invalid size provided to TopTips:" << size;
        return;
    }

    QString text = tr("Recording area: %1x%2").arg(size.width()).arg(size.height());
    QString displayText;

    if (m_showRecorderTips && size.width() * size.height() > kStandardWidth * kStandardHeight && 
        size.width() != m_width && size.height() != m_height) {
        
        // 建议:如果目标是保持 16:9,可以直接计算
        // int w = qMin(size.width(), kStandardWidth); 
        // int h = w * 9 / 16;
        // 或者保留原逻辑,但使用常量
        int h = static_cast<int>(sqrt(static_cast<double>(kStandardWidth) * kStandardHeight * size.height() / size.width()));
        int w = static_cast<int>(sqrt(static_cast<double>(kStandardWidth) * kStandardHeight * size.width() / size.height()));
        
        QString recorderTips = tr(" Adjust the recording area within %1*%2 to get better video effect");
        qCDebug(dsrApp) << "Setting content with recording area adjustment tip:" << w << "x" << h;
        displayText = text + recorderTips.arg(w).arg(h);
    } else {
        qCDebug(dsrApp) << "Setting content size:" << size;
        displayText = text;
    }

    setText(displayText);
    
    // 统一设置大小,减少重复代码
    // 使用 boundingRect 的 width() 可能比 horizontalAdvance 更准确处理换行,但单行文本 horizontalAdvance 性能更好
    // 这里增加一点 padding (例如 10px) 防止文字贴边
    int textWidth = fontMetrics().horizontalAdvance(displayText);
    int textHeight = fontMetrics().boundingRect(displayText).height();
    setFixedSize(textWidth + 10, textHeight); 
}

总结

该 diff 成功地将固定尺寸改为动态计算尺寸,是一个正向的改进。主要建议是:消除魔法数字、增加除零检查、以及提取重复的逻辑以优化代码结构。

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/merge

@deepin-bot

deepin-bot Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

This pr cannot be merged! (status: unstable)

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit e5f66dd into linuxdeepin:master Apr 20, 2026
9 of 10 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