csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.8 KiB
124 lines
3.8 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="Interactive.cs" company="Steven Kirk">
|
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive;
|
|
using System.Reactive.Linq;
|
|
using Perspex.Layout;
|
|
|
|
public class Interactive : Layoutable
|
|
{
|
|
private Dictionary<RoutedEvent, List<Delegate>> eventHandlers = new Dictionary<RoutedEvent, List<Delegate>>();
|
|
|
|
public void AddHandler(RoutedEvent routedEvent, Delegate handler)
|
|
{
|
|
Contract.Requires<NullReferenceException>(routedEvent != null);
|
|
Contract.Requires<NullReferenceException>(handler != null);
|
|
|
|
List<Delegate> delegates;
|
|
|
|
if (!this.eventHandlers.TryGetValue(routedEvent, out delegates))
|
|
{
|
|
delegates = new List<Delegate>();
|
|
this.eventHandlers.Add(routedEvent, delegates);
|
|
}
|
|
|
|
delegates.Add(handler);
|
|
}
|
|
|
|
public IObservable<EventPattern<T>> GetObservable<T>(RoutedEvent<T> routedEvent) where T : RoutedEventArgs
|
|
{
|
|
Contract.Requires<NullReferenceException>(routedEvent != null);
|
|
|
|
return Observable.FromEventPattern<T>(
|
|
handler => this.AddHandler(routedEvent, handler),
|
|
handler => this.RemoveHandler(routedEvent, handler));
|
|
}
|
|
|
|
public void RemoveHandler(RoutedEvent routedEvent, Delegate handler)
|
|
{
|
|
Contract.Requires<NullReferenceException>(routedEvent != null);
|
|
Contract.Requires<NullReferenceException>(handler != null);
|
|
|
|
List<Delegate> delegates;
|
|
|
|
if (this.eventHandlers.TryGetValue(routedEvent, out delegates))
|
|
{
|
|
delegates.Remove(handler);
|
|
}
|
|
}
|
|
|
|
public void RaiseEvent(RoutedEventArgs e)
|
|
{
|
|
Contract.Requires<NullReferenceException>(e != null);
|
|
|
|
if (e.RoutedEvent != null)
|
|
{
|
|
switch (e.RoutedEvent.RoutingStrategy)
|
|
{
|
|
case RoutingStrategy.Bubble:
|
|
this.BubbleEvent(e);
|
|
break;
|
|
case RoutingStrategy.Direct:
|
|
this.RaiseEventImpl(e);
|
|
break;
|
|
case RoutingStrategy.Tunnel:
|
|
this.TunnelEvent(e);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void BubbleEvent(RoutedEventArgs e)
|
|
{
|
|
Contract.Requires<NullReferenceException>(e != null);
|
|
|
|
foreach (var target in this.GetVisualAncestorsAndSelf().OfType<Interactive>())
|
|
{
|
|
target.RaiseEventImpl(e);
|
|
|
|
if (e.Handled)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void TunnelEvent(RoutedEventArgs e)
|
|
{
|
|
Contract.Requires<NullReferenceException>(e != null);
|
|
|
|
foreach (var target in this.GetVisualAncestorsAndSelf().OfType<Interactive>().Reverse())
|
|
{
|
|
target.RaiseEventImpl(e);
|
|
|
|
if (e.Handled)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RaiseEventImpl(RoutedEventArgs e)
|
|
{
|
|
Contract.Requires<NullReferenceException>(e != null);
|
|
|
|
List<Delegate> delegates;
|
|
|
|
if (this.eventHandlers.TryGetValue(e.RoutedEvent, out delegates))
|
|
{
|
|
foreach (Delegate handler in delegates)
|
|
{
|
|
handler.DynamicInvoke(this, e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|