-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathTabContainer.swift
More file actions
271 lines (244 loc) · 7.8 KB
/
TabContainer.swift
File metadata and controls
271 lines (244 loc) · 7.8 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
import ComposableArchitecture
import Dependencies
import Foundation
import LaunchAgentManager
import SwiftUI
import Toast
import UpdateChecker
import Client
import Logger
import Combine
@MainActor
public let hostAppStore: StoreOf<HostApp> = .init(initialState: .init(), reducer: { HostApp() })
public struct TabContainer: View {
let store: StoreOf<HostApp>
@ObservedObject var toastController: ToastController
@ObservedObject private var featureFlags = FeatureFlagManager.shared
@State private var tabBarItems = [TabBarItem]()
@Binding var tag: TabIndex
public init() {
toastController = ToastControllerDependencyKey.liveValue
store = hostAppStore
_tag = Binding(
get: { hostAppStore.state.activeTabIndex },
set: { hostAppStore.send(.setActiveTab($0)) }
)
}
init(store: StoreOf<HostApp>, toastController: ToastController) {
self.store = store
self.toastController = toastController
_tag = Binding(
get: { store.state.activeTabIndex },
set: { store.send(.setActiveTab($0)) }
)
}
public var body: some View {
WithPerceptionTracking {
VStack(spacing: 0) {
TabBar(tag: $tag, tabBarItems: tabBarItems)
.padding(.bottom, 8)
ZStack(alignment: .center) {
GeneralView(store: store.scope(state: \.general, action: \.general)).tabBarItem(for: .general)
AdvancedSettings().tabBarItem(for: .advanced)
if featureFlags.isAgentModeEnabled {
MCPConfigView().tabBarItem(for: .tools)
}
if featureFlags.isBYOKEnabled {
BYOKConfigView().tabBarItem(for: .byok)
}
}
.environment(\.tabBarTabTag, tag)
.frame(minHeight: 400)
}
.focusable(false)
.padding(.top, 8)
.background(Color(nsColor: .controlBackgroundColor))
.handleToast()
.onPreferenceChange(TabBarItemPreferenceKey.self) { items in
tabBarItems = items
}
.onAppear {
store.send(.appear)
}
.onChange(of: featureFlags.isAgentModeEnabled) { isEnabled in
if hostAppStore.state.activeTabIndex == .tools && !isEnabled {
hostAppStore.send(.setActiveTab(.general))
}
}
.onChange(of: featureFlags.isBYOKEnabled) { isEnabled in
if hostAppStore.state.activeTabIndex == .byok && !isEnabled {
hostAppStore.send(.setActiveTab(.general))
}
}
}
}
}
struct TabBar: View {
@Binding var tag: TabIndex
fileprivate var tabBarItems: [TabBarItem]
var body: some View {
HStack {
ForEach(tabBarItems) { tab in
TabBarButton(
currentTag: $tag,
tag: tab.tag,
title: tab.title,
image: tab.image,
isSystemImage: tab.isSystemImage
)
}
}
}
}
struct TabBarButton: View {
@Binding var currentTag: TabIndex
@State var isHovered = false
var tag: TabIndex
var title: String
var image: String
var isSystemImage: Bool = true
private var tabImage: Image {
isSystemImage ? Image(systemName: image) : Image(image)
}
private var isSelected: Bool {
tag == currentTag
}
var body: some View {
Button(action: {
self.currentTag = tag
}) {
VStack(spacing: 2) {
tabImage
.renderingMode(.template)
.resizable()
.scaledToFit()
.frame(width: 24, height: 24)
Text(title)
}
.foregroundColor(isSelected ? .blue : .gray)
.font(.body)
.padding(.horizontal, 12)
.padding(.vertical, 4)
.padding(.top, 4)
.background(
isSelected
? Color(nsColor: .textColor).opacity(0.1)
: Color.clear,
in: RoundedRectangle(cornerRadius: 8)
)
.background(
isHovered
? Color(nsColor: .textColor).opacity(0.05)
: Color.clear,
in: RoundedRectangle(cornerRadius: 8)
)
}
.onHover(perform: { yes in
isHovered = yes
})
.buttonStyle(.borderless)
}
}
private struct TabBarTabViewWrapper<Content: View>: View {
@Environment(\.tabBarTabTag) var tabBarTabTag
var tag: TabIndex
var title: String
var image: String
var isSystemImage: Bool = true
var content: () -> Content
var body: some View {
Group {
if tag == tabBarTabTag {
content()
} else {
Color.clear
}
}
.preference(
key: TabBarItemPreferenceKey.self,
value: [.init(tag: tag, title: title, image: image, isSystemImage: isSystemImage)]
)
}
}
private extension View {
func tabBarItem(for tag: TabIndex) -> some View {
TabBarTabViewWrapper(
tag: tag,
title: tag.title,
image: tag.image,
isSystemImage: tag.isSystemImage,
content: { self }
)
}
}
private struct TabBarItem: Identifiable, Equatable {
var id: TabIndex { tag }
var tag: TabIndex
var title: String
var image: String
var isSystemImage: Bool = true
}
private struct TabBarItemPreferenceKey: PreferenceKey {
static var defaultValue: [TabBarItem] = []
static func reduce(value: inout [TabBarItem], nextValue: () -> [TabBarItem]) {
value.append(contentsOf: nextValue())
}
}
private struct TabBarTabTagKey: EnvironmentKey {
static var defaultValue: TabIndex = .general
}
private extension EnvironmentValues {
var tabBarTabTag: TabIndex {
get { self[TabBarTabTagKey.self] }
set { self[TabBarTabTagKey.self] = newValue }
}
}
struct UpdateCheckerKey: EnvironmentKey {
static var defaultValue: UpdateCheckerProtocol = NoopUpdateChecker()
}
public extension EnvironmentValues {
var updateChecker: UpdateCheckerProtocol {
get { self[UpdateCheckerKey.self] }
set { self[UpdateCheckerKey.self] = newValue }
}
}
// MARK: - Previews
struct TabContainer_Previews: PreviewProvider {
static var previews: some View {
TabContainer()
.frame(width: 800)
}
}
struct TabContainer_Toasts_Previews: PreviewProvider {
static var previews: some View {
TabContainer(
store: .init(initialState: .init(), reducer: { HostApp() }),
toastController: .init(messages: [
.init(id: UUID(), level: .info, content: Text("info")),
.init(id: UUID(), level: .error, content: Text("error")),
.init(id: UUID(), level: .warning, content: Text("warning")),
])
)
.frame(width: 800)
}
}
@available(macOS 14.0, *)
@MainActor
public struct SettingsEnvironment: View {
@Environment(\.openSettings) public var openSettings: OpenSettingsAction
public init() {}
public var body: some View {
EmptyView().onAppear {
openSettings()
}
}
public func open() {
let controller = NSHostingController(rootView: self)
let window = NSWindow(contentViewController: controller)
window.orderFront(nil)
// Close the temporary window after settings are opened
DispatchQueue.main.async {
window.close()
}
}
}