From 6db2f05e7e352ebd7fe231afda9d0fe678c7eba8 Mon Sep 17 00:00:00 2001 From: chinboy Date: Sat, 10 Jan 2026 22:41:20 +0800 Subject: [PATCH] bugfix(Popop): Fix popup offset adjust bug for macOS --- .../Views/DataEntry/TreeSelectShowCase.axaml | 2 +- .../Flyouts/MenuFlyout.cs | 6 +++++ .../Flyouts/PopupFlyoutBase.cs | 2 -- .../Popup/PlacementAwareDecorator.cs | 1 + src/AtomUI.Desktop.Controls/Popup/Popup.cs | 23 ++++++------------- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/TreeSelectShowCase.axaml b/controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/TreeSelectShowCase.axaml index b59aa447e..ef46fe7e4 100644 --- a/controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/TreeSelectShowCase.axaml +++ b/controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/TreeSelectShowCase.axaml @@ -128,7 +128,7 @@ TreeItemsSource="{Binding LeftAddTreeNodes}" IsAllowClear="True" PlaceholderText="Please select" - IsTreeDefaultExpandAll="True" + IsTreeDefaultExpandAll="True" LeftAddOn="Prefix" IsFilterEnabled="True"/> (8); + VerticalOffsetProperty.OverrideDefaultValue(8); + } public MenuFlyout() { diff --git a/src/AtomUI.Desktop.Controls/Flyouts/PopupFlyoutBase.cs b/src/AtomUI.Desktop.Controls/Flyouts/PopupFlyoutBase.cs index 6b12ea5e0..8ad511f99 100644 --- a/src/AtomUI.Desktop.Controls/Flyouts/PopupFlyoutBase.cs +++ b/src/AtomUI.Desktop.Controls/Flyouts/PopupFlyoutBase.cs @@ -209,8 +209,6 @@ public abstract class PopupFlyoutBase : FlyoutBase, IPopupHostProvider static PopupFlyoutBase() { Control.ContextFlyoutProperty.Changed.Subscribe(OnContextFlyoutPropertyChanged); - HorizontalOffsetProperty.OverrideDefaultValue(8); - VerticalOffsetProperty.OverrideDefaultValue(8); } public PopupFlyoutBase() diff --git a/src/AtomUI.Desktop.Controls/Popup/PlacementAwareDecorator.cs b/src/AtomUI.Desktop.Controls/Popup/PlacementAwareDecorator.cs index c35a95875..a127136a3 100644 --- a/src/AtomUI.Desktop.Controls/Popup/PlacementAwareDecorator.cs +++ b/src/AtomUI.Desktop.Controls/Popup/PlacementAwareDecorator.cs @@ -33,4 +33,5 @@ internal class PlacementAwareDecorator : ContentControl set => SetValue(MarginToAnchorProperty, value); } #endregion + } \ No newline at end of file diff --git a/src/AtomUI.Desktop.Controls/Popup/Popup.cs b/src/AtomUI.Desktop.Controls/Popup/Popup.cs index 77a72a26c..49e62853d 100644 --- a/src/AtomUI.Desktop.Controls/Popup/Popup.cs +++ b/src/AtomUI.Desktop.Controls/Popup/Popup.cs @@ -13,6 +13,7 @@ using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives.PopupPositioning; using Avalonia.Input; using Avalonia.Input.Raw; +using Avalonia.Layout; using Avalonia.LogicalTree; using Avalonia.Media; using Avalonia.Styling; @@ -199,7 +200,7 @@ public class Popup : AvaloniaPopup, IMotionAwareControl } else { - popupHostProvider.PopupHostChanged += HandlePopupHostChanged; + popupHostProvider.PopupHostChanged += HandlePopupHostChanged; } } @@ -217,6 +218,7 @@ public class Popup : AvaloniaPopup, IMotionAwareControl { _hostDecoratorDisposable?.Dispose(); _placementAwareDecorator = null; + HostDecoratorDirection = GetHostDecoratorDirection(Placement); if (popupHost is PopupRoot popupRoot) { MotionActor = popupRoot.FindDescendantOfType(); @@ -283,6 +285,7 @@ public class Popup : AvaloniaPopup, IMotionAwareControl else if (Host is OverlayPopupHost overlayPopupHost) { overlayPopupHost.PropertyChanged += HandleOverlayPopupHostPropertyChanged; + overlayPopupHost.SizeChanged += (o, eventArgs) => AdjustPopupHostPosition(placementTarget); } } } @@ -511,20 +514,14 @@ public class Popup : AvaloniaPopup, IMotionAwareControl { var topLevel = TopLevel.GetTopLevel(placementTarget)!; - Point location = default; - Size popupSize = default; + Size popupSize = default; if (Host is PopupRoot popupRoot) { - if (popupRoot.PlatformImpl?.Position is not null) - { - location = new Point(popupRoot.PlatformImpl.Position.X, popupRoot.PlatformImpl.Position.Y); - } popupSize = popupRoot.ClientSize; } else if (Host is OverlayPopupHost overlayPopupHost) { - location = new Point(Canvas.GetLeft(overlayPopupHost), Canvas.GetTop(overlayPopupHost)); - popupSize = overlayPopupHost.Bounds.Size; + popupSize = overlayPopupHost.DesiredSize; } IManagedPopupPositionerPopup? positionerPopup = null; @@ -542,8 +539,6 @@ public class Popup : AvaloniaPopup, IMotionAwareControl if (Placement != PlacementMode.Center && Placement != PlacementMode.Pointer) { - var effectiveOffsetX = location.X; - var effectiveOffsetY = location.Y; // 计算是否 flip var parameters = new PopupPositionerParameters(); var offset = new Point(HorizontalOffset, VerticalOffset); @@ -577,16 +572,12 @@ public class Popup : AvaloniaPopup, IMotionAwareControl Placement = flipPlacement; PlacementAnchor = flipAnchorAndGravity.Item1; PlacementGravity = flipAnchorAndGravity.Item2; - - IsFlipped = true; + IsFlipped = true; } else { IsFlipped = false; } - - var effectivePosition = new Point(effectiveOffsetX, effectiveOffsetY); - positionerPopup.MoveAndResize(effectivePosition, popupSize); PositionFlipped?.Invoke(this, new PopupFlippedEventArgs(IsFlipped)); } }