mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:06:30 +00:00
add debounced config save after 7 sec after window resizing/moving
This commit is contained in:
@@ -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);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user