diff --git a/Azaion.Annotator/App.xaml.cs b/Azaion.Annotator/App.xaml.cs index f293936..0127f66 100644 --- a/Azaion.Annotator/App.xaml.cs +++ b/Azaion.Annotator/App.xaml.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Serilog; +using Azaion.Annotator.Extensions; namespace Azaion.Annotator; @@ -71,6 +72,6 @@ public partial class App : Application private void GlobalClick(object sender, RoutedEventArgs e) { var args = (KeyEventArgs)e; - _mediator.Publish(new KeyEvent(sender, args)); + _ = ThrottleExt.Throttle(() => _mediator.Publish(new KeyEvent(sender, args)), TimeSpan.FromMilliseconds(50)); } } \ No newline at end of file diff --git a/Azaion.Annotator/Extensions/FuncExtensions.cs b/Azaion.Annotator/Extensions/FuncExtensions.cs deleted file mode 100644 index b7a1ce5..0000000 --- a/Azaion.Annotator/Extensions/FuncExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Azaion.Annotator.Extensions; - -public static class FuncExtensions -{ - private static CancellationTokenSource? _lastCToken; - - public static Action Debounce(this Action func, TimeSpan? throttleTime = null) => - () => - { - _lastCToken?.Cancel(); - - var tokenSrc = _lastCToken = new CancellationTokenSource(); - Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500), tokenSrc.Token) - .ContinueWith(_ => func(), tokenSrc.Token); - }; -} \ No newline at end of file diff --git a/Azaion.Annotator/Extensions/ThrottleExtensions.cs b/Azaion.Annotator/Extensions/ThrottleExtensions.cs new file mode 100644 index 0000000..17d3d10 --- /dev/null +++ b/Azaion.Annotator/Extensions/ThrottleExtensions.cs @@ -0,0 +1,16 @@ +namespace Azaion.Annotator.Extensions; + +public static class ThrottleExt +{ + private static bool _throttleOn; + public static async Task Throttle(Func func, TimeSpan? throttleTime = null) + { + if (_throttleOn) + return; + + _throttleOn = true; + await func(); + await Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500)); + _throttleOn = false; + } +} \ No newline at end of file diff --git a/Azaion.Annotator/GalleryManager.cs b/Azaion.Annotator/GalleryManager.cs new file mode 100644 index 0000000..775d721 --- /dev/null +++ b/Azaion.Annotator/GalleryManager.cs @@ -0,0 +1,10 @@ +namespace Azaion.Annotator; + +public class GalleryManager +{ + + public void CreateThumbnails() + { + + } +} \ No newline at end of file diff --git a/Azaion.Annotator/MainWindow.xaml.cs b/Azaion.Annotator/MainWindow.xaml.cs index 9e163fb..157775e 100644 --- a/Azaion.Annotator/MainWindow.xaml.cs +++ b/Azaion.Annotator/MainWindow.xaml.cs @@ -150,16 +150,16 @@ public partial class MainWindow Volume.ValueChanged += (_, newValue) => _mediator.Publish(new VolumeChangedEvent((int)newValue)); - SizeChanged += (_, _) => SaveUserSettings(); - LocationChanged += (_, _) => SaveUserSettings(); - StateChanged += (_, _) => SaveUserSettings(); + SizeChanged += async (_, _) => await SaveUserSettings(); + LocationChanged += async (_, _) => await SaveUserSettings(); + StateChanged += async (_, _) => await SaveUserSettings(); Editor.FormState = _formState; Editor.Mediator = _mediator; DgAnnotations.ItemsSource = _formState.AnnotationResults; } - private void SaveUserSettings() + private async Task SaveUserSettings() { if (_suspendLayout) return; @@ -169,8 +169,11 @@ public partial class MainWindow _config.WindowSize = new Size(Width, Height); _config.WindowLocation = new Point(Left, Top); _config.FullScreen = WindowState == WindowState.Maximized; - var saveConfigFn = () => _configRepository.Save(_config); - saveConfigFn.Debounce(TimeSpan.FromSeconds(5)).Invoke(); + await ThrottleExt.Throttle(() => + { + _configRepository.Save(_config); + return Task.CompletedTask; + }, TimeSpan.FromSeconds(5)); } public void ShowCurrentAnnotations() => ShowTimeAnnotations(TimeSpan.FromMilliseconds(_mediaPlayer.Time)); diff --git a/Azaion.Annotator/PlayerControlHandler.cs b/Azaion.Annotator/PlayerControlHandler.cs index 3c306cf..3a85a91 100644 --- a/Azaion.Annotator/PlayerControlHandler.cs +++ b/Azaion.Annotator/PlayerControlHandler.cs @@ -1,7 +1,6 @@ using System.IO; using System.Windows; using System.Windows.Input; -using System.Windows.Threading; using Azaion.Annotator.DTO; using LibVLCSharp.Shared; using MediatR; @@ -24,8 +23,6 @@ public class PlayerControlHandler(LibVLC libVLC, private const int LARGE_STEP = 5000; private const int RESULT_WIDTH = 1280; - private static readonly string[] CatchSenders = ["ForegroundWindow", "ScrollViewer", "VideoView", "GridSplitter"]; - private readonly Dictionary _keysControlEnumDict = new() { { Key.Space, PlaybackControlEnum.Pause }, @@ -55,8 +52,7 @@ public class PlayerControlHandler(LibVLC libVLC, public async Task Handle(KeyEvent notification, CancellationToken cancellationToken) { - if (!CatchSenders.Contains(notification.Sender.GetType().Name)) - return; + logger.LogInformation($"Catch {notification.Args.Key} by {notification.Sender.GetType().Name}"); var key = notification.Args.Key; var keyNumber = (int?)null;