Repository navigation
Expand file tree
/
Copy pathOutlinerSelectionToolbar.swift
More file actions
189 lines (172 loc) · 7.9 KB
/
Copy pathOutlinerSelectionToolbar.swift
File metadata and controls
189 lines (172 loc) · 7.9 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
#if os(iOS)
import LUIAppleBackend
import SwiftUI
import UIKit
@MainActor
struct OutlinerSelectionToolbar: View {
let context: LUIAppleExtensionViewContext
@Environment(\.luiSemanticColors) private var semanticColors
private static let cell: CGFloat = 44
private static let capsuleGap: CGFloat = 12
static func register(in registry: LUIAppleExtensionRegistry) throws {
try registry.register(LUIAppleExtension(
identifier: "native-outliner-selection-toolbar",
fingerprint: "lui-extension-v1|33:native-outliner-selection-toolbar|profiles:ios/swiftui|standard-children:1|children:|properties:10:more-label:string:required:none,12:more-actions:string:required:none,12:status-label:string:required:none,8:statuses:string:required:none|events:13:select-status[5:value:string:required],14:perform-action[5:value:string:required]",
acceptsStandardChildren: true,
properties: [.init(name: "more-actions", kind: .string, isRequired: true),
.init(name: "more-label", kind: .string, isRequired: true),
.init(name: "status-label", kind: .string, isRequired: true),
.init(name: "statuses", kind: .string, isRequired: true)],
events: [.init(name: "perform-action", fields: [.init(name: "value", kind: .string, isRequired: true)]),
.init(name: "select-status", fields: [.init(name: "value", kind: .string, isRequired: true)])]
) { AnyView(OutlinerSelectionToolbar(context: $0)) })
}
var body: some View {
if let toolbar = context.childIDs.first {
let _ = context.revision
let _ = context.measureStamp(of: toolbar)
let children = context.childIDs(of: toolbar)
GeometryReader { proxy in
HStack(spacing: 0) {
leadingCapsule(children, availableWidth: proxy.size.width - Self.cell - Self.capsuleGap)
Spacer(minLength: Self.capsuleGap)
moreButton
}
}
.frame(height: Self.cell)
.frame(height: 58)
.padding(.horizontal, 16)
}
}
private var statuses: [OutlinerRowPayload.Status] {
guard case let .string(encoded) = context.property("statuses") else { return [] }
return (try? JSONDecoder().decode([OutlinerRowPayload.Status].self, from: Data(encoded.utf8))) ?? []
}
private var statusLabel: String {
if case let .string(value) = context.property("status-label") { return value }
return "Status"
}
private func leadingCapsule(_ ids: [Int], availableWidth: CGFloat) -> some View {
let intrinsic = CGFloat(ids.count) * Self.cell
let width = min(intrinsic, max(Self.cell, availableWidth))
return ScrollView(.horizontal) {
HStack(spacing: 0) {
ForEach(ids, id: \.self) { id in
Group {
if id == ids.first {
SelectionStatusMenu(label: statusLabel, statuses: statuses, semanticColors: semanticColors) { uuid in
try? context.emit(name: "select-status", values: ["value": .string(uuid)])
}
} else {
context.nodeView(of: id).buttonStyle(.plain)
}
}
.frame(width: Self.cell, height: Self.cell)
}
}
}
.scrollIndicators(.hidden)
.scrollBounceBehavior(.basedOnSize, axes: .horizontal)
.scrollDisabled(intrinsic <= width)
.frame(width: width, height: Self.cell)
.clipShape(Capsule())
.modifier(LGBottomBarGlass(surface: .selection))
.accessibilityElement(children: .contain)
.accessibilityIdentifier("toolbar.outliner.selection")
}
private var moreButton: some View {
let label: String
if case let .string(value) = context.property("more-label") { label = value }
else { label = "More actions" }
let actions: [SelectionActionsMenu.Action]
if case let .string(value) = context.property("more-actions") {
actions = (try? JSONDecoder().decode([SelectionActionsMenu.Action].self, from: Data(value.utf8))) ?? []
} else {
actions = []
}
return SelectionActionsMenu(label: label, actions: actions) { action in
guard context.isUserInteractionEnabled else { return }
try? context.emit(name: "perform-action", values: ["value": .string(action)])
}
.frame(width: Self.cell, height: Self.cell)
.modifier(LGBottomBarGlass(surface: .trailing))
}
}
private struct SelectionActionsMenu: UIViewRepresentable {
struct Action: Decodable, Equatable {
let action: String
let title: String
let icon: String
let destructive: Bool
}
final class Coordinator {
var actions: [Action] = []
var perform: ((String) -> Void)?
}
let label: String
let actions: [Action]
let perform: (String) -> Void
func makeCoordinator() -> Coordinator { Coordinator() }
func makeUIView(context: Context) -> UIButton {
let button = UIButton(type: .system)
var configuration = UIButton.Configuration.plain()
configuration.image = UIImage(systemName: "ellipsis", withConfiguration: UIImage.SymbolConfiguration(pointSize: 19))
configuration.contentInsets = .zero
configuration.baseForegroundColor = .label
button.configuration = configuration
button.showsMenuAsPrimaryAction = true
button.accessibilityIdentifier = "button.outliner.selection.more"
return button
}
func updateUIView(_ button: UIButton, context: Context) {
let coordinator = context.coordinator
coordinator.perform = perform
button.accessibilityLabel = label
button.isEnabled = !actions.isEmpty
// Keep UIKit's presented menu attached during unrelated graph updates.
guard coordinator.actions != actions else { return }
coordinator.actions = actions
let icons = LGIconPolicy.icons
button.menu = UIMenu(children: actions.map { item in
let image: UIImage?
if case let .systemName(name) = icons[item.icon] {
image = UIImage(systemName: name)
} else {
image = nil
}
return UIAction(
title: item.title,
image: image,
identifier: UIAction.Identifier("button.outliner.selection.menu." + item.action),
attributes: item.destructive ? .destructive : []
) { [weak coordinator] _ in
coordinator?.perform?(item.action)
}
})
}
}
private struct SelectionStatusMenu: UIViewRepresentable {
let label: String
let statuses: [OutlinerRowPayload.Status]
let semanticColors: [String: Color]
let select: (String) -> Void
func makeUIView(context: Context) -> UIButton {
let button = UIButton(type: .system)
var configuration = UIButton.Configuration.plain()
configuration.image = UIImage(systemName: "checkmark.square", withConfiguration: UIImage.SymbolConfiguration(pointSize: 19))
configuration.contentInsets = .zero
configuration.baseForegroundColor = .label
button.configuration = configuration
button.showsMenuAsPrimaryAction = true
button.accessibilityIdentifier = "button.outliner.selection.status"
return button
}
func updateUIView(_ button: UIButton, context: Context) {
button.accessibilityLabel = label
button.isEnabled = !statuses.isEmpty
button.menu = UIMenu(children: statuses.map { status in
UIAction(title: status.title, image: status.menuImage(in: semanticColors)) { _ in select(status.uuid) }
})
}
}
#endif