|
1 | 1 | // |
2 | | -// Setting.swift |
| 2 | +// SettingTheme.swift |
3 | 3 | // Setting |
4 | 4 | // |
5 | 5 | // Created by A. Zheng (github.com/aheze) on 2/21/23. |
|
9 | 9 | import SwiftUI |
10 | 10 |
|
11 | 11 | /** |
12 | | - A collection of default color values. |
| 12 | + The base protocol for views shown in the `SettingBuilder`. |
13 | 13 | */ |
14 | | -public enum Setting { |
15 | | - public static var labelColor: Color = { |
16 | | - #if os(iOS) |
17 | | - return Color(uiColor: .label) |
18 | | - #else |
19 | | - return Color(nsColor: .labelColor) |
20 | | - #endif |
21 | | - }() |
| 14 | +public protocol Setting { |
| 15 | + var id: AnyHashable? { get set } |
| 16 | +} |
| 17 | + |
| 18 | +public extension Setting { |
| 19 | + /** |
| 20 | + A unique identifier for the view. |
| 21 | + */ |
| 22 | + var identifier: AnyHashable { |
| 23 | + if let id { |
| 24 | + return id |
| 25 | + } |
22 | 26 |
|
23 | | - public static var secondaryLabelColor: Color = { |
24 | | - #if os(iOS) |
25 | | - return Color(uiColor: .secondaryLabel) |
26 | | - #else |
27 | | - return Color(nsColor: .secondaryLabelColor) |
28 | | - #endif |
29 | | - }() |
| 27 | + return textIdentifier |
| 28 | + } |
30 | 29 |
|
31 | | - public static var backgroundColor: Color = { |
32 | | - #if os(iOS) |
33 | | - return Color(uiColor: .systemBackground) |
34 | | - #else |
35 | | - return Color(nsColor: .textBackgroundColor) |
36 | | - #endif |
37 | | - }() |
| 30 | + /** |
| 31 | + The identifier generated from the item's title. |
| 32 | + */ |
| 33 | + var textIdentifier: String? { |
| 34 | + switch self { |
| 35 | + case let text as SettingText: |
| 36 | + return text.title |
| 37 | + case let button as SettingButton: |
| 38 | + return button.title |
| 39 | + case let toggle as SettingToggle: |
| 40 | + return toggle.title |
| 41 | + case is SettingSlider: |
| 42 | + return nil |
| 43 | + case let picker as SettingPicker: |
| 44 | + return picker.title |
| 45 | + case let page as SettingPage: |
| 46 | + return page.title |
| 47 | + case let group as SettingGroup: |
| 48 | + return group.tuple.textIdentifier |
| 49 | + case let tuple as SettingTupleView: |
| 50 | + return tuple.flattened.compactMap { $0.textIdentifier }.joined() |
| 51 | + case let customView as SettingCustomView: |
| 52 | + return customView.titleForSearch ?? "Custom" |
| 53 | + default: |
| 54 | + print("Nil! \(type(of: self))") |
| 55 | + return nil |
| 56 | + } |
| 57 | + } |
38 | 58 |
|
39 | | - public static var secondaryBackgroundColor: Color = { |
40 | | - #if os(iOS) |
41 | | - return Color(uiColor: .secondarySystemBackground) |
42 | | - #else |
43 | | - return Color(nsColor: .windowBackgroundColor) |
44 | | - #endif |
45 | | - }() |
| 59 | + /** |
| 60 | + Text for searching. |
| 61 | + */ |
| 62 | + var text: String? { |
| 63 | + switch self { |
| 64 | + case let text as SettingText: |
| 65 | + return text.title |
| 66 | + case let button as SettingButton: |
| 67 | + return button.title |
| 68 | + case let toggle as SettingToggle: |
| 69 | + return toggle.title |
| 70 | + case is SettingSlider: |
| 71 | + return nil |
| 72 | + case let picker as SettingPicker: |
| 73 | + return picker.title |
| 74 | + case let page as SettingPage: |
| 75 | + return page.title |
| 76 | + case let group as SettingGroup: |
| 77 | + return group.header |
| 78 | + case is SettingTupleView: |
| 79 | + return nil |
| 80 | + case let customView as SettingCustomView: |
| 81 | + return customView.titleForSearch |
| 82 | + default: |
| 83 | + return nil |
| 84 | + } |
| 85 | + } |
46 | 86 | } |
0 commit comments