Skip to content

Commit c78a83a

Browse files
committed
优化拖放逻辑和计算方法
在 `DockTargetButton.xaml.cs` 中,重构了 `OnDragEnter` 方法,简化了逻辑,提升了可读性。现在根据 `Target` 类型处理拖动预览。 在 `LayoutPanel.xaml.cs` 中,简化了 `CalculateHeight` 和 `CalculateWidth` 方法的返回逻辑,使用三元运算符替代了原有的 `if` 语句,使代码更加简洁。
1 parent 2053b48 commit c78a83a

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

src/WinUI.Dock/Controls/DockTargetButton.xaml.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ protected override async void OnDragEnter(DragEventArgs e)
3737
{
3838
base.OnDragEnter(e);
3939

40-
string dragKey = (string)await e.DataView.GetDataAsync(DragDropHelpers.FormatId);
41-
42-
if (DragDropHelpers.GetDocument(dragKey) is Document document)
40+
if (Target is DockManager dockManager)
4341
{
44-
if (Target is DockManager dockManager)
42+
string dragKey = (string)await e.DataView.GetDataAsync(DragDropHelpers.FormatId);
43+
44+
if (DragDropHelpers.GetDocument(dragKey) is Document document)
4545
{
4646
dockManager.ShowDockPreview(document, DockTarget);
4747
}
48-
else if (Target is DocumentGroup documentGroup)
49-
{
50-
documentGroup.ShowDockPreview(DockTarget);
51-
}
48+
}
49+
else if (Target is DocumentGroup documentGroup)
50+
{
51+
documentGroup.ShowDockPreview(DockTarget);
5252
}
5353
}
5454

src/WinUI.Dock/LayoutPanel.xaml.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ internal double CalculateHeight(DockModule module, bool isStar)
125125
double totalHeight = children.Sum(static item => double.IsNaN(item.DockHeight) ? 1.0 : item.DockHeight);
126126
double moduleHeight = double.IsNaN(module.DockHeight) ? totalHeight / children.Length : module.DockHeight;
127127

128-
if (isStar)
129-
{
130-
return moduleHeight;
131-
}
132-
133-
return ActualHeight / totalHeight * moduleHeight;
128+
return isStar ? moduleHeight : ActualHeight / totalHeight * moduleHeight;
134129
}
135130

136131
internal double CalculateWidth(DockModule module, bool isStar)
@@ -140,11 +135,6 @@ internal double CalculateWidth(DockModule module, bool isStar)
140135
double totalWidth = children.Sum(static item => double.IsNaN(item.DockWidth) ? 1.0 : item.DockWidth);
141136
double moduleWidth = double.IsNaN(module.DockWidth) ? totalWidth / children.Length : module.DockWidth;
142137

143-
if (isStar)
144-
{
145-
return moduleWidth;
146-
}
147-
148-
return ActualWidth / totalWidth * moduleWidth;
138+
return isStar ? moduleWidth : ActualWidth / totalWidth * moduleWidth;
149139
}
150140
}

0 commit comments

Comments
 (0)