forked from nikitabobko/AeroSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapCommand.swift
More file actions
51 lines (45 loc) · 1.92 KB
/
SwapCommand.swift
File metadata and controls
51 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import AppKit
import Common
struct SwapCommand: Command {
let args: SwapCmdArgs
func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
guard let target = args.resolveTargetOrReportError(env, io), let currentWindow = target.windowOrNil else {
return io.err(noWindowIsFocused)
}
var targetWindow: Window?
switch args.target.val {
case .direction(let direction):
if let (parent, ownIndex) = currentWindow.closestParent(hasChildrenInDirection: direction, withLayout: nil) {
targetWindow = parent.children[ownIndex + direction.focusOffset].findFocusTargetRecursive(snappedTo: direction.opposite)
} else if args.wrapAround {
targetWindow = target.workspace.findFocusTargetRecursive(snappedTo: direction.opposite)
} else {
return false
}
case .dfsRelative(let nextPrev):
let windows = target.workspace.rootTilingContainer.allLeafWindowsRecursive
guard let currentIndex = windows.firstIndex(where: { $0 == target.windowOrNil }) else {
return false
}
var targetIndex = switch nextPrev {
case .next: currentIndex + 1
case .prev: currentIndex - 1
}
if targetIndex < 0 || targetIndex >= windows.count {
if !args.wrapAround {
return false
}
targetIndex = (targetIndex + windows.count) % windows.count
}
targetWindow = windows[targetIndex]
}
guard let targetWindow else {
return false
}
swapWindows(currentWindow, targetWindow)
if args.swapFocus {
return targetWindow.focusWindow()
}
return currentWindow.focusWindow()
}
}