Skip to content

Commit

Permalink
feat: convert mouse coordinates from integers to doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Sep 1, 2021
1 parent 9c9e6cb commit bbd4534
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
14 changes: 8 additions & 6 deletions Sources/SendKeysLib/Commands/MouseDragCommand.swift
Expand Up @@ -4,10 +4,12 @@ public class MouseDragCommand: MouseMoveCommand {
public override class var commandType: CommandType { return .mouseDrag }

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

public init(x1: Int?, y1: Int?, x2: Int, y2: Int, duration: TimeInterval, button: String?, modifiers: [String]) {
public init(
x1: Double?, y1: Double?, x2: Double, y2: Double, duration: TimeInterval, button: String?, modifiers: [String]
) {
super.init()

self.x1 = x1
Expand All @@ -21,10 +23,10 @@ public class MouseDragCommand: MouseMoveCommand {

required public init(arguments: [String?]) {
super.init()
self.x1 = Int(arguments[2] ?? "")
self.y1 = Int(arguments[3] ?? "")
self.x2 = Int(arguments[4]!)!
self.y2 = Int(arguments[5]!)!
self.x1 = Double(arguments[2] ?? "")
self.y1 = Double(arguments[3] ?? "")
self.x2 = Double(arguments[4]!)!
self.y2 = Double(arguments[5]!)!
self.duration = TimeInterval(arguments[7] ?? "0")!
self.button = arguments[9] ?? "left"
self.modifiers = arguments[11]?.components(separatedBy: ",").filter({ !$0.isEmpty }) ?? []
Expand Down
22 changes: 12 additions & 10 deletions Sources/SendKeysLib/Commands/MouseFocusCommand.swift
Expand Up @@ -5,18 +5,20 @@ public class MouseFocusCommand: MouseClickCommand {

// <mf:centerX,centerY:radiusX[,radiusY]:angleFrom,angleTo:duration>
private static let _expression = try! NSRegularExpression(
pattern: "\\<mf:(-?\\d+),(-?\\d+):(\\d+)(,(\\d+))?:(-?[.\\d]+),(-?[.\\d]+):([.\\d]+)\\>")
pattern: "\\<mf:(-?[.\\d]+),(-?[.\\d]+):([.\\d]+)(,([.\\d]+))?:(-?[.\\d]+),(-?[.\\d]+):([.\\d]+)\\>")
public override class var expression: NSRegularExpression { return _expression }

var x: Int
var y: Int
var rx: Int
var ry: Int
var x: Double
var y: Double
var rx: Double
var ry: Double
var from: Double
var to: Double
var duration: TimeInterval

public init(x: Int, y: Int, rx: Int, ry: Int, angleFrom: Double, angleTo: Double, duration: TimeInterval) {
public init(
x: Double, y: Double, rx: Double, ry: Double, angleFrom: Double, angleTo: Double, duration: TimeInterval
) {
self.x = x
self.y = y
self.rx = rx
Expand All @@ -29,10 +31,10 @@ public class MouseFocusCommand: MouseClickCommand {
}

required public init(arguments: [String?]) {
self.x = Int(arguments[1]!)!
self.y = Int(arguments[2]!)!
self.rx = Int(arguments[3]!)!
self.ry = Int(arguments[5] ?? arguments[3]!)!
self.x = Double(arguments[1]!)!
self.y = Double(arguments[2]!)!
self.rx = Double(arguments[3]!)!
self.ry = Double(arguments[5] ?? arguments[3]!)!
self.from = Double(arguments[6]!)!
self.to = Double(arguments[7]!)!
self.duration = TimeInterval(arguments[8]!)!
Expand Down
20 changes: 10 additions & 10 deletions Sources/SendKeysLib/Commands/MouseMoveCommand.swift
Expand Up @@ -4,20 +4,20 @@ public class MouseMoveCommand: MouseClickCommand {
public override class var commandType: CommandType { return .mouseMove }

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

var x1: Int?
var y1: Int?
var x2: Int = 0
var y2: Int = 0
var x1: Double?
var y1: Double?
var x2: Double = 0
var y2: Double = 0
var duration: TimeInterval = 0

override init() {
super.init()
}

public init(x1: Int?, y1: Int?, x2: Int, y2: Int, duration: TimeInterval, modifiers: [String]) {
public init(x1: Double?, y1: Double?, x2: Double, y2: Double, duration: TimeInterval, modifiers: [String]) {
super.init()

self.x1 = x1
Expand All @@ -30,10 +30,10 @@ public class MouseMoveCommand: MouseClickCommand {

required public init(arguments: [String?]) {
super.init()
self.x1 = Int(arguments[2] ?? "")
self.y1 = Int(arguments[3] ?? "")
self.x2 = Int(arguments[4]!)!
self.y2 = Int(arguments[5]!)!
self.x1 = Double(arguments[2] ?? "")
self.y1 = Double(arguments[3] ?? "")
self.x2 = Double(arguments[4]!)!
self.y2 = Double(arguments[5]!)!
self.duration = TimeInterval(arguments[7] ?? "0")!
self.modifiers = arguments[9]?.components(separatedBy: ",").filter({ !$0.isEmpty }) ?? []
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/SendKeysLib/Commands/MouseScrollCommand.swift
Expand Up @@ -4,14 +4,14 @@ public class MouseScrollCommand: MouseClickCommand {
public override class var commandType: CommandType { return .mouseScroll }

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

var x: Int
var y: Int
var x: Double
var y: Double
var duration: TimeInterval

public init(x: Int, y: Int, duration: TimeInterval, modifiers: [String]) {
public init(x: Double, y: Double, duration: TimeInterval, modifiers: [String]) {
self.x = x
self.y = y
self.duration = duration
Expand All @@ -21,8 +21,8 @@ public class MouseScrollCommand: MouseClickCommand {
}

required public init(arguments: [String?]) {
self.x = Int(arguments[1]!)!
self.y = Int(arguments[2]!)!
self.x = Double(arguments[1]!)!
self.y = Double(arguments[2]!)!
self.duration = TimeInterval(arguments[4] ?? "0")!

super.init()
Expand Down
20 changes: 10 additions & 10 deletions Tests/SendKeysTests/CommandIteratorTests.swift
Expand Up @@ -173,11 +173,11 @@ final class CommandIteratorTests: XCTestCase {
}

func testParsesMouseMove() throws {
let commands = getCommands(CommandsIterator("<m:1,2,3,4>"))
let commands = getCommands(CommandsIterator("<m:1.5,2.5,3.5,4.5>"))
XCTAssertEqual(
commands,
[
MouseMoveCommand(x1: 1, y1: 2, x2: 3, y2: 4, duration: 0, modifiers: [])
MouseMoveCommand(x1: 1.5, y1: 2.5, x2: 3.5, y2: 4.5, duration: 0, modifiers: [])
])
}

Expand Down Expand Up @@ -282,11 +282,11 @@ final class CommandIteratorTests: XCTestCase {
}

func testParsesMouseDrag() throws {
let commands = getCommands(CommandsIterator("<d:1,2,3,4>"))
let commands = getCommands(CommandsIterator("<d:1.5,2.5,3.5,4.5>"))
XCTAssertEqual(
commands,
[
MouseDragCommand(x1: 1, y1: 2, x2: 3, y2: 4, duration: 0, button: "left", modifiers: [])
MouseDragCommand(x1: 1.5, y1: 2.5, x2: 3.5, y2: 4.5, duration: 0, button: "left", modifiers: [])
])
}

Expand Down Expand Up @@ -319,11 +319,11 @@ final class CommandIteratorTests: XCTestCase {
}

func testParsesMouseDragWithDurationWithNegativeCoordinates() throws {
let commands = getCommands(CommandsIterator("<d:-1,-2,-3,-4:0.1>"))
let commands = getCommands(CommandsIterator("<d:-1.5,-2,-3,-4:0.1>"))
XCTAssertEqual(
commands,
[
MouseDragCommand(x1: -1, y1: -2, x2: -3, y2: -4, duration: 0.1, button: "left", modifiers: [])
MouseDragCommand(x1: -1.5, y1: -2, x2: -3, y2: -4, duration: 0.1, button: "left", modifiers: [])
])
}

Expand Down Expand Up @@ -373,11 +373,11 @@ final class CommandIteratorTests: XCTestCase {
}

func testParsesMouseScroll() throws {
let commands = getCommands(CommandsIterator("<s:0,10>"))
let commands = getCommands(CommandsIterator("<s:0,10.5>"))
XCTAssertEqual(
commands,
[
MouseScrollCommand(x: 0, y: 10, duration: 0, modifiers: [])
MouseScrollCommand(x: 0, y: 10.5, duration: 0, modifiers: [])
])
}

Expand Down Expand Up @@ -454,11 +454,11 @@ final class CommandIteratorTests: XCTestCase {
}

func testParsesMouseFocus() throws {
let commands = getCommands(CommandsIterator("<mf:0,0:100,50:0,360:1>"))
let commands = getCommands(CommandsIterator("<mf:0.5,0.5:100.5,50.5:0.5,360.5:1>"))
XCTAssertEqual(
commands,
[
MouseFocusCommand(x: 0, y: 0, rx: 100, ry: 50, angleFrom: 0, angleTo: 360, duration: 1)
MouseFocusCommand(x: 0.5, y: 0.5, rx: 100.5, ry: 50.5, angleFrom: 0.5, angleTo: 360.5, duration: 1)
])
}

Expand Down

0 comments on commit bbd4534

Please sign in to comment.