Skip to content

Commit 64ae7e0

Browse files
committed
Add more input element triggers
1 parent b08965b commit 64ae7e0

File tree

7 files changed

+311
-0
lines changed

7 files changed

+311
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Interactivity;
4+
using Avalonia.Xaml.Interactivity;
5+
6+
namespace Avalonia.Xaml.Interactions.Custom;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public class DoubleTappedTrigger : RoutedEventTriggerBase
12+
{
13+
/// <summary>
14+
///
15+
/// </summary>
16+
/// <param name="disposables"></param>
17+
protected override void OnAttached(CompositeDisposable disposables)
18+
{
19+
if (AssociatedObject is InputElement element)
20+
{
21+
var disposable = element.AddDisposableHandler(
22+
InputElement.DoubleTappedEvent,
23+
OnDoubleTapped,
24+
EventRoutingStrategy);
25+
disposables.Add(disposable);
26+
}
27+
}
28+
29+
private void OnDoubleTapped(object? sender, TappedEventArgs e)
30+
{
31+
if (!IsEnabled)
32+
{
33+
return;
34+
}
35+
36+
e.Handled = MarkAsHandled;
37+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Interactivity;
4+
using Avalonia.Xaml.Interactivity;
5+
6+
namespace Avalonia.Xaml.Interactions.Custom;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public class GotFocusTrigger : RoutedEventTriggerBase
12+
{
13+
/// <summary>
14+
///
15+
/// </summary>
16+
/// <param name="disposables"></param>
17+
protected override void OnAttached(CompositeDisposable disposables)
18+
{
19+
if (AssociatedObject is InputElement element)
20+
{
21+
var disposable = element.AddDisposableHandler(
22+
InputElement.GotFocusEvent,
23+
OnGotFocus,
24+
EventRoutingStrategy);
25+
disposables.Add(disposable);
26+
}
27+
}
28+
29+
private void OnGotFocus(object? sender, GotFocusEventArgs e)
30+
{
31+
if (!IsEnabled)
32+
{
33+
return;
34+
}
35+
36+
e.Handled = MarkAsHandled;
37+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Interactivity;
4+
using Avalonia.Xaml.Interactivity;
5+
6+
namespace Avalonia.Xaml.Interactions.Custom;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public class HoldingTrigger : RoutedEventTriggerBase
12+
{
13+
/// <summary>
14+
///
15+
/// </summary>
16+
/// <param name="disposables"></param>
17+
protected override void OnAttached(CompositeDisposable disposables)
18+
{
19+
if (AssociatedObject is InputElement element)
20+
{
21+
var disposable = element.AddDisposableHandler(
22+
InputElement.HoldingEvent,
23+
OnHolding,
24+
EventRoutingStrategy);
25+
disposables.Add(disposable);
26+
}
27+
}
28+
29+
private void OnHolding(object? sender, HoldingRoutedEventArgs e)
30+
{
31+
if (!IsEnabled)
32+
{
33+
return;
34+
}
35+
36+
e.Handled = MarkAsHandled;
37+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Interactivity;
4+
using Avalonia.Xaml.Interactivity;
5+
6+
namespace Avalonia.Xaml.Interactions.Custom;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public class LostFocusTrigger : RoutedEventTriggerBase
12+
{
13+
/// <summary>
14+
///
15+
/// </summary>
16+
/// <param name="disposables"></param>
17+
protected override void OnAttached(CompositeDisposable disposables)
18+
{
19+
if (AssociatedObject is InputElement element)
20+
{
21+
var disposable = element.AddDisposableHandler(
22+
InputElement.LostFocusEvent,
23+
OnLostFocus,
24+
EventRoutingStrategy);
25+
disposables.Add(disposable);
26+
}
27+
}
28+
29+
private void OnLostFocus(object? sender, RoutedEventArgs e)
30+
{
31+
if (!IsEnabled)
32+
{
33+
return;
34+
}
35+
36+
e.Handled = MarkAsHandled;
37+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Interactivity;
4+
using Avalonia.Xaml.Interactivity;
5+
6+
namespace Avalonia.Xaml.Interactions.Custom;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public class TappedTrigger : RoutedEventTriggerBase
12+
{
13+
/// <summary>
14+
///
15+
/// </summary>
16+
/// <param name="disposables"></param>
17+
protected override void OnAttached(CompositeDisposable disposables)
18+
{
19+
if (AssociatedObject is InputElement element)
20+
{
21+
var disposable = element.AddDisposableHandler(
22+
InputElement.TappedEvent,
23+
OnTapped,
24+
EventRoutingStrategy);
25+
disposables.Add(disposable);
26+
}
27+
}
28+
29+
private void OnTapped(object? sender, TappedEventArgs e)
30+
{
31+
if (!IsEnabled)
32+
{
33+
return;
34+
}
35+
36+
e.Handled = MarkAsHandled;
37+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Input.TextInput;
4+
using Avalonia.Interactivity;
5+
using Avalonia.Xaml.Interactivity;
6+
7+
namespace Avalonia.Xaml.Interactions.Custom;
8+
9+
/// <summary>
10+
///
11+
/// </summary>
12+
public class TextInputMethodClientRequestedTrigger : RoutedEventTriggerBase
13+
{
14+
static TextInputMethodClientRequestedTrigger()
15+
{
16+
EventRoutingStrategyProperty.OverrideMetadata<TextInputMethodClientRequestedTrigger>(
17+
new StyledPropertyMetadata<RoutingStrategies>(
18+
defaultValue: RoutingStrategies.Tunnel | RoutingStrategies.Bubble));
19+
}
20+
21+
/// <summary>
22+
///
23+
/// </summary>
24+
/// <param name="disposables"></param>
25+
protected override void OnAttached(CompositeDisposable disposables)
26+
{
27+
if (AssociatedObject is InputElement element)
28+
{
29+
var disposable = element.AddDisposableHandler(
30+
InputElement.TextInputMethodClientRequestedEvent,
31+
OnTextInputMethodClientRequested,
32+
EventRoutingStrategy);
33+
disposables.Add(disposable);
34+
}
35+
}
36+
37+
private void OnTextInputMethodClientRequested(object? sender, TextInputMethodClientRequestedEventArgs e)
38+
{
39+
if (!IsEnabled)
40+
{
41+
return;
42+
}
43+
44+
e.Handled = MarkAsHandled;
45+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Reactive.Disposables;
2+
using Avalonia.Input;
3+
using Avalonia.Interactivity;
4+
using Avalonia.Xaml.Interactivity;
5+
6+
namespace Avalonia.Xaml.Interactions.Custom;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public class TextInputTrigger : RoutedEventTriggerBase
12+
{
13+
static TextInputTrigger()
14+
{
15+
EventRoutingStrategyProperty.OverrideMetadata<TextInputTrigger>(
16+
new StyledPropertyMetadata<RoutingStrategies>(
17+
defaultValue: RoutingStrategies.Tunnel | RoutingStrategies.Bubble));
18+
}
19+
20+
/// <summary>
21+
///
22+
/// </summary>
23+
public static readonly StyledProperty<string?> TextProperty =
24+
AvaloniaProperty.Register<KeyDownTrigger, string?>(nameof(Text));
25+
26+
/// <summary>
27+
///
28+
/// </summary>
29+
public string? Text
30+
{
31+
get => GetValue(TextProperty);
32+
set => SetValue(TextProperty, value);
33+
}
34+
35+
/// <summary>
36+
///
37+
/// </summary>
38+
/// <param name="disposables"></param>
39+
protected override void OnAttached(CompositeDisposable disposables)
40+
{
41+
if (AssociatedObject is InputElement element)
42+
{
43+
var disposable = element.AddDisposableHandler(
44+
InputElement.TextInputEvent,
45+
OnTextInput,
46+
EventRoutingStrategy);
47+
disposables.Add(disposable);
48+
}
49+
}
50+
51+
private void OnTextInput(object? sender, TextInputEventArgs e)
52+
{
53+
if (!IsEnabled)
54+
{
55+
return;
56+
}
57+
58+
var text = Text;
59+
var haveText = text is not null && e.Text == text;
60+
61+
if (text is null || haveText)
62+
{
63+
return;
64+
}
65+
66+
e.Handled = MarkAsHandled;
67+
Interaction.ExecuteActions(AssociatedObject, Actions, e);
68+
}
69+
}

0 commit comments

Comments
 (0)