mirror of
https://github.com/cyanfish/naps2.git
synced 2024-11-11 02:45:19 +03:00
Throttle imagelist update events
This commit is contained in:
parent
8d1e1d9e5f
commit
3d90047898
@ -1,28 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NAPS2.Images.Storage;
|
||||
using NAPS2.Util;
|
||||
|
||||
namespace NAPS2.Images
|
||||
{
|
||||
public class ScannedImageList
|
||||
{
|
||||
private readonly ImageContext imageContext;
|
||||
private readonly TimedThrottle runUpdateEventsThrottle;
|
||||
private Memento savedState = Memento.Empty;
|
||||
private ListSelection<ScannedImage> selection;
|
||||
|
||||
public ScannedImageList(ImageContext imageContext)
|
||||
: this(imageContext, new List<ScannedImage>())
|
||||
{
|
||||
this.imageContext = imageContext;
|
||||
Images = new List<ScannedImage>();
|
||||
Selection = ListSelection.Empty<ScannedImage>();
|
||||
}
|
||||
|
||||
public ScannedImageList(ImageContext imageContext, List<ScannedImage> images)
|
||||
{
|
||||
this.imageContext = imageContext;
|
||||
runUpdateEventsThrottle = new TimedThrottle(RunUpdateEvents, TimeSpan.FromMilliseconds(100));
|
||||
Images = images;
|
||||
Selection = ListSelection.Empty<ScannedImage>();
|
||||
}
|
||||
|
||||
public ThumbnailRenderer ThumbnailRenderer { get; set; }
|
||||
@ -54,15 +56,13 @@ namespace NAPS2.Images
|
||||
public void Mutate(ListMutation<ScannedImage> mutation, ListSelection<ScannedImage> selectionToMutate = null)
|
||||
{
|
||||
MutateInternal(mutation, selectionToMutate);
|
||||
UpdateImageMetadata();
|
||||
ImagesUpdated?.Invoke(this, EventArgs.Empty);
|
||||
runUpdateEventsThrottle.RunAction(SynchronizationContext.Current);
|
||||
}
|
||||
|
||||
public async Task MutateAsync(ListMutation<ScannedImage> mutation, ListSelection<ScannedImage> selectionToMutate = null)
|
||||
{
|
||||
await Task.Run(() => MutateInternal(mutation, selectionToMutate));
|
||||
UpdateImageMetadata();
|
||||
ImagesUpdated?.Invoke(this, EventArgs.Empty);
|
||||
runUpdateEventsThrottle.RunAction(SynchronizationContext.Current);
|
||||
}
|
||||
|
||||
private void MutateInternal(ListMutation<ScannedImage> mutation, ListSelection<ScannedImage> selectionToMutate)
|
||||
@ -77,6 +77,12 @@ namespace NAPS2.Images
|
||||
}
|
||||
}
|
||||
|
||||
private void RunUpdateEvents()
|
||||
{
|
||||
UpdateImageMetadata();
|
||||
ImagesUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private void UpdateImageMetadata()
|
||||
{
|
||||
int i = 0;
|
||||
|
@ -309,6 +309,7 @@
|
||||
<Compile Include="Platform\MonoRuntimeCompat.cs" />
|
||||
<Compile Include="Util\ExpFallback.cs" />
|
||||
<Compile Include="Util\NativeLibrary.cs" />
|
||||
<Compile Include="Util\TimedThrottle.cs" />
|
||||
<Compile Include="WinForms\ImageClipboard.cs" />
|
||||
<Compile Include="Remoting\RemotingHelper.cs" />
|
||||
<Compile Include="Util\IInvoker.cs" />
|
||||
|
54
NAPS2.Sdk/Util/TimedThrottle.cs
Normal file
54
NAPS2.Sdk/Util/TimedThrottle.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace NAPS2.Util
|
||||
{
|
||||
public class TimedThrottle
|
||||
{
|
||||
private readonly Action action;
|
||||
private readonly TimeSpan interval;
|
||||
private Timer timer;
|
||||
private DateTime lastRun = DateTime.MinValue;
|
||||
|
||||
public TimedThrottle(Action action, TimeSpan interval)
|
||||
{
|
||||
this.action = action;
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
public void RunAction(SynchronizationContext syncContext)
|
||||
{
|
||||
bool doRunAction = false;
|
||||
lock (this)
|
||||
{
|
||||
if (timer == null && lastRun < DateTime.Now - interval)
|
||||
{
|
||||
doRunAction = true;
|
||||
lastRun = DateTime.Now;
|
||||
}
|
||||
else if (timer == null)
|
||||
{
|
||||
timer = new Timer(Tick, syncContext, interval, TimeSpan.FromMilliseconds(-1));
|
||||
}
|
||||
}
|
||||
|
||||
if (doRunAction)
|
||||
{
|
||||
action();
|
||||
}
|
||||
}
|
||||
|
||||
private void Tick(object state)
|
||||
{
|
||||
var syncContext = (SynchronizationContext) state;
|
||||
lock (this)
|
||||
{
|
||||
timer?.Dispose();
|
||||
timer = null;
|
||||
lastRun = DateTime.Now;
|
||||
}
|
||||
|
||||
syncContext.Post(_ => action(), null);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user