// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Avalonia.Layout;
using Avalonia.VisualTree;
namespace Avalonia.Interactivity
{
///
/// Base class for objects that raise routed events.
///
public class Interactive : Layoutable, IInteractive
{
private Dictionary> _eventHandlers;
///
/// Gets the interactive parent of the object for bubbling and tunnelling events.
///
IInteractive IInteractive.InteractiveParent => ((IVisual)this).VisualParent as IInteractive;
private Dictionary> EventHandlers
{
get { return _eventHandlers ?? (_eventHandlers = new Dictionary>()); }
}
///
/// Adds a handler for the specified routed event.
///
/// The routed event.
/// The handler.
/// The routing strategies to listen to.
/// Whether handled events should also be listened for.
/// A disposable that terminates the event subscription.
public IDisposable AddHandler(
RoutedEvent routedEvent,
Delegate handler,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false)
{
Contract.Requires(routedEvent != null);
Contract.Requires(handler != null);
List subscriptions;
if (!EventHandlers.TryGetValue(routedEvent, out subscriptions))
{
subscriptions = new List();
EventHandlers.Add(routedEvent, subscriptions);
}
var sub = new EventSubscription
{
Handler = handler,
Routes = routes,
AlsoIfHandled = handledEventsToo,
};
subscriptions.Add(sub);
return Disposable.Create(() => subscriptions.Remove(sub));
}
///
/// Adds a handler for the specified routed event.
///
/// The type of the event's args.
/// The routed event.
/// The handler.
/// The routing strategies to listen to.
/// Whether handled events should also be listened for.
/// A disposable that terminates the event subscription.
public IDisposable AddHandler(
RoutedEvent routedEvent,
EventHandler handler,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false) where TEventArgs : RoutedEventArgs
{
return AddHandler(routedEvent, (Delegate)handler, routes, handledEventsToo);
}
///
/// Removes a handler for the specified routed event.
///
/// The routed event.
/// The handler.
public void RemoveHandler(RoutedEvent routedEvent, Delegate handler)
{
Contract.Requires(routedEvent != null);
Contract.Requires(handler != null);
List subscriptions = null;
if (_eventHandlers?.TryGetValue(routedEvent, out subscriptions) == true)
{
subscriptions.RemoveAll(x => x.Handler == handler);
}
}
///
/// Removes a handler for the specified routed event.
///
/// The type of the event's args.
/// The routed event.
/// The handler.
public void RemoveHandler(RoutedEvent routedEvent, EventHandler handler)
where TEventArgs : RoutedEventArgs
{
RemoveHandler(routedEvent, (Delegate)handler);
}
///
/// Raises a routed event.
///
/// The event args.
public void RaiseEvent(RoutedEventArgs e)
{
Contract.Requires(e != null);
e.Source = e.Source ?? this;
if (e.RoutedEvent.RoutingStrategies == RoutingStrategies.Direct)
{
e.Route = RoutingStrategies.Direct;
RaiseEventImpl(e);
e.RoutedEvent.InvokeRouteFinished(e);
}
if ((e.RoutedEvent.RoutingStrategies & RoutingStrategies.Tunnel) != 0)
{
TunnelEvent(e);
e.RoutedEvent.InvokeRouteFinished(e);
}
if ((e.RoutedEvent.RoutingStrategies & RoutingStrategies.Bubble) != 0)
{
BubbleEvent(e);
e.RoutedEvent.InvokeRouteFinished(e);
}
}
///
/// Bubbles an event.
///
/// The event args.
private void BubbleEvent(RoutedEventArgs e)
{
Contract.Requires(e != null);
e.Route = RoutingStrategies.Bubble;
foreach (var target in this.GetBubbleEventRoute())
{
((Interactive)target).RaiseEventImpl(e);
}
}
///
/// Tunnels an event.
///
/// The event args.
private void TunnelEvent(RoutedEventArgs e)
{
Contract.Requires(e != null);
e.Route = RoutingStrategies.Tunnel;
foreach (var target in this.GetTunnelEventRoute())
{
((Interactive)target).RaiseEventImpl(e);
}
}
///
/// Carries out the actual invocation of an event on this object.
///
/// The event args.
private void RaiseEventImpl(RoutedEventArgs e)
{
Contract.Requires(e != null);
e.RoutedEvent.InvokeRaised(this, e);
List subscriptions = null;
if (_eventHandlers?.TryGetValue(e.RoutedEvent, out subscriptions) == true)
{
foreach (var sub in subscriptions.ToList())
{
bool correctRoute = (e.Route & sub.Routes) != 0;
bool notFinished = !e.Handled || sub.AlsoIfHandled;
if (correctRoute && notFinished)
{
sub.Handler.DynamicInvoke(this, e);
}
}
}
}
}
}