From c4f0c1143baae196013d774f079a90df464136e4 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Tue, 23 Jul 2024 23:34:49 +0800 Subject: [PATCH] fix: 1. Fix modal status leak through issue 2. Fix the issue that dialog host cannot be unregistered properly. --- .../Controls/UrsaWindow.axaml | 2 +- .../OverlayShared/OverlayDialogHost.Shared.cs | 40 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml b/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml index 1762846..464be89 100644 --- a/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml +++ b/src/Ursa.Themes.Semi/Controls/UrsaWindow.axaml @@ -64,7 +64,7 @@ Margin="{Binding $parent[u:UrsaWindow].TitleBarMargin}" LeftContent="{Binding $parent[u:UrsaWindow].LeftContent}" RightContent="{Binding $parent[u:UrsaWindow].RightContent}" /> - + HasModalProperty = AvaloniaProperty.RegisterDirect( - nameof(HasModal), o => o.HasModal); - private bool _hasModal; - [Obsolete("Use IsInModalStatus")] - public bool HasModal - { - get => _hasModal; - private set => SetAndRaise(HasModalProperty, ref _hasModal, value); - } - public static readonly AttachedProperty IsModalStatusScopeProperty = AvaloniaProperty.RegisterAttached("IsModalStatusScope"); @@ -57,6 +47,15 @@ public partial class OverlayDialogHost: Canvas internal static void SetIsInModalStatus(Control obj, bool value) => obj.SetValue(IsInModalStatusProperty, value); public static bool GetIsInModalStatus(Control obj) => obj.GetValue(IsInModalStatusProperty); + public static readonly StyledProperty IsModalStatusReporterProperty = AvaloniaProperty.Register( + nameof(IsModalStatusReporter)); + + public bool IsModalStatusReporter + { + get => GetValue(IsModalStatusReporterProperty); + set => SetValue(IsModalStatusReporterProperty, value); + } + public bool IsInModalStatus { get => GetValue(IsInModalStatusProperty); @@ -70,11 +69,6 @@ public partial class OverlayDialogHost: Canvas ClipToBoundsProperty.OverrideDefaultValue(true); _maskAppearAnimation = CreateOpacityAnimation(true); _maskDisappearAnimation = CreateOpacityAnimation(false); - // This is only a temporary solution, will be removed in release candidate mode. - IsInModalStatusProperty.Changed.AddClassHandler((host, args) => - { - host.HasModal = args.NewValue.Value; - }); } private static Animation CreateOpacityAnimation(bool appear) @@ -141,17 +135,24 @@ public partial class OverlayDialogHost: Canvas } } private IDisposable? _modalStatusSubscription; + private int? _toplevelHash; protected sealed override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); - var hash = TopLevel.GetTopLevel(this)?.GetHashCode(); + _toplevelHash = TopLevel.GetTopLevel(this)?.GetHashCode(); var modalHost = this.GetVisualAncestors().OfType().FirstOrDefault(GetIsModalStatusScope); if (modalHost is not null) { _modalStatusSubscription = this.GetObservable(IsInModalStatusProperty) - .Subscribe(a => OverlayDialogHost.SetIsInModalStatus(modalHost, a)); + .Subscribe(a => + { + if (IsModalStatusReporter) + { + SetIsInModalStatus(modalHost, a); + } + }); } - OverlayDialogManager.RegisterHost(this, HostId, hash); + OverlayDialogManager.RegisterHost(this, HostId, _toplevelHash); } protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) @@ -161,8 +162,7 @@ public partial class OverlayDialogHost: Canvas _layers[0].Element.Close(); } _modalStatusSubscription?.Dispose(); - var hash = TopLevel.GetTopLevel(this)?.GetHashCode(); - OverlayDialogManager.UnregisterHost(HostId, hash); + OverlayDialogManager.UnregisterHost(HostId, _toplevelHash); base.OnDetachedFromVisualTree(e); }