Skip to content

Commit

Permalink
Merge pull request #30 from wieslawsoltes/AddNewTriggers
Browse files Browse the repository at this point in the history
Add new event triggers
  • Loading branch information
wieslawsoltes authored Jan 6, 2025
2 parents 9feb9d1 + 3fbf51c commit 161cfc4
Show file tree
Hide file tree
Showing 54 changed files with 1,287 additions and 80 deletions.
43 changes: 24 additions & 19 deletions samples/BehaviorsTestApplication/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,10 @@ public partial class MainWindowViewModel : ViewModelBase
{
private int _value;

[Reactive]
public partial int Count { get; set; }

[Reactive]
public partial double Position { get; set; }

[Reactive]
public partial ObservableCollection<ItemViewModel>? Items { get; set; }

public IObservable<int> Values { get; }

public ICommand InitializeCommand { get; set; }

public ICommand MoveLeftCommand { get; set; }

public ICommand MoveRightCommand { get; set; }

public ICommand ResetMoveCommand { get; set; }

public MainWindowViewModel()
{
PointerTriggersViewModel = new PointerTriggersViewModel();

Count = 0;
Position = 100.0;
InitializeCommand = ReactiveCommand.Create(Initialize);
Expand Down Expand Up @@ -93,6 +76,28 @@ public MainWindowViewModel()
Values = Observable.Interval(TimeSpan.FromSeconds(1)).Select(_ => _value++);
}

[Reactive]
public partial PointerTriggersViewModel PointerTriggersViewModel { get; set; }

[Reactive]
public partial int Count { get; set; }

[Reactive]
public partial double Position { get; set; }

[Reactive]
public partial ObservableCollection<ItemViewModel>? Items { get; set; }

public IObservable<int> Values { get; }

public ICommand InitializeCommand { get; set; }

public ICommand MoveLeftCommand { get; set; }

public ICommand MoveRightCommand { get; set; }

public ICommand ResetMoveCommand { get; set; }

private void Initialize()
{
Console.WriteLine("InitializeCommand");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Windows.Input;
using ReactiveUI;

namespace BehaviorsTestApplication.ViewModels;

public partial class PointerTriggersViewModel : ViewModelBase
{
public PointerTriggersViewModel()
{
PointerPressedCommand = ReactiveCommand.Create<(double X, double Y)>(PointerPressed);
PointerReleasedCommand = ReactiveCommand.Create<(double X, double Y)>(PointerReleased);
PointerMovedCommand = ReactiveCommand.Create<(double X, double Y)>(PointerMoved);
}

public ICommand PointerPressedCommand { get; set; }

public ICommand PointerReleasedCommand { get; set; }

public ICommand PointerMovedCommand { get; set; }

private void PointerPressed((double X, double Y) point)
{
Console.WriteLine($"Pressed: {point}");
}

private void PointerReleased((double X, double Y) point)
{
Console.WriteLine($"Released: {point}");
}

private void PointerMoved((double X, double Y) point)
{
Console.WriteLine($"Moved: {point}");
}
}
3 changes: 3 additions & 0 deletions samples/BehaviorsTestApplication/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@
<TabItem Header="TemplateBinding">
<pages:TemplateBindingView />
</TabItem>
<TabItem Header="Pointer Triggers">
<pages:PointerTriggersView DataContext="{Binding PointerTriggersViewModel}" />
</TabItem>
</TabControl>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<UserControl x:Class="BehaviorsTestApplication.Views.Pages.PointerTriggersView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:BehaviorsTestApplication.ViewModels"
x:DataType="vm:PointerTriggersViewModel"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450">
<Design.DataContext>
<vm:PointerTriggersViewModel />
</Design.DataContext>

<Canvas Width="500" Height="500" Background="WhiteSmoke">
<Interaction.Behaviors>
<PointerPressedTrigger EventRoutingStrategy="Bubble">
<CapturePointerAction />
<InvokeCommandAction Command="{Binding PointerPressedCommand}"
InputConverter="{PointerEventArgsConverter}" />
</PointerPressedTrigger>
<PointerReleasedTrigger EventRoutingStrategy="Bubble">
<ReleasePointerCaptureAction />
<InvokeCommandAction Command="{Binding PointerReleasedCommand}"
InputConverter="{PointerEventArgsConverter}" />
</PointerReleasedTrigger>
<PointerMovedTrigger EventRoutingStrategy="Bubble">
<InvokeCommandAction Command="{Binding PointerMovedCommand}"
InputConverter="{PointerEventArgsConverter}" />
</PointerMovedTrigger>
</Interaction.Behaviors>
</Canvas>

</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace BehaviorsTestApplication.Views.Pages;

public partial class PointerTriggersView : UserControl
{
public PointerTriggersView()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
2 changes: 1 addition & 1 deletion samples/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageVersion Include="Avalonia.Fonts.Inter" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.Browser" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.Headless.XUnit" Version="$(AvaloniaVersion)" />
<PackageVersion Include="ReactiveGenerator" Version="0.9.11" />
<PackageVersion Include="ReactiveGenerator" Version="0.10.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="System.Reactive" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Globalization;
using Avalonia.Data.Converters;
using Avalonia.Input;

namespace Avalonia.Xaml.Interactions.Custom.Converters;

/// <summary>
/// Converter for <see cref="PointerEventArgs"/>.
/// </summary>
public class PointerEventArgsConverter : IValueConverter
{
/// <summary>
/// Gets the instance of <see cref="PointerEventArgsConverter"/>.
/// </summary>
public static readonly PointerEventArgsConverter Instance = new();

/// <inheritdoc />
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
switch (value)
{
case PointerPressedEventArgs pointerPressedEventArgs:
{
if (pointerPressedEventArgs.Source is not Visual visual)
{
return AvaloniaProperty.UnsetValue;
}

var (x, y) = pointerPressedEventArgs.GetPosition(visual);

return (x, y);
}
case PointerReleasedEventArgs pointerReleasedEventArgs:
{
if (pointerReleasedEventArgs.Source is not Visual visual)
{
return AvaloniaProperty.UnsetValue;
}

var (x, y) = pointerReleasedEventArgs.GetPosition(visual);

return (x, y);
}
case PointerDeltaEventArgs pointerDeltaEventArgs:
{
var (x, y) = pointerDeltaEventArgs.Delta;

return (x, y);
}
case PointerWheelEventArgs pointerWheelEventArgs:
{
var (x, y) = pointerWheelEventArgs.Delta;

return (x, y);
}
case PointerEventArgs pointerEventArgs:
{
if (pointerEventArgs.Source is not Visual visual)
{
return AvaloniaProperty.UnsetValue;
}

var (x, y) = pointerEventArgs.GetPosition(visual);

return (x, y);
}
default:
return AvaloniaProperty.UnsetValue;
}
}

/// <inheritdoc />
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return AvaloniaProperty.UnsetValue;
}

/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
/// <returns></returns>
public IValueConverter ProvideValue(IServiceProvider serviceProvider) => Instance;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class ActualThemeVariantChangedTrigger : StyledElementTrigger<StyledElement>
{
/// <inheritdoc />
protected override void OnActualThemeVariantChangedEvent()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class AttachedToLogicalTreeTrigger : StyledElementTrigger<StyledElement>
{
/// <inheritdoc />
protected override void OnAttachedToLogicalTree()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class AttachedToVisualTreeTrigger : StyledElementTrigger<Visual>
{
/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class DataContextChangedTrigger : StyledElementTrigger<StyledElement>
{
/// <inheritdoc />
protected override void OnDataContextChangedEvent()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class DetachedFromLogicalTreeTrigger : StyledElementTrigger<StyledElement>
{
/// <inheritdoc />
protected override void OnDetachedFromLogicalTree()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class DetachedFromVisualTreeTrigger : StyledElementTrigger<Visual>
{
/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
20 changes: 20 additions & 0 deletions src/Avalonia.Xaml.Interactions.Custom/Core/InitializedTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class InitializedTrigger : StyledElementTrigger<StyledElement>
{
/// <inheritdoc />
protected override void OnInitializedEvent()
{
if (!IsEnabled)
{
return;
}

Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null);
}
}
Loading

0 comments on commit 161cfc4

Please sign in to comment.