// 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.Linq;
namespace Avalonia.Interactivity
{
///
/// Provides extension methods for the interface.
///
public static class InteractiveExtensions
{
///
/// Gets an observable for a .
///
/// The object to listen for events on.
/// The routed event.
/// The routing strategies to listen to.
/// Whether handled events should also be listened for.
///
/// An observable which fires each time the event is raised.
///
public static IObservable GetObservable(
this IInteractive o,
RoutedEvent routedEvent,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false)
where TEventArgs : RoutedEventArgs
{
return Observable.Create(x => o.AddHandler(
routedEvent,
(_, e) => x.OnNext(e),
routes,
handledEventsToo));
}
///
/// Gets the route for bubbling events from the specified interactive.
///
/// The interactive.
/// The event route.
internal static IEnumerable GetBubbleEventRoute(this IInteractive interactive)
{
while (interactive != null)
{
yield return interactive;
interactive = interactive.InteractiveParent;
}
}
///
/// Gets the route for tunneling events from the specified interactive.
///
/// The interactive.
/// The event route.
internal static IEnumerable GetTunnelEventRoute(this IInteractive interactive)
{
return interactive.GetBubbleEventRoute().Reverse();
}
}
}