-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolBarView.swift
More file actions
658 lines (579 loc) · 23.2 KB
/
Copy pathToolBarView.swift
File metadata and controls
658 lines (579 loc) · 23.2 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//
// ToolBarView.swift
//
// Created on 17.12.2025.
// Copyright © 2026 IGR Soft. All rights reserved.
//
import CodeEditLanguages
import CodeEditSourceEditor
import DeveloperSupportStore
import SwiftUI
/// A toolbar providing all code style and indicator settings with an export button.
///
/// `ToolBarView` is the main settings interface for Bifcode. It presents controls
/// organized into sections for code style, indicator customization, and export.
///
/// ## Overview
///
/// The toolbar contains three main sections:
///
/// ### Code Style Section
/// - **Language Picker** - Select from 19 programming languages
/// - **Theme Picker** - Choose from 7 syntax highlighting themes
/// - **Layout Toggle** - Switch between horizontal and vertical layouts
/// - **Font Size Slider** - Adjust code font (10-24pt)
/// - **Panel Titles** - Edit "Do" and "Don't" panel titles
///
/// ### Indicator Style Section
/// - **Style Picker** - Icon only, text only, or both
/// - **Position Picker** - Top-right or bottom-right
/// - **Size Slider** - Badge size (32-80px)
/// - **Icon Pickers** - Choose icons for Do/Don't badges
/// - **Label Fields** - Custom text for indicator labels
///
/// ### Export Section
/// - **Export Button** - Save as PNG (disabled when panels are empty)
/// - **Choose Button** - Select save location folder
///
/// ## Settings Persistence
///
/// All settings are automatically persisted via `@AppStorage` to UserDefaults.
///
/// ## Topics
///
/// ### Creating a Toolbar
///
/// - ``init(doTitleSetting:dontTitleSetting:isExportDisabled:onExport:onLanguageChange:)``
public struct ToolBarView: View {
// MARK: - App Storage
@AppStorage("windowLayout") private var windowLayout: String = WindowLayout.horizontal.rawValue
@AppStorage("indicatorStyle") private var indicatorStyle: String = IndicatorStyle.iconAndText.rawValue
@AppStorage("indicatorPosition") private var indicatorPosition: String = IndicatorPosition.topRight.rawValue
@AppStorage("indicatorSize") private var indicatorSize: Double = 48
@AppStorage("doIndicatorIcon") private var doIndicatorIcon: String = "checkmark"
@AppStorage("dontIndicatorIcon") private var dontIndicatorIcon: String = "xmark"
@AppStorage("doIndicatorLabel") private var doIndicatorLabel: String = "Do's"
@AppStorage("dontIndicatorLabel") private var dontIndicatorLabel: String = "Don'ts"
@AppStorage("fontSize") private var fontSize: Double = 14
@AppStorage("selectedTheme") private var selectedThemeRaw: String = EditorThemeOption.atomOneDark.rawValue
@AppStorage("themeMode") private var themeModeRaw: String = ThemeMode.dark.rawValue
@AppStorage("selectedLanguage") private var selectedLanguageRaw: String = "swift"
@Environment(\.accessibilityReduceMotion) private var reduceMotion
/// Current indicator style for disable logic
private var currentIndicatorStyle: IndicatorStyle {
IndicatorStyle(rawValue: indicatorStyle) ?? .iconAndText
}
/// Selected language derived from stored raw value
private var selectedLanguage: CodeLanguage {
CodeLanguage.allLanguages.first { $0.id.rawValue == selectedLanguageRaw } ?? .swift
}
/// Current theme mode derived from stored raw value
private var currentThemeMode: ThemeMode {
ThemeMode(rawValue: themeModeRaw) ?? .dark
}
/// Current selected theme derived from stored raw value
private var currentTheme: EditorThemeOption {
EditorThemeOption(rawValue: selectedThemeRaw) ?? .atomOneDark
}
/// All available themes (not filtered by mode)
private var availableThemes: [EditorThemeOption] {
EditorThemeOption.allCases
}
@Binding private var doTitleSetting: String
@Binding private var dontTitleSetting: String
// MARK: - Callbacks
var onExport: () -> Void
var onLanguageChange: (CodeLanguage) -> Void
var isExportDisabled: Bool
// MARK: - Initialization
/// Creates a new toolbar view with the specified configuration.
///
/// - Parameters:
/// - doTitleSetting: Binding to the "Do's" panel title.
/// - dontTitleSetting: Binding to the "Don'ts" panel title.
/// - isExportDisabled: Whether the export button should be disabled.
/// Set to `true` when both code panels are empty.
/// - onExport: Closure called when the export button is tapped.
/// - onLanguageChange: Closure called when the language selection changes,
/// providing the newly selected ``CodeLanguage``.
///
/// ```swift
/// ToolBarView(
/// doTitleSetting: $doTitle,
/// dontTitleSetting: $dontTitle,
/// isExportDisabled: viewModel.doPanel.code.isEmpty,
/// onExport: { Task { await exportImage() } },
/// onLanguageChange: { viewModel.setLanguage($0) }
/// )
/// ```
public init(
doTitleSetting: Binding<String>,
dontTitleSetting: Binding<String>,
isExportDisabled: Bool = false,
onExport: @escaping () -> Void,
onLanguageChange: @escaping (CodeLanguage) -> Void
) {
_doTitleSetting = doTitleSetting
_dontTitleSetting = dontTitleSetting
self.isExportDisabled = isExportDisabled
self.onExport = onExport
self.onLanguageChange = onLanguageChange
}
// MARK: - Body
public var body: some View {
HStack(alignment: .top, spacing: 24) {
codeSettingsView()
Divider()
.frame(height: 184)
.padding(.vertical, 8)
indicatorSettingsView()
Divider()
.frame(height: 184)
.padding(.vertical, 8)
exportButton
}
.padding(.horizontal, 20)
.padding(.vertical, 12)
.frame(maxWidth: .infinity)
.background(Color.toolbarBackground)
}
@ViewBuilder
private func codeSettingsView() -> some View {
VStack(alignment: .leading, spacing: 12) {
// Section Header - HIG: Use clear, hierarchical typography
Text("Code Style")
.font(.subheadline.weight(.semibold))
.foregroundStyle(Color.sectionHeader)
// Language & Theme Row
HStack(spacing: 12) {
languagePicker
themePicker
}
// Font Size with Label
VStack(alignment: .leading, spacing: 4) {
Text("Font Size")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
fontSizeControl
}
// Mode & Layout Controls
HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 4) {
Text("Theme")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
themeModeToggle
}
VStack(alignment: .leading, spacing: 4) {
Text("Layout")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
layoutToggle
}
}
// Panel Titles
HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 4) {
Text("Don't Title")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
dontTitleField
}
VStack(alignment: .leading, spacing: 4) {
Text("Do Title")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
doTitleField
}
}
}
.fixedSize(horizontal: true, vertical: false)
}
@ViewBuilder
private func indicatorSettingsView() -> some View {
VStack(alignment: .leading, spacing: 12) {
// Section Header - HIG: Use clear, hierarchical typography
Text("Indicator Style")
.font(.subheadline.weight(.semibold))
.foregroundStyle(Color.sectionHeader)
// Style & Position Pickers
HStack(spacing: 12) {
indicatorStylePicker
indicatorPositionPicker
}
// Indicator Size with Label
VStack(alignment: .leading, spacing: 4) {
Text("Badge Size")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
indicatorSizeControl
}
// Icon Pickers with Labels
HStack(spacing: 16) {
VStack(alignment: .leading, spacing: 4) {
Text("Don't Icon")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
dontIconPicker
}
VStack(alignment: .leading, spacing: 4) {
Text("Do Icon")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
doIconPicker
}
}
// Indicator Labels
HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 4) {
Text("Don't Label")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
dontIndicatorLabelField
}
VStack(alignment: .leading, spacing: 4) {
Text("Do Label")
.font(.caption)
.foregroundStyle(Color.tertiaryText)
doIndicatorLabelField
}
}
}
.fixedSize(horizontal: true, vertical: false)
}
// MARK: - Language Picker
private var languagePicker: some View {
HStack(spacing: 4) {
Picker("", selection: $selectedLanguageRaw) {
ForEach(commonLanguages, id: \.id) { lang in
Text(lang.id.rawValue.capitalized).tag(lang.id.rawValue)
}
}
.pickerStyle(.menu)
.fixedSize()
.accessibilityLabel("Programming Language")
.accessibilityHint("Select syntax highlighting language")
.onChange(of: selectedLanguageRaw) { _, _ in
onLanguageChange(selectedLanguage)
}
}
}
private var commonLanguages: [CodeLanguage] {
[
// Popular languages
.swift, .python, .javascript, .typescript,
.java, .kotlin, .go, .rust, .ruby,
.c, .cpp, .cSharp, .php, .sql,
// Web & markup
.html, .css, .json, .yaml, .markdown,
// DevOps & config
.bash, .dockerfile, .toml,
// Functional & other
.scala, .elixir, .haskell, .lua,
.dart, .julia, .perl, .zig
]
}
// MARK: - Theme Mode Toggle
private var themeModeToggle: some View {
Picker("", selection: $themeModeRaw) {
Image(systemName: "moon.fill").tag(ThemeMode.dark.rawValue)
Image(systemName: "sun.max.fill").tag(ThemeMode.light.rawValue)
}
.pickerStyle(.segmented)
.fixedSize()
.accessibilityLabel("Theme Mode")
.accessibilityHint("Toggle dark or light mode")
}
// MARK: - Theme Picker
private var themePicker: some View {
Picker("", selection: $selectedThemeRaw) {
ForEach(availableThemes, id: \.rawValue) { theme in
Text(theme.label).tag(theme.rawValue)
}
}
.pickerStyle(.menu)
.fixedSize()
.accessibilityLabel("Color Theme")
.accessibilityHint("Select code highlighting theme")
}
// MARK: - Layout Toggle
private var layoutToggle: some View {
Picker("", selection: $windowLayout) {
Image(systemName: "rectangle.split.2x1").tag(WindowLayout.horizontal.rawValue)
Image(systemName: "rectangle.split.1x2").tag(WindowLayout.vertical.rawValue)
}
.pickerStyle(.segmented)
.accessibilityLabel("Layout")
.accessibilityHint("Horizontal or vertical panel arrangement")
}
// MARK: - Indicator Style
private var indicatorStylePicker: some View {
Picker("", selection: Binding(
get: { IndicatorStyle(rawValue: indicatorStyle) ?? .iconAndText },
set: { indicatorStyle = $0.rawValue }
)) {
ForEach(IndicatorStyle.allCases, id: \.self) { style in
Text(style.label).tag(style)
}
}
.pickerStyle(.menu)
.fixedSize()
.accessibilityLabel("Indicator Style")
.accessibilityHint("Show icon, text, or both")
}
// MARK: - Indicator Position
private var indicatorPositionPicker: some View {
Picker("", selection: Binding(
get: { IndicatorPosition(rawValue: indicatorPosition) ?? .topRight },
set: { indicatorPosition = $0.rawValue }
)) {
ForEach(IndicatorPosition.allCases, id: \.self) { position in
Text(position.label).tag(position)
}
}
.pickerStyle(.menu)
.fixedSize()
.accessibilityLabel("Indicator Position")
.accessibilityHint("Badge position on panels")
}
// MARK: - Icon Pickers
private var doIconPicker: some View {
Picker("", selection: $doIndicatorIcon) {
ForEach(IndicatorIcon.positiveIcons, id: \.rawValue) { icon in
Image(systemName: icon.systemName)
.tag(icon.systemName)
}
}
.pickerStyle(.menu)
.fixedSize()
.disabled(currentIndicatorStyle == .textOnly)
.accessibilityLabel("Do Icon")
.accessibilityHint(currentIndicatorStyle == .textOnly
? "Disabled: Change indicator style to enable"
: "Select icon for positive examples")
}
private var dontIconPicker: some View {
Picker("", selection: $dontIndicatorIcon) {
ForEach(IndicatorIcon.negativeIcons, id: \.rawValue) { icon in
Image(systemName: icon.systemName)
.tag(icon.systemName)
}
}
.pickerStyle(.menu)
.fixedSize()
.disabled(currentIndicatorStyle == .textOnly)
.accessibilityLabel("Don't Icon")
.accessibilityHint(currentIndicatorStyle == .textOnly
? "Disabled: Change indicator style to enable"
: "Select icon for negative examples")
}
// MARK: - Indicator Label Fields
private var doIndicatorLabelField: some View {
TextField("Do's", text: $doIndicatorLabel)
.textFieldStyle(.roundedBorder)
.frame(width: 120)
.disabled(currentIndicatorStyle == .iconOnly)
.onChange(of: doIndicatorLabel) { _, newValue in
if newValue.count > 10 {
doIndicatorLabel = String(newValue.prefix(10))
}
}
.accessibilityLabel("Do Indicator Text")
.accessibilityHint(currentIndicatorStyle == .iconOnly
? "Disabled: Change indicator style to enable"
: "Custom label for positive indicator")
}
private var dontIndicatorLabelField: some View {
TextField("Don'ts", text: $dontIndicatorLabel)
.textFieldStyle(.roundedBorder)
.frame(width: 120)
.disabled(currentIndicatorStyle == .iconOnly)
.onChange(of: dontIndicatorLabel) { _, newValue in
if newValue.count > 10 {
dontIndicatorLabel = String(newValue.prefix(10))
}
}
.accessibilityLabel("Don't Indicator Text")
.accessibilityHint(currentIndicatorStyle == .iconOnly
? "Disabled: Change indicator style to enable"
: "Custom label for negative indicator")
}
// MARK: - Indicator Size
private var indicatorSizeControl: some View {
HStack(spacing: 8) {
Slider(value: $indicatorSize, in: 32...80, step: 4)
.frame(width: 236)
Text("\(Int(indicatorSize))")
.font(.caption.monospacedDigit())
.foregroundStyle(Color.secondaryText)
.frame(width: 24, alignment: .trailing)
}
.accessibilityElement(children: .combine)
.accessibilityLabel("Badge Size")
.accessibilityValue("\(Int(indicatorSize)) pixels")
.accessibilityHint("Adjust from 32 to 80 pixels")
}
// MARK: - Font Size
private var fontSizeControl: some View {
HStack(spacing: 8) {
Slider(value: $fontSize, in: 10...24, step: 1)
.frame(width: 248)
Text("\(Int(fontSize))")
.font(.caption.monospacedDigit())
.foregroundStyle(Color.secondaryText)
.frame(width: 24, alignment: .trailing)
}
.accessibilityElement(children: .combine)
.accessibilityLabel("Font Size")
.accessibilityValue("\(Int(fontSize)) points")
.accessibilityHint("Adjust from 10 to 24 points")
}
// MARK: - Title Fields
private var doTitleField: some View {
TextField("Do Title", text: $doTitleSetting)
.textFieldStyle(.roundedBorder)
.frame(width: 120)
.onChange(of: doTitleSetting) { _, newValue in
if newValue.count > 70 {
doTitleSetting = String(newValue.prefix(70))
}
}
.accessibilityLabel("Do Panel Title")
.accessibilityHint("Title shown above positive code example")
}
private var dontTitleField: some View {
TextField("Don't Title", text: $dontTitleSetting)
.textFieldStyle(.roundedBorder)
.frame(width: 120)
.onChange(of: dontTitleSetting) { _, newValue in
if newValue.count > 70 {
dontTitleSetting = String(newValue.prefix(70))
}
}
.accessibilityLabel("Don't Panel Title")
.accessibilityHint("Title shown above negative code example")
}
// MARK: - Export Button
@State private var isExportButtonHovered = false
@State private var isStorePresented = false
/// Tracks whether the user has made any purchase (subscription or tip).
/// Persisted via AppStorage so the green icon state survives app restarts.
@AppStorage("hasMadePurchase") private var hasMadePurchase = false
private let storeConfiguration = BifcodeStoreConfiguration()
private let storeService = StoreService()
private var exportButton: some View {
VStack(spacing: 12) {
// Primary Export Action - HIG: Use .prominent for key actions
Button { onExport() } label: {
Image(systemName: "square.and.arrow.up")
.font(.system(size: 22, weight: .medium))
.frame(width: 48, height: 48)
}
.buttonStyle(.borderedProminent)
.clipShape(Circle())
.scaleEffect(isExportButtonHovered && !isExportDisabled ? 1.05 : 1.0)
.animation(reduceMotion ? nil : .easeInOut(duration: 0.15), value: isExportButtonHovered)
.onHover { hovering in
isExportButtonHovered = hovering
}
.disabled(isExportDisabled)
.help("Export as PNG")
.accessibilityLabel("Export as PNG")
.accessibilityHint(isExportDisabled
? "Disabled: Add code to enable export"
: "Save comparison image to selected folder")
// Secondary Actions
VStack(spacing: 8) {
Button {
chooseSaveLocation()
} label: {
Label("Location", systemImage: "folder")
.font(.caption)
}
.buttonStyle(.borderless)
.help("Choose save location")
.accessibilityLabel("Choose Save Location")
.accessibilityHint("Select folder for exported images")
storeButton
.padding(.top, 32)
}
}
.frame(minWidth: 80)
}
private var storeButton: some View {
Button {
isStorePresented = true
} label: {
Image(systemName: hasMadePurchase ? "heart.fill" : "storefront")
.font(.system(size: 24))
.foregroundStyle(hasMadePurchase ? Color.storePurchased : Color.storeDefault)
}
.buttonStyle(.borderless)
.help(hasMadePurchase ? "Thank you for supporting!" : "Support development")
.accessibilityLabel(hasMadePurchase ? "Thank you for supporting" : "Support Development")
.sheet(isPresented: $isStorePresented) {
DeveloperSupportStoreView(
configuration: storeConfiguration,
storeService: storeService,
onPurchaseSuccess: { _ in
hasMadePurchase = true
},
onDismiss: {
isStorePresented = false
}
)
}
.task {
await checkPurchaseStatus()
}
}
private func checkPurchaseStatus() async {
// Skip sync if user has already made a purchase (persisted via AppStorage)
guard !hasMadePurchase else { return }
do {
try await storeService.syncStoreData()
// Check if there's an active subscription
if storeService.hasActiveSubscription {
hasMadePurchase = true
}
} catch {
// Silently fail - purchase status will remain false
}
}
// MARK: - Actions
private func chooseSaveLocation() {
let panel = NSOpenPanel()
panel.canChooseFiles = false
panel.canChooseDirectories = true
panel.allowsMultipleSelection = false
panel.prompt = "Choose"
panel.message = "Select a folder to save exported images"
if panel.runModal() == .OK, let url = panel.url {
do {
try BookmarkManager.shared.storeBookmark(for: url)
} catch {
// Log bookmark creation failure - user can try again
print("Failed to create bookmark: \(error)")
}
}
}
}
// MARK: - Preview
#Preview("ToolBarView") {
ToolBarView(
doTitleSetting: .constant("1"),
dontTitleSetting: .constant("2"),
isExportDisabled: false,
onExport: {},
onLanguageChange: { _ in }
)
}
#Preview("ToolBarView - Export Disabled") {
ToolBarView(
doTitleSetting: .constant("1"),
dontTitleSetting: .constant("2"),
isExportDisabled: true,
onExport: {},
onLanguageChange: { _ in }
)
}