Skip to content

Commit

Permalink
Follow-Up: When multiple windows are available, display a message to …
Browse files Browse the repository at this point in the history
…inform the user that he should activate one of the windows.

Issue: #27
  • Loading branch information
kpreisser committed Aug 12, 2018
1 parent 05b808e commit 7b839ef
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 120 deletions.
8 changes: 5 additions & 3 deletions TTMouseclickSimulator/Core/Actions/AbstractAction.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System;
using System.Threading.Tasks;

using TTMouseclickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions
{
public abstract class AbstractAction : IAction
{
public abstract Task RunAsync(IInteractionProvider provider);

public event Action<string> ActionInformationUpdated;

public abstract Task RunAsync(IInteractionProvider provider);

protected void OnActionInformationUpdated(string text) =>
protected void OnActionInformationUpdated(string text)
{
ActionInformationUpdated?.Invoke(text);
}
}
}
7 changes: 4 additions & 3 deletions TTMouseclickSimulator/Core/Actions/AbstractActionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace TTMouseclickSimulator.Core.Actions
{
public abstract class AbstractActionContainer : AbstractAction, IActionContainer
{
public abstract IList<IAction> SubActions { get; }

public event Action<int?> SubActionStartedOrStopped;

public abstract IList<IAction> SubActions { get; }

protected void OnSubActionStartedOrStopped(int? index) =>
protected void OnSubActionStartedOrStopped(int? index)
{
SubActionStartedOrStopped?.Invoke(index);
}
}
}
36 changes: 20 additions & 16 deletions TTMouseclickSimulator/Core/Actions/CompoundAction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

using TTMouseclickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions
Expand All @@ -14,7 +14,6 @@ public class CompoundAction : AbstractActionContainer
public const int PauseIntervalMinimum = 0;
public const int PauseIntervalMaximum = 60000;


private readonly IList<IAction> actionList;
private readonly CompoundActionType type;

Expand All @@ -24,9 +23,7 @@ public class CompoundAction : AbstractActionContainer

private readonly Random rng = new Random();

public override sealed IList<IAction> SubActions => this.actionList;



/// <summary>
///
/// </summary>
Expand All @@ -39,9 +36,12 @@ public class CompoundAction : AbstractActionContainer
/// <param name="loop">If false, the action will return after a complete run. Otherwise
/// it will loop endlessly. Note that using false is not possible when specifying
/// CompoundActionType.RandomIndex as type.</param>
public CompoundAction(IList<IAction> actionList,
CompoundActionType type = CompoundActionType.Sequential,
int minimumPause = 0, int maximumPause = 0, bool loop = true)
public CompoundAction(
IList<IAction> actionList,
CompoundActionType type = CompoundActionType.Sequential,
int minimumPause = 0,
int maximumPause = 0,
bool loop = true)
{

if (actionList == null || actionList.Count == 0)
Expand All @@ -67,11 +67,14 @@ public CompoundAction(IList<IAction> actionList,
}


public override sealed IList<IAction> SubActions => this.actionList;


public override sealed async Task RunAsync(IInteractionProvider provider)
{
// Run the actions.
int currentIdx = -1;
int[] randomOrder = null;
var randomOrder = null as int[];

Func<int> getNextActionIndex;
if (this.type == CompoundActionType.Sequential)
Expand Down Expand Up @@ -115,7 +118,7 @@ public override sealed async Task RunAsync(IInteractionProvider provider)

OnActionInformationUpdated($"Running action {nextIdx + 1}");

for (;;)
while (true)
{
try
{
Expand All @@ -125,8 +128,7 @@ public override sealed async Task RunAsync(IInteractionProvider provider)
OnSubActionStartedOrStopped(nextIdx);
try
{
var action = this.actionList[nextIdx];
await action.RunAsync(provider);
await this.actionList[nextIdx].RunAsync(provider);
}
finally
{
Expand All @@ -141,17 +143,19 @@ public override sealed async Task RunAsync(IInteractionProvider provider)
}
catch (Exception ex) when (!(ex is SimulatorCanceledException))
{
await provider.CheckRetryForExceptionAsync(ExceptionDispatchInfo.Capture(ex));
await provider.CheckRetryForExceptionAsync(ex);
continue;
}
break;
}
}
}


public override string ToString() => $"Compound – Type: {this.type}, "
+ $"MinPause: {this.minimumPauseDuration}, MaxPause: {this.maximumPauseDuration}, Loop: {this.loop}";
public override string ToString()
{
return $"Compound – Type: {this.type}, " +
$"MinPause: {this.minimumPauseDuration}, MaxPause: {this.maximumPauseDuration}, Loop: {this.loop}";
}


public enum CompoundActionType : int
Expand Down
12 changes: 0 additions & 12 deletions TTMouseclickSimulator/Core/Actions/DefaultGenericActionFactory.cs

This file was deleted.

15 changes: 8 additions & 7 deletions TTMouseclickSimulator/Core/Actions/IAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;

using TTMouseclickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions
Expand All @@ -12,17 +13,17 @@ namespace TTMouseclickSimulator.Core.Actions
/// </summary>
public interface IAction
{
/// <summary>
/// Asynchonously runs the action using the specified IInteractionProvider.
/// </summary>
/// <param name="waitable"></param>
/// <returns></returns>
Task RunAsync(IInteractionProvider provider);

/// <summary>
/// An event that is rised when the action wants to let subscribers know
/// that its state has changed. This is useful for the GUI.
/// </summary>
event Action<string> ActionInformationUpdated;

/// <summary>
/// Asynchonously runs the action using the specified IInteractionProvider.
/// </summary>
/// <param name="waitable"></param>
/// <returns></returns>
Task RunAsync(IInteractionProvider provider);
}
}
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/Core/Actions/IActionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace TTMouseclickSimulator.Core.Actions
{
public interface IActionContainer : IAction
{
IList<IAction> SubActions { get; }

/// <summary>
/// An event that is raised when a subaction has been started
/// or stopped.
/// </summary>
event Action<int?> SubActionStartedOrStopped;

IList<IAction> SubActions { get; }
}
}
12 changes: 0 additions & 12 deletions TTMouseclickSimulator/Core/Actions/IActionFactory.cs

This file was deleted.

15 changes: 11 additions & 4 deletions TTMouseclickSimulator/Core/Actions/LoopAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

using TTMouseclickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions
Expand All @@ -12,11 +13,13 @@ namespace TTMouseclickSimulator.Core.Actions
public class LoopAction : AbstractActionContainer
{
private readonly IAction action;

/// <summary>
/// Specifies how often the action is run. null means infinite.
/// </summary>
private readonly int? count;


public LoopAction(IAction action, int? count = null)
{
if (action == null)
Expand All @@ -28,16 +31,18 @@ public LoopAction(IAction action, int? count = null)
this.count = count;
}


public override IList<IAction> SubActions => new List<IAction>() { this.action };


public override sealed async Task RunAsync(IInteractionProvider provider)
{
OnSubActionStartedOrStopped(0);
try
{
for (int i = 0; !this.count.HasValue || i < this.count.Value; i++)
{
for (;;)
while (true)
{
try
{
Expand All @@ -47,7 +52,7 @@ public override sealed async Task RunAsync(IInteractionProvider provider)
}
catch (Exception ex) when (!(ex is SimulatorCanceledException))
{
await provider.CheckRetryForExceptionAsync(ExceptionDispatchInfo.Capture(ex));
await provider.CheckRetryForExceptionAsync(ex);
continue;
}
break;
Expand All @@ -60,7 +65,9 @@ public override sealed async Task RunAsync(IInteractionProvider provider)
}
}


public override string ToString() => $"Loop – Count: {this.count?.ToString() ?? "∞"}";
public override string ToString()
{
return $"Loop – Count: {this.count?.ToString() ?? "∞"}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ If we would want to place the mouse cursor at the rightmost pixel of the second
return new Coordinates(resX, resY);
}


public void PressKey(VirtualKeyShort keyCode)
{
PressOrReleaseKey(keyCode, true);
Expand Down Expand Up @@ -273,7 +272,6 @@ public void WriteText(string characters)
}



public unsafe class ScreenshotContent : IScreenshotContent
{
private bool disposed;
Expand Down Expand Up @@ -351,7 +349,6 @@ public static ScreenshotContent Create(
return existingScreenshot;
}


private void OpenBitmapData()
{
if (this.bmpData == null)
Expand Down
13 changes: 7 additions & 6 deletions TTMouseclickSimulator/Core/Environment/IInteractionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IInteractionProvider
/// re-throw the exception or throw an <see cref="SimulatorCanceledException"/>.
/// </summary>
/// <param name="ex"></param>
Task CheckRetryForExceptionAsync(ExceptionDispatchInfo ex);
Task CheckRetryForExceptionAsync(Exception ex);

/// <summary>
/// Checks that the Simulator has not been canceled.
Expand Down Expand Up @@ -70,6 +70,7 @@ public interface IInteractionProvider
void ReleaseMouseButton();

void PressKey(AbstractWindowsEnvironment.VirtualKeyShort key);

void ReleaseKey(AbstractWindowsEnvironment.VirtualKeyShort key);

/// <summary>
Expand All @@ -85,9 +86,9 @@ public interface IInteractionProvider
/// </summary>
public class SimulatorCanceledException : Exception
{
public SimulatorCanceledException() :
base("The Simulator has been canceled.")
{ }
}

public SimulatorCanceledException()
: base("The Simulator has been canceled.")
{
}
}
}
6 changes: 5 additions & 1 deletion TTMouseclickSimulator/Core/Environment/IScreenshotContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Windows.Media;

namespace TTMouseclickSimulator.Core.Environment
{
Expand Down Expand Up @@ -39,6 +40,9 @@ public byte GetValueFromIndex(int index)
}
}

public System.Windows.Media.Color ToColor() => System.Windows.Media.Color.FromArgb(255, this.r, this.g, this.b);
public Color ToColor()
{
return Color.FromArgb(255, this.r, this.g, this.b);
}
}
}
Loading

0 comments on commit 7b839ef

Please sign in to comment.