Skip to content

fix(slider): optimize tick label alignment to prevent text overflow#598

Open
add-uos wants to merge 1 commit intolinuxdeepin:masterfrom
add-uos:fix-slider-tip-alignment
Open

fix(slider): optimize tick label alignment to prevent text overflow#598
add-uos wants to merge 1 commit intolinuxdeepin:masterfrom
add-uos:fix-slider-tip-alignment

Conversation

@add-uos
Copy link
Copy Markdown
Contributor

@add-uos add-uos commented Apr 1, 2026

Add isFirst/isLast properties and effectiveHorizontalAlignment to align first/last tick labels outward (left for first, right for last), preventing text overflow at slider edges. Use negative margins to offset label padding for better visual alignment with tick marks. Add null safety checks and documentation comments.

为 SliderTipItem 添加首尾刻度标签特殊对齐逻辑,首刻度左对齐、尾刻度右对齐,
防止文本在滑块边缘溢出。使用负边距抵消标签 padding,使文字与刻度对齐。
添加空值安全检查和文档注释。

Log: 优化滑块刻度标签对齐,防止边缘文本溢出
PMS: https://pms.uniontech.com/bug-view-305223.html
Influence: TipsSlider 的首尾刻度标签现在会自动向外对齐,提升视觉效果并防止文本溢出。

Summary by Sourcery

Optimize slider tick label alignment to improve appearance and prevent text overflow at the track edges.

Enhancements:

  • Add first/last tick detection and use an effective horizontal alignment so edge tick labels align outward on horizontal sliders.
  • Adjust tick label anchor margins to visually align text with tick marks while compensating for label padding.
  • Update SliderTipItem copyright metadata years.

Add isFirst/isLast properties and effectiveHorizontalAlignment to align
first/last tick labels outward (left for first, right for last), preventing
text overflow at slider edges. Use negative margins to offset label padding
for better visual alignment with tick marks. Add null safety checks and
documentation comments.

为 SliderTipItem 添加首尾刻度标签特殊对齐逻辑,首刻度左对齐、尾刻度右对齐,
防止文本在滑块边缘溢出。使用负边距抵消标签 padding,使文字与刻度对齐。
添加空值安全检查和文档注释。

Log: 优化滑块刻度标签对齐,防止边缘文本溢出
PMS: BUG-305223
Influence: TipsSlider 的首尾刻度标签现在会自动向外对齐,提升视觉效果并防止文本溢出。
@deepin-ci-robot
Copy link
Copy Markdown
Contributor

[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
Copy Markdown

sourcery-ai bot commented Apr 1, 2026

Reviewer's Guide

Adjusts SliderTipItem tick label alignment to align the first/last horizontal tick labels outward and uses negative margins to visually align text with tick marks while adding simple null-safe access to ticks and updating the license header years.

Flow diagram for effectiveHorizontalAlignment selection

flowchart TD
  A_start[Start alignment resolution] --> B_checkHorizontal{Is slider horizontal?}
  B_checkHorizontal -- No --> C_useDefault[Use textHorizontalAlignment]
  C_useDefault --> Z_end[Return alignment]
  B_checkHorizontal -- Yes --> D_checkFirst{Is tick first?}
  D_checkFirst -- Yes --> E_alignLeft[Set alignment to Text.AlignLeft]
  E_alignLeft --> Z_end
  D_checkFirst -- No --> F_checkLast{Is tick last?}
  F_checkLast -- Yes --> G_alignRight[Set alignment to Text.AlignRight]
  G_alignRight --> Z_end
  F_checkLast -- No --> H_useDefault2[Use textHorizontalAlignment]
  H_useDefault2 --> Z_end
Loading

File-Level Changes

Change Details Files
Add first/last tick detection and an effectiveHorizontalAlignment property to control outward alignment for edge tick labels.
  • Introduce readonly isFirst and isLast properties that safely read parent.parent.ticks and compare against the current control instance.
  • Add effectiveHorizontalAlignment computed property that overrides horizontal alignment to AlignLeft for the first tick and AlignRight for the last tick when the slider is horizontal, otherwise falling back to textHorizontalAlignment.
  • Document the alignment behavior with an inline comment to clarify the intent of outward-aligned edge labels.
qt6/src/qml/SliderTipItem.qml
Update label anchoring and padding/margin behavior so text visually aligns with tick marks and uses the new effectiveHorizontalAlignment.
  • Adjust topMargin and bottomMargin for the text item to use negative vPadding when inactive to counter the label padding and maintain alignment around the tick mark.
  • Update leftMargin and rightMargin for vertical sliders to use negative hPadding where appropriate, again offsetting the label padding so text aligns with the tick mark reference rectangle.
  • Replace uses of textHorizontalAlignment in anchor bindings (left, right, horizontalCenter) with effectiveHorizontalAlignment so first/last horizontal ticks align outward.
  • Bind the Text element's horizontalAlignment to control.effectiveHorizontalAlignment instead of textHorizontalAlignment to keep visual alignment consistent with the anchor logic.
qt6/src/qml/SliderTipItem.qml
Refresh SPDX file copyright years.
  • Extend the SPDX-FileCopyrightText year range from 2022 to 2022 - 2026.
qt6/src/qml/SliderTipItem.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 found 1 issue, and left some high level feedback:

  • For sliders with only a single tick, isFirst and isLast will both be true so the label will end up right-aligned; consider explicitly handling the single-tick case (e.g., keeping it centered) if that layout is undesirable.
  • The isFirst/isLast bindings recompute ticks.indexOf(control) every time they are evaluated; if ticks can be large or change frequently, consider deriving these flags from existing index information or caching to avoid repeated linear scans.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- For sliders with only a single tick, `isFirst` and `isLast` will both be true so the label will end up right-aligned; consider explicitly handling the single-tick case (e.g., keeping it centered) if that layout is undesirable.
- The `isFirst`/`isLast` bindings recompute `ticks.indexOf(control)` every time they are evaluated; if `ticks` can be large or change frequently, consider deriving these flags from existing index information or caching to avoid repeated linear scans.

## Individual Comments

### Comment 1
<location path="qt6/src/qml/SliderTipItem.qml" line_range="21-23" />
<code_context>
+        var ticks = parent.parent.ticks || []
+        return ticks.indexOf(control) === 0
+    }
+    readonly property bool isLast: {
+        var ticks = parent.parent.ticks || []
+        return ticks.indexOf(control) === ticks.length - 1
+    }
+    // Align first/last tick labels outward to prevent text overflow at slider edges
</code_context>
<issue_to_address>
**issue:** Guard against `ticks` being empty when computing `isLast`.

Because `ticks = parent.parent.ticks || []` can be `[]`, `ticks.indexOf(control)` and `ticks.length - 1` are both `-1`, so `isLast` evaluates to `true` when there are no ticks. If an empty tick list is possible, this will incorrectly treat the label as the last tick and apply the outward alignment. Consider guarding with `ticks.length > 0`, e.g. `return ticks.length > 0 && ticks.indexOf(control) === ticks.length - 1`.
</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 +21 to +23
readonly property bool isLast: {
var ticks = parent.parent.ticks || []
return ticks.indexOf(control) === ticks.length - 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue: Guard against ticks being empty when computing isLast.

Because ticks = parent.parent.ticks || [] can be [], ticks.indexOf(control) and ticks.length - 1 are both -1, so isLast evaluates to true when there are no ticks. If an empty tick list is possible, this will incorrectly treat the label as the last tick and apply the outward alignment. Consider guarding with ticks.length > 0, e.g. return ticks.length > 0 && ticks.indexOf(control) === ticks.length - 1.

@add-uos add-uos force-pushed the fix-slider-tip-alignment branch from d98cb14 to 0d767ca Compare April 1, 2026 06:22
@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot bot commented Apr 9, 2026

TAG Bot

New tag: 6.7.39
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #601

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.

2 participants