From 1553a936b58444c1426d743b70e50e10c5b6ee54 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sun, 25 Aug 2024 15:47:12 +0800 Subject: [PATCH] feat: add fix to drawer. --- .../Controls/Drawer/CustomDrawerControl.cs | 2 +- .../Controls/Drawer/DefaultDrawerControl.cs | 72 +++++++++---------- src/Ursa/Controls/Drawer/DrawerControlBase.cs | 10 +-- .../Controls/Drawer/Options/DrawerOptions.cs | 2 +- 4 files changed, 37 insertions(+), 49 deletions(-) diff --git a/src/Ursa/Controls/Drawer/CustomDrawerControl.cs b/src/Ursa/Controls/Drawer/CustomDrawerControl.cs index 61a55fe..51be7c0 100644 --- a/src/Ursa/Controls/Drawer/CustomDrawerControl.cs +++ b/src/Ursa/Controls/Drawer/CustomDrawerControl.cs @@ -10,7 +10,7 @@ public class CustomDrawerControl: DrawerControlBase base.OnApplyTemplate(e); if (_closeButton is not null) { - _closeButton.IsVisible = IsCloseButtonVisible; + _closeButton.IsVisible = IsCloseButtonVisible ?? true; } } diff --git a/src/Ursa/Controls/Drawer/DefaultDrawerControl.cs b/src/Ursa/Controls/Drawer/DefaultDrawerControl.cs index baac81a..00ded96 100644 --- a/src/Ursa/Controls/Drawer/DefaultDrawerControl.cs +++ b/src/Ursa/Controls/Drawer/DefaultDrawerControl.cs @@ -13,20 +13,30 @@ namespace Ursa.Controls; [TemplatePart(PART_NoButton, typeof(Button))] [TemplatePart(PART_OKButton, typeof(Button))] [TemplatePart(PART_CancelButton, typeof(Button))] -public class DefaultDrawerControl: DrawerControlBase +public class DefaultDrawerControl : DrawerControlBase { public const string PART_YesButton = "PART_YesButton"; public const string PART_NoButton = "PART_NoButton"; public const string PART_OKButton = "PART_OKButton"; public const string PART_CancelButton = "PART_CancelButton"; - - private Button? _yesButton; + + public static readonly StyledProperty ButtonsProperty = + AvaloniaProperty.Register( + nameof(Buttons), DialogButton.OKCancel); + + public static readonly StyledProperty ModeProperty = + AvaloniaProperty.Register( + nameof(Mode), DialogMode.None); + + public static readonly StyledProperty TitleProperty = + AvaloniaProperty.Register( + nameof(Title)); + + private Button? _cancelButton; private Button? _noButton; private Button? _okButton; - private Button? _cancelButton; - public static readonly StyledProperty ButtonsProperty = AvaloniaProperty.Register( - nameof(Buttons), DialogButton.OKCancel); + private Button? _yesButton; public DialogButton Buttons { @@ -34,24 +44,18 @@ public class DefaultDrawerControl: DrawerControlBase set => SetValue(ButtonsProperty, value); } - public static readonly StyledProperty ModeProperty = AvaloniaProperty.Register( - nameof(Mode), DialogMode.None); - public DialogMode Mode { get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); } - public static readonly StyledProperty TitleProperty = AvaloniaProperty.Register( - nameof(Title)); - public string? Title { get => GetValue(TitleProperty); set => SetValue(TitleProperty, value); } - + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); @@ -63,58 +67,50 @@ public class DefaultDrawerControl: DrawerControlBase Button.ClickEvent.AddHandler(OnDefaultButtonClick, _yesButton, _noButton, _okButton, _cancelButton); SetButtonVisibility(); } - + private void SetButtonVisibility() { - bool isCloseButtonVisible = DataContext is IDialogContext || Buttons != DialogButton.YesNo; - Button.IsVisibleProperty.SetValue(isCloseButtonVisible, _closeButton); + var isCloseButtonVisible = + IsCloseButtonVisible ?? (DataContext is IDialogContext || Buttons != DialogButton.YesNo); + IsVisibleProperty.SetValue(isCloseButtonVisible, _closeButton); switch (Buttons) { case DialogButton.None: - Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton); + IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton); break; case DialogButton.OK: - Button.IsVisibleProperty.SetValue(true, _okButton); - Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton); + IsVisibleProperty.SetValue(true, _okButton); + IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton); break; case DialogButton.OKCancel: - Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton); - Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton); + IsVisibleProperty.SetValue(true, _okButton, _cancelButton); + IsVisibleProperty.SetValue(false, _yesButton, _noButton); break; case DialogButton.YesNo: - Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton); - Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton); + IsVisibleProperty.SetValue(false, _okButton, _cancelButton); + IsVisibleProperty.SetValue(true, _yesButton, _noButton); break; case DialogButton.YesNoCancel: - Button.IsVisibleProperty.SetValue(false, _okButton); - Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton); + IsVisibleProperty.SetValue(false, _okButton); + IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton); break; } } - + private void OnDefaultButtonClick(object? sender, RoutedEventArgs e) { if (sender is Button button) { if (button == _okButton) - { OnElementClosing(this, DialogResult.OK); - } else if (button == _cancelButton) - { OnElementClosing(this, DialogResult.Cancel); - } else if (button == _yesButton) - { OnElementClosing(this, DialogResult.Yes); - } - else if (button == _noButton) - { - OnElementClosing(this, DialogResult.No); - } + else if (button == _noButton) OnElementClosing(this, DialogResult.No); } } - + public override void Close() { if (DataContext is IDialogContext context) @@ -123,7 +119,7 @@ public class DefaultDrawerControl: DrawerControlBase } else { - DialogResult result = Buttons switch + var result = Buttons switch { DialogButton.None => DialogResult.None, DialogButton.OK => DialogResult.OK, diff --git a/src/Ursa/Controls/Drawer/DrawerControlBase.cs b/src/Ursa/Controls/Drawer/DrawerControlBase.cs index 45468ca..f377276 100644 --- a/src/Ursa/Controls/Drawer/DrawerControlBase.cs +++ b/src/Ursa/Controls/Drawer/DrawerControlBase.cs @@ -37,15 +37,7 @@ public abstract class DrawerControlBase: OverlayFeedbackElement set => SetValue(IsOpenProperty, value); } - public static readonly StyledProperty IsCloseButtonVisibleProperty = - AvaloniaProperty.Register( - nameof(IsCloseButtonVisible), defaultValue: true); - - public bool IsCloseButtonVisible - { - get => GetValue(IsCloseButtonVisibleProperty); - set => SetValue(IsCloseButtonVisibleProperty, value); - } + internal bool? IsCloseButtonVisible { get; set; } protected internal bool CanLightDismiss { get; set; } diff --git a/src/Ursa/Controls/Drawer/Options/DrawerOptions.cs b/src/Ursa/Controls/Drawer/Options/DrawerOptions.cs index 3e7aea6..04d5e0e 100644 --- a/src/Ursa/Controls/Drawer/Options/DrawerOptions.cs +++ b/src/Ursa/Controls/Drawer/Options/DrawerOptions.cs @@ -7,7 +7,7 @@ public class DrawerOptions internal static DrawerOptions Default => new (); public Position Position { get; set; } = Position.Right; public bool CanLightDismiss { get; set; } = true; - public bool IsCloseButtonVisible { get; set; } = true; + public bool? IsCloseButtonVisible { get; set; } = true; public double? MinWidth { get; set; } = null; public double? MinHeight { get; set; } = null; public double? MaxWidth { get; set; } = null;