From ccf1c40e58653c6f00506a64a411a0f64b723a96 Mon Sep 17 00:00:00 2001 From: clsjacome Date: Thu, 24 Apr 2025 16:31:37 -0600 Subject: [PATCH 1/4] Update MainWindow.xaml.cs --- WPF.Playground/MainWindow.xaml.cs | 52 +++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/WPF.Playground/MainWindow.xaml.cs b/WPF.Playground/MainWindow.xaml.cs index b7f7a09..9885c37 100644 --- a/WPF.Playground/MainWindow.xaml.cs +++ b/WPF.Playground/MainWindow.xaml.cs @@ -1,7 +1,12 @@ -using System.Windows; +using System; +using System.Collections.Generic; +using System.Windows; using System.Windows.Input; +using DeftSharp.Windows.Input.Extensions; +using DeftSharp.Windows.Input.Interceptors; using DeftSharp.Windows.Input.Keyboard; using DeftSharp.Windows.Input.Mouse; +using DeftSharp.Windows.Input.Mouse.Interceptors; namespace WPF.Playground { @@ -32,17 +37,23 @@ public partial class MainWindow public MainWindow() => InitializeComponent(); - + private ScrollDisable sd = new ScrollDisable(); + private MouseLog mml = new MouseLog(); private void OnLoaded(object sender, RoutedEventArgs e) { } private void OnClickButton1(object sender, RoutedEventArgs e) { + + sd.Hook(); + mml.Hook(); } private void OnClickButton2(object sender, RoutedEventArgs e) { + sd.Unhook(); + mml.Unhook(); } private void OnClickButton3(object sender, RoutedEventArgs e) @@ -57,4 +68,41 @@ private void OnClickButton5(object sender, RoutedEventArgs e) { } } + + public class ScrollDisable : MouseInterceptor + { + protected override bool IsInputAllowed(MouseInputArgs args) + { + if(args.Event is MouseInputEvent.Scroll) + { + return false; + } + + return true; + } + } + + public class MouseLog : MouseInterceptor + { + protected override bool IsInputAllowed(MouseInputArgs args) + { + return true; + } + + protected override void OnInputSuccess(MouseInputArgs args) + { + if (args.Event is MouseInputEvent.Move) + return; + + Console.WriteLine(args.Event); + + } + + protected override void OnInputFailure(MouseInputArgs args, IEnumerable failedInterceptors) + { + Console.WriteLine($"{args.Event} failed at {failedInterceptors.ToNames}"); + } + + + } } \ No newline at end of file From ca3725615fcd18183685f7f6c3e20633c94b061b Mon Sep 17 00:00:00 2001 From: clsjacome Date: Fri, 25 Apr 2025 19:35:53 -0600 Subject: [PATCH 2/4] Initial Draft Needs to better logic to handle aync log events --- .../CustomInterceptorTests.cs | 93 +++++++++++++++++++ WPF.Playground/MainWindow.xaml.cs | 49 ++-------- 2 files changed, 103 insertions(+), 39 deletions(-) create mode 100644 DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs diff --git a/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs new file mode 100644 index 0000000..2bbd08f --- /dev/null +++ b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs @@ -0,0 +1,93 @@ +using DeftSharp.Windows.Input.Interceptors; +using DeftSharp.Windows.Input.Mouse.Interceptors; + + +namespace DeftSharp.Windows.Input.Tests.CustomInterceptors; + +public sealed class CustomInterceptorTests +{ + private readonly MouseManipulator _mouseManipulator = new(); + private readonly ScrollDisabler _scrollDisable = new(); + private readonly MouseLogger _mouseLog = new(); + + + [Fact] + public async void CustomInterceptor_CatchEvent() + { + + + await Task.Run(() => + { + _scrollDisable.Hook(); + _mouseLog.Hook(); + + _mouseManipulator.Click(); + _mouseManipulator.Click(); + + Assert.False(_mouseLog.WasEventBlocked); + Assert.True(_mouseLog.WasEventCatched); + + + _scrollDisable.Unhook(); + _mouseLog.Unhook(); + }); + + + } + + //[Fact] + //public async void CustomInterceptor_BlockEvent() + //{ + + + // await Task.Run(() => + // { + // _scrollDisable.Hook(); + // _mouseLog.Hook(); + + // _mouseManipulator.Scroll(400); + // _mouseManipulator.Scroll(-800); + + // Assert.False(_mouseLog.WasEventCatched); + // //Assert.True(_mouseLog.WasEventBlocked); + + + // _scrollDisable.Unhook(); + // _mouseLog.Unhook(); + // }); + + + //} + +} + +//Helper Custom Interceptor Classes +internal class ScrollDisabler : MouseInterceptor +{ + protected override bool IsInputAllowed(MouseInputArgs args) + { + if (args.Event is MouseInputEvent.Scroll) + return false; + + return true; + } +} + +internal class MouseLogger : MouseInterceptor +{ + internal bool WasEventCatched { get; private set; } = false; + internal bool WasEventBlocked { get; private set; } = false; + + protected override bool IsInputAllowed(MouseInputArgs args) => true; + + protected override void OnInputSuccess(MouseInputArgs args) + { + WasEventCatched = true; + } + + protected override void OnInputFailure(MouseInputArgs args, IEnumerable failedInterceptors) + { + WasEventBlocked = true; + } +} + diff --git a/WPF.Playground/MainWindow.xaml.cs b/WPF.Playground/MainWindow.xaml.cs index 9885c37..73384d1 100644 --- a/WPF.Playground/MainWindow.xaml.cs +++ b/WPF.Playground/MainWindow.xaml.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; using System.Windows; using System.Windows.Input; using DeftSharp.Windows.Input.Extensions; @@ -37,8 +39,8 @@ public partial class MainWindow public MainWindow() => InitializeComponent(); - private ScrollDisable sd = new ScrollDisable(); - private MouseLog mml = new MouseLog(); + private ScrollDisabler sd = new ScrollDisabler(); + private MouseLogger mml = new MouseLogger(); private void OnLoaded(object sender, RoutedEventArgs e) { } @@ -58,51 +60,20 @@ private void OnClickButton2(object sender, RoutedEventArgs e) private void OnClickButton3(object sender, RoutedEventArgs e) { + Trace.WriteLine("Sleeping..."); + Thread.Sleep(3000); + _mouseManipulator.Click(); } private void OnClickButton4(object sender, RoutedEventArgs e) { + Trace.WriteLine("Sleeping..."); + Thread.Sleep(3000); + _mouseManipulator.Scroll(400); } private void OnClickButton5(object sender, RoutedEventArgs e) { } } - - public class ScrollDisable : MouseInterceptor - { - protected override bool IsInputAllowed(MouseInputArgs args) - { - if(args.Event is MouseInputEvent.Scroll) - { - return false; - } - - return true; - } - } - - public class MouseLog : MouseInterceptor - { - protected override bool IsInputAllowed(MouseInputArgs args) - { - return true; - } - - protected override void OnInputSuccess(MouseInputArgs args) - { - if (args.Event is MouseInputEvent.Move) - return; - - Console.WriteLine(args.Event); - - } - - protected override void OnInputFailure(MouseInputArgs args, IEnumerable failedInterceptors) - { - Console.WriteLine($"{args.Event} failed at {failedInterceptors.ToNames}"); - } - - - } } \ No newline at end of file From aa9067ed81d8d978f1d1f3fa7b51aa451a9ac3e7 Mon Sep 17 00:00:00 2001 From: clsjacome Date: Mon, 28 Apr 2025 21:30:09 -0600 Subject: [PATCH 3/4] Update CustomInterceptorTests.cs --- .../CustomInterceptorTests.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs index 2bbd08f..94a2e8c 100644 --- a/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs +++ b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs @@ -7,14 +7,13 @@ namespace DeftSharp.Windows.Input.Tests.CustomInterceptors; public sealed class CustomInterceptorTests { private readonly MouseManipulator _mouseManipulator = new(); - private readonly ScrollDisabler _scrollDisable = new(); - private readonly MouseLogger _mouseLog = new(); [Fact] public async void CustomInterceptor_CatchEvent() { - + ScrollDisabler _scrollDisable = new(); + MouseLogger _mouseLog = new(); await Task.Run(() => { @@ -24,7 +23,7 @@ await Task.Run(() => _mouseManipulator.Click(); _mouseManipulator.Click(); - Assert.False(_mouseLog.WasEventBlocked); + //Assert.False(_mouseLog.WasEventBlocked); Assert.True(_mouseLog.WasEventCatched); @@ -35,29 +34,30 @@ await Task.Run(() => } - //[Fact] - //public async void CustomInterceptor_BlockEvent() - //{ - + [Fact] + public async void CustomInterceptor_BlockEvent() + { + ScrollDisabler _scrollDisable = new(); + MouseLogger _mouseLog = new(); - // await Task.Run(() => - // { - // _scrollDisable.Hook(); - // _mouseLog.Hook(); + await Task.Run(() => + { + _scrollDisable.Hook(); + _mouseLog.Hook(); - // _mouseManipulator.Scroll(400); - // _mouseManipulator.Scroll(-800); + _mouseManipulator.Scroll(400); + _mouseManipulator.Scroll(-800); - // Assert.False(_mouseLog.WasEventCatched); - // //Assert.True(_mouseLog.WasEventBlocked); + //Assert.False(_mouseLog.WasEventCatched); + Assert.True(_mouseLog.WasEventBlocked); - // _scrollDisable.Unhook(); - // _mouseLog.Unhook(); - // }); + _scrollDisable.Unhook(); + _mouseLog.Unhook(); + }); - //} + } } From 01f4dafe2f6141e51f77d811f1086fb47027bc04 Mon Sep 17 00:00:00 2001 From: clsjacome Date: Tue, 29 Apr 2025 15:38:35 -0600 Subject: [PATCH 4/4] Added Correct Await Logic Added correct async handling of the interceptor trigger --- .../CustomInterceptorTests.cs | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs index 94a2e8c..d79cfea 100644 --- a/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs +++ b/DeftSharp.Windows.Input.Tests/CustomInterceptors/CustomInterceptorTests.cs @@ -15,23 +15,24 @@ public async void CustomInterceptor_CatchEvent() ScrollDisabler _scrollDisable = new(); MouseLogger _mouseLog = new(); - await Task.Run(() => - { - _scrollDisable.Hook(); - _mouseLog.Hook(); - - _mouseManipulator.Click(); - _mouseManipulator.Click(); - - //Assert.False(_mouseLog.WasEventBlocked); - Assert.True(_mouseLog.WasEventCatched); - - - _scrollDisable.Unhook(); - _mouseLog.Unhook(); - }); + _scrollDisable.Hook(); + _mouseLog.Hook(); + + //Simulate a mouse click + _mouseManipulator.Click(); + + //Assert + var exception = await _mouseLog.ExceptionThrown.Task.WaitAsync(TimeSpan.FromSeconds(1)); + Assert.NotNull(exception); + Assert.IsType(exception); + + + _scrollDisable.Unhook(); + _mouseLog.Unhook(); + + } [Fact] @@ -40,25 +41,27 @@ public async void CustomInterceptor_BlockEvent() ScrollDisabler _scrollDisable = new(); MouseLogger _mouseLog = new(); - await Task.Run(() => - { - _scrollDisable.Hook(); - _mouseLog.Hook(); + _scrollDisable.Hook(); + _mouseLog.Hook(); + + + //Simulate a mouse scroll + _mouseManipulator.Scroll(400); - _mouseManipulator.Scroll(400); - _mouseManipulator.Scroll(-800); + //Assert + var exception = await _mouseLog.ExceptionThrown.Task.WaitAsync(TimeSpan.FromSeconds(1)); - //Assert.False(_mouseLog.WasEventCatched); - Assert.True(_mouseLog.WasEventBlocked); + Assert.NotNull(exception); + Assert.IsType(exception); - _scrollDisable.Unhook(); - _mouseLog.Unhook(); - }); + _scrollDisable.Unhook(); + _mouseLog.Unhook(); } + } //Helper Custom Interceptor Classes @@ -75,19 +78,32 @@ protected override bool IsInputAllowed(MouseInputArgs args) internal class MouseLogger : MouseInterceptor { - internal bool WasEventCatched { get; private set; } = false; - internal bool WasEventBlocked { get; private set; } = false; + internal TaskCompletionSource ExceptionThrown { get; } = new(); protected override bool IsInputAllowed(MouseInputArgs args) => true; protected override void OnInputSuccess(MouseInputArgs args) { - WasEventCatched = true; + ExceptionThrown.TrySetResult(new EventCatchedException("Click Catched")); } protected override void OnInputFailure(MouseInputArgs args, IEnumerable failedInterceptors) { - WasEventBlocked = true; + ExceptionThrown.TrySetResult(new EventBlockedException("Scroll Blocked")); + } +} + +public class EventCatchedException : Exception +{ + public EventCatchedException(string message) : base(message) + { + } +} + +public class EventBlockedException : Exception +{ + public EventBlockedException(string message) : base(message) + { } }