add debounced config save after 7 sec after window resizing/moving

This commit is contained in:
Oleksandr Bezdieniezhnykh
2024-07-20 19:35:42 +03:00
parent 8869a92730
commit f452059407
2 changed files with 24 additions and 0 deletions
@@ -0,0 +1,16 @@
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);
};
}
+8
View File
@@ -132,12 +132,20 @@ public partial class MainWindow
SizeChanged += (sender, args) => SizeChanged += (sender, args) =>
{ {
if (!_suspendLayout) if (!_suspendLayout)
{
_config.WindowSize = args.NewSize; _config.WindowSize = args.NewSize;
var saveConfigFn = () => _configRepository.Save(_config);
saveConfigFn.Debounce(TimeSpan.FromSeconds(7)).Invoke();
}
}; };
LocationChanged += (_, _) => LocationChanged += (_, _) =>
{ {
if (!_suspendLayout) if (!_suspendLayout)
{
_config.WindowLocation = new Point(Left, Top); _config.WindowLocation = new Point(Left, Top);
var saveConfigFn = () => _configRepository.Save(_config);
saveConfigFn.Debounce(TimeSpan.FromSeconds(7)).Invoke();
}
}; };
Editor.FormState = _formState; Editor.FormState = _formState;