diff --git a/Perspex.Controls/Popup.cs b/Perspex.Controls/Popup.cs index 35d3f0e51e..f872397acf 100644 --- a/Perspex.Controls/Popup.cs +++ b/Perspex.Controls/Popup.cs @@ -94,10 +94,7 @@ namespace Perspex.Controls this.popupRoot.PointerPressed += this.MaybeClose; this.topLevel.Deactivated += this.MaybeClose; - this.topLevel.AddHandler( - TopLevel.PointerPressedEvent, - (EventHandler)this.MaybeClose, - RoutingStrategies.Tunnel); + this.topLevel.AddHandler(TopLevel.PointerPressedEvent, this.MaybeClose, RoutingStrategies.Tunnel); this.popupRoot.Show(); } @@ -107,7 +104,7 @@ namespace Perspex.Controls if (this.popupRoot != null) { this.popupRoot.PointerPressed -= this.MaybeClose; - this.topLevel.RemoveHandler(TopLevel.PointerPressedEvent, (EventHandler)this.MaybeClose); + this.topLevel.RemoveHandler(TopLevel.PointerPressedEvent, this.MaybeClose); this.topLevel.Deactivated -= this.MaybeClose; this.popupRoot.Hide(); } diff --git a/Perspex.Controls/Presenters/ScrollContentPresenter.cs b/Perspex.Controls/Presenters/ScrollContentPresenter.cs index 5e04878220..89458b834b 100644 --- a/Perspex.Controls/Presenters/ScrollContentPresenter.cs +++ b/Perspex.Controls/Presenters/ScrollContentPresenter.cs @@ -36,9 +36,7 @@ namespace Perspex.Controls.Presenters public ScrollContentPresenter() { - this.AddHandler( - Control.RequestBringIntoViewEvent, - new EventHandler(this.BringIntoViewRequested)); + this.AddHandler(Control.RequestBringIntoViewEvent, this.BringIntoViewRequested); } public Size Extent diff --git a/Perspex.Diagnostics/DevTools.cs b/Perspex.Diagnostics/DevTools.cs index 131dc96afa..085afe0786 100644 --- a/Perspex.Diagnostics/DevTools.cs +++ b/Perspex.Diagnostics/DevTools.cs @@ -108,10 +108,7 @@ namespace Perspex.Diagnostics public static IDisposable Attach(Window w) { - return w.AddHandler( - Window.KeyDownEvent, - (EventHandler)WindowPreviewKeyDown, - Interactivity.RoutingStrategies.Tunnel); + return w.AddHandler(Window.KeyDownEvent, WindowPreviewKeyDown, Interactivity.RoutingStrategies.Tunnel); } private static void WindowPreviewKeyDown(object sender, KeyEventArgs e) diff --git a/Perspex.Input/InputElement.cs b/Perspex.Input/InputElement.cs index b775761321..9ebcf8224f 100644 --- a/Perspex.Input/InputElement.cs +++ b/Perspex.Input/InputElement.cs @@ -70,15 +70,15 @@ namespace Perspex.Input { IsEnabledProperty.Changed.Subscribe(IsEnabledChanged); - GotFocusEvent.AddClassHandler(x => x.OnGotFocus, RoutingStrategies.Bubble); - LostFocusEvent.AddClassHandler(x => x.OnLostFocus, RoutingStrategies.Bubble); - KeyDownEvent.AddClassHandler(x => x.OnKeyDown, RoutingStrategies.Bubble); - PointerEnterEvent.AddClassHandler(x => x.OnPointerEnter, RoutingStrategies.Direct); - PointerLeaveEvent.AddClassHandler(x => x.OnPointerLeave, RoutingStrategies.Direct); - PointerMovedEvent.AddClassHandler(x => x.OnPointerMoved, RoutingStrategies.Bubble); - PointerPressedEvent.AddClassHandler(x => x.OnPointerPressed, RoutingStrategies.Bubble); - PointerReleasedEvent.AddClassHandler(x => x.OnPointerReleased, RoutingStrategies.Bubble); - PointerWheelChangedEvent.AddClassHandler(x => x.OnPointerWheelChanged, RoutingStrategies.Bubble); + GotFocusEvent.AddClassHandler(x => x.OnGotFocus); + LostFocusEvent.AddClassHandler(x => x.OnLostFocus); + KeyDownEvent.AddClassHandler(x => x.OnKeyDown); + PointerEnterEvent.AddClassHandler(x => x.OnPointerEnter); + PointerLeaveEvent.AddClassHandler(x => x.OnPointerLeave); + PointerMovedEvent.AddClassHandler(x => x.OnPointerMoved); + PointerPressedEvent.AddClassHandler(x => x.OnPointerPressed); + PointerReleasedEvent.AddClassHandler(x => x.OnPointerReleased); + PointerWheelChangedEvent.AddClassHandler(x => x.OnPointerWheelChanged); } public event EventHandler GotFocus diff --git a/Perspex.Interactivity/Interactive.cs b/Perspex.Interactivity/Interactive.cs index 915cccbcf3..493b0c3380 100644 --- a/Perspex.Interactivity/Interactive.cs +++ b/Perspex.Interactivity/Interactive.cs @@ -49,6 +49,15 @@ namespace Perspex.Interactivity return Disposable.Create(() => subscriptions.Remove(sub)); } + public IDisposable AddHandler( + RoutedEvent routedEvent, + EventHandler handler, + RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, + bool handledEventsToo = false) where TEventArgs : RoutedEventArgs + { + return this.AddHandler(routedEvent, (Delegate)handler, routes, handledEventsToo); + } + public IObservable> GetObservable(RoutedEvent routedEvent) where T : RoutedEventArgs { Contract.Requires(routedEvent != null); @@ -71,6 +80,12 @@ namespace Perspex.Interactivity } } + public void RemoveHandler(RoutedEvent routedEvent, EventHandler handler) + where TEventArgs : RoutedEventArgs + { + this.RemoveHandler(routedEvent, (Delegate)handler); + } + public void RaiseEvent(RoutedEventArgs e) { Contract.Requires(e != null); diff --git a/Perspex.Interactivity/RoutedEvent.cs b/Perspex.Interactivity/RoutedEvent.cs index 6bf85a294c..0e35db16da 100644 --- a/Perspex.Interactivity/RoutedEvent.cs +++ b/Perspex.Interactivity/RoutedEvent.cs @@ -104,8 +104,7 @@ namespace Perspex.Interactivity foreach (var sub in this.subscriptions) { if (sub.TargetType.GetTypeInfo().IsAssignableFrom(sender.GetType().GetTypeInfo()) && - (e.Route == RoutingStrategies.Direct && sub.Routes == RoutingStrategies.Direct) || - (e.Route != RoutingStrategies.Direct && (e.Route & sub.Routes) != 0)) + (e.Route == RoutingStrategies.Direct) || (e.Route & sub.Routes) != 0) { sub.Handler.DynamicInvoke(sender, e); } @@ -131,7 +130,8 @@ namespace Perspex.Interactivity public void AddClassHandler( Func> handler, - RoutingStrategies routes) where TTarget : class + RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble) + where TTarget : class { var adaptor = (EventHandler)((s, e) => handler((TTarget)s)((TEventArgs)e)); this.AddClassHandler(typeof(TTarget), adaptor, routes);