Skip to content

Commit

Permalink
window commands support window search methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rcktrncn committed Jul 23, 2021
1 parent 16277b5 commit aaa9475
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 52 deletions.
67 changes: 63 additions & 4 deletions taskt/Core/Automation/Commands/ActivateWindowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public class ActivateWindowCommand : ScriptCommand
[Attributes.PropertyAttributes.Remarks("")]
public string v_WindowName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Window title search method (Default is Contains)")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Contains")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Start with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("End with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Exact match")]
[Attributes.PropertyAttributes.SampleUsage("**Contains** or **Start with** or **End with** or **Exact match**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_SearchMethod { get; set; }

[XmlIgnore]
[NonSerialized]
public ComboBox WindowNameControl;
Expand All @@ -36,16 +47,62 @@ public ActivateWindowCommand()
public override void RunCommand(object sender)
{
string windowName = v_WindowName.ConvertToUserVariable(sender);
string searchMethod = v_SearchMethod.ConvertToUserVariable(sender);
if (String.IsNullOrEmpty(searchMethod))
{
searchMethod = "Contains";
}

var targetWindows = User32Functions.FindTargetWindows(windowName, (windowName == ((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword));
bool targetIsCurrentWindow = windowName == ((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword;
var targetWindows = User32Functions.FindTargetWindows(windowName, targetIsCurrentWindow);

//loop each window
foreach (var targetedWindow in targetWindows)
if (searchMethod == "Contains" || targetIsCurrentWindow)
{
User32Functions.SetWindowState(targetedWindow, User32Functions.WindowState.SW_SHOWNORMAL);
User32Functions.SetForegroundWindow(targetedWindow);
foreach (var targetedWindow in targetWindows)
{
User32Functions.SetWindowState(targetedWindow, User32Functions.WindowState.SW_SHOWNORMAL);
User32Functions.SetForegroundWindow(targetedWindow);
}
}
else
{
Func<string, bool> searchFunc;
switch(searchMethod)
{
case "Start with":
searchFunc = (s) => s.StartsWith(windowName);
break;

case "End with":
searchFunc = (s) => s.EndsWith(windowName);
break;

case "Exact match":
searchFunc = (s) => (s == windowName);
break;

default:
throw new Exception("Search method " + searchMethod + " is not support.");
break;
}

bool isActivated = false;
foreach (var targetedWindow in targetWindows)
{
if (searchFunc(User32Functions.GetWindowTitle(targetedWindow)))
{
User32Functions.SetWindowState(targetedWindow, User32Functions.WindowState.SW_SHOWNORMAL);
User32Functions.SetForegroundWindow(targetedWindow);
isActivated = true;
}
}

if (!isActivated)
{
throw new Exception("Window '" + windowName + "' is not found. Search method is " + searchMethod + ".");
}
}
}

public override List<Control> Render(frmCommandEditor editor)
Expand All @@ -58,6 +115,8 @@ public override List<Control> Render(frmCommandEditor editor)
RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateUIHelpersFor("v_WindowName", this, new Control[] { WindowNameControl }, editor));
RenderedControls.Add(WindowNameControl);

RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateDefaultDropdownGroupFor("v_SearchMethod", this, editor));

return RenderedControls;

}
Expand Down
70 changes: 66 additions & 4 deletions taskt/Core/Automation/Commands/CloseWindowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public class CloseWindowCommand : ScriptCommand
[Attributes.PropertyAttributes.Remarks("")]
public string v_WindowName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Window title search method (Default is Contains)")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Contains")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Start with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("End with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Exact match")]
[Attributes.PropertyAttributes.SampleUsage("**Contains** or **Start with** or **End with** or **Exact match**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_SearchMethod { get; set; }

[XmlIgnore]
[NonSerialized]
public ComboBox WindowNameControl;
Expand All @@ -39,13 +50,62 @@ public CloseWindowCommand()
public override void RunCommand(object sender)
{
string windowName = v_WindowName.ConvertToUserVariable(sender);
string searchMethod = v_SearchMethod.ConvertToUserVariable(sender);
if (String.IsNullOrEmpty(searchMethod))
{
searchMethod = "Contains";
}

bool targetIsCurrentWindow = ((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword == windowName;

var targetWindows = User32Functions.FindTargetWindows(windowName, (((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword == windowName));
var targetWindows = User32Functions.FindTargetWindows(windowName, targetIsCurrentWindow);

//loop each window
foreach (var targetedWindow in targetWindows)
if (searchMethod == "Contains" || targetIsCurrentWindow)
{
User32Functions.CloseWindow(targetedWindow);
//loop each window
foreach (var targetedWindow in targetWindows)
{
User32Functions.CloseWindow(targetedWindow);
}
}
else
{
Func<string, bool> searchFunc;
switch (searchMethod)
{
case "Start with":
searchFunc = (s) => s.StartsWith(windowName);
break;

case "End with":
searchFunc = (s) => s.EndsWith(windowName);
break;

case "Exact match":
searchFunc = (s) => (s == windowName);
break;

default:
throw new Exception("Search method " + searchMethod + " is not support.");
break;
}

bool isCloseWindow = false;

//loop each window
foreach (var targetedWindow in targetWindows)
{
if (searchFunc(User32Functions.GetWindowTitle(targetedWindow)))
{
User32Functions.CloseWindow(targetedWindow);
isCloseWindow = true;
}
}

if (!isCloseWindow)
{
throw new Exception("Window name " + windowName + " is not found. Search method is " + searchMethod + ".");
}
}
}
public override List<Control> Render(frmCommandEditor editor)
Expand All @@ -58,6 +118,8 @@ public override List<Control> Render(frmCommandEditor editor)
RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateUIHelpersFor("v_WindowName", this, new Control[] { WindowNameControl }, editor));
RenderedControls.Add(WindowNameControl);

RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateDefaultDropdownGroupFor("v_SearchMethod", this, editor));

return RenderedControls;

}
Expand Down
83 changes: 71 additions & 12 deletions taskt/Core/Automation/Commands/MoveWindowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public class MoveWindowCommand : ScriptCommand
[Attributes.PropertyAttributes.Remarks("")]
public string v_WindowName { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Window title search method (Default is Contains)")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Contains")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Start with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("End with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Exact match")]
[Attributes.PropertyAttributes.SampleUsage("**Contains** or **Start with** or **End with** or **Exact match**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_SearchMethod { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the new X horizontal coordinate (pixel) for the window's location. 0 starts at the left of the screen. (ex. 0, 100, {{{vXPos}}})")]
[Attributes.PropertyAttributes.InputSpecification("Input the new horizontal coordinate of the window, 0 starts at the left and goes to the right")]
Expand Down Expand Up @@ -52,29 +62,76 @@ public MoveWindowCommand()

public override void RunCommand(object sender)
{

string windowName = v_WindowName.ConvertToUserVariable(sender);

var targetWindows = User32Functions.FindTargetWindows(windowName, (((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword == windowName));
string searchMethod = v_SearchMethod.ConvertToUserVariable(sender);
if (String.IsNullOrEmpty(searchMethod))
{
searchMethod = "Contains";
}

bool targetIsCurrentWindow = ((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword == windowName;

//loop each window
foreach (var targetedWindow in targetWindows)
var targetWindows = User32Functions.FindTargetWindows(windowName, targetIsCurrentWindow);

var variableXPosition = v_XWindowPosition.ConvertToUserVariable(sender);
var variableYPosition = v_YWindowPosition.ConvertToUserVariable(sender);

if (!int.TryParse(variableXPosition, out int xPos))
{
throw new Exception("X Position Invalid - " + v_XWindowPosition);
}
if (!int.TryParse(variableYPosition, out int yPos))
{
var variableXPosition = v_XWindowPosition.ConvertToUserVariable(sender);
var variableYPosition = v_YWindowPosition.ConvertToUserVariable(sender);
throw new Exception("X Position Invalid - " + v_XWindowPosition);
}

if (!int.TryParse(variableXPosition, out int xPos))
if (searchMethod == "Contains" || targetIsCurrentWindow)
{
//loop each window
foreach (var targetedWindow in targetWindows)
{
throw new Exception("X Position Invalid - " + v_XWindowPosition);
User32Functions.SetWindowPosition(targetedWindow, xPos, yPos);
}
if (!int.TryParse(variableYPosition, out int yPos))
}
else
{
Func<string, bool> searchFunc;
switch (searchMethod)
{
throw new Exception("X Position Invalid - " + v_XWindowPosition);
}
case "Start with":
searchFunc = (s) => s.StartsWith(windowName);
break;

case "End with":
searchFunc = (s) => s.EndsWith(windowName);
break;

case "Exact match":
searchFunc = (s) => (s == windowName);
break;

default:
throw new Exception("Search method " + searchMethod + " is not support.");
break;
}

User32Functions.SetWindowPosition(targetedWindow, xPos, yPos);
bool isMoveWindow = false;
//loop each window
foreach (var targetedWindow in targetWindows)
{
if (searchFunc(User32Functions.GetWindowTitle(targetedWindow)))
{
User32Functions.SetWindowPosition(targetedWindow, xPos, yPos);
isMoveWindow = true;
}
}
if (!isMoveWindow)
{
throw new Exception("Window name '" + windowName + "' is not found.Search method " + searchMethod + ".");
}
}

}
public override List<Control> Render(frmCommandEditor editor)
{
Expand All @@ -86,6 +143,8 @@ public override List<Control> Render(frmCommandEditor editor)
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_WindowName", this, new Control[] { WindowNameControl }, editor));
RenderedControls.Add(WindowNameControl);

RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_SearchMethod", this, editor));

var xGroup = CommandControls.CreateDefaultInputGroupFor("v_XWindowPosition", this, editor);
var yGroup = CommandControls.CreateDefaultInputGroupFor("v_YWindowPosition", this, editor);
RenderedControls.AddRange(xGroup);
Expand Down
Loading

0 comments on commit aaa9475

Please sign in to comment.