fix: prevent negative margins in dock center alignment#1495
Merged
wjyrich merged 1 commit intolinuxdeepin:masterfrom Mar 12, 2026
Merged
fix: prevent negative margins in dock center alignment#1495wjyrich merged 1 commit intolinuxdeepin:masterfrom
wjyrich merged 1 commit intolinuxdeepin:masterfrom
Conversation
18202781743
reviewed
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
deepin pr auto review这段代码主要对 Dock 面板(任务栏)的居中布局逻辑进行了修改。以下是对这段 diff 的审查意见,包括语法逻辑、代码质量、性能和安全方面的分析。 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
综合改进建议虽然当前的修改解决了负边距的问题,但代码的可维护性较差。建议进行以下重构以提高代码质量: 改进后的代码示例(逻辑等价,但更清晰): // 定义一个常量或者属性来代表那个硬编码的 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总结: |
18202781743
approved these changes
Mar 12, 2026
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
works correctly
fix: 修复任务栏居中布局负边距问题
修复了任务栏居中布局计算,防止在居中任务栏项目时出现负边距。将
Layout.leftMargin和Layout.topMargin计算改为使用Math.max(0, ...),确保边
距永远不会为负值。这解决了当任务栏项目大于可用空间时的布局问题。
问题发生在任务栏中心部分的隐式宽度/高度大于可用任务栏空间时,导致负边距
可能破坏布局。修复确保边距被限制在最小零值,即使在空间受限的情况下也能保
持正确的视觉对齐。
Influence:
PMS: BUG-351805