Skip to content

fix: include layout contentsMargins in placeholder text elision - #99

Open
mhduiy wants to merge 1 commit into
masterfrom
fix/pms-bug-351887
Open

mhduiy wants to merge 1 commit into
masterfrom
fix/pms-bug-351887

Conversation

@mhduiy

@mhduiy mhduiy commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

修复 PMS BUG-351887

问题

锁屏密码框错误提示文案过长使用省略形式时,仍与大写图标、加密图标、提示图标等重叠。

根因

DLineEditEx::paintEvent() 自定义绘制占位文本时,首次修复仅读取了 textMargins,但未包含 AuthPassword::initUI() 中 passwordLayout 的 contentsMargins(10, 0, 10, 0),导致右侧仍残留约 10px 区域使文本与图标重叠。

修复方案

  1. 在 paintEvent() 中,合并 textMargins + layout()->contentsMargins() 计算有效文本区域
  2. 在 setPlaceholderTextFont() 中,使用相同的有效宽度作为字体缩小阈值

影响范围

  • 仅影响输入为空且聚焦时的占位文本渲染
  • 正常文本输入/显示不受影响

关联

Summary by Sourcery

Ensure password-field placeholder text respects both text and layout margins when sizing and eliding content.

Bug Fixes:

  • Prevent long focused placeholder text from overlapping password-field status and action icons by accounting for all horizontal margins during elision.

Enhancements:

  • Align placeholder font-size fitting with the same effective text width used for rendering.

@sourcery-ai

sourcery-ai Bot commented Sep 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fix focused, empty password-field placeholder rendering by including internal layout contentsMargins alongside textMargins, keeping long error text from overlapping right-side icons and aligning font-size reduction with the elided drawing width.

Sequence diagram for corrected password placeholder rendering

sequenceDiagram
    participant DLineEditEx
    participant QLineEdit
    participant Layout
    participant QFontMetrics
    participant QPainter

    DLineEditEx->>QLineEdit: textMargins()
    DLineEditEx->>QLineEdit: layout()
    QLineEdit-->>DLineEditEx: Layout
    DLineEditEx->>Layout: contentsMargins()
    Layout-->>DLineEditEx: layoutMargins
    DLineEditEx->>QFontMetrics: elidedText(placeholderText, ElideRight, textRect.width())
    QFontMetrics-->>DLineEditEx: elidedText
    DLineEditEx->>QPainter: drawText(textRect, elidedText)
Loading

File-Level Changes

Change Details Files
Account for both QLineEdit text margins and the internal layout’s contents margins when determining placeholder text width.
  • Compute an effective available width before reducing the placeholder font.
  • Use the combined horizontal margins to build the elision and drawing rectangle.
  • Preserve the existing right-side elision and tooltip behavior while avoiding overlap with status and action icons.
src/widgets/dlineeditex.cpp

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

…cal centering

1. Root cause: first fix (BUG-351887) read textMargins but missed the
   passwordLayout contentsMargins(10,0,10,0) set in AuthPassword::initUI,
   leaving ~10px gap on the right where text still overlapped icons
2. Fix: in paintEvent(), combine textMargins + layout()->contentsMargins()
   to compute the effective text rect; in setPlaceholderTextFont(), use
   the same effective width as the font-shrink threshold
3. Additional fix: textRect y/height now uses leRect.y()/leRect.height()
   instead of rect().y()/rect().height(), so placeholder text is vertically
   centered within the actual QLineEdit geometry, matching user input text
4. Impact: only affects placeholder text rendering when input is empty
   and focused; normal text input/display unaffected

Log: 修复占位文本省略宽度未包含 layout contentsMargins 及垂直方向未使用 QLineEdit 几何导致与图标重叠及上下不居中

Influence:
1. 测试输入错误密码后密码框清空时占位提示文本不与图标重叠
2. 验证大写状态、密码显示、密码提示图标区域无文本覆盖
3. 测试无图标的 DLineEditEx 占位文本正常居中显示
4. 验证占位文本垂直居中与实际输入文本位置一致

PMS: BUG-351887
deepin-ci-robot added a commit to linuxdeepin/dde-session-shell-snipe that referenced this pull request Sep 29, 2026
Synchronize source files from linuxdeepin/dde-session-shell.

Source-pull-request: linuxdeepin/dde-session-shell#99
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 94 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 94 分,大于 70 分通过阈值,代码质量符合要求。本次修改正确修复了 BUG-351887 中锁屏密码框占位文本与图标重叠的问题,通过合并 textMargins 和 layout contentsMargins 计算有效文本区域,逻辑清晰,实现合理。

🔍 详细分析

1. 语法逻辑 ✅

评价: 良好 ✅ 通过

潜在问题:

  1. src/widgets/dlineeditex.cpp:89 - availWidth 可能为负数(textMargins + layoutMargins > width()),未显式处理边界情况
  2. src/widgets/dlineeditex.cpp:181 - textRect.width() 可能为负数(leftOffset + rightOffset > leRect.width()),elidedText 传入负宽度返回空字符串

建议: 建议在 availWidth 和 textRect.width() 计算后增加下限保护,如 availWidth = qMax(availWidth, 0),避免极端边距场景下的异常行为


2. 代码质量 ✅

评价: 良好 ✅ 通过

潜在问题:

  1. src/widgets/dlineeditex.cpp:84 - textMargins 和 layoutMargins 计算逻辑在 setPlaceholderTextFont() 和 paintEvent() 中重复,建议提取辅助方法

建议: 建议将 textMargins + layoutMargins 的计算逻辑提取为私有辅助方法(如 QPair<QMargins, QMargins> getMargins() const 或 int effectiveTextWidth() const),减少代码重复,提高可维护性


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 性能良好,资源使用合理。margin 计算为 O(1) 常量时间操作,对渲染性能无可感知影响


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 安全合规,无安全风险。代码仅涉及 Qt 控件渲染逻辑,无用户输入处理、无外部数据交互、无文件/网络/命令操作


💡 改进建议代码示例

// 建议提取辅助方法,减少重复代码并增加边界保护

// 在 DLineEditEx 类中添加私有辅助方法
QPair<int, int> DLineEditEx::getHorizontalMargins() const
{
    QMargins textMargins = lineEdit()->textMargins();
    QMargins layoutMargins(0, 0, 0, 0);
    if (auto *layout = lineEdit()->layout()) {
        layoutMargins = layout->contentsMargins();
    }
    int left = textMargins.left() + layoutMargins.left();
    int right = textMargins.right() + layoutMargins.right();
    return {left, right};
}

// setPlaceholderTextFont() 中使用
void DLineEditEx::setPlaceholderTextFont(const QFont &font)
{
    const QString &text = lineEdit()->placeholderText();
    QFont fontTmp = font;
    auto [left, right] = getHorizontalMargins();
    int availWidth = qMax(width() - left - right, 0);  // 增加下限保护
    while (QFontMetrics(fontTmp).boundingRect(text).width() > availWidth) {
        if (fontTmp.pointSize() <= 1) {
            qWarning() << "Password line edit font size" << font.pointSize() << fontTmp.pointSize();
            return;
        }
        fontTmp.setPointSize(fontTmp.pointSize() - 1);
    }
    setFont(fontTmp);
}

// paintEvent() 中使用
void DLineEditEx::paintEvent(QPaintEvent *event)
{
    // ...
    QRect leRect = lineEdit()->geometry();
    auto [leftOffset, rightOffset] = getHorizontalMargins();
    int textWidth = qMax(leRect.width() - leftOffset - rightOffset, 0);  // 增加下限保护
    QRect textRect(leRect.x() + leftOffset, leRect.y(), textWidth, leRect.height());
    QString elidedText = fm.elidedText(placeholderText, Qt::ElideRight, textWidth);
    pa.drawText(textRect, Qt::AlignCenter | Qt::TextSingleLine, elidedText);
    // ...
}

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy, yixinshark

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

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