diff --git a/Toolkit.Avalonia/ImageReader.cs b/Toolkit.Avalonia/ImageReader.cs index 42fbf31..d2b233a 100644 --- a/Toolkit.Avalonia/ImageReader.cs +++ b/Toolkit.Avalonia/ImageReader.cs @@ -14,4 +14,4 @@ public class ImageReader(IImageResizer imageResizer) : Bitmap resizedImage = imageResizer.Resize(stream, width, height, maintainAspectRatio); return new ImageDescriptor(resizedImage, width, height); } -} \ No newline at end of file +} diff --git a/Toolkit.Avalonia/UserInteraction.cs b/Toolkit.Avalonia/UserInteraction.cs new file mode 100644 index 0000000..a694646 --- /dev/null +++ b/Toolkit.Avalonia/UserInteraction.cs @@ -0,0 +1,51 @@ +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; +using Toolkit.Foundation; + +namespace Toolkit.Avalonia; + + +public class UserInteraction(ITopLevelProvider topLevelProvider) : + IUserInteraction +{ + public event EventHandler? UserInteracted; + + private void OnPointerMoved(object? sender, + PointerEventArgs args) + { + UserInteracted?.Invoke(this, new UserInteractedEventArgs()); + } + + private void OnKeyDown(object? sender, + KeyEventArgs args) + { + UserInteracted?.Invoke(this, new UserInteractedEventArgs()); + } + + private void OnKeyUp(object? sender, + KeyEventArgs args) + { + UserInteracted?.Invoke(this, new UserInteractedEventArgs()); + } + + public void Stop() + { + if (topLevelProvider.Get() is TopLevel topLevel) + { + topLevel.RemoveHandler(InputElement.PointerMovedEvent, OnPointerMoved); + topLevel.RemoveHandler(InputElement.KeyDownEvent, OnKeyDown); + topLevel.RemoveHandler(InputElement.KeyUpEvent, OnKeyUp); + } + } + + public void Start() + { + if (topLevelProvider.Get() is TopLevel topLevel) + { + topLevel.AddHandler(InputElement.PointerMovedEvent, OnPointerMoved, RoutingStrategies.Tunnel); + topLevel.AddHandler(InputElement.KeyDownEvent, OnKeyDown, RoutingStrategies.Tunnel); + topLevel.AddHandler(InputElement.KeyUpEvent, OnKeyUp, RoutingStrategies.Tunnel); + } + } +} diff --git a/Toolkit.Foundation/ActivatedEventArgs.cs b/Toolkit.Foundation/ActivatedEventArgs.cs index 99fcacf..4f1a0b6 100644 --- a/Toolkit.Foundation/ActivatedEventArgs.cs +++ b/Toolkit.Foundation/ActivatedEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record ActivatedEventArgs(TSender? Sender = default); \ No newline at end of file +public record ActivatedEventArgs(TSender? Sender = default); diff --git a/Toolkit.Foundation/IUserInteraction.cs b/Toolkit.Foundation/IUserInteraction.cs new file mode 100644 index 0000000..59432ac --- /dev/null +++ b/Toolkit.Foundation/IUserInteraction.cs @@ -0,0 +1,10 @@ +namespace Toolkit.Foundation; + +public interface IUserInteraction +{ + event EventHandler? UserInteracted; + + void Stop(); + + void Start(); +} \ No newline at end of file diff --git a/Toolkit.Foundation/UserInteractedEventArgs.cs b/Toolkit.Foundation/UserInteractedEventArgs.cs new file mode 100644 index 0000000..0291a9e --- /dev/null +++ b/Toolkit.Foundation/UserInteractedEventArgs.cs @@ -0,0 +1,4 @@ +namespace Toolkit.Foundation; + +public class UserInteractedEventArgs : + EventArgs;