From 7e57a0c0ef4f8e4ba4307a7c6041293b0337e04c Mon Sep 17 00:00:00 2001 From: rabbitism Date: Wed, 7 Feb 2024 19:04:51 +0800 Subject: [PATCH] feat: refactor to simplify null control property and event initialization. --- src/Ursa/Common/EventHelper.cs | 41 ------ src/Ursa/Common/PropertyHelper.cs | 14 -- .../Controls/Dialog/DefaultDialogControl.cs | 36 ++--- .../Controls/Dialog/DefaultDialogWindow.cs | 7 +- src/Ursa/Controls/Dialog/DialogControlBase.cs | 5 +- src/Ursa/Controls/Dialog/DialogWindow.cs | 11 +- .../Controls/Drawer/DefaultDrawerControl.cs | 36 ++--- src/Ursa/Controls/Drawer/DrawerControlBase.cs | 5 +- .../Controls/MessageBox/MessageBoxControl.cs | 29 ++-- .../Controls/MessageBox/MessageBoxWindow.cs | 136 +++++------------- .../OverlayShared/OverlayFeedbackElement.cs | 5 - .../ThemeSelector/ThemeToggleButton.cs | 9 +- src/Ursa/Ursa.csproj | 1 + 13 files changed, 96 insertions(+), 239 deletions(-) delete mode 100644 src/Ursa/Common/EventHelper.cs delete mode 100644 src/Ursa/Common/PropertyHelper.cs diff --git a/src/Ursa/Common/EventHelper.cs b/src/Ursa/Common/EventHelper.cs deleted file mode 100644 index 4b1dd09..0000000 --- a/src/Ursa/Common/EventHelper.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Avalonia.Controls; -using Avalonia.Interactivity; - -namespace Ursa.Common; - -internal static class EventHelper -{ - public static void RegisterClickEvent(EventHandler handler, params Button?[] buttons) - { - foreach (var button in buttons) - { - if(button is not null) button.Click += handler; - } - } - - public static void UnregisterClickEvent(EventHandler handler, params Button?[] buttons) - { - foreach (var button in buttons) - { - if(button is not null) button.Click -= handler; - } - } - - public static void RegisterEvent(RoutedEvent routedEvent, EventHandler handler, params Button?[] controls) - where TArgs : RoutedEventArgs - { - foreach (var control in controls) - { - control?.AddHandler(routedEvent, handler); - } - } - - public static void UnregisterEvent(RoutedEvent routedEvent, EventHandler handler, params Button?[] controls) - where TArgs : RoutedEventArgs - { - foreach (var control in controls) - { - control?.RemoveHandler(routedEvent, handler); - } - } -} \ No newline at end of file diff --git a/src/Ursa/Common/PropertyHelper.cs b/src/Ursa/Common/PropertyHelper.cs deleted file mode 100644 index c673e8b..0000000 --- a/src/Ursa/Common/PropertyHelper.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Avalonia; - -namespace Ursa.Common; - -public static class PropertyHelper -{ - public static void SetValue(AvaloniaProperty property, TValue value, params AvaloniaObject?[] elements) - { - foreach (var element in elements) - { - element?.SetValue(property, value); - } - } -} \ No newline at end of file diff --git a/src/Ursa/Controls/Dialog/DefaultDialogControl.cs b/src/Ursa/Controls/Dialog/DefaultDialogControl.cs index 0070925..ea21f32 100644 --- a/src/Ursa/Controls/Dialog/DefaultDialogControl.cs +++ b/src/Ursa/Controls/Dialog/DefaultDialogControl.cs @@ -4,6 +4,7 @@ using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Interactivity; +using Irihi.Avalonia.Shared.Helpers; using Ursa.Common; using Ursa.EventArgs; @@ -55,12 +56,12 @@ public class DefaultDialogControl: DialogControlBase protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); - EventHelper.UnregisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton); + Button.ClickEvent.RemoveHandler(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton); _okButton = e.NameScope.Find