Skip to content

Commit e8f7374

Browse files
committed
Rename Item to Setting
1 parent cbd6bd7 commit e8f7374

20 files changed

Lines changed: 132 additions & 169 deletions

Example/SettingExample/PreferencesView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct PreferencesView: View {
100100
SettingToggle(title: "Turbo Mode", isOn: $model.turboMode)
101101

102102
if model.turboMode {
103-
SettingText(title: "Turbo mode is on!", foregroundColor: Setting.secondaryLabelColor)
103+
SettingText(title: "Turbo mode is on!", foregroundColor: SettingTheme.secondaryLabelColor)
104104
}
105105

106106
SettingButton(title: "Show Alert") {
@@ -266,7 +266,7 @@ struct PreferencesView: View {
266266
Image("Twitter")
267267
.resizable()
268268
.aspectRatio(contentMode: .fit)
269-
.foregroundColor(Setting.secondaryLabelColor)
269+
.foregroundColor(SettingTheme.secondaryLabelColor)
270270
.frame(width: 30, height: 30)
271271
.padding(6)
272272
.contentShape(Rectangle())

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Add your own custom SwiftUI views.
1313
- Works on iOS and macOS.
1414

15-
![Screenshots of views created with Setting](Assets/Setting.png)
15+
![Screenshots of views created with Setting](Assets/SettingTheme.png)
1616

1717
![Screenshots of a nested page and search results](Assets/Details.png)
1818

Sources/Setting.swift

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Setting.swift
2+
// SettingTheme.swift
33
// Setting
44
//
55
// Created by A. Zheng (github.com/aheze) on 2/21/23.
@@ -9,38 +9,78 @@
99
import SwiftUI
1010

1111
/**
12-
A collection of default color values.
12+
The base protocol for views shown in the `SettingBuilder`.
1313
*/
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+
}
2226

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+
}
3029

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+
}
3858

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+
}
4686
}

Sources/SettingPath.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import SwiftUI
1313
*/
1414
public struct SettingPath: Identifiable {
1515
public let id = UUID()
16-
public var items: [SettingItem]
16+
public var items: [Setting]
1717

18-
public init(items: [SettingItem]) {
18+
public init(items: [Setting]) {
1919
self.items = items
2020
}
2121
}

Sources/SettingSearchResult.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public struct SettingSearchResultView: View {
2626
public var searchResult: SettingSearchResult
2727
public var spacing = CGFloat(20)
2828
public var verticalPadding = CGFloat(6)
29-
public var backgroundColor = Setting.secondaryBackgroundColor
29+
public var backgroundColor = SettingTheme.secondaryBackgroundColor
3030

3131
public init(
3232
searchResult: SettingSearchResult,
3333
spacing: CGFloat = CGFloat(20),
3434
verticalPadding: CGFloat = CGFloat(6),
35-
backgroundColor: Color = Setting.secondaryBackgroundColor
35+
backgroundColor: Color = SettingTheme.secondaryBackgroundColor
3636
) {
3737
self.searchResult = searchResult
3838
self.spacing = spacing
@@ -61,7 +61,7 @@ public struct SettingSearchResultView: View {
6161
let firstItem = firstPath.items.first,
6262
firstItem is SettingCustomView
6363
{
64-
SettingItemView(item: firstItem)
64+
SettingView(item: firstItem)
6565
} else {
6666
VStack {
6767
SettingGroupView(
@@ -72,7 +72,7 @@ public struct SettingSearchResultView: View {
7272
/// If it's only 1 item, the item is on the main page - so just show it.
7373
if path.items.count == 1 {
7474
if let item = path.items.first {
75-
SettingItemView(item: item)
75+
SettingView(item: item)
7676
}
7777
} else {
7878
SettingJumpLink(path: path)

Sources/SettingStyles.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public struct SettingRowButtonStyle: ButtonStyle {
1717
.contentShape(Rectangle())
1818
.background {
1919
if configuration.isPressed {
20-
Setting.labelColor
20+
SettingTheme.labelColor
2121
.opacity(0.1)
2222
}
2323
}
File renamed without changes.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct SettingJumpLink: View {
5050
.background {
5151
if let destinationPage {
5252
NavigationLink(isActive: $isActive) {
53-
SettingItemView(item: destinationPage, isPagePreview: false)
53+
SettingView(item: destinationPage, isPagePreview: false)
5454
} label: {
5555
EmptyView()
5656
}
@@ -59,7 +59,7 @@ public struct SettingJumpLink: View {
5959
}
6060
}
6161

62-
@ViewBuilder func preview(destinationPage: SettingItem?) -> some View {
62+
@ViewBuilder func preview(destinationPage: Setting?) -> some View {
6363
let title = getDestinationTitle()
6464
let titles = getPathTitles()
6565

@@ -90,15 +90,15 @@ public struct SettingJumpLink: View {
9090
}
9191
}
9292
.font(.footnote)
93-
.foregroundColor(Setting.secondaryLabelColor)
93+
.foregroundColor(SettingTheme.secondaryLabelColor)
9494
.frame(maxWidth: .infinity, alignment: .leading)
9595
}
9696
}
9797
.padding(.vertical, verticalPadding)
9898

9999
if destinationPage != nil {
100100
Image(systemName: indicator)
101-
.foregroundColor(Setting.secondaryLabelColor)
101+
.foregroundColor(SettingTheme.secondaryLabelColor)
102102
}
103103
}
104104
.padding(.horizontal, horizontalPadding)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111
/**
1212
A plain button.
1313
*/
14-
public struct SettingButton: View, SettingItem {
14+
public struct SettingButton: View, Setting {
1515
public var id: AnyHashable?
1616
public var title: String
1717
public var indicator: String? = "arrow.up.forward"
@@ -68,7 +68,7 @@ struct SettingButtonView: View {
6868

6969
if let indicator {
7070
Image(systemName: indicator)
71-
.foregroundColor(Setting.secondaryLabelColor)
71+
.foregroundColor(SettingTheme.secondaryLabelColor)
7272
}
7373
}
7474
.padding(.horizontal, horizontalPadding)

0 commit comments

Comments
 (0)