forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageQueryToolbar.swift
More file actions
290 lines (259 loc) · 9.1 KB
/
Copy pathPageQueryToolbar.swift
File metadata and controls
290 lines (259 loc) · 9.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import OmiTheme
import SwiftUI
/// The compact vertical rhythm shared by every destination panel.
///
/// Each gap has exactly one owner: the destination owns the navigation-to-
/// surface gap, the panel owns its top inset, the preceding control row owns
/// the row gap, and content owns the final eight points before its first item.
/// This prevents adjacent views from stacking otherwise reasonable padding.
enum PagePanelVerticalRhythm {
static let horizontalPadding = QueryShellLayout.panelPaddingHorizontal
static let panelTopPadding = QueryShellLayout.panelPaddingTop
static let rowGap = QueryShellLayout.panelHeaderSpacing
static let contentGap = OmiSpacing.sm
static let sectionGap = OmiSpacing.lg
static let contentBottomPadding = OmiSpacing.lg
}
enum PagePanelFirstRowMetrics {
static let horizontalPadding = PagePanelVerticalRhythm.horizontalPadding
static let topPadding = PagePanelVerticalRhythm.panelTopPadding
static let bottomPadding: CGFloat = 0
}
extension View {
func pagePanelFirstRowInsets() -> some View {
padding(.horizontal, PagePanelFirstRowMetrics.horizontalPadding)
.padding(.top, PagePanelFirstRowMetrics.topPadding)
.padding(.bottom, PagePanelFirstRowMetrics.bottomPadding)
}
/// A refinement row below Brain navigation. Navigation already owns the six
/// points between rows, so this row only owns horizontal alignment.
func pagePanelSubsequentRowInsets() -> some View {
padding(.horizontal, PagePanelVerticalRhythm.horizontalPadding)
}
@ViewBuilder
func pagePanelToolbarInsets(isBelowNavigation: Bool) -> some View {
if isBelowNavigation {
pagePanelSubsequentRowInsets()
} else {
pagePanelFirstRowInsets()
}
}
}
/// Shared chrome for the controls that refine a page's search results.
///
/// Search remains in the product-wide search panel. This row belongs to the
/// content it changes and gives filters, sorting, modes, and actions distinct
/// positions instead of rendering all of them as an undifferentiated chip row.
struct PageQueryToolbar<Refinement: View, ActiveFilters: View, Actions: View>: View {
let refinement: Refinement
let activeFilters: ActiveFilters
let actions: Actions
init(
@ViewBuilder refinement: () -> Refinement,
@ViewBuilder activeFilters: () -> ActiveFilters,
@ViewBuilder actions: () -> Actions = { EmptyView() }
) {
self.refinement = refinement()
self.activeFilters = activeFilters()
self.actions = actions()
}
var body: some View {
ViewThatFits(in: .horizontal) {
toolbarRow(showsActiveFilters: true)
toolbarRow(showsActiveFilters: false)
}
.frame(minHeight: QueryShellLayout.chipHeight)
.accessibilityElement(children: .contain)
}
private func toolbarRow(showsActiveFilters: Bool) -> some View {
HStack(alignment: .center, spacing: OmiSpacing.sm) {
refinement
.fixedSize(horizontal: true, vertical: false)
if showsActiveFilters {
activeFilters
.layoutPriority(-1)
}
Spacer(minLength: OmiSpacing.xs)
actions
.fixedSize(horizontal: true, vertical: false)
}
}
}
extension PageQueryToolbar where ActiveFilters == EmptyView {
init(
@ViewBuilder refinement: () -> Refinement,
@ViewBuilder actions: () -> Actions = { EmptyView() }
) {
self.init(refinement: refinement, activeFilters: { EmptyView() }, actions: actions)
}
}
/// A labelled value used as a Menu or Button label in `PageQueryToolbar`.
/// The dimension is always visible so values such as "All" and "Default" do
/// not force users to infer what they control.
struct PageQueryControlLabel: View {
let icon: String
let dimension: String?
let value: String
var isActive = false
var showsDisclosure = true
var dimensionSeparator = ":"
var body: some View {
HStack(spacing: OmiSpacing.xs) {
Image(systemName: icon)
.scaledFont(size: OmiType.caption, weight: .medium)
if let dimension, !dimension.isEmpty {
Text("\(dimension)\(dimensionSeparator)")
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundStyle(Ink.secondary)
}
Text(value)
.scaledFont(size: OmiType.caption, weight: isActive ? .semibold : .medium)
.foregroundStyle(Ink.primary)
.lineLimit(1)
if showsDisclosure {
Image(systemName: "chevron.down")
.scaledFont(size: 10, weight: .semibold)
.foregroundStyle(Ink.secondary)
}
}
.padding(.horizontal, OmiSpacing.md)
.frame(height: QueryShellLayout.chipHeight)
.glassChip(isActive: isActive)
.fixedSize(horizontal: true, vertical: false)
.accessibilityElement(children: .combine)
.accessibilityLabel(
dimension.map { "\($0), \(value)" } ?? value
)
}
}
/// Textual action chrome for page-level operations. Primary actions invert the
/// shared ink; secondary actions retain the neutral chip treatment.
struct PageQueryActionLabel: View {
let icon: String
let title: String
var isPrimary = false
@State private var isHovering = false
var body: some View {
ViewThatFits(in: .horizontal) {
HStack(spacing: OmiSpacing.xs) {
Image(systemName: icon)
.scaledFont(size: OmiType.caption, weight: .semibold)
Text(title)
.scaledFont(size: OmiType.caption, weight: .semibold)
.lineLimit(1)
}
Image(systemName: icon)
.scaledFont(size: OmiType.caption, weight: .semibold)
}
.foregroundStyle(isPrimary ? Ink.surface : Ink.primary)
.tint(isPrimary ? Ink.surface : Ink.primary)
.padding(.horizontal, OmiSpacing.sm)
.frame(minWidth: QueryShellLayout.chipHeight)
.frame(height: QueryShellLayout.chipHeight)
.background {
if isPrimary {
Capsule(style: .continuous)
.fill(Ink.primary)
} else {
Capsule(style: .continuous)
.fill(isHovering ? Ink.rowFillHover : Ink.rowFill)
.overlay {
Capsule(style: .continuous)
.stroke(Ink.separator, lineWidth: 1)
}
}
}
.contentShape(Capsule(style: .continuous))
.onHover { isHovering = $0 }
.accessibilityElement(children: .combine)
.accessibilityLabel(title)
}
}
/// A filter value and the action that removes it. Keeping these as data lets
/// the strip progressively collapse values into one `+N` menu instead of
/// wrapping the page header or clipping the selected value.
struct PageActiveFilter: Identifiable {
let id: String
let title: String
let onRemove: () -> Void
init(id: String, title: String, onRemove: @escaping () -> Void) {
self.id = id
self.title = title
self.onRemove = onRemove
}
}
struct ActivePageFilterStrip: View {
let filters: [PageActiveFilter]
var onClearAll: (() -> Void)?
var body: some View {
if !filters.isEmpty {
ViewThatFits(in: .horizontal) {
filterRow(visibleCount: filters.count)
filterRow(visibleCount: min(2, filters.count))
filterRow(visibleCount: min(1, filters.count))
filterRow(visibleCount: 0)
}
.accessibilityIdentifier("active-page-filters")
}
}
private func filterRow(visibleCount: Int) -> some View {
let visible = Array(filters.prefix(visibleCount))
let hidden = Array(filters.dropFirst(visibleCount))
return HStack(spacing: OmiSpacing.xs) {
ForEach(visible) { filter in
ActivePageFilterChip(label: filter.title, onRemove: filter.onRemove)
}
if !hidden.isEmpty {
Menu {
Section("Active filters") {
ForEach(hidden) { filter in
Button("Remove \(filter.title)", action: filter.onRemove)
}
}
if filters.count > 1, let onClearAll {
Divider()
Button("Clear all filters", action: onClearAll)
}
} label: {
Text("+\(hidden.count)")
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.primary)
.padding(.horizontal, OmiSpacing.sm)
.frame(height: QueryShellLayout.chipHeight)
.glassChip(isActive: true)
}
.menuStyle(.borderlessButton)
.menuIndicator(.hidden)
.fixedSize()
.help("Show \(hidden.count) more active filters")
.accessibilityLabel("\(hidden.count) more active filters")
}
}
.fixedSize(horizontal: true, vertical: false)
}
}
/// One removable value in the single active-filter row shared by list pages.
struct ActivePageFilterChip: View {
let label: String
let onRemove: () -> Void
var body: some View {
Button(action: onRemove) {
HStack(spacing: OmiSpacing.xs) {
Text(label)
.scaledFont(size: OmiType.caption, weight: .semibold)
.lineLimit(1)
.truncationMode(.tail)
Image(systemName: "xmark")
.scaledFont(size: 9, weight: .bold)
}
.foregroundStyle(Ink.primary)
.padding(.horizontal, OmiSpacing.sm)
.frame(maxWidth: 150)
.frame(height: QueryShellLayout.chipHeight)
.glassChip(isActive: true)
}
.buttonStyle(.plain)
.help("Remove \(label) filter")
.accessibilityLabel("Remove \(label) filter")
}
}