From 01e97aec7ededc99af869d2701bfdb65ad620a52 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 5 Feb 2015 22:44:10 +0100 Subject: [PATCH] Removed Preview* tunneling events. You need to call AddHandler to get to them now. --- Perspex.Controls/Popup.cs | 9 ++++-- Perspex.Diagnostics/DevTools.cs | 6 ++-- Perspex.Input/InputElement.cs | 43 ++++++++++------------------ Perspex.Input/KeyboardDevice.cs | 4 +-- Perspex.Input/MouseDevice.cs | 4 +-- Perspex.Interactivity/Interactive.cs | 13 ++++++--- 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/Perspex.Controls/Popup.cs b/Perspex.Controls/Popup.cs index bcfc2fb769..35d3f0e51e 100644 --- a/Perspex.Controls/Popup.cs +++ b/Perspex.Controls/Popup.cs @@ -92,8 +92,13 @@ namespace Perspex.Controls this.popupRoot.SetPosition(this.GetPosition()); this.popupRoot.PointerPressed += this.MaybeClose; - this.topLevel.PreviewPointerPressed += this.MaybeClose; this.topLevel.Deactivated += this.MaybeClose; + + this.topLevel.AddHandler( + TopLevel.PointerPressedEvent, + (EventHandler)this.MaybeClose, + RoutingStrategies.Tunnel); + this.popupRoot.Show(); } @@ -102,7 +107,7 @@ namespace Perspex.Controls if (this.popupRoot != null) { this.popupRoot.PointerPressed -= this.MaybeClose; - this.topLevel.PreviewPointerPressed -= this.MaybeClose; + this.topLevel.RemoveHandler(TopLevel.PointerPressedEvent, (EventHandler)this.MaybeClose); this.topLevel.Deactivated -= this.MaybeClose; this.popupRoot.Hide(); } diff --git a/Perspex.Diagnostics/DevTools.cs b/Perspex.Diagnostics/DevTools.cs index 8f80c8a30d..131dc96afa 100644 --- a/Perspex.Diagnostics/DevTools.cs +++ b/Perspex.Diagnostics/DevTools.cs @@ -108,8 +108,10 @@ namespace Perspex.Diagnostics public static IDisposable Attach(Window w) { - w.PreviewKeyDown += WindowPreviewKeyDown; - return Disposable.Create(() => w.PreviewKeyDown -= WindowPreviewKeyDown); + return w.AddHandler( + Window.KeyDownEvent, + (EventHandler)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 e186728861..b775761321 100644 --- a/Perspex.Input/InputElement.cs +++ b/Perspex.Input/InputElement.cs @@ -36,13 +36,9 @@ namespace Perspex.Input RoutedEvent.Register("LostFocus", RoutingStrategies.Bubble); public static readonly RoutedEvent KeyDownEvent = - RoutedEvent.Register("KeyDown", RoutingStrategies.Bubble); - - public static readonly RoutedEvent PreviewKeyDownEvent = - RoutedEvent.Register("PreviewKeyDown", RoutingStrategies.Tunnel); - - public static readonly RoutedEvent PreviewPointerPressedEvent = - RoutedEvent.Register("PreviewPointerPressed", RoutingStrategies.Tunnel); + RoutedEvent.Register( + "KeyDown", + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); public static readonly RoutedEvent PointerEnterEvent = RoutedEvent.Register("PointerEnter", RoutingStrategies.Direct); @@ -51,16 +47,24 @@ namespace Perspex.Input RoutedEvent.Register("PointerLeave", RoutingStrategies.Direct); public static readonly RoutedEvent PointerMovedEvent = - RoutedEvent.Register("PointerMove", RoutingStrategies.Bubble); + RoutedEvent.Register( + "PointerMove", + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); public static readonly RoutedEvent PointerPressedEvent = - RoutedEvent.Register("PointerPressed", RoutingStrategies.Bubble); + RoutedEvent.Register( + "PointerPressed", + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); public static readonly RoutedEvent PointerReleasedEvent = - RoutedEvent.Register("PointerReleased", RoutingStrategies.Bubble); + RoutedEvent.Register( + "PointerReleased", + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); public static readonly RoutedEvent PointerWheelChangedEvent = - RoutedEvent.Register("PointerWheelChanged", RoutingStrategies.Bubble); + RoutedEvent.Register( + "PointerWheelChanged", + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); static InputElement() { @@ -69,7 +73,6 @@ namespace Perspex.Input GotFocusEvent.AddClassHandler(x => x.OnGotFocus, RoutingStrategies.Bubble); LostFocusEvent.AddClassHandler(x => x.OnLostFocus, RoutingStrategies.Bubble); KeyDownEvent.AddClassHandler(x => x.OnKeyDown, RoutingStrategies.Bubble); - PreviewKeyDownEvent.AddClassHandler(x => x.OnPreviewKeyDown, RoutingStrategies.Tunnel); PointerEnterEvent.AddClassHandler(x => x.OnPointerEnter, RoutingStrategies.Direct); PointerLeaveEvent.AddClassHandler(x => x.OnPointerLeave, RoutingStrategies.Direct); PointerMovedEvent.AddClassHandler(x => x.OnPointerMoved, RoutingStrategies.Bubble); @@ -96,18 +99,6 @@ namespace Perspex.Input remove { this.RemoveHandler(KeyDownEvent, value); } } - public event EventHandler PreviewKeyDown - { - add { this.AddHandler(PreviewKeyDownEvent, value); } - remove { this.RemoveHandler(PreviewKeyDownEvent, value); } - } - - public event EventHandler PreviewPointerPressed - { - add { this.AddHandler(PreviewPointerPressedEvent, value); } - remove { this.RemoveHandler(PreviewPointerPressedEvent, value); } - } - public event EventHandler PointerEnter { add { this.AddHandler(PointerEnterEvent, value); } @@ -211,10 +202,6 @@ namespace Perspex.Input { } - protected virtual void OnPreviewKeyDown(KeyEventArgs e) - { - } - protected virtual void OnPointerEnter(PointerEventArgs e) { this.IsPointerOver = true; diff --git a/Perspex.Input/KeyboardDevice.cs b/Perspex.Input/KeyboardDevice.cs index 199189c72c..e9f37d3efa 100644 --- a/Perspex.Input/KeyboardDevice.cs +++ b/Perspex.Input/KeyboardDevice.cs @@ -51,7 +51,7 @@ namespace Perspex.Input case RawKeyEventType.KeyDown: KeyEventArgs ev = new KeyEventArgs { - RoutedEvent = InputElement.PreviewKeyDownEvent, + RoutedEvent = InputElement.KeyDownEvent, Device = this, Key = e.Key, Text = e.Text, @@ -59,8 +59,6 @@ namespace Perspex.Input OriginalSource = element, }; - element.RaiseEvent(ev); - ev.RoutedEvent = InputElement.KeyDownEvent; element.RaiseEvent(ev); break; } diff --git a/Perspex.Input/MouseDevice.cs b/Perspex.Input/MouseDevice.cs index db1e7b7988..83b34d126c 100644 --- a/Perspex.Input/MouseDevice.cs +++ b/Perspex.Input/MouseDevice.cs @@ -131,15 +131,13 @@ namespace Perspex.Input var e = new PointerPressEventArgs { Device = this, - RoutedEvent = InputElement.PreviewPointerPressedEvent, + RoutedEvent = InputElement.PointerPressedEvent, OriginalSource = source, Source = source, ClickCount = this.clickCount, }; source.RaiseEvent(e); - e.RoutedEvent = InputElement.PointerPressedEvent; - source.RaiseEvent(e); } IInputElement focusable = this.GetFocusable(hit); diff --git a/Perspex.Interactivity/Interactive.cs b/Perspex.Interactivity/Interactive.cs index df39aa0d39..fe25293436 100644 --- a/Perspex.Interactivity/Interactive.cs +++ b/Perspex.Interactivity/Interactive.cs @@ -10,6 +10,7 @@ namespace Perspex.Interactivity using System.Collections.Generic; using System.Linq; using System.Reactive; + using System.Reactive.Disposables; using System.Reactive.Linq; using Perspex.Layout; using Perspex.VisualTree; @@ -19,7 +20,7 @@ namespace Perspex.Interactivity private Dictionary> eventHandlers = new Dictionary>(); - public void AddHandler( + public IDisposable AddHandler( RoutedEvent routedEvent, Delegate handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, @@ -36,12 +37,16 @@ namespace Perspex.Interactivity this.eventHandlers.Add(routedEvent, subscriptions); } - subscriptions.Add(new EventSubscription + var sub = new EventSubscription { Handler = handler, Routes = routes, AlsoIfHandled = handledEventsToo, - }); + }; + + subscriptions.Add(sub); + + return Disposable.Create(() => subscriptions.Remove(sub)); } public IObservable> GetObservable(RoutedEvent routedEvent) where T : RoutedEventArgs @@ -125,7 +130,7 @@ namespace Perspex.Interactivity if (this.eventHandlers.TryGetValue(e.RoutedEvent, out subscriptions)) { - foreach (var sub in subscriptions) + foreach (var sub in subscriptions.ToList()) { bool correctRoute = (e.Route == RoutingStrategies.Direct && sub.Routes == RoutingStrategies.Direct) ||