-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettingsViewController.swift
978 lines (838 loc) · 32.6 KB
/
SettingsViewController.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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//
// SettingsViewController.swift
// LNPopupSettings
//
// Created by Léo Natan on 2023-12-15.
// Copyright © 2023-2024 Léo Natan. All rights reserved.
//
import SwiftUI
#if LNPOPUP
import LNPopupController
extension Notification.Name {
static let textVisited = Notification.Name("textVisited")
}
extension UserDefaults {
func object(forKey setting: PopupSetting) -> Any? {
object(forKey: setting.rawValue)
}
func integer(forKey setting: PopupSetting) -> Int {
integer(forKey: setting.rawValue)
}
func bool(forKey setting: PopupSetting) -> Bool {
bool(forKey: setting.rawValue)
}
func set(_ value: Any?, forKey setting: PopupSetting) {
set(value, forKey: setting.rawValue)
}
func removeObject(forKey setting: PopupSetting) {
removeObject(forKey: setting.rawValue)
}
}
extension AppStorage {
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value == Bool {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value == Int {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value == Double {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value == String {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value == URL {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value == Data {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value : RawRepresentable, Value.RawValue == Int {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
public init(wrappedValue: Value, _ key: PopupSetting, store: UserDefaults? = nil) where Value : RawRepresentable, Value.RawValue == String {
self.init(wrappedValue: wrappedValue, key.rawValue, store: store)
}
}
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
extension AppStorage where Value : ExpressibleByNilLiteral {
public init(_ key: PopupSetting, store: UserDefaults? = nil) where Value == Bool? {
self.init(key.rawValue, store: store)
}
public init(_ key: PopupSetting, store: UserDefaults? = nil) where Value == Int? {
self.init(key.rawValue, store: store)
}
public init(_ key: PopupSetting, store: UserDefaults? = nil) where Value == Double? {
self.init(key.rawValue, store: store)
}
public init(_ key: PopupSetting, store: UserDefaults? = nil) where Value == String? {
self.init(key.rawValue, store: store)
}
public init(_ key: PopupSetting, store: UserDefaults? = nil) where Value == URL? {
self.init(key.rawValue, store: store)
}
public init(_ key: PopupSetting, store: UserDefaults? = nil) where Value == Data? {
self.init(key.rawValue, store: store)
}
}
extension UIBlurEffect.Style {
static let `default` = UIBlurEffect.Style(rawValue: 0xffff)!
}
fileprivate extension Picker where Label == EmptyView {
init(selection: Binding<SelectionValue>, @ViewBuilder content: () -> Content) {
self.init(selection: selection, content: content) {
EmptyView()
}
}
}
@MainActor fileprivate var isLNPopupUIExample: Bool = {
return ProcessInfo.processInfo.processName == "LNPopupUIExample"
}()
fileprivate extension String {
func matches(_ another: String) -> Bool {
return range(of: another, options: [.caseInsensitive]) != nil
}
}
@MainActor
fileprivate struct LNText: View {
let text: Text
public init(_ content: String) {
NotificationCenter.default.post(name: .textVisited, object: content)
@AppStorage(PopupSetting.forceRTL) var forceRTL: Bool = false
if isLNPopupUIExample || forceRTL == false {
text = Text(LocalizedStringKey(content))
} else {
text = Text(content.applyingTransform(.latinToHebrew, reverse: false)!)
}
}
var body: some View {
text
}
}
@MainActor
fileprivate func LNTextCollector<Content>(_ container: inout [String], content: () -> Content) -> Content {
var results = [String]()
let observer = NotificationCenter.default.addMainQueueObserver(forName: .textVisited, object: nil) { note in
results.append(note.object as! String)
}
let rv = content()
NotificationCenter.default.removeObserver(observer)
container.append(contentsOf: results)
return rv
}
@MainActor
fileprivate struct LNHeaderFooterView: View {
let content: LNText
public init(_ content: String) {
self.content = LNText(content)
}
var body: some View {
content.font(.footnote)
}
}
fileprivate func requiredPadding() -> CGFloat? {
guard UserDefaults.standard.bool(forKey: "com.apple.SwiftUI.DisableCollectionViewBackedGroupedLists") == false else {
return 0
}
return 4.167
}
@MainActor
fileprivate struct CellPaddedText: View {
let content: LNText
public init(_ content: String) {
self.content = LNText(content)
}
var body: some View {
content
.padding([.top, .bottom], requiredPadding())
}
}
@MainActor
fileprivate struct CellPaddedToggle: View {
let isHidden: Bool
let title: LNText
let isOn: Binding<Bool>
let onTapGesture: (() -> Void)?
init(_ title: String, isOn: Binding<Bool>, searchString: String, onTapGesture: (() -> Void)? = nil) {
isHidden = searchString.isEmpty == false && title.matches(searchString) == false
self.title = LNText(title)
self.isOn = isOn
self.onTapGesture = onTapGesture
}
var body: some View {
if isHidden {
EmptyView()
} else {
ZStack {
Toggle(isOn: isOn, label: {
title
.padding([.top, .bottom], requiredPadding())
}).allowsHitTesting(onTapGesture == nil)
if let onTapGesture {
Color.red.opacity(0.001).onTapGesture(perform: onTapGesture)
}
}
}
}
}
@MainActor
fileprivate struct SearchAdaptingSection<Content, Header, Footer> {
let searchString: String
let searchTerms: [String]
let isPicker: Bool
let content: Content
let header: Header
let footer: Footer
}
fileprivate protocol PickerProtocol {}
extension Picker : PickerProtocol {}
extension SearchAdaptingSection: View where Content: View, Header: View, Footer: View {
init(_ searchString: String, includeHeaderAndFooter include: Bool = false, @ViewBuilder content: (String) -> Content, @ViewBuilder header: () -> Header, @ViewBuilder footer: () -> Footer) {
self.searchString = searchString
var searchTerms = [String]()
self.header = LNTextCollector(&searchTerms) {
header()
}
self.footer = LNTextCollector(&searchTerms) {
footer()
}
let matchesHeaderOrFooter = include && !searchString.isEmpty && searchTerms.firstIndex(where: { $0.matches(searchString) }) != nil
self.content = LNTextCollector(&searchTerms) {
content(matchesHeaderOrFooter ? "" : searchString)
}
isPicker = self.content is PickerProtocol
self.searchTerms = searchTerms
}
@ViewBuilder var body: some View {
if searchString.isEmpty == false && (searchTerms.isEmpty || searchTerms.first(where: { $0.matches(searchString) }) == nil) {
EmptyView()
}
else {
if content is EmptyView == false && !isPicker && !searchString.isEmpty {
content
} else {
Section {
content
} header: {
// if searchString.isEmpty {
header
// }
} footer: {
if searchString.isEmpty || content is EmptyView {
footer
}
}
}
}
}
}
extension SearchAdaptingSection where Content: View, Header: View, Footer == EmptyView {
init(_ searchString: String, includeHeaderAndFooter include: Bool = false, @ViewBuilder content: (String) -> Content, @ViewBuilder header: () -> Header) {
self.searchString = searchString
var searchTerms = [String]()
self.header = LNTextCollector(&searchTerms) {
header()
}
self.footer = EmptyView()
let matchesHeaderOrFooter = include && !searchString.isEmpty && searchTerms.firstIndex(where: { $0.matches(searchString) }) != nil
self.content = LNTextCollector(&searchTerms) {
content(matchesHeaderOrFooter ? "" : searchString)
}
isPicker = self.content is PickerProtocol
self.searchTerms = searchTerms
}
}
extension SearchAdaptingSection where Content: View, Header == EmptyView, Footer: View {
init(_ searchString: String, includeHeaderAndFooter include: Bool = false, @ViewBuilder content: (String) -> Content, @ViewBuilder footer: () -> Footer) {
self.searchString = searchString
var searchTerms = [String]()
self.header = EmptyView()
self.footer = LNTextCollector(&searchTerms) {
footer()
}
let matchesHeaderOrFooter = include && !searchString.isEmpty && searchTerms.firstIndex(where: { $0.matches(searchString) }) != nil
self.content = LNTextCollector(&searchTerms) {
content(matchesHeaderOrFooter ? "" : searchString)
}
isPicker = self.content is PickerProtocol
self.searchTerms = searchTerms
}
}
fileprivate struct PickerGroupContent {
@ViewBuilder let content: () -> any View
@ViewBuilder let footer: () -> any View
}
fileprivate struct PickerGroupContentFulfilled {
var content: any View
var footer: any View
}
@resultBuilder
fileprivate struct PickerGroupContentBuilder {
static func buildBlock(_ parts: PickerGroupContent...) -> [PickerGroupContent] {
parts
}
}
@MainActor
fileprivate struct SearchAdaptingPickerGroup<Header: View, SelectionValue: Hashable>: View {
let searchString: String
let searchTerms: [String]
let selection: Binding<SelectionValue>
let content: [PickerGroupContentFulfilled]
let header: Header
init(_ searchString: String, selection: Binding<SelectionValue>, @PickerGroupContentBuilder content: () -> [PickerGroupContent], @ViewBuilder header: () -> Header) {
self.searchString = searchString
self.selection = selection
var searchTerms = [String]()
self.content = LNTextCollector(&searchTerms) {
content().map { PickerGroupContentFulfilled(content: $0.content(), footer: $0.footer()) }
}
self.header = LNTextCollector(&searchTerms) {
header()
}
self.searchTerms = searchTerms
}
var body: some View {
if !searchString.isEmpty && searchTerms.first(where: { $0.matches(searchString) }) == nil {
EmptyView()
} else {
if !searchString.isEmpty {
SearchAdaptingSection("") { _ in
Picker(selection: selection) {
ForEach(Array(content.enumerated()), id: \.offset) {
AnyView(erasing: $1.content)
}
}
} header: {
header
}
}
else {
ForEach(content.indices, id: \.self) { idx in
let current = content[idx]
SearchAdaptingSection("") { _ in
Picker(selection: selection) {
AnyView(erasing: current.content)
}
} header: {
if idx == 0 {
header
} else {
EmptyView()
}
} footer: {
AnyView(current.footer)
}
}
}
}
}
}
struct SettingsForm : View {
@AppStorage(.barStyle, store: .settings) var barStyle: LNPopupBar.Style = .default
@AppStorage(.interactionStyle, store: .settings) var interactionStyle: UIViewController.__PopupInteractionStyle = .default
@AppStorage(.closeButtonStyle, store: .settings) var closeButtonStyle: LNPopupCloseButton.Style = .default
@AppStorage(.progressViewStyle, store: .settings) var progressViewStyle: LNPopupBar.ProgressViewStyle = .default
@AppStorage(.marqueeEnabled, store: .settings) var marqueeEnabled: Bool = false
@AppStorage(.marqueeCoordinationEnabled, store: .settings) var marqueeCoordinationEnabled: Bool = true
@AppStorage(.hapticFeedbackEnabled, store: .settings) var hapticFeedback: Bool = true
@AppStorage(.visualEffectViewBlurEffect, store: .settings) var blurEffectStyle: UIBlurEffect.Style = .default
@AppStorage(.extendBar, store: .settings) var extendBar: Bool = true
@AppStorage(.limitFloatingWidth, store: .settings) var limitFloatingWidth: Bool = true
@AppStorage(.hidesBottomBarWhenPushed, store: .settings) var hideBottomBar: Bool = true
@AppStorage(.disableScrollEdgeAppearance, store: .settings) var disableScrollEdgeAppearance: Bool = false
@AppStorage(.customBarEverywhereEnabled, store: .settings) var customPopupBar: Bool = false
@AppStorage(.enableCustomizations, store: .settings) var enableCustomizations: Bool = false
@AppStorage(.contextMenuEnabled, store: .settings) var contextMenu: Bool = false
@AppStorage(.touchVisualizerEnabled, store: .settings) var touchVisualizer: Bool = false
@AppStorage(.barHideContentView, store: .settings) var hidePopupBarContentView: Bool = false
@AppStorage(.barHideShadow, store: .settings) var hidePopupBarShadow: Bool = false
@AppStorage(.barEnableLayoutDebug, store: .settings) var layoutDebug: Bool = false
@AppStorage(.forceRTL) var forceRTL: Bool = false
@AppStorage(.debugScaling, store: .settings) var debugScaling: Double = 0
@AppStorage(.tabBarHasSidebar, store: .settings) var tabBarHasSidebar: Bool = true
@AppStorage(.disableDemoSceneColors, store: .settings) var disableDemoSceneColors: Bool = false
@AppStorage(.enableFunkyInheritedFont, store: .settings) var enableFunkyInheritedFont: Bool = false
@AppStorage(.enableExternalScenes, store: .settings) var enableExternalScenes: Bool = false
@AppStorage(.enableCustomLabels, store: .settings) var enableCustomLabels: Bool = false
@AppStorage(.useScrollingPopupContent, store: .settings) var useScrollingPopupContent: Int = 0
@Environment(\.isSearching) private var isSearching
@Environment(\.dismissSearch) private var dismissSearch
let searchText: String
let isDefault: Bool
init(isDefault: Bool, searchText: String) {
self.isDefault = isDefault
self.searchText = isDefault ? "" : searchText
}
@ViewBuilder var body: some View {
if isDefault == false && (isSearching == false || searchText.isEmpty) {
Color.black.opacity(isSearching ? 0.12 : 0.0).ignoresSafeArea()
.transition(.opacity)
.animation(.default, value: isSearching)
.onTapGesture {
dismissSearch()
}
}
else {
Form {
SearchAdaptingSection(searchText) { _ in
Picker(selection: $barStyle) {
CellPaddedText("Default").tag(LNPopupBar.Style.default)
CellPaddedText("Compact").tag(LNPopupBar.Style.compact)
CellPaddedText("Prominent").tag(LNPopupBar.Style.prominent)
CellPaddedText("Floating").tag(LNPopupBar.Style.floating)
}
} header: {
LNHeaderFooterView("Bar Style")
}
SearchAdaptingSection(searchText) { _ in
Picker(selection: $interactionStyle) {
CellPaddedText("Default").tag(UIViewController.__PopupInteractionStyle.default)
CellPaddedText("Drag").tag(UIViewController.__PopupInteractionStyle.drag)
CellPaddedText("Snap").tag(UIViewController.__PopupInteractionStyle.snap)
CellPaddedText("Scroll").tag(UIViewController.__PopupInteractionStyle.scroll)
CellPaddedText("None").tag(UIViewController.__PopupInteractionStyle.none)
}
} header: {
LNHeaderFooterView("Interaction Style")
}
SearchAdaptingSection(searchText) { _ in
Picker(selection: $closeButtonStyle) {
CellPaddedText("Default").tag(LNPopupCloseButton.Style.default)
CellPaddedText("Round").tag(LNPopupCloseButton.Style.round)
CellPaddedText("Chevron").tag(LNPopupCloseButton.Style.chevron)
CellPaddedText("Grabber").tag(LNPopupCloseButton.Style.grabber)
CellPaddedText("None").tag(LNPopupCloseButton.Style.none)
}
} header: {
LNHeaderFooterView("Close Button Style")
}
SearchAdaptingSection(searchText) { _ in
Picker(selection: $progressViewStyle) {
CellPaddedText("Default").tag(LNPopupBar.ProgressViewStyle.default)
CellPaddedText("Top").tag(LNPopupBar.ProgressViewStyle.top)
CellPaddedText("Bottom").tag(LNPopupBar.ProgressViewStyle.bottom)
CellPaddedText("None").tag(LNPopupBar.ProgressViewStyle.none)
}
} header: {
LNHeaderFooterView("Progress View Style")
}
SearchAdaptingPickerGroup(searchText, selection: $blurEffectStyle) {
PickerGroupContent {
CellPaddedText("Default").tag(UIBlurEffect.Style.default)
} footer: {
LNHeaderFooterView("Uses the default material chosen by the system.")
}
PickerGroupContent {
CellPaddedText("Ultra Thin Material").tag(UIBlurEffect.Style.systemUltraThinMaterial)
CellPaddedText("Thin Material").tag(UIBlurEffect.Style.systemThinMaterial)
CellPaddedText("Material").tag(UIBlurEffect.Style.systemMaterial)
CellPaddedText("Thick Material").tag(UIBlurEffect.Style.systemThickMaterial)
CellPaddedText("Chrome Material").tag(UIBlurEffect.Style.systemChromeMaterial)
} footer: {
LNHeaderFooterView("Material styles which automatically adapt to the user interface style. Available in iOS 13 and above.")
}
PickerGroupContent {
CellPaddedText("Regular").tag(UIBlurEffect.Style.regular)
CellPaddedText("Prominent").tag(UIBlurEffect.Style.prominent)
} footer: {
LNHeaderFooterView("Styles which automatically show one of the traditional blur styles, depending on the user interface style. Available in iOS 10 and above.")
}
PickerGroupContent {
CellPaddedText("Extra Light").tag(UIBlurEffect.Style.extraLight)
CellPaddedText("Light").tag(UIBlurEffect.Style.light)
CellPaddedText("Dark").tag(UIBlurEffect.Style.dark)
} footer: {
LNHeaderFooterView("Traditional blur styles. Available in iOS 8 and above.")
}
} header: {
LNHeaderFooterView("Background Blur Style")
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Title & Subtitle Label Marquee", isOn: $marqueeEnabled, searchString: searchText)
if marqueeEnabled {
CellPaddedToggle("Coordinate Marquee Labels", isOn: $marqueeCoordinationEnabled, searchString: searchText)
}
} header: {
LNHeaderFooterView("Marquee")
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Popup Interaction Haptic Feedback", isOn: $hapticFeedback, searchString: searchText)
} header: {
LNHeaderFooterView("Haptic Feedback")
} footer: {
LNHeaderFooterView("Enables haptic feedback when the user interacts with the popup.")
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Extend Bar Under Safe Area", isOn: $extendBar, searchString: searchText)
} header: {
LNHeaderFooterView("Settings")
} footer: {
if isLNPopupUIExample {
LNHeaderFooterView("Calls the `popupBarShouldExtendPopupBarUnderSafeArea()` modifier with a value of `true` in standard demo scenes.")
} else {
LNHeaderFooterView("Sets the `shouldExtendPopupBarUnderSafeArea` property to `true` in standard demo scenes.")
}
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Limit Width of Floating Bar", isOn: $limitFloatingWidth, searchString: searchText)
} footer: {
LNHeaderFooterView("Limits the width of a floating popup bar to a system-determined value in standard demo scenes.")
}
if #available(iOS 18.0, *), UIDevice.current.userInterfaceIdiom == .pad {
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Tab \(isLNPopupUIExample ? "Views" : "Bar Controllers") Have Sidebars", isOn: $tabBarHasSidebar, searchString: searchText)
} footer: {
LNHeaderFooterView("Add support for sidebar to standard tab \(isLNPopupUIExample ? "view" : "bar controller") scenes.")
}
}
if isLNPopupUIExample == false {
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Hides Bottom Bar When Pushed", isOn: $hideBottomBar, searchString: searchText)
} footer: {
LNHeaderFooterView("Sets the `hidesBottomBarWhenPushed` property of pushed controllers in standard demo scenes.")
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Disable Scroll Edge Appearance", isOn: $disableScrollEdgeAppearance, searchString: searchText)
} footer: {
LNHeaderFooterView("Disables the scroll edge appearance for system bars in standard demo scenes.")
}
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Context Menu Interactions", isOn: $contextMenu, searchString: searchText)
} footer: {
LNHeaderFooterView("Enables popup bar context menu interaction in standard demo scenes.")
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Customizations", isOn: $enableCustomizations, searchString: searchText)
} footer: {
LNHeaderFooterView("Enables popup bar customizations in standard demo scenes.")
}
if isLNPopupUIExample {
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Custom Labels", isOn: $enableCustomLabels, searchString: searchText)
} footer: {
LNHeaderFooterView("Enables the use of custom labels in standard demo scenes.")
}
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Custom Popup Bar", isOn: $customPopupBar, searchString: searchText)
} footer: {
LNHeaderFooterView("Enables a custom popup bar in standard demo scenes.")
}
if !isLNPopupUIExample {
SearchAdaptingSection(searchText) { searchText in
Picker(selection: $useScrollingPopupContent) {
CellPaddedText("None").tag(0)
CellPaddedText("Vertical").tag(10)
CellPaddedText("Horizontal").tag(11)
CellPaddedText("Paged Vertical").tag(20)
CellPaddedText("Paged Horizontal").tag(21)
CellPaddedText("Paged Vertical & Paged Horizontal").tag(22)
CellPaddedText("Paged Horizontal & Paged Vertical").tag(23)
CellPaddedText("Map View").tag(100)
} label: {
CellPaddedText("Scrolling Popup Content")
}
.pickerStyle(.menu)
.tint(.secondary)
} footer: {
switch useScrollingPopupContent {
case 1:
LNHeaderFooterView("Uses vertical scrolling popup content in standard demo scenes.")
case 2:
LNHeaderFooterView("Uses horizontal scrolling popup content in standard demo scenes.")
default:
LNHeaderFooterView("Uses standard popup content in standard demo scenes.")
}
}
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Disable Demo Scene Colors", isOn: $disableDemoSceneColors, searchString: searchText)
} footer: {
LNHeaderFooterView("Disables random background colors in standard demo scenes.")
}
if isLNPopupUIExample {
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Use Funky Inherited Font", isOn: $enableFunkyInheritedFont, searchString: searchText)
} footer: {
LNHeaderFooterView("Enables an environment font that is inherited by the popup bar.")
}
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Layout Debug", isOn: $layoutDebug, searchString: searchText)
CellPaddedToggle("Hide Content View", isOn: $hidePopupBarContentView, searchString: searchText)
CellPaddedToggle("Hide Floating Shadow", isOn: $hidePopupBarShadow, searchString: searchText)
CellPaddedToggle("Use Right-to-Left Pseudolanguage With Right-to-Left Strings", isOn: $forceRTL, searchString: searchText) {
SettingsViewController.toggleRTL { accepted in
guard accepted else {
return
}
forceRTL.toggle()
}
}
if isDefault || "Scaling".matches(searchText) {
NavigationLink {
Form {
Section {
Picker(selection: $debugScaling) {
CellPaddedText("Default").tag(0.0)
}
} footer: {
LNHeaderFooterView("Uses the default scaling according to screen size and “Display Zoom” setting.")
}
Section {
Picker(selection: $debugScaling) {
CellPaddedText("320").tag(320.0)
}
} footer: {
LNHeaderFooterView("Classic phones as well as “Larger Text” non-Max & non-Plus phones.")
}
Section {
Picker(selection: $debugScaling) {
CellPaddedText("375").tag(375.0)
CellPaddedText("390").tag(390.0)
CellPaddedText("393").tag(393.0)
}
} footer: {
LNHeaderFooterView("Non-Max & non-Plus phones as well as “Larger Text” Max & Plus phones.")
}
Section {
Picker(selection: $debugScaling) {
CellPaddedText("414").tag(414.0)
CellPaddedText("428").tag(428.0)
CellPaddedText("430").tag(430.0)
}
} footer: {
LNHeaderFooterView("Max & Plus phones.")
}
}.pickerStyle(.inline).navigationTitle("Scaling")
} label: {
HStack {
CellPaddedText("Scaling")
Spacer()
CellPaddedText(debugScaling == 0 ? "Default" : "\(String(format: "%.0f", debugScaling))").foregroundColor(.secondary)
}
}
}
} header: {
LNHeaderFooterView("Popup Bar Debug")
}
SearchAdaptingSection(searchText) { searchText in
CellPaddedToggle("Touch Visualizer", isOn: $touchVisualizer, searchString: searchText)
} header: {
LNHeaderFooterView("Demonstration")
} footer: {
LNHeaderFooterView("Enables visualization of touches within the app, for demo purposes.")
}
if isLNPopupUIExample {
SearchAdaptingSection(searchText, includeHeaderAndFooter: true) { searchText in
CellPaddedToggle("CompactSlider", isOn: $enableExternalScenes, searchString: searchText)
} header: {
LNHeaderFooterView("External Libraries")
} footer: {
LNHeaderFooterView("Enables scenes for testing with external libraries.")
}
}
SearchAdaptingSection(searchText) { _ in
} footer: {
LNHeaderFooterView("\(Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String) Version \(Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String)")
}
}
.background {
if isDefault == false {
Color(.systemGroupedBackground).edgesIgnoringSafeArea(.all)
}
}
}
}
}
struct SettingsView : View {
@State private var searchText = ""
@AppStorage(.forceRTL) var forceRTL: Bool = false
@AppStorage(.marqueeEnabled, store: .settings) var marqueeEnabled: Bool = false
let onDismiss: (() -> ())?
@Environment(\.presentationMode) var presentationMode
init(onDismiss: (() -> ())? = nil) {
self.onDismiss = onDismiss
}
@ViewBuilder var body: some View {
ZStack {
SettingsForm(isDefault: true, searchText: searchText)
SettingsForm(isDefault: false, searchText: searchText)
}
.navigationTitle(NSLocalizedString("Settings", comment: ""))
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(NSLocalizedString("Reset", comment: "")) {
SettingsViewController.reset()
}
}
ToolbarItem(placement: .confirmationAction) {
Button(NSLocalizedString("Done", comment: "")) {
if let onDismiss {
onDismiss()
} else {
self.presentationMode.wrappedValue.dismiss()
}
}
}
}
.pickerStyle(.inline)
.animation(.default, value: marqueeEnabled)
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: NSLocalizedString("Search", comment: ""))
}
}
class SettingsViewController: UIHostingController<SettingsView> {
required init() {
weak var weakSelf: SettingsViewController?
super.init(rootView: SettingsView(onDismiss: {
weakSelf?.presentingViewController?.dismiss(animated: true)
}))
weakSelf = self
self.preferredContentSize = CGSize(width: 375, height: 600)
}
required init?(coder aDecoder: NSCoder) {
weak var weakSelf: SettingsViewController?
super.init(coder: aDecoder, rootView: SettingsView(onDismiss: {
weakSelf?.presentingViewController?.dismiss(animated: true)
}))
weakSelf = self
self.preferredContentSize = CGSize(width: 375, height: 600)
}
enum ResetAlertChoice {
case cancelled
case resetWithoutRestart
case reset
}
class func alertRestartNeeded(allowSafeReset: Bool, completion: @escaping (ResetAlertChoice) -> ()) {
let alertController = UIAlertController(title: NSLocalizedString("Restart Required", comment: ""), message: NSLocalizedString("Changing some settings requires exiting the app and restarting it.", comment: ""), preferredStyle: .alert)
alertController.view.tintColor = .systemBlue
if #available(iOS 16.0, *) {
alertController.severity = .critical
}
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: { _ in
completion(.cancelled)
}))
if allowSafeReset {
alertController.addAction(UIAlertAction(title: NSLocalizedString("Reset Without Exiting", comment: ""), style: .default, handler: { _ in
completion(.resetWithoutRestart)
}))
}
alertController.addAction(UIAlertAction(title: NSLocalizedString("Exit", comment: ""), style: .destructive, handler: { _ in
completion(.reset)
UserDefaults.settings.synchronize()
exit(0)
}))
let window = UIWindow.value(forKey: "keyWindow") as! UIWindow
var controller = window.rootViewController!
while controller.presentedViewController != nil {
controller = controller.presentedViewController!
}
controller.present(alertController, animated: true)
}
class func setRTL() {
UserDefaults.standard.set(true, forKey: "AppleTextDirection")
UserDefaults.standard.set(true, forKey: "NSForceRightToLeftWritingDirection")
UserDefaults.standard.set(true, forKey: "NSForceRightToLeftLocalizedStrings")
UserDefaults.standard.synchronize()
}
class func resetRTL() {
UserDefaults.standard.removeObject(forKey: "AppleTextDirection")
UserDefaults.standard.removeObject(forKey: "NSForceRightToLeftWritingDirection")
UserDefaults.standard.removeObject(forKey: "NSForceRightToLeftLocalizedStrings")
UserDefaults.standard.synchronize()
}
class func toggleRTL(completion: @escaping (Bool) -> ()) {
alertRestartNeeded(allowSafeReset: false) { response in
guard response == .reset else {
completion(false)
return
}
completion(true)
let wantsRTL = UserDefaults.standard.bool(forKey: .forceRTL)
if wantsRTL {
setRTL()
} else {
resetRTL()
}
}
}
class func reset() {
let actualReset: (Bool) -> () = { includeRTL in
UserDefaults.settings.set(true, forKey: .extendBar)
UserDefaults.settings.set(true, forKey: .hidesBottomBarWhenPushed)
UserDefaults.settings.set(true, forKey: .hapticFeedbackEnabled)
UserDefaults.settings.set(true, forKey: .marqueeCoordinationEnabled)
if includeRTL {
UserDefaults.standard.removeObject(forKey: .forceRTL)
resetRTL()
}
UserDefaults.settings.removeObject(forKey: .debugScaling)
let settingsToRemove: [PopupSetting] = [.barStyle, .interactionStyle, .closeButtonStyle, .progressViewStyle, .enableCustomizations, .disableScrollEdgeAppearance, .touchVisualizerEnabled, .customBarEverywhereEnabled, .contextMenuEnabled, .barHideContentView, .barHideShadow, .barEnableLayoutDebug, .disableDemoSceneColors, .enableFunkyInheritedFont, .enableExternalScenes, .marqueeEnabled, .enableCustomLabels, .useScrollingPopupContent, .limitFloatingWidth, .tabBarHasSidebar]
for key in settingsToRemove {
UserDefaults.settings.removeObject(forKey: key)
}
UserDefaults.settings.set(0xffff, forKey: .visualEffectViewBlurEffect)
}
if UserDefaults.standard.bool(forKey: .forceRTL) {
alertRestartNeeded(allowSafeReset: true) { response in
guard response != .cancelled else {
return
}
actualReset(response == .reset)
}
} else {
actualReset(false)
}
}
@IBAction func reset() {
SettingsViewController.reset()
}
}
@available(iOS 16.0, *)
struct SettingsNavView: View {
var body: some View {
NavigationStack {
SettingsView()
}
}
}
#else
struct NoSettingsView : View {
let onDismiss: (() -> ())?
@Environment(\.presentationMode) var presentationMode
init(onDismiss: (() -> ())? = nil) {
self.onDismiss = onDismiss
}
var body: some View {
Text("No Settings")
.fontWeight(.semibold)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(UIColor.systemGroupedBackground))
.navigationTitle("Settings")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
if let onDismiss {
onDismiss()
} else {
self.presentationMode.wrappedValue.dismiss()
}
}
}
}
}
}
class SettingsViewController: UIHostingController<NoSettingsView> {
required init?(coder aDecoder: NSCoder) {
weak var weakSelf: SettingsViewController?
super.init(coder: aDecoder, rootView: NoSettingsView(onDismiss: {
weakSelf?.presentingViewController?.dismiss(animated: true)
}))
weakSelf = self
}
}
#endif