Skip to content

Commit

Permalink
feat: output mouse positions as decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Sep 1, 2021
1 parent bbd4534 commit d723082
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
11 changes: 6 additions & 5 deletions Sources/SendKeysLib/MouseEventProcessor.swift
Expand Up @@ -52,7 +52,8 @@ class MouseEvent: CustomStringConvertible {
var moveParts: [String] = []
var clickParts: [String] = []

moveParts.append(String(format: "%.0f,%.0f", endPoint.x, endPoint.y))
moveParts.append(
"\(Self.numberFormatter.string(for: endPoint.x)!),\(Self.numberFormatter.string(for: endPoint.y)!)")

if duration > 0 {
moveParts.append(Self.numberFormatter.string(for: duration)!)
Expand All @@ -64,7 +65,9 @@ class MouseEvent: CustomStringConvertible {
case .drag:
var parts: [String] = []

parts.append(String(format: "%.0f,%.0f,%.0f,%.0f", startPoint.x, startPoint.y, endPoint.x, endPoint.y))
parts.append(
"\(Self.numberFormatter.string(for: startPoint.x)!),\(Self.numberFormatter.string(for: startPoint.y)!),\(Self.numberFormatter.string(for: endPoint.x)!),\(Self.numberFormatter.string(for: endPoint.y)!)"
)

if duration > 0 {
parts.append(Self.numberFormatter.string(for: duration)!)
Expand All @@ -79,9 +82,7 @@ class MouseEvent: CustomStringConvertible {
static func createNumberFormatter() -> NumberFormatter {
let numberFormatter = NumberFormatter()

numberFormatter.usesSignificantDigits = true
numberFormatter.minimumSignificantDigits = 1
numberFormatter.maximumSignificantDigits = 3
numberFormatter.maximumFractionDigits = 2

return numberFormatter
}
Expand Down
29 changes: 19 additions & 10 deletions Sources/SendKeysLib/MousePosition.swift
Expand Up @@ -28,20 +28,30 @@ class MousePosition: ParsableCommand {

static let eventProcessor = MouseEventProcessor()

private static func createNumberFormatter() -> NumberFormatter {
let numberFormatter = NumberFormatter()

numberFormatter.maximumFractionDigits = 2

return numberFormatter
}

required init() {
}

func run() {
if watch {
watchMouseInput()
} else {
printMousePosition()
printMousePosition(nil)
}
}

func printMousePosition() {
let location = MouseController(animationRefreshInterval: 0.01).getLocation()!
print(String(format: "%.0f,%.0f", location.x, location.y))
func printMousePosition(_ position: CGPoint?) {
let numberFormatter = Self.createNumberFormatter()
let location = position ?? MouseController(animationRefreshInterval: 0.01).getLocation()!

printAndFlush("\(numberFormatter.string(for: location.x)!),\(numberFormatter.string(for: location.y)!)")
}

func listenForInput() {
Expand All @@ -50,7 +60,7 @@ class MousePosition: ParsableCommand {
stderr)

waitForCharInput { _ in
printMousePosition()
printMousePosition(nil)
}
}

Expand Down Expand Up @@ -109,8 +119,7 @@ class MousePosition: ParsableCommand {
switch command.mode {
case .coordinates:
if mouseEvent.eventType == .click {
command.printAndFlush(
String(format: "%.0f,%.0f", mouseEvent.endPoint.x, mouseEvent.endPoint.y))
command.printMousePosition(mouseEvent.endPoint)
}
case .commands:
command.printAndFlush(mouseEvent.description)
Expand Down Expand Up @@ -139,11 +148,11 @@ class MousePosition: ParsableCommand {
{
switch mode {
case .coordinates:
printMousePosition()
printMousePosition(nil)
case .commands:
printMousePosition()
printMousePosition(nil)
}
print("Event \(type) \(type.rawValue)")
printAndFlush("Event \(type) \(type.rawValue)")

return Unmanaged.passRetained(event)
}
Expand Down

0 comments on commit d723082

Please sign in to comment.