-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
UIElementCommandView.swift
174 lines (165 loc) · 5.79 KB
/
UIElementCommandView.swift
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 Bonzai
import Inject
import SwiftUI
struct UIElementCommandView: View {
@ObserveInjection var inject
enum Action {
case updateCommand(UIElementCommand)
case commandAction(CommandContainerAction)
}
@State var model: UIElementCommand
private let metaData: CommandViewModel.MetaData
private let debounce: DebounceManager<UIElementCommand>
private let onAction: (Action) -> Void
private let iconSize: CGSize
private let menuStyle = ZenStyleConfiguration(color: .systemGray,
padding: .init(horizontal: .small, vertical: .small))
init(metaData: CommandViewModel.MetaData,
model: UIElementCommand,
iconSize: CGSize,
onAction: @escaping (Action) -> Void) {
self.metaData = metaData
self.model = model
self.iconSize = iconSize
self.debounce = DebounceManager(for: .milliseconds(500)) { newCommand in
onAction(.updateCommand(newCommand))
}
self.onAction = onAction
}
var body: some View {
CommandContainerView(metaData, placeholder: model.placeholder) { _ in
UIElementIconView(size: iconSize.width)
} content: { _ in
VStack(alignment: .leading, spacing: 4) {
ForEach(model.predicates.indices, id: \.self) { index in
Grid {
GridRow {
Text("Value:")
.font(.caption)
HStack {
Menu {
ForEach(UIElementCommand.Predicate.Compare.allCases, id: \.displayName) { compare in
Button(action: { model.predicates[index].compare = compare },
label: {
Text(compare.displayName)
.font(.callout)
})
}
} label: {
Text(model.predicates[index].compare.displayName)
.font(.caption)
}
.fixedSize()
.menuStyle(.zen(menuStyle))
HStack {
TextField("", text: $model.predicates[index].value)
.textFieldStyle(
.zen(
.init(
backgroundColor: Color(.windowBackgroundColor),
font: .caption,
padding: .small
)
)
)
Button {
model.predicates.remove(at: index)
if model.predicates.isEmpty {
onAction(.commandAction(.delete))
}
} label: {
Image(systemName: "xmark")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 8, height: 8)
}
.buttonStyle(.calm(color: .systemRed, padding: .medium))
}
}
}
GridRow {
Text("Type:")
.font(.caption)
HStack {
Menu {
ForEach(UIElementCommand.Kind.allCases, id: \.displayName) { kind in
Button(action: { model.predicates[index].kind = kind },
label: {
Text(kind.displayName)
.font(.callout)
})
}
} label: {
Text(model.predicates[index].kind.displayName)
.font(.caption)
}
.menuStyle(.zen(menuStyle))
ForEach(UIElementCommand.Predicate.Properties.allCases) { property in
HStack {
ZenCheckbox(
isOn: Binding<Bool>(
get: { model.predicates[index].properties.contains(property) },
set: {
if $0 {
model.predicates[index].properties.append(property)
} else {
model.predicates[index].properties.removeAll(where: { $0 == property })
}
}
)
)
Text(property.displayName)
.font(.caption)
.lineLimit(1)
.truncationMode(.tail)
.allowsTightening(true)
}
.help(property.displayName)
}
}
}
}
if index < model.predicates.count - 1 {
ZenDivider()
}
}
}
.roundedContainer(padding: 4, margin: 0)
} subContent: { metaData in
ZenCheckbox("Notify", style: .small, isOn: Binding(get: {
if case .bezel = metaData.notification.wrappedValue { return true } else { return false }
}, set: { newValue in
metaData.notification.wrappedValue = newValue ? .bezel : nil
onAction(.commandAction(.toggleNotify(newValue ? .bezel : nil)))
})) { value in
if value {
onAction(.commandAction(.toggleNotify(metaData.notification.wrappedValue)))
} else {
onAction(.commandAction(.toggleNotify(nil)))
}
}
.offset(x: 1)
} onAction: { action in
onAction(.commandAction(action))
}
.onChange(of: model, perform: { value in
debounce.send(value)
})
.enableInjection()
}
}
#Preview {
UIElementCommandView(
metaData: .init(name: "Some UI Element", namePlaceholder: ""),
model: .init(
predicates: [
.init(
value: "issues",
properties: [.identifier]
)
]
), iconSize: .init(width: 24, height: 24)) { _ in
}
.designTime()
.previewLayout(.sizeThatFits)
}