|
| 1 | +import AppKit |
| 2 | +import Common |
| 3 | + |
| 4 | +struct MasterStackCommand: Command { |
| 5 | + let args: MasterStackCmdArgs |
| 6 | + /*conforms*/ let shouldResetClosedWindowsCache: Bool = true |
| 7 | + |
| 8 | + func run(_ env: CmdEnv, _ io: CmdIo) -> Bool { |
| 9 | + guard let target = args.resolveTargetOrReportError(env, io) else { return false } |
| 10 | + let workspace = target.workspace |
| 11 | + let root = workspace.rootTilingContainer |
| 12 | + |
| 13 | + let allWindows = root.allLeafWindowsRecursive |
| 14 | + guard allWindows.count >= 2 else { return true } |
| 15 | + |
| 16 | + let master: Window |
| 17 | + if args.cycle { |
| 18 | + // Use stable window ID order so cycling visits all windows |
| 19 | + let sortedById = allWindows.sorted { $0.windowId < $1.windowId } |
| 20 | + // Current master is the first leaf in the tree (DFS) |
| 21 | + guard let currentMaster = allWindows.first else { return true } |
| 22 | + let currentIndex = sortedById.firstIndex(of: currentMaster) ?? 0 |
| 23 | + let nextIndex = (currentIndex + 1) % sortedById.count |
| 24 | + master = sortedById[nextIndex] |
| 25 | + } else { |
| 26 | + guard let focusedWindow = target.windowOrNil else { |
| 27 | + return io.err(noWindowIsFocused) |
| 28 | + } |
| 29 | + master = focusedWindow |
| 30 | + } |
| 31 | + |
| 32 | + // Step 1: Flatten all windows to root (same as flatten-workspace-tree) |
| 33 | + for window in allWindows { |
| 34 | + window.bind(to: root, adaptiveWeight: 1, index: INDEX_BIND_LAST) |
| 35 | + } |
| 36 | + root.changeOrientation(.h) |
| 37 | + |
| 38 | + // Step 2: Collect non-master windows |
| 39 | + var others: [Window] = [] |
| 40 | + for window in root.allLeafWindowsRecursive where window !== master { |
| 41 | + others.append(window) |
| 42 | + } |
| 43 | + |
| 44 | + // Step 3: Move master to index 0 with weight 7 |
| 45 | + master.bind(to: root, adaptiveWeight: 7, index: 0) |
| 46 | + |
| 47 | + // Step 4: Create stack container and build spiral |
| 48 | + if others.count == 1 { |
| 49 | + others[0].bind(to: root, adaptiveWeight: 3, index: 1) |
| 50 | + } else { |
| 51 | + let stackContainer = TilingContainer( |
| 52 | + parent: root, |
| 53 | + adaptiveWeight: 3, |
| 54 | + .v, |
| 55 | + .tiles, |
| 56 | + index: 1, |
| 57 | + ) |
| 58 | + buildSpiral(windows: others, parent: stackContainer, startOrientation: .h) |
| 59 | + } |
| 60 | + |
| 61 | + // Focus the master window |
| 62 | + if args.cycle { |
| 63 | + _ = master.focusWindow() |
| 64 | + } |
| 65 | + |
| 66 | + return true |
| 67 | + } |
| 68 | + |
| 69 | + @MainActor private func buildSpiral(windows: [Window], parent: TilingContainer, startOrientation: Orientation) { |
| 70 | + if windows.count == 1 { |
| 71 | + windows[0].bind(to: parent, adaptiveWeight: WEIGHT_AUTO, index: INDEX_BIND_LAST) |
| 72 | + } else if windows.count == 2 { |
| 73 | + windows[0].bind(to: parent, adaptiveWeight: 1, index: 0) |
| 74 | + windows[1].bind(to: parent, adaptiveWeight: 1, index: 1) |
| 75 | + } else { |
| 76 | + windows[0].bind(to: parent, adaptiveWeight: 1, index: 0) |
| 77 | + let sub = TilingContainer( |
| 78 | + parent: parent, |
| 79 | + adaptiveWeight: 1, |
| 80 | + startOrientation, |
| 81 | + .tiles, |
| 82 | + index: 1, |
| 83 | + ) |
| 84 | + buildSpiral(windows: Array(windows.dropFirst()), parent: sub, startOrientation: startOrientation.opposite) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments