From b50049c8228fd466337ad9d42bd3d3a3a5faae8c Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Sun, 12 Apr 2026 09:08:33 +0800 Subject: [PATCH] =?UTF-8?q?improve:UI=E9=9D=A2=E6=9D=BF=E5=BC=B9=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/MainWindow_cs/MW_Colors.cs | 65 ++++-------- .../MainWindow_cs/MW_FloatingBarIcons.cs | 100 ++++++++++++++++++ Ink Canvas/MainWindow_cs/MW_ShapeDrawing.cs | 1 + 3 files changed, 119 insertions(+), 47 deletions(-) diff --git a/Ink Canvas/MainWindow_cs/MW_Colors.cs b/Ink Canvas/MainWindow_cs/MW_Colors.cs index 133d23c9..c3b444e4 100644 --- a/Ink Canvas/MainWindow_cs/MW_Colors.cs +++ b/Ink Canvas/MainWindow_cs/MW_Colors.cs @@ -554,37 +554,22 @@ namespace Ink_Canvas BoardHighlightPenTabButton.Background = new SolidColorBrush(Colors.Transparent); BoardHighlightPenTabButtonIndicator.Visibility = Visibility.Collapsed; - // PenPalette.Margin = new Thickness(-160, -200, -33, 32); + // 动态计算面板位置,使其对齐笔按钮(考虑快捷调色盘等动态宽度) await Dispatcher.InvokeAsync(() => { - var marginAnimation = new ThicknessAnimation - { - Duration = TimeSpan.FromSeconds(0.1), - From = PenPalette.Margin, - To = new Thickness(-160, -200, -33, 32), - EasingFunction = new CubicEase() - }; - PenPalette.BeginAnimation(MarginProperty, marginAnimation); + PenPalette.BeginAnimation(MarginProperty, null); + var currentMargin = PenPalette.Margin; + // 先设置正确的Top/Bottom,保持当前Left/Right + PenPalette.Margin = new Thickness(currentMargin.Left, -200, currentMargin.Right, 32); + UpdatePenPalettePosition(); }); await Dispatcher.InvokeAsync(() => { - var marginAnimation = new ThicknessAnimation - { - Duration = TimeSpan.FromSeconds(0.1), - From = PenPalette.Margin, - To = new Thickness(-160, -200, -33, 50), - EasingFunction = new CubicEase() - }; - BoardPenPaletteGrid.BeginAnimation(MarginProperty, marginAnimation); + BoardPenPaletteGrid.BeginAnimation(MarginProperty, null); + var currentMargin = BoardPenPaletteGrid.Margin; + BoardPenPaletteGrid.Margin = new Thickness(currentMargin.Left, -200, currentMargin.Right, 50); }); - - - await Task.Delay(100); - - await Dispatcher.InvokeAsync(() => { PenPalette.Margin = new Thickness(-160, -200, -33, 32); }); - - await Dispatcher.InvokeAsync(() => { BoardPenPaletteGrid.Margin = new Thickness(-160, -200, -33, 50); }); } else if (penType == 1) { @@ -622,36 +607,22 @@ namespace Ink_Canvas BoardHighlightPenTabButton.Background = new SolidColorBrush(Color.FromArgb(72, 219, 234, 254)); BoardHighlightPenTabButtonIndicator.Visibility = Visibility.Visible; - // PenPalette.Margin = new Thickness(-160, -157, -33, 32); + // 动态计算面板位置,使其对齐笔按钮(考虑快捷调色盘等动态宽度) await Dispatcher.InvokeAsync(() => { - var marginAnimation = new ThicknessAnimation - { - Duration = TimeSpan.FromSeconds(0.1), - From = PenPalette.Margin, - To = new Thickness(-160, -157, -33, 32), - EasingFunction = new CubicEase() - }; - PenPalette.BeginAnimation(MarginProperty, marginAnimation); + PenPalette.BeginAnimation(MarginProperty, null); + var currentMargin = PenPalette.Margin; + // 荧光笔模式面板稍小,使用不同的Top/Bottom + PenPalette.Margin = new Thickness(currentMargin.Left, -157, currentMargin.Right, 32); + UpdatePenPalettePosition(); }); await Dispatcher.InvokeAsync(() => { - var marginAnimation = new ThicknessAnimation - { - Duration = TimeSpan.FromSeconds(0.1), - From = PenPalette.Margin, - To = new Thickness(-160, -154, -33, 50), - EasingFunction = new CubicEase() - }; - BoardPenPaletteGrid.BeginAnimation(MarginProperty, marginAnimation); + BoardPenPaletteGrid.BeginAnimation(MarginProperty, null); + var currentMargin = BoardPenPaletteGrid.Margin; + BoardPenPaletteGrid.Margin = new Thickness(currentMargin.Left, -154, currentMargin.Right, 50); }); - - await Task.Delay(100); - - await Dispatcher.InvokeAsync(() => { PenPalette.Margin = new Thickness(-160, -157, -33, 32); }); - - await Dispatcher.InvokeAsync(() => { BoardPenPaletteGrid.Margin = new Thickness(-160, -154, -33, 50); }); } } diff --git a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs index 2788a77f..962e1fcc 100644 --- a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs +++ b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs @@ -49,6 +49,7 @@ namespace Ink_Canvas else { HideSubPanels(); + UpdateTwoFingerGestureBorderPosition(); AnimationsHelper.ShowWithSlideFromBottomAndFade(TwoFingerGestureBorder); AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardTwoFingerGestureBorder); } @@ -1742,6 +1743,7 @@ namespace Ink_Canvas else { HideSubPanels(); + UpdateBorderToolsPosition(); AnimationsHelper.ShowWithSlideFromBottomAndFade(BorderTools); AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardBorderTools); } @@ -2439,6 +2441,7 @@ namespace Ink_Canvas else { HideSubPanels(); + UpdatePenPalettePosition(); AnimationsHelper.ShowWithSlideFromBottomAndFade(PenPalette); AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardPenPalette); } @@ -2548,6 +2551,7 @@ namespace Ink_Canvas // 已是橡皮状态,再次点击才弹出/收起面板 if (EraserSizePanel.Visibility == Visibility.Collapsed) { + UpdateEraserSizePanelPosition(); AnimationsHelper.ShowWithSlideFromBottomAndFade(EraserSizePanel); if (BoardEraserSizePanel != null) AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardEraserSizePanel); @@ -2598,6 +2602,7 @@ namespace Ink_Canvas // 已是橡皮状态,再次点击才弹出/收起面板 if (BoardEraserSizePanel != null && BoardEraserSizePanel.Visibility == Visibility.Collapsed) { + UpdateEraserSizePanelPosition(); AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardEraserSizePanel); AnimationsHelper.ShowWithSlideFromBottomAndFade(EraserSizePanel); } @@ -4209,6 +4214,101 @@ namespace Ink_Canvas } } + /// + /// 通用子面板位置更新方法:根据触发按钮的位置,动态调整子面板的水平位置, + /// 使面板水平中心对齐按钮中心。不改变面板大小,不改变上下边距。 + /// + /// 触发按钮元素 + /// 需要定位的子面板 + /// 面板默认宽度(当无法从Margin计算时使用) + private void UpdateSubPanelPosition(FrameworkElement button, FrameworkElement panel, double defaultPanelWidth) + { + try + { + if (button == null || panel == null) return; + if (!(panel.Parent is FrameworkElement panelContainer)) return; + + var ancestor = StackPanelFloatingBar; + if (ancestor == null) return; + + // 获取按钮中心的X坐标(相对于 StackPanelFloatingBar 坐标系) + var buttonTransform = button.TransformToAncestor(ancestor); + var buttonOrigin = buttonTransform.Transform(new Point(0, 0)); + double buttonCenterX = buttonOrigin.X + button.ActualWidth / 2.0; + + // 获取面板容器(零宽度Grid)的X坐标(相对于 StackPanelFloatingBar 坐标系) + var containerTransform = panelContainer.TransformToAncestor(ancestor); + var containerOrigin = containerTransform.Transform(new Point(0, 0)); + double containerX = containerOrigin.X; + + // 计算当前面板宽度(保持不变):panelWidth = -Margin.Left - Margin.Right + double currentLeft = panel.Margin.Left; + double currentRight = panel.Margin.Right; + double panelWidth = -currentLeft - currentRight; + if (panelWidth <= 0) panelWidth = defaultPanelWidth; + + // 计算新的左边距,使面板水平中心对齐按钮: + // panel_center = containerX + newLeft + panelWidth/2 = buttonCenterX + // => newLeft = buttonCenterX - containerX - panelWidth/2 + double newLeft = buttonCenterX - containerX - panelWidth / 2.0; + + // 保持面板宽度不变:-newLeft - newRight = panelWidth + // => newRight = -panelWidth - newLeft + double newRight = -panelWidth - newLeft; + + // 清除可能残留的 Margin 动画(HoldEnd 会阻止本地值生效) + panel.BeginAnimation(FrameworkElement.MarginProperty, null); + + // 更新边距,仅调整Left/Right,保持Top/Bottom不变 + var margin = panel.Margin; + panel.Margin = new Thickness(newLeft, margin.Top, newRight, margin.Bottom); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"更新子面板位置失败: {ex.Message}", LogHelper.LogType.Error); + } + } + + /// + /// 更新批注子面板(PenPalette)的弹出位置,使其水平中心对齐笔按钮。 + /// + private void UpdatePenPalettePosition() + { + UpdateSubPanelPosition(Pen_Icon, PenPalette, 193); + } + + /// + /// 更新工具面板(BorderTools)的弹出位置,使其水平中心对齐工具按钮。 + /// + private void UpdateBorderToolsPosition() + { + UpdateSubPanelPosition(ToolsFloatingBarBtn, BorderTools, 119); + } + + /// + /// 更新橡皮擦尺寸面板(EraserSizePanel)的弹出位置,使其水平中心对齐橡皮擦按钮。 + /// + private void UpdateEraserSizePanelPosition() + { + UpdateSubPanelPosition(Eraser_Icon, EraserSizePanel, 120); + } + + /// + /// 更新形状绘制面板(BorderDrawShape)的弹出位置,使其水平中心对齐形状按钮。 + /// + private void UpdateBorderDrawShapePosition() + { + UpdateSubPanelPosition(ShapeDrawFloatingBarBtn, BorderDrawShape, 317); + } + + /// + /// 更新手势面板(TwoFingerGestureBorder)的弹出位置,使其水平中心对齐手势按钮。 + /// + private void UpdateTwoFingerGestureBorderPosition() + { + UpdateSubPanelPosition(EnableTwoFingerGestureBorder, TwoFingerGestureBorder, 119); + } + /// /// 隐藏浮动栏高光显示 /// diff --git a/Ink Canvas/MainWindow_cs/MW_ShapeDrawing.cs b/Ink Canvas/MainWindow_cs/MW_ShapeDrawing.cs index 2b09e2e4..b5b5d4f4 100644 --- a/Ink Canvas/MainWindow_cs/MW_ShapeDrawing.cs +++ b/Ink Canvas/MainWindow_cs/MW_ShapeDrawing.cs @@ -49,6 +49,7 @@ namespace Ink_Canvas else { HideSubPanels(); + UpdateBorderDrawShapePosition(); AnimationsHelper.ShowWithSlideFromBottomAndFade(BorderDrawShape); AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardBorderDrawShape); }