Skip to content

fix(notification): improve combobox width calculation and styling#1449

Open
add-uos wants to merge 1 commit intolinuxdeepin:masterfrom
add-uos:master
Open

fix(notification): improve combobox width calculation and styling#1449
add-uos wants to merge 1 commit intolinuxdeepin:masterfrom
add-uos:master

Conversation

@add-uos
Copy link

@add-uos add-uos commented Feb 12, 2026

  • Replace fixed width with dynamic calculation based on text content using TextMetrics
  • Add custom DciIcon indicator for dropdown arrow with proper theming
  • Adjust padding values for better visual alignment
  • Constrain max width to 200px to prevent excessive expansion

log: improve combobox width calculation and styling
bug: PMS-350643

Summary by Sourcery

Improve the notification panel combobox by dynamically sizing it to its content and updating its visual styling and dropdown indicator.

Enhancements:

  • Dynamically calculate the combobox width based on displayed text while enforcing a maximum width constraint.
  • Update the combobox padding, alignment, and dropdown indicator icon for better visual consistency with theming.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos

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

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 12, 2026

Reviewer's Guide

Refines the notification panel combobox to size dynamically based on its text content, updates the dropdown indicator to a themed DciIcon, and adjusts padding for better alignment while capping maximum width.

Flow diagram for dynamic combobox width calculation and indicator layout

flowchart LR
  A[ComboBox_created] --> B[Compute_displayText]
  B --> C[TextMetrics_uses_comboBox_font_and_displayText]
  C --> D[Measure_text_width]
  D --> E[Add_padding_and_icon_space]
  E --> F[implicitWidth_equals_Min_200_sum]
  F --> G[Apply_Layout_maximumWidth_200]
  G --> H[Render_combobox_with_dynamic_width]
  H --> I[Position_DciIcon_indicator]
  I --> J[Indicator_x_comboBox_width_minus_icon_width_minus_rightPadding]
  I --> K[Indicator_y_centered_in_availableHeight]
  J --> L[Paint_indicator_with_palette_and_theme]
  K --> L[Paint_indicator_with_palette_and_theme]
  L --> M[User_sees_aligned_thematic_dropdown_arrow]
Loading

File-Level Changes

Change Details Files
Make the ComboBox width dynamically adapt to its text content with a maximum width cap.
  • Remove fixed implicitWidth and introduce TextMetrics bound to the ComboBox font and displayText.
  • Compute implicitWidth using measured text width plus padding, right padding, and icon space.
  • Limit the calculated width to a maximum of 200 pixels.
panels/notification/plugin/NotifyAction.qml
Customize ComboBox visuals for better alignment and theming.
  • Set ComboBox padding and rightPadding for tighter layout in the notification panel.
  • Add a DciIcon-based indicator positioned relative to the ComboBox width and padding, using design system icon size and palette, and proper control state/theme bindings.
  • Set padding to 0 on the NotifyActionButton delegate to align items visually with the updated ComboBox layout.
panels/notification/plugin/NotifyAction.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

@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 found 1 issue, and left some high level feedback:

  • The implicitWidth calculation uses a hard-coded 28 pixel constant for extra width; consider deriving this from existing style constants (e.g., icon size + margins) or at least documenting what this value represents for future maintainability.
  • You now constrain the width twice (Layout.maximumWidth: 200 and Math.min(200, ...) in implicitWidth); it would be simpler and less error-prone to centralize this limit in one place.
  • In the custom indicator you manually compute x using comboBox.width - width - comboBox.rightPadding; switching to anchors.right/anchors.rightMargin would be more idiomatic QML and less fragile if padding/layout changes later.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `implicitWidth` calculation uses a hard-coded `28` pixel constant for extra width; consider deriving this from existing style constants (e.g., icon size + margins) or at least documenting what this value represents for future maintainability.
- You now constrain the width twice (`Layout.maximumWidth: 200` and `Math.min(200, ...)` in `implicitWidth`); it would be simpler and less error-prone to centralize this limit in one place.
- In the custom `indicator` you manually compute `x` using `comboBox.width - width - comboBox.rightPadding`; switching to `anchors.right`/`anchors.rightMargin` would be more idiomatic QML and less fragile if padding/layout changes later.

## Individual Comments

### Comment 1
<location> `panels/notification/plugin/NotifyAction.qml:161-162` </location>
<code_context>
+                
+                implicitWidth: Math.min(200, textMetrics.width + comboBox.padding + comboBox.rightPadding + 28)
+                
+                indicator: DciIcon {
+                    x: comboBox.width - width - comboBox.rightPadding
+                    y: comboBox.topPadding + (comboBox.availableHeight - height) / 2
+                    sourceSize.width: DS.Style.comboBox.iconSize
</code_context>

<issue_to_address>
**issue:** Indicator positioning may not respect layout mirroring / RTL setups.

The manual `x` calculation (`comboBox.width - width - comboBox.rightPadding`) assumes a left‑to‑right layout and will misplace the indicator in RTL/mirrored setups. Prefer anchoring to `right` (or `left`, per design) with `anchors.rightMargin` so positioning automatically adapts to layout direction and stays maintainable.
</issue_to_address>

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.

Comment on lines +161 to +162
indicator: DciIcon {
x: comboBox.width - width - comboBox.rightPadding
Copy link

Choose a reason for hiding this comment

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

issue: Indicator positioning may not respect layout mirroring / RTL setups.

The manual x calculation (comboBox.width - width - comboBox.rightPadding) assumes a left‑to‑right layout and will misplace the indicator in RTL/mirrored setups. Prefer anchoring to right (or left, per design) with anchors.rightMargin so positioning automatically adapts to layout direction and stays maintainable.

- Replace fixed width with dynamic calculation based on text content using TextMetrics
- Add custom DciIcon indicator for dropdown arrow with proper theming
- Adjust padding values for better visual alignment
- Constrain max width to 200px to prevent excessive expansion

log: improve combobox width calculation and styling
bug: PMS-350643
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码是一个对 NotifyAction.qml 文件的修改,主要涉及 ComboBox 组件的样式和交互调整。以下是对该 diff 的详细审查意见,涵盖语法逻辑、代码质量、代码性能和代码安全四个方面:

1. 语法逻辑

  • 版权年份更新
    • SPDX-FileCopyrightText: 2024 -> SPDX-FileCopyrightText: 2026
    • 意见:通常版权年份应更新为当前年份(如 2024 或 2025)。除非这是一个针对未来版本的计划性提交,否则直接改为 2026 可能是一个笔误,建议核实。
  • 属性绑定与计算
    • implicitWidth: Math.min(200, textMetrics.width + comboBox.padding + comboBox.rightPadding + 28)
    • 意见:这里的逻辑是动态计算宽度,但 comboBox.padding 在上方被显式设置为 0。因此计算公式实际上变成了 textMetrics.width + rightPadding + 28。逻辑是通的,但建议直接使用数值或注释说明 padding 为 0 的意图,以免后续维护者困惑为何加了 padding 却没生效。
  • 布局对齐
    • indicatorx 坐标计算:x: comboBox.width - width - comboBox.rightPadding
    • 意见:逻辑正确,确保了图标紧贴右边界并保留了 rightPadding 的间距。

2. 代码质量

  • 魔法数字
    • implicitWidth 计算中的 28
    • 意见28 这个数字看起来是为了给指示器图标和文字留出空间。建议将其定义为常量(例如 indicatorWidthextraSpacing),以提高代码可读性和可维护性。
  • 命名规范
    • expandActions 命名清晰,但 comboBox 作为 ID 略显通用。考虑到这是在 NotifyAction 的上下文中,actionComboBox 可能更具描述性。
  • 组件复用与一致性
    • indicator 使用了 DciIcon 并配置了 D.ColorSelector
    • 意见:这看起来符合项目(可能是 DTK 或 Deepin 相关项目)的 UI 规范。代码结构清晰,属性设置完整。

3. 代码性能

  • TextMetrics 使用
    • 引入了 TextMetrics 来测量文本宽度。
    • 意见:这是一个很好的实践,比在 JS 中动态计算字体度量要高效。TextMetrics 会在文本变化时自动更新,开销较小。
  • 绑定频率
    • implicitWidth 依赖于 textMetrics.width
    • 意见:当 displayText 改变时会触发重绘和重计算,这是必要的。但在某些高频更新场景下,如果 displayText 变化极其频繁,可能需要考虑防抖,不过对于 ComboBox 这种交互组件,通常不需要担心。

4. 代码安全

  • 数组切片
    • property var expandActions: actions.slice(1)
    • 意见actions.slice(1) 创建了一个新数组,避免了直接修改外部传入的 actions 数组。这是一种安全的做法,防止了副作用。
  • 模型索引访问
    • delegate 中使用 actionData: expandActions[index]
    • 意见:由于 expandActionsslice 的结果,索引是安全的。只要 model 绑定正确,越界风险由 QML 引擎控制,通常没有问题。

改进建议总结

  1. 修正版权年份:确认是否应为当前年份。
  2. 消除魔法数字:将 28 提取为常量属性。
  3. 代码注释:建议在 implicitWidth 计算处添加注释,解释 28 的构成(例如:图标宽度 + 左右间距)。
  4. ID 命名:建议将 comboBox 改为更具上下文意义的名称,如 actionComboBox

优化后的代码片段示例

ComboBox {
    id: actionComboBox
    // ... 其他属性 ...

    readonly property int indicatorSpace: 28 // 图标和额外间距

    TextMetrics {
        id: textMetrics
        font: actionComboBox.font
        text: actionComboBox.displayText
    }
    
    // padding 显式设为0,宽度计算仅依赖 rightPadding
    implicitWidth: Math.min(200, textMetrics.width + actionComboBox.rightPadding + indicatorSpace)
    
    // ... 其他代码 ...
}

palette: DTK.makeIconPalette(comboBox.palette)
name: "arrow_ordinary_down"
mode: comboBox.D.ColorSelector.controlState
theme: comboBox.D.ColorSelector.controlTheme
Copy link
Contributor

Choose a reason for hiding this comment

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

comboBox.D.ColorSelector.controlTheme
这里不需要“D.”,这里没有这个别名,


implicitWidth: Math.min(200, textMetrics.width + comboBox.padding + comboBox.rightPadding + 28)

indicator: DciIcon {
Copy link
Contributor

Choose a reason for hiding this comment

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

dtk的样式不适用这里的么?我看bug单子上说样式不一样,主要是背景吧,这里是主要修指示器?

TextMetrics {
id: textMetrics
font: comboBox.font
text: comboBox.displayText
Copy link
Contributor

Choose a reason for hiding this comment

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

TextMetrics 可以设置最大显示宽度吧,

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