Skip to content

fix: prevent negative margins in dock center alignment#1495

Merged
wjyrich merged 1 commit intolinuxdeepin:masterfrom
wjyrich:fix-bug-351805
Mar 12, 2026
Merged

fix: prevent negative margins in dock center alignment#1495
wjyrich merged 1 commit intolinuxdeepin:masterfrom
wjyrich:fix-bug-351805

Conversation

@wjyrich
Copy link
Contributor

@wjyrich wjyrich commented Mar 11, 2026

Fixed dock layout calculation to prevent negative margins when centering
dock items. Changed Layout.leftMargin and Layout.topMargin calculations
to use Math.max(0, ...) to ensure margins are never negative. This
prevents layout issues when dock items are larger than available space.

The issue occurred when the dock center part's implicit width/height
was larger than the available dock space, causing negative margins that
could break the layout. The fix ensures margins are clamped to zero
minimum, maintaining proper visual alignment even in constrained space
conditions.

Influence:

  1. Test dock with various item counts and sizes to verify centering
    works correctly
  2. Verify dock layout when items exceed available space
  3. Test both horizontal and vertical dock orientations
  4. Check that dragging and resizing behaviors remain smooth
  5. Validate center alignment with different panel sizes

fix: 修复任务栏居中布局负边距问题

修复了任务栏居中布局计算,防止在居中任务栏项目时出现负边距。将
Layout.leftMargin和Layout.topMargin计算改为使用Math.max(0, ...),确保边
距永远不会为负值。这解决了当任务栏项目大于可用空间时的布局问题。

问题发生在任务栏中心部分的隐式宽度/高度大于可用任务栏空间时,导致负边距
可能破坏布局。修复确保边距被限制在最小零值,即使在空间受限的情况下也能保
持正确的视觉对齐。

Influence:

  1. 测试不同项目数量和大小下任务栏的居中功能
  2. 验证项目超过可用空间时的任务栏布局
  3. 测试水平和垂直两种任务栏方向
  4. 检查拖拽和调整大小行为是否保持流畅
  5. 验证不同面板大小下的居中对齐效果

PMS: BUG-351805

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.

Sorry @wjyrich, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

Fixed dock layout calculation to prevent negative margins when centering
dock items. Changed Layout.leftMargin and Layout.topMargin calculations
to use Math.max(0, ...) to ensure margins are never negative. This
prevents layout issues when dock items are larger than available space.

The issue occurred when the dock center part's implicit width/height
was larger than the available dock space, causing negative margins that
could break the layout. The fix ensures margins are clamped to zero
minimum, maintaining proper visual alignment even in constrained space
conditions.

Influence:
1. Test dock with various item counts and sizes to verify centering
works correctly
2. Verify dock layout when items exceed available space
3. Test both horizontal and vertical dock orientations
4. Check that dragging and resizing behaviors remain smooth
5. Validate center alignment with different panel sizes

fix: 修复任务栏居中布局负边距问题

修复了任务栏居中布局计算,防止在居中任务栏项目时出现负边距。将
Layout.leftMargin和Layout.topMargin计算改为使用Math.max(0, ...),确保边
距永远不会为负值。这解决了当任务栏项目大于可用空间时的布局问题。

问题发生在任务栏中心部分的隐式宽度/高度大于可用任务栏空间时,导致负边距
可能破坏布局。修复确保边距被限制在最小零值,即使在空间受限的情况下也能保
持正确的视觉对齐。

Influence:
1. 测试不同项目数量和大小下任务栏的居中功能
2. 验证项目超过可用空间时的任务栏布局
3. 测试水平和垂直两种任务栏方向
4. 检查拖拽和调整大小行为是否保持流畅
5. 验证不同面板大小下的居中对齐效果

PMS: BUG-351805
@wjyrich wjyrich changed the title feat: adjust task manager layout calculation for center applets fix: prevent negative margins in dock center alignment Mar 12, 2026
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要对 Dock 面板(任务栏)的居中布局逻辑进行了修改。以下是对这段 diff 的审查意见,包括语法逻辑、代码质量、性能和安全方面的分析。

1. 语法逻辑

  • 修改意图:代码在计算 Layout.leftMarginLayout.topMargin 时,外层增加了一个 Math.max(0, ...) 的包裹。
  • 逻辑分析
    • 原逻辑:直接计算偏移量。如果计算结果为负数(例如 Dock 宽度不足以容纳所有图标,或者左右两侧预留空间过大),负值会被赋值给 Margin。在 QML 中,负的 Margin 通常意味着元素会超出容器边界,可能导致图标重叠或显示在 Dock 区域之外。
    • 新逻辑:通过 Math.max(0, ...) 强制将计算结果限制为非负数。这意味着如果计算出的居中偏移量会导致元素"向外溢出"或产生负边距,系统会将其重置为 0。
    • 结论:这个修改逻辑是合理的。它增加了一个边界保护,防止布局计算出现负值导致的 UI 渲染错误(如内容被截断或位置异常),确保了在极端尺寸下布局的稳定性。

2. 代码质量

  • 可读性:当前的公式非常长且复杂,嵌套了多次加减法和 Math.min/max
    • (dock.width - dockCenterPart.implicitWidth) / 2 这部分出现了两次。
    • 硬编码的 20 像素值(可能是间距 padding)没有命名常量,降低了可维护性。
  • 建议:虽然逻辑正确,但为了提高可读性,建议将计算过程拆解,或者提取公共部分。

3. 代码性能

  • 计算频率:这段代码位于 QML 属性绑定中。每当 dock.widthdockCenterPart.implicitWidth 等依赖属性发生变化时,都会触发重新计算。
  • 影响Math.maxMath.min 是非常轻量级的操作,增加一次 Math.max 调用对性能的影响微乎其微,可以忽略不计。
  • 结论:性能方面没有问题。

4. 代码安全

  • 边界检查:如前所述,新增的 Math.max(0, ...) 实际上充当了边界检查的角色,防止了负 Margin 可能导致的布局崩溃或未定义行为。这是一个健壮性的改进。
  • 数据类型:涉及的属性均为数值类型,不存在类型转换风险。

综合改进建议

虽然当前的修改解决了负边距的问题,但代码的可维护性较差。建议进行以下重构以提高代码质量:

改进后的代码示例(逻辑等价,但更清晰):

// 定义一个常量或者属性来代表那个硬编码的 20
readonly property int itemSpacing: 20

Layout.leftMargin: !useColumnLayout && Panel.itemAlignment === Dock.CenterAlignment ? calculateHorizontalMargin() : 0
Layout.topMargin: useColumnLayout && Panel.itemAlignment === Dock.CenterAlignment ? calculateVerticalMargin() : 0

// 可以在同一个文件中定义这些辅助函数,或者直接内联优化
function calculateHorizontalMargin() {
    var centerAvailableSpace = dock.width - dockCenterPart.implicitWidth;
    var leftOffset = centerAvailableSpace / 2 - (dockLeftPart.implicitWidth + itemSpacing);
    var rightOffset = centerAvailableSpace / 2 - (dockRightPart.implicitWidth + itemSpacing);
    
    // 原逻辑: leftOffset + Math.min(rightOffset, 0)
    // 新逻辑: Math.max(0, leftOffset + Math.min(rightOffset, 0))
    var totalOffset = leftOffset + Math.min(rightOffset, 0);
    return Math.max(0, totalOffset);
}

function calculateVerticalMargin() {
    var centerAvailableSpace = dock.height - dockCenterPart.implicitHeight;
    var topOffset = centerAvailableSpace / 2 - (dockLeftPart.implicitHeight + itemSpacing);
    var bottomOffset = centerAvailableSpace / 2 - (dockRightPart.implicitHeight + itemSpacing);
    
    var totalOffset = topOffset + Math.min(bottomOffset, 0);
    return Math.max(0, totalOffset);
}

或者如果不希望引入函数,仅优化当前的单行表达式(减少重复计算):

Layout.leftMargin: !useColumnLayout && Panel.itemAlignment === Dock.CenterAlignment ? {
    var space = (dock.width - dockCenterPart.implicitWidth) / 2;
    var offset = space - (dockLeftPart.implicitWidth + 20) + Math.min(space - (dockRightPart.implicitWidth + 20), 0);
    Math.max(0, offset)
} : 0

总结
这个 diff 是一个好的修复,它修复了潜在的布局溢出 Bug。虽然代码表达式较长,但考虑到 QML 的特性,直接修改也是可以接受的。如果时间允许,按照上述建议进行重构会让代码更易于后续维护。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, wjyrich

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

@wjyrich wjyrich merged commit 270248f into linuxdeepin:master Mar 12, 2026
11 of 12 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