Skip to content

Commit c93d835

Browse files
committed
Improve efficiency by reusing the last captured screenshot if we didn't pause since then.
1 parent 3d82357 commit c93d835

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

TTMouseclickSimulator/Core/Environment/InteractionProvider.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public abstract class InteractionProvider : IInteractionProvider, IDisposable
2323
private IntPtr windowHandle;
2424

2525
private WindowsEnvironment.ScreenshotContent? currentScreenshot;
26+
private bool pausedSinceLastScreenshot;
2627

2728
private bool isMouseButtonPressed;
2829

@@ -183,7 +184,7 @@ public void Initialize()
183184
{
184185
// Verify that we actually can create a screenshot directly from the
185186
// window instead of from the screen.
186-
this.GetCurrentWindowScreenshot(isInitialization: true);
187+
this.CaptureCurrentWindowScreenshot(isInitialization: true);
187188
}
188189

189190
if (this.simulator.RequiredCapabilities.IsSet(SimulatorCapabilities.MouseInput))
@@ -305,8 +306,20 @@ public WindowPosition GetCurrentWindowPosition()
305306
public IScreenshotContent GetCurrentWindowScreenshot()
306307
{
307308
this.ThrowIfCapabilityNotSet(SimulatorCapabilities.CaptureScreenshot);
309+
this.CancellationToken.ThrowIfCancellationRequested();
308310

309-
return this.GetCurrentWindowScreenshot(false);
311+
// Only capture a new screenshot if we paused since getting the last once, so that we
312+
// don't unnecessarily create new ones if nearly no time has passed since then.
313+
// For example, the AutomaticFishingAction might want to get a screenshot after
314+
// casting to check for an error dialog, but then immediately needs to get a screenshot
315+
// again to check for fish bubbles.
316+
if (this.currentScreenshot is null || this.pausedSinceLastScreenshot)
317+
{
318+
this.CaptureCurrentWindowScreenshot();
319+
this.pausedSinceLastScreenshot = false;
320+
}
321+
322+
return this.currentScreenshot!;
310323
}
311324

312325
public void PressKey(WindowsEnvironment.VirtualKey key)
@@ -580,6 +593,7 @@ private void ThrowIfCapabilityNotSet(SimulatorCapabilities capabilities)
580593
private void WaitCore(int milliseconds, bool checkWindow = true)
581594
{
582595
this.CancellationToken.ThrowIfCancellationRequested();
596+
this.pausedSinceLastScreenshot = true;
583597

584598
if (!checkWindow)
585599
{
@@ -634,7 +648,7 @@ private WindowPosition GetWindowPositionCore(bool failIfMinimized = true)
634648
return windowPosition;
635649
}
636650

637-
private IScreenshotContent GetCurrentWindowScreenshot(bool isInitialization)
651+
private void CaptureCurrentWindowScreenshot(bool isInitialization = false)
638652
{
639653
this.CancellationToken.ThrowIfCancellationRequested();
640654

@@ -674,8 +688,6 @@ private IScreenshotContent GetCurrentWindowScreenshot(bool isInitialization)
674688
"Please disable background mode and try again.");
675689
}
676690
}
677-
678-
return this.currentScreenshot;
679691
}
680692

681693
private void CheckRetryForException(Exception ex, bool reinitialize)

0 commit comments

Comments
 (0)