forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppsHeaderRowLayoutTests.swift
More file actions
199 lines (179 loc) · 6.04 KB
/
Copy pathAppsHeaderRowLayoutTests.swift
File metadata and controls
199 lines (179 loc) · 6.04 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import AppKit
import SwiftUI
import XCTest
@testable import OmiTheme
@testable import Omi_Computer
@MainActor
final class AppsHeaderRowLayoutTests: XCTestCase {
func testSearchFieldIsCappedOnAWideWindow() {
let recorder = AppsHeaderLayoutRecorder()
layOut(width: 1000, recorder: recorder)
guard let search = recorder.frame(of: .search) else {
return XCTFail("the search field never laid out")
}
XCTAssertEqual(search.width, AppsHeaderMetrics.searchFieldMaxWidth, accuracy: 0.5)
}
func testLabelledFiltersRemainLegibleAtOrdinaryAndCompactWidths() {
let intrinsicFilterWidth = NSHostingView(
rootView: AppsHeaderFilterFixture().fixedSize()
).fittingSize.width
for width in [720.0, 380.0] {
let recorder = AppsHeaderLayoutRecorder()
layOut(width: width, recorder: recorder)
guard let filters = recorder.frame(of: .filters) else {
return XCTFail("the filters never laid out at \(width)pt")
}
XCTAssertGreaterThanOrEqual(filters.width, intrinsicFilterWidth - 0.5)
}
}
func testCreateControlMovesBelowFiltersWhenTheCompactRowWouldOverflow() {
let recorder = AppsHeaderLayoutRecorder()
let width: CGFloat = 380
layOut(
width: width,
recorder: recorder,
filters: Color.clear.frame(width: 280, height: AppsHeaderMetrics.controlHeight)
)
guard let filters = recorder.frame(of: .filters),
let create = recorder.frame(of: .create)
else {
return XCTFail("the compact header controls never laid out")
}
XCTAssertLessThanOrEqual(filters.maxX, width + 0.5)
XCTAssertLessThanOrEqual(create.maxX, width + 0.5)
XCTAssertGreaterThanOrEqual(
create.minY,
filters.maxY - 0.5,
"Create App should move below filters instead of extending the compact row"
)
}
func testAppsToolbarControlsShareTheSearchFieldHeight() {
let recorder = AppsHeaderLayoutRecorder()
let host = NSHostingView(
rootView: HStack(spacing: OmiSpacing.md) {
AppsHeaderLayoutProbe(recorder: recorder, slot: .search) {
Color.clear
.frame(width: 240, height: AppsHeaderMetrics.controlHeight)
}
AppsHeaderLayoutProbe(recorder: recorder, slot: .installed) {
FilterToggle(icon: "arrow.down.circle", label: "Installed", isActive: false) {}
}
AppsHeaderLayoutProbe(recorder: recorder, slot: .category) {
SearchableDropdown(
title: "Category",
label: "Category",
options: [SearchableDropdownOption(id: "", title: "All Categories")],
selectedId: "",
minWidth: 180,
controlHeight: AppsHeaderMetrics.controlHeight,
usesHeaderChrome: true
) { _ in }
}
AppsHeaderLayoutProbe(recorder: recorder, slot: .create) {
SmallHeaderButton(icon: "app.badge.fill", label: "Create App", color: Ink.secondary) {}
}
}
.fixedSize()
)
host.frame = NSRect(x: 0, y: 0, width: 900, height: 80)
host.layoutSubtreeIfNeeded()
for slot in [AppsHeaderLayoutSlot.search, .installed, .category, .create] {
guard let frame = recorder.frame(of: slot) else {
return XCTFail("\(slot) never laid out")
}
XCTAssertEqual(
frame.height,
AppsHeaderMetrics.controlHeight,
accuracy: 0.5,
"\(slot) should occupy the shared Apps toolbar height"
)
}
}
private func layOut(width: CGFloat, recorder: AppsHeaderLayoutRecorder) {
layOut(width: width, recorder: recorder, filters: AppsHeaderFilterFixture())
}
private func layOut<Filters: View>(
width: CGFloat,
recorder: AppsHeaderLayoutRecorder,
filters: Filters
) {
let host = NSHostingView(
rootView: AppsHeaderRow(
search: {
AppsHeaderLayoutProbe(recorder: recorder, slot: .search) {
Color.clear.frame(maxWidth: .infinity).frame(height: AppsHeaderMetrics.controlHeight)
}
},
filters: {
AppsHeaderLayoutProbe(recorder: recorder, slot: .filters) {
filters
}
},
create: {
AppsHeaderLayoutProbe(recorder: recorder, slot: .create) {
Color.clear.frame(width: 100, height: AppsHeaderMetrics.controlHeight)
}
},
dismiss: { EmptyView() }
)
.frame(width: width)
)
host.frame = NSRect(x: 0, y: 0, width: width, height: 200)
host.layoutSubtreeIfNeeded()
}
}
private struct AppsHeaderFilterFixture: View {
var body: some View {
HStack(spacing: OmiSpacing.sm) {
Text("Installed")
.padding(.horizontal, OmiSpacing.md)
Text("All Categories")
.padding(.horizontal, OmiSpacing.md)
}
.frame(height: AppsHeaderMetrics.controlHeight)
}
}
private enum AppsHeaderLayoutSlot {
case search
case filters
case installed
case category
case create
}
private final class AppsHeaderLayoutRecorder: @unchecked Sendable {
private let lock = NSLock()
private var frames: [AppsHeaderLayoutSlot: CGRect] = [:]
func record(_ slot: AppsHeaderLayoutSlot, _ frame: CGRect) {
lock.lock()
frames[slot] = frame
lock.unlock()
}
func frame(of slot: AppsHeaderLayoutSlot) -> CGRect? {
lock.lock()
defer { lock.unlock() }
return frames[slot]
}
}
private struct AppsHeaderLayoutProbe: Layout {
let recorder: AppsHeaderLayoutRecorder
let slot: AppsHeaderLayoutSlot
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
subviews.reduce(into: .zero) { size, subview in
let child = subview.sizeThatFits(proposal)
size.width = max(size.width, child.width)
size.height = max(size.height, child.height)
}
}
func placeSubviews(
in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()
) {
recorder.record(slot, bounds)
for subview in subviews {
subview.place(
at: CGPoint(x: bounds.minX, y: bounds.minY),
anchor: .topLeading,
proposal: ProposedViewSize(width: bounds.width, height: bounds.height)
)
}
}
}