mirror of
https://github.com/azaion/annotations.git
synced 2026-04-23 01:06:30 +00:00
rework throttle
reuse throttle mechanism for catching global keys
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Azaion.Annotator.Extensions;
|
||||
|
||||
public static class ThrottleExt
|
||||
{
|
||||
private static bool _throttleOn;
|
||||
public static async Task Throttle(Func<Task> func, TimeSpan? throttleTime = null)
|
||||
{
|
||||
if (_throttleOn)
|
||||
return;
|
||||
|
||||
_throttleOn = true;
|
||||
await func();
|
||||
await Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500));
|
||||
_throttleOn = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Azaion.Annotator;
|
||||
|
||||
public class GalleryManager
|
||||
{
|
||||
|
||||
public void CreateThumbnails()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
@@ -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<Key, PlaybackControlEnum> _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;
|
||||
|
||||
Reference in New Issue
Block a user