Skip to content

Commit

Permalink
feat: make scaleY option in path command
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Sep 1, 2021
1 parent d723082 commit f1fe840
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -203,14 +203,15 @@ Example usage:
#### Mouse path

The mouse path command can be used move the mouse cursor along a path. The mouse path command uses the following markup:
`<mpath:path[:ofssetX,offsetY[,scaleX,scaleY]]:duration>`
`<mpath:path[:ofssetX,offsetY[,scaleX[,scaleY]]]:duration>`

- `path` is required and defines path for the mouse cursor to follow. The path is described using
[SVG Path data](https://www.w3.org/TR/SVG/paths.html#PathData)
- `ofssetX` and `offsetY` are optional and can be used to offset path coordinates by their respective `x` and `y`
values.
values. Defaults to `0,0`.
- `scaleX` and `scaleY` are also optional and can be used to scale path coordinates by their respective `x` and `y`
values.
values. Defaults to `1,1`. If `scaleY` is omitted while `scaleX` is provided, a uniform scale will be assumed. i.e.
`x` = `y`.
- `duration` is required and determines the number of seconds (supports partial seconds) used to complete the animation
along the `path`.

Expand Down
9 changes: 5 additions & 4 deletions Sources/SendKeysLib/Commands/MousePathCommand.swift
Expand Up @@ -4,7 +4,8 @@ public class MousePathCommand: MouseClickCommand {
public override class var commandType: CommandType { return .mousePath }

private static let _expression = try! NSRegularExpression(
pattern: "\\<mpath:([^:\\>]+)(:(-?[\\d.]+),(-?[\\d.]+)(,(-?[\\d.]+),(-?[\\d.]+))?)?:([\\d.]+)(:([a-z,]+))?\\>")
pattern:
"\\<mpath:([^:\\>]+)(:(-?[\\d.]+),(-?[\\d.]+)(,(-?[\\d.]+)(,(-?[\\d.]+))?)?)?:([\\d.]+)(:([a-z,]+))?\\>")
public override class var expression: NSRegularExpression { return _expression }

var path: String
Expand Down Expand Up @@ -34,11 +35,11 @@ public class MousePathCommand: MouseClickCommand {
self.offsetX = Double(arguments[3] ?? "0")!
self.offsetY = Double(arguments[4] ?? "0")!
self.scaleX = Double(arguments[6] ?? "1")!
self.scaleY = Double(arguments[7] ?? "1")!
self.duration = TimeInterval(arguments[8] ?? "0")!
self.scaleY = Double(arguments[8] ?? arguments[6] ?? "1")!
self.duration = TimeInterval(arguments[9] ?? "0")!

super.init()
self.modifiers = arguments[9]?.components(separatedBy: ",").filter({ !$0.isEmpty }) ?? []
self.modifiers = arguments[10]?.components(separatedBy: ",").filter({ !$0.isEmpty }) ?? []
}

public override func execute() throws {
Expand Down
30 changes: 30 additions & 0 deletions Tests/SendKeysTests/CommandIteratorTests.swift
Expand Up @@ -281,6 +281,36 @@ final class CommandIteratorTests: XCTestCase {
])
}

func testParsesMousePathWithOffset() throws {
let commands = getCommands(CommandsIterator("<mpath:L 200 400:100,200:2>"))
XCTAssertEqual(
commands,
[
MousePathCommand(
path: "L 200 400", offsetX: 100, offsetY: 200, scaleX: 1, scaleY: 1, duration: 2, modifiers: [])
])
}

func testParsesMousePathWithOffsetAndScale() throws {
let commands = getCommands(CommandsIterator("<mpath:L 200 400:100,200,0.5,2.5:2>"))
XCTAssertEqual(
commands,
[
MousePathCommand(
path: "L 200 400", offsetX: 100, offsetY: 200, scaleX: 0.5, scaleY: 2.5, duration: 2, modifiers: [])
])
}

func testParsesMousePathWithOffsetAndPartialScale() throws {
let commands = getCommands(CommandsIterator("<mpath:L 200 400:100,200,0.4:2>"))
XCTAssertEqual(
commands,
[
MousePathCommand(
path: "L 200 400", offsetX: 100, offsetY: 200, scaleX: 0.4, scaleY: 0.4, duration: 2, modifiers: [])
])
}

func testParsesMouseDrag() throws {
let commands = getCommands(CommandsIterator("<d:1.5,2.5,3.5,4.5>"))
XCTAssertEqual(
Expand Down

0 comments on commit f1fe840

Please sign in to comment.