From 1018a7779565d185aa2f1c12775ede2f9ba0c8c3 Mon Sep 17 00:00:00 2001 From: robloo Date: Sat, 18 Feb 2023 21:25:19 -0500 Subject: [PATCH] Switch to routed events in SplitView and support cancellation in opening/closing --- src/Avalonia.Controls/SplitView/SplitView.cs | 109 +++++++++++++++---- 1 file changed, 85 insertions(+), 24 deletions(-) diff --git a/src/Avalonia.Controls/SplitView/SplitView.cs b/src/Avalonia.Controls/SplitView/SplitView.cs index da8190d764..1099a40f08 100644 --- a/src/Avalonia.Controls/SplitView/SplitView.cs +++ b/src/Avalonia.Controls/SplitView/SplitView.cs @@ -97,6 +97,38 @@ namespace Avalonia.Controls public static readonly StyledProperty TemplateSettingsProperty = AvaloniaProperty.Register(nameof(TemplateSettings)); + /// + /// Defines the event. + /// + public static readonly RoutedEvent PaneClosedEvent = + RoutedEvent.Register( + nameof(PaneClosed), + RoutingStrategies.Bubble); + + /// + /// Defines the event. + /// + public static readonly RoutedEvent PaneClosingEvent = + RoutedEvent.Register( + nameof(PaneClosing), + RoutingStrategies.Bubble); + + /// + /// Defines the event. + /// + public static readonly RoutedEvent PaneOpenedEvent = + RoutedEvent.Register( + nameof(PaneOpened), + RoutingStrategies.Bubble); + + /// + /// Defines the event. + /// + public static readonly RoutedEvent PaneOpeningEvent = + RoutedEvent.Register( + nameof(PaneOpening), + RoutingStrategies.Bubble); + private Panel? _pane; private IDisposable? _pointerDisposable; @@ -208,24 +240,48 @@ namespace Avalonia.Controls } /// - /// Fired when the pane is closed + /// Fired when the pane is closed. /// - public event EventHandler? PaneClosed; + public event EventHandler? PaneClosed + { + add => AddHandler(PaneClosedEvent, value); + remove => RemoveHandler(PaneClosedEvent, value); + } /// - /// Fired when the pane is closing + /// Fired when the pane is closing. /// - public event EventHandler? PaneClosing; + /// + /// The event args property may be set to true to cancel the event + /// and keep the pane open. + /// + public event EventHandler? PaneClosing + { + add => AddHandler(PaneClosingEvent, value); + remove => RemoveHandler(PaneClosingEvent, value); + } /// - /// Fired when the pane is opened + /// Fired when the pane is opened. /// - public event EventHandler? PaneOpened; + public event EventHandler? PaneOpened + { + add => AddHandler(PaneOpenedEvent, value); + remove => RemoveHandler(PaneOpenedEvent, value); + } /// - /// Fired when the pane is opening + /// Fired when the pane is opening. /// - public event EventHandler? PaneOpening; + /// + /// The event args property may be set to true to cancel the event + /// and keep the pane closed. + /// + public event EventHandler? PaneOpening + { + add => AddHandler(PaneOpeningEvent, value); + remove => RemoveHandler(PaneOpeningEvent, value); + } protected override bool RegisterContentPresenter(IContentPresenter presenter) { @@ -308,13 +364,15 @@ namespace Avalonia.Controls { PseudoClasses.Add(":open"); PseudoClasses.Remove(":closed"); - OnPaneOpened(EventArgs.Empty); + + OnPaneOpened(new RoutedEventArgs(PaneOpenedEvent, this)); } else { PseudoClasses.Add(":closed"); PseudoClasses.Remove(":open"); - OnPaneClosed(EventArgs.Empty); + + OnPaneClosed(new RoutedEventArgs(PaneClosedEvent, this)); } } else if (change.Property == PaneProperty) @@ -386,24 +444,24 @@ namespace Avalonia.Controls return (DisplayMode == SplitViewDisplayMode.CompactOverlay || DisplayMode == SplitViewDisplayMode.Overlay); } - protected virtual void OnPaneOpening(EventArgs args) + protected virtual void OnPaneOpening(CancelRoutedEventArgs args) { - PaneOpening?.Invoke(this, args); + RaiseEvent(args); } - protected virtual void OnPaneOpened(EventArgs args) + protected virtual void OnPaneOpened(RoutedEventArgs args) { - PaneOpened?.Invoke(this, args); + RaiseEvent(args); } - protected virtual void OnPaneClosing(SplitViewPaneClosingEventArgs args) + protected virtual void OnPaneClosing(CancelRoutedEventArgs args) { - PaneClosing?.Invoke(this, args); + RaiseEvent(args); } - protected virtual void OnPaneClosed(EventArgs args) + protected virtual void OnPaneClosed(RoutedEventArgs args) { - PaneClosed?.Invoke(this, args); + RaiseEvent(args); } /// @@ -440,19 +498,22 @@ namespace Avalonia.Controls /// The value to coerce. protected virtual bool OnCoerceIsPaneOpen(bool value) { + CancelRoutedEventArgs eventArgs; + if (value) { - OnPaneOpening(EventArgs.Empty); + eventArgs = new CancelRoutedEventArgs(PaneOpeningEvent, this); + OnPaneOpening(eventArgs); } else { - var eventArgs = new SplitViewPaneClosingEventArgs(false); + eventArgs = new CancelRoutedEventArgs(PaneClosingEvent, this); OnPaneClosing(eventArgs); + } - if (eventArgs.Cancel) - { - return !value; - } + if (eventArgs.Cancel) + { + return !value; } return value;