-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
879cb61
commit 2fa4be0
Showing
10 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import AppKit | ||
import Common | ||
|
||
struct MoveMouseCommand: Command { | ||
let args: MoveMouseCmdArgs | ||
|
||
func _run(_ state: CommandMutableState, stdin: String) -> Bool { | ||
check(Thread.current.isMainThread) | ||
// todo bug it's bad that we operate on the "ax physical" state directly. command seq won't work correctly | ||
// focus <direction> command has the similar problem | ||
let mouse = mouseLocation | ||
let point: Result<CGPoint, String> = switch args.mouseTarget.val { | ||
case .windowLazyCenter: | ||
windowSubjectRect(state) | ||
.flatMap { $0.takeIf { !$0.contains(mouse) }.orFailure("The mouse already belongs to the window") } | ||
.map(\.center) | ||
case .windowForceCenter: | ||
windowSubjectRect(state).map(\.center) | ||
case .monitorLazyCenter: | ||
Result.success(state.subject.workspace.workspaceMonitor.rect) | ||
.flatMap { $0.takeIf { !$0.contains(mouse) }.orFailure("The mouse already belongs to the monitor") } | ||
.map(\.center) | ||
case .monitorForceCenter: | ||
.success(state.subject.workspace.workspaceMonitor.rect.center) | ||
} | ||
switch point { | ||
case .success(let point): | ||
CGEvent( | ||
mouseEventSource: nil, | ||
mouseType: CGEventType.mouseMoved, | ||
mouseCursorPosition: point, | ||
mouseButton: CGMouseButton.left | ||
)?.post(tap: CGEventTapLocation.cghidEventTap) | ||
return true | ||
case .failure(let msg): | ||
return state.failCmd(msg: msg) | ||
} | ||
} | ||
} | ||
|
||
private func windowSubjectRect(_ state: CommandMutableState) -> Result<Rect, String> { | ||
if let window: Window = state.subject.windowOrNil { | ||
if let rect = window.getRect() { | ||
return .success(rect) | ||
} else { | ||
return .failure("Failed to get rect of window '\(window.windowId)'") | ||
} | ||
} else { | ||
return .failure(noWindowIsFocused) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
public struct MoveMouseCmdArgs: CmdArgs, RawCmdArgs { | ||
public let rawArgs: EquatableNoop<[String]> | ||
init(rawArgs: [String]) { self.rawArgs = .init(rawArgs) } | ||
public static let parser: CmdParser<Self> = cmdParser( | ||
kind: .moveMouse, | ||
allowInConfig: true, | ||
help: """ | ||
USAGE: move-mouse [-h|--help] <mouse-position> | ||
OPTIONS: | ||
-h, --help Print help | ||
ARGUMENTS: | ||
<mouse-position> Position to move mouse to. See the man page for the possible values. | ||
""", | ||
options: [:], | ||
arguments: [newArgParser(\.mouseTarget, parseMouseTarget, mandatoryArgPlaceholder: "<mouse-position>")] | ||
) | ||
|
||
public var mouseTarget: Lateinit<MouseTarget> = .uninitialized | ||
} | ||
|
||
func parseMouseTarget(arg: String, nextArgs: inout [String]) -> Parsed<MouseTarget> { | ||
parseEnum(arg, MouseTarget.self) | ||
} | ||
|
||
public enum MouseTarget: String, CaseIterable { | ||
case monitorLazyCenter = "monitor-lazy-center" | ||
case monitorForceCenter = "monitor-force-center" | ||
|
||
case windowLazyCenter = "window-lazy-center" | ||
case windowForceCenter = "window-force-center" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
= aerospace-move-mouse(1) | ||
include::./util/man-attributes.adoc[] | ||
// tag::purpose[] | ||
:manpurpose: Move mouse to the requested position | ||
// end::purpose[] | ||
:manname: aerospace-move-mouse | ||
|
||
// =========================================================== Synopsis | ||
== Synopsis | ||
// tag::synopsis[] | ||
aerospace move-mouse [-h|--help] <mouse-position> | ||
// end::synopsis[] | ||
|
||
// =========================================================== Description | ||
== Description | ||
|
||
// tag::body[] | ||
{manpurpose} | ||
|
||
// =========================================================== Options | ||
include::./util/conditional-options-header.adoc[] | ||
|
||
-h, --help:: Print help | ||
|
||
// =========================================================== Arguments | ||
include::./util/conditional-arguments-header.adoc[] | ||
|
||
<mouse-position>:: | ||
Position to move mouse to. | ||
Possible values: | ||
+ | ||
[cols="1,3"] | ||
|=== | ||
|Value |Description | ||
|
||
|`monitor-lazy-center` | ||
|Move mouse to the center of the focused monitor, *unless* it is already within the monitor boundaries | ||
|
||
|`monitor-force-center` | ||
|Move mouse to the center of the focused monitor | ||
|
||
|`window-lazy-center` | ||
|Move mouse to the center of the focused window, *unless* it is already within the window boundaries | ||
|
||
|`window-force-center` | ||
|Move mouse to the center of the focused window | ||
|
||
|=== | ||
|
||
// =========================================================== Examples | ||
include::util/conditional-examples-header.adoc[] | ||
|
||
* Try to move mouse to the center of the window. If there is no window in focus, move mouse to the center of the monitor: + | ||
`aerospace move-mouse window-lazy-center || aerospace move-mouse monitor-lazy-center` | ||
|
||
// end::body[] | ||
|
||
// =========================================================== Footer | ||
include::./util/man-footer.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters