-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
RoutedEventTriggerBehavior.cs
136 lines (117 loc) · 4.54 KB
/
RoutedEventTriggerBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Reactive;
using Avalonia.Xaml.Interactivity;
namespace Avalonia.Xaml.Interactions.Custom;
/// <summary>
/// A behavior that listens for a <see cref="RoutedEvent"/> event on its source and executes its actions when that event is fired.
/// </summary>
public class RoutedEventTriggerBehavior : Trigger<Interactive>
{
/// <summary>
/// Identifies the <seealso cref="RoutedEvent"/> avalonia property.
/// </summary>
public static readonly StyledProperty<RoutedEvent?> RoutedEventProperty =
AvaloniaProperty.Register<RoutedEventTriggerBehavior, RoutedEvent?>(nameof(RoutedEvent));
/// <summary>
/// Identifies the <seealso cref="RoutingStrategies"/> avalonia property.
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<RoutedEventTriggerBehavior, RoutingStrategies>(nameof(RoutingStrategies), RoutingStrategies.Direct | RoutingStrategies.Bubble);
/// <summary>
/// Identifies the <seealso cref="SourceInteractive"/> avalonia property.
/// </summary>
public static readonly StyledProperty<Interactive?> SourceInteractiveProperty =
AvaloniaProperty.Register<RoutedEventTriggerBehavior, Interactive?>(nameof(SourceInteractive));
private bool _isInitialized;
private bool _isAttached;
/// <summary>
/// Gets or sets routing event to listen for. This is a avalonia property.
/// </summary>
public RoutedEvent? RoutedEvent
{
get => GetValue(RoutedEventProperty);
set => SetValue(RoutedEventProperty, value);
}
/// <summary>
/// Gets or sets the routing event <see cref="RoutingStrategies"/>. This is a avalonia property.
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}
/// <summary>
/// Gets or sets the source object from which this behavior listens for events.
/// If <seealso cref="SourceInteractive"/> is not set, the source will default to <seealso cref="Behavior.AssociatedObject"/>. This is a avalonia property.
/// </summary>
[ResolveByName]
public Interactive? SourceInteractive
{
get => GetValue(SourceInteractiveProperty);
set => SetValue(SourceInteractiveProperty, value);
}
static RoutedEventTriggerBehavior()
{
RoutedEventProperty.Changed.Subscribe(
new AnonymousObserver<AvaloniaPropertyChangedEventArgs<RoutedEvent?>>(OnValueChanged));
RoutingStrategiesProperty.Changed.Subscribe(
new AnonymousObserver<AvaloniaPropertyChangedEventArgs<RoutingStrategies>>(OnValueChanged));
SourceInteractiveProperty.Changed.Subscribe(
new AnonymousObserver<AvaloniaPropertyChangedEventArgs<Interactive?>>(OnValueChanged));
}
private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args)
{
if (args.Sender is not RoutedEventTriggerBehavior behavior || behavior.AssociatedObject is null)
{
return;
}
if (behavior._isInitialized && behavior._isAttached)
{
behavior.RemoveHandler();
behavior.AddHandler();
}
}
/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
_isAttached = true;
AddHandler();
}
/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
_isAttached = false;
RemoveHandler();
}
private void AddHandler()
{
var interactive = ComputeResolvedSourceInteractive();
if (interactive is { } && RoutedEvent is { })
{
interactive.AddHandler(RoutedEvent, Handler, RoutingStrategies);
_isInitialized = true;
}
}
private void RemoveHandler()
{
var interactive = ComputeResolvedSourceInteractive();
if (interactive is { } && RoutedEvent is { } && _isInitialized)
{
interactive.RemoveHandler(RoutedEvent, Handler);
_isInitialized = false;
}
}
private Interactive? ComputeResolvedSourceInteractive()
{
return GetValue(SourceInteractiveProperty) is { } ? SourceInteractive : AssociatedObject;
}
private void Handler(object? sender, RoutedEventArgs e)
{
var interactive = ComputeResolvedSourceInteractive();
if (interactive is { })
{
Interaction.ExecuteActions(interactive, Actions, e);
}
}
}