Browse Source

Added generic Add/RemoveHandler methods.

Prevents annoying casts when calling.
pull/39/head
Steven Kirk 11 years ago
parent
commit
a92219625a
  1. 7
      Perspex.Controls/Popup.cs
  2. 4
      Perspex.Controls/Presenters/ScrollContentPresenter.cs
  3. 5
      Perspex.Diagnostics/DevTools.cs
  4. 18
      Perspex.Input/InputElement.cs
  5. 15
      Perspex.Interactivity/Interactive.cs
  6. 6
      Perspex.Interactivity/RoutedEvent.cs

7
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<RoutedEventArgs>)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<RoutedEventArgs>)this.MaybeClose);
this.topLevel.RemoveHandler(TopLevel.PointerPressedEvent, this.MaybeClose);
this.topLevel.Deactivated -= this.MaybeClose;
this.popupRoot.Hide();
}

4
Perspex.Controls/Presenters/ScrollContentPresenter.cs

@ -36,9 +36,7 @@ namespace Perspex.Controls.Presenters
public ScrollContentPresenter()
{
this.AddHandler(
Control.RequestBringIntoViewEvent,
new EventHandler<RequestBringIntoViewEventArgs>(this.BringIntoViewRequested));
this.AddHandler(Control.RequestBringIntoViewEvent, this.BringIntoViewRequested);
}
public Size Extent

5
Perspex.Diagnostics/DevTools.cs

@ -108,10 +108,7 @@ namespace Perspex.Diagnostics
public static IDisposable Attach(Window w)
{
return w.AddHandler(
Window.KeyDownEvent,
(EventHandler<KeyEventArgs>)WindowPreviewKeyDown,
Interactivity.RoutingStrategies.Tunnel);
return w.AddHandler(Window.KeyDownEvent, WindowPreviewKeyDown, Interactivity.RoutingStrategies.Tunnel);
}
private static void WindowPreviewKeyDown(object sender, KeyEventArgs e)

18
Perspex.Input/InputElement.cs

@ -70,15 +70,15 @@ namespace Perspex.Input
{
IsEnabledProperty.Changed.Subscribe(IsEnabledChanged);
GotFocusEvent.AddClassHandler<InputElement>(x => x.OnGotFocus, RoutingStrategies.Bubble);
LostFocusEvent.AddClassHandler<InputElement>(x => x.OnLostFocus, RoutingStrategies.Bubble);
KeyDownEvent.AddClassHandler<InputElement>(x => x.OnKeyDown, RoutingStrategies.Bubble);
PointerEnterEvent.AddClassHandler<InputElement>(x => x.OnPointerEnter, RoutingStrategies.Direct);
PointerLeaveEvent.AddClassHandler<InputElement>(x => x.OnPointerLeave, RoutingStrategies.Direct);
PointerMovedEvent.AddClassHandler<InputElement>(x => x.OnPointerMoved, RoutingStrategies.Bubble);
PointerPressedEvent.AddClassHandler<InputElement>(x => x.OnPointerPressed, RoutingStrategies.Bubble);
PointerReleasedEvent.AddClassHandler<InputElement>(x => x.OnPointerReleased, RoutingStrategies.Bubble);
PointerWheelChangedEvent.AddClassHandler<InputElement>(x => x.OnPointerWheelChanged, RoutingStrategies.Bubble);
GotFocusEvent.AddClassHandler<InputElement>(x => x.OnGotFocus);
LostFocusEvent.AddClassHandler<InputElement>(x => x.OnLostFocus);
KeyDownEvent.AddClassHandler<InputElement>(x => x.OnKeyDown);
PointerEnterEvent.AddClassHandler<InputElement>(x => x.OnPointerEnter);
PointerLeaveEvent.AddClassHandler<InputElement>(x => x.OnPointerLeave);
PointerMovedEvent.AddClassHandler<InputElement>(x => x.OnPointerMoved);
PointerPressedEvent.AddClassHandler<InputElement>(x => x.OnPointerPressed);
PointerReleasedEvent.AddClassHandler<InputElement>(x => x.OnPointerReleased);
PointerWheelChangedEvent.AddClassHandler<InputElement>(x => x.OnPointerWheelChanged);
}
public event EventHandler<RoutedEventArgs> GotFocus

15
Perspex.Interactivity/Interactive.cs

@ -49,6 +49,15 @@ namespace Perspex.Interactivity
return Disposable.Create(() => subscriptions.Remove(sub));
}
public IDisposable AddHandler<TEventArgs>(
RoutedEvent<TEventArgs> routedEvent,
EventHandler<TEventArgs> handler,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false) where TEventArgs : RoutedEventArgs
{
return this.AddHandler(routedEvent, (Delegate)handler, routes, handledEventsToo);
}
public IObservable<EventPattern<T>> GetObservable<T>(RoutedEvent<T> routedEvent) where T : RoutedEventArgs
{
Contract.Requires<NullReferenceException>(routedEvent != null);
@ -71,6 +80,12 @@ namespace Perspex.Interactivity
}
}
public void RemoveHandler<TEventArgs>(RoutedEvent<TEventArgs> routedEvent, EventHandler<TEventArgs> handler)
where TEventArgs : RoutedEventArgs
{
this.RemoveHandler(routedEvent, (Delegate)handler);
}
public void RaiseEvent(RoutedEventArgs e)
{
Contract.Requires<NullReferenceException>(e != null);

6
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<TTarget>(
Func<TTarget, Action<TEventArgs>> handler,
RoutingStrategies routes) where TTarget : class
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble)
where TTarget : class
{
var adaptor = (EventHandler<RoutedEventArgs>)((s, e) => handler((TTarget)s)((TEventArgs)e));
this.AddClassHandler(typeof(TTarget), adaptor, routes);

Loading…
Cancel
Save