forked from nikitabobko/AeroSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayoutRecursive.swift
More file actions
174 lines (161 loc) · 8.44 KB
/
layoutRecursive.swift
File metadata and controls
174 lines (161 loc) · 8.44 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import AppKit
extension Workspace {
@MainActor
func layoutWorkspace() async throws {
if isEffectivelyEmpty { return }
let rect = workspaceMonitor.visibleRectPaddedByOuterGaps
// If monitors are aligned vertically and the monitor below has smaller width, then macOS may not allow the
// window on the upper monitor to take full width. rect.height - 1 resolves this problem
// But I also faced this problem in monitors horizontal configuration. ¯\_(ツ)_/¯
try await layoutRecursive(rect.topLeftCorner, width: rect.width, height: rect.height - 1, virtual: rect, LayoutContext(self))
}
}
extension TreeNode {
@MainActor
fileprivate func layoutRecursive(_ point: CGPoint, width: CGFloat, height: CGFloat, virtual: Rect, _ context: LayoutContext) async throws {
let physicalRect = Rect(topLeftX: point.x, topLeftY: point.y, width: width, height: height)
switch nodeCases {
case .workspace(let workspace):
lastAppliedLayoutPhysicalRect = physicalRect
lastAppliedLayoutVirtualRect = virtual
try await workspace.rootTilingContainer.layoutRecursive(point, width: width, height: height, virtual: virtual, context)
for window in workspace.children.filterIsInstance(of: Window.self) {
window.lastAppliedLayoutPhysicalRect = nil
window.lastAppliedLayoutVirtualRect = nil
try await window.layoutFloatingWindow(context)
}
case .window(let window):
if window.windowId != currentlyManipulatedWithMouseWindowId {
lastAppliedLayoutVirtualRect = virtual
if window.isFullscreen && window == context.workspace.rootTilingContainer.mostRecentWindowRecursive {
lastAppliedLayoutPhysicalRect = nil
window.layoutFullscreen(context)
} else {
lastAppliedLayoutPhysicalRect = physicalRect
window.isFullscreen = false
window.setAxFrame(point, CGSize(width: width, height: height))
}
}
case .tilingContainer(let container):
lastAppliedLayoutPhysicalRect = physicalRect
lastAppliedLayoutVirtualRect = virtual
switch container.layout {
case .tiles:
try await container.layoutTiles(point, width: width, height: height, virtual: virtual, context)
case .accordion:
try await container.layoutAccordion(point, width: width, height: height, virtual: virtual, context)
}
case .macosMinimizedWindowsContainer, .macosFullscreenWindowsContainer,
.macosPopupWindowsContainer, .macosHiddenAppsWindowsContainer:
return // Nothing to do for weirdos
}
}
}
private struct LayoutContext {
let workspace: Workspace
let resolvedGaps: ResolvedGaps
@MainActor
init(_ workspace: Workspace) {
self.workspace = workspace
self.resolvedGaps = ResolvedGaps(gaps: config.gaps, monitor: workspace.workspaceMonitor)
}
}
extension Window {
@MainActor
fileprivate func layoutFloatingWindow(_ context: LayoutContext) async throws {
let workspace = context.workspace
let windowRect = try await getAxRect() // Probably not idempotent
let currentMonitor = windowRect?.center.monitorApproximation
if let currentMonitor, let windowRect, workspace != currentMonitor.activeWorkspace {
let windowTopLeftCorner = windowRect.topLeftCorner
let xProportion = (windowTopLeftCorner.x - currentMonitor.visibleRect.topLeftX) / currentMonitor.visibleRect.width
let yProportion = (windowTopLeftCorner.y - currentMonitor.visibleRect.topLeftY) / currentMonitor.visibleRect.height
let workspaceRect = workspace.workspaceMonitor.visibleRect
var newX = workspaceRect.topLeftX + xProportion * workspaceRect.width
var newY = workspaceRect.topLeftY + yProportion * workspaceRect.height
let windowWidth = windowRect.width
let windowHeight = windowRect.height
newX = newX.coerceIn(workspaceRect.minX ... max(workspaceRect.minX, workspaceRect.maxX - windowWidth))
newY = newY.coerceIn(workspaceRect.minY ... max(workspaceRect.minY, workspaceRect.maxY - windowHeight))
setAxFrame(CGPoint(x: newX, y: newY), nil)
}
if isFullscreen {
layoutFullscreen(context)
isFullscreen = false
}
}
@MainActor
fileprivate func layoutFullscreen(_ context: LayoutContext) {
let monitorRect = noOuterGapsInFullscreen
? context.workspace.workspaceMonitor.visibleRect
: context.workspace.workspaceMonitor.visibleRectPaddedByOuterGaps
setAxFrame(monitorRect.topLeftCorner, CGSize(width: monitorRect.width, height: monitorRect.height))
}
}
extension TilingContainer {
@MainActor
fileprivate func layoutTiles(_ point: CGPoint, width: CGFloat, height: CGFloat, virtual: Rect, _ context: LayoutContext) async throws {
var point = point
var virtualPoint = virtual.topLeftCorner
guard let delta = ((orientation == .h ? width : height) - CGFloat(children.sumOfDouble { $0.getWeight(orientation) }))
.div(children.count) else { return }
let lastIndex = children.indices.last
for (i, child) in children.enumerated() {
child.setWeight(orientation, child.getWeight(orientation) + delta)
let rawGap = context.resolvedGaps.inner.get(orientation).toDouble()
// Gaps. Consider 4 cases:
// 1. Multiple children. Layout first child
// 2. Multiple children. Layout last child
// 3. Multiple children. Layout child in the middle
// 4. Single child let rawGap = gaps.inner.get(orientation).toDouble()
let gap = rawGap - (i == 0 ? rawGap / 2 : 0) - (i == lastIndex ? rawGap / 2 : 0)
try await child.layoutRecursive(
i == 0 ? point : point.addingOffset(orientation, rawGap / 2),
width: orientation == .h ? child.hWeight - gap : width,
height: orientation == .v ? child.vWeight - gap : height,
virtual: Rect(
topLeftX: virtualPoint.x,
topLeftY: virtualPoint.y,
width: orientation == .h ? child.hWeight : width,
height: orientation == .v ? child.vWeight : height,
),
context,
)
virtualPoint = orientation == .h ? virtualPoint.addingXOffset(child.hWeight) : virtualPoint.addingYOffset(child.vWeight)
point = orientation == .h ? point.addingXOffset(child.hWeight) : point.addingYOffset(child.vWeight)
}
}
@MainActor
fileprivate func layoutAccordion(_ point: CGPoint, width: CGFloat, height: CGFloat, virtual: Rect, _ context: LayoutContext) async throws {
guard let mruIndex: Int = mostRecentChild?.ownIndex else { return }
for (index, child) in children.enumerated() {
let padding = CGFloat(config.accordionPadding)
let (lPadding, rPadding): (CGFloat, CGFloat) = switch index {
case 0 where children.count == 1: (0, 0)
case 0: (0, padding)
case children.indices.last: (padding, 0)
case mruIndex - 1: (0, 2 * padding)
case mruIndex + 1: (2 * padding, 0)
default: (padding, padding)
}
switch orientation {
case .h:
try await child.layoutRecursive(
point + CGPoint(x: lPadding, y: 0),
width: width - rPadding - lPadding,
height: height,
virtual: virtual,
context,
)
case .v:
try await child.layoutRecursive(
point + CGPoint(x: 0, y: lPadding),
width: width,
height: height - lPadding - rPadding,
virtual: virtual,
context,
)
}
}
}
}