Skip to content

Commit 211dc89

Browse files
committed
Add settings to host app
1 parent 070c136 commit 211dc89

File tree

4 files changed

+262
-138
lines changed

4 files changed

+262
-138
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import SuggestionModel
2+
import SwiftUI
3+
4+
struct SuggestionFeatureDisabledLanguageListView: View {
5+
final class Settings: ObservableObject {
6+
@AppStorage(\.suggestionFeatureDisabledLanguageList)
7+
var suggestionFeatureDisabledLanguageList: [String]
8+
9+
init(suggestionFeatureDisabledLanguageList: AppStorage<[String]>? = nil) {
10+
if let list = suggestionFeatureDisabledLanguageList {
11+
_suggestionFeatureDisabledLanguageList = list
12+
}
13+
}
14+
}
15+
16+
var isOpen: Binding<Bool>
17+
@State var isAddingNewProject = false
18+
@StateObject var settings = Settings()
19+
20+
var body: some View {
21+
VStack(spacing: 0) {
22+
HStack {
23+
Button(action: {
24+
self.isOpen.wrappedValue = false
25+
}) {
26+
Image(systemName: "xmark.circle.fill")
27+
.foregroundStyle(.secondary)
28+
.padding()
29+
}
30+
.buttonStyle(.plain)
31+
Text("Enabled Projects")
32+
Spacer()
33+
Button(action: {
34+
isAddingNewProject = true
35+
}) {
36+
Image(systemName: "plus.circle.fill")
37+
.foregroundStyle(.secondary)
38+
.padding()
39+
}
40+
.buttonStyle(.plain)
41+
}
42+
.background(Color(nsColor: .separatorColor))
43+
44+
List {
45+
ForEach(
46+
settings.suggestionFeatureDisabledLanguageList,
47+
id: \.self
48+
) { language in
49+
HStack {
50+
Text(language.capitalized)
51+
.contextMenu {
52+
Button("Remove") {
53+
settings.suggestionFeatureDisabledLanguageList.removeAll(
54+
where: { $0 == language }
55+
)
56+
}
57+
}
58+
Spacer()
59+
60+
Button(action: {
61+
settings.suggestionFeatureDisabledLanguageList.removeAll(
62+
where: { $0 == language }
63+
)
64+
}) {
65+
Image(systemName: "trash.fill")
66+
.foregroundStyle(.secondary)
67+
}
68+
.buttonStyle(.plain)
69+
}
70+
}
71+
}
72+
.removeBackground()
73+
.overlay {
74+
if settings.suggestionFeatureDisabledLanguageList.isEmpty {
75+
Text("""
76+
Empty
77+
Disable the language of a file by right clicking the circular widget.
78+
""")
79+
.multilineTextAlignment(.center)
80+
}
81+
}
82+
}
83+
.focusable(false)
84+
.frame(width: 300, height: 400)
85+
.background(Color(nsColor: .windowBackgroundColor))
86+
}
87+
}
88+
89+
struct SuggestionFeatureDisabledLanguageListView_Preview: PreviewProvider {
90+
static var previews: some View {
91+
SuggestionFeatureDisabledLanguageListView(
92+
isOpen: .constant(true),
93+
settings: .init(suggestionFeatureDisabledLanguageList: .init(wrappedValue: [
94+
"hello/2",
95+
"hello/3",
96+
"hello/4",
97+
], "SuggestionFeatureDisabledLanguageListView_Preview"))
98+
)
99+
}
100+
}
101+
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import SwiftUI
2+
3+
struct SuggestionFeatureEnabledProjectListView: View {
4+
final class Settings: ObservableObject {
5+
@AppStorage(\.suggestionFeatureEnabledProjectList)
6+
var suggestionFeatureEnabledProjectList: [String]
7+
8+
init(suggestionFeatureEnabledProjectList: AppStorage<[String]>? = nil) {
9+
if let list = suggestionFeatureEnabledProjectList {
10+
_suggestionFeatureEnabledProjectList = list
11+
}
12+
}
13+
}
14+
15+
var isOpen: Binding<Bool>
16+
@State var isAddingNewProject = false
17+
@StateObject var settings = Settings()
18+
19+
var body: some View {
20+
VStack(spacing: 0) {
21+
HStack {
22+
Button(action: {
23+
self.isOpen.wrappedValue = false
24+
}) {
25+
Image(systemName: "xmark.circle.fill")
26+
.foregroundStyle(.secondary)
27+
.padding()
28+
}
29+
.buttonStyle(.plain)
30+
Text("Enabled Projects")
31+
Spacer()
32+
Button(action: {
33+
isAddingNewProject = true
34+
}) {
35+
Image(systemName: "plus.circle.fill")
36+
.foregroundStyle(.secondary)
37+
.padding()
38+
}
39+
.buttonStyle(.plain)
40+
}
41+
.background(Color(nsColor: .separatorColor))
42+
43+
List {
44+
ForEach(
45+
settings.suggestionFeatureEnabledProjectList,
46+
id: \.self
47+
) { project in
48+
HStack {
49+
Text(project)
50+
.contextMenu {
51+
Button("Remove") {
52+
settings.suggestionFeatureEnabledProjectList.removeAll(
53+
where: { $0 == project }
54+
)
55+
}
56+
}
57+
Spacer()
58+
59+
Button(action: {
60+
settings.suggestionFeatureEnabledProjectList.removeAll(
61+
where: { $0 == project }
62+
)
63+
}) {
64+
Image(systemName: "trash.fill")
65+
.foregroundStyle(.secondary)
66+
}
67+
.buttonStyle(.plain)
68+
}
69+
}
70+
}
71+
.removeBackground()
72+
.overlay {
73+
if settings.suggestionFeatureEnabledProjectList.isEmpty {
74+
Text("""
75+
Empty
76+
Add project with "+" button
77+
Or right clicking the circular widget
78+
""")
79+
.multilineTextAlignment(.center)
80+
}
81+
}
82+
}
83+
.focusable(false)
84+
.frame(width: 300, height: 400)
85+
.background(Color(nsColor: .windowBackgroundColor))
86+
.sheet(isPresented: $isAddingNewProject) {
87+
SuggestionFeatureAddEnabledProjectView(isOpen: $isAddingNewProject, settings: settings)
88+
}
89+
}
90+
}
91+
92+
struct SuggestionFeatureAddEnabledProjectView: View {
93+
var isOpen: Binding<Bool>
94+
var settings: SuggestionFeatureEnabledProjectListView.Settings
95+
@State var rootPath = ""
96+
97+
var body: some View {
98+
VStack {
99+
Text(
100+
"Enter the root path of the project. Do not use `~` to replace /Users/yourUserName."
101+
)
102+
TextField("Root path", text: $rootPath)
103+
HStack {
104+
Spacer()
105+
Button("Cancel") {
106+
isOpen.wrappedValue = false
107+
}
108+
Button("Add") {
109+
settings.suggestionFeatureEnabledProjectList.append(rootPath)
110+
isOpen.wrappedValue = false
111+
}
112+
}
113+
}
114+
.padding()
115+
.frame(minWidth: 500)
116+
.background(Color(nsColor: .windowBackgroundColor))
117+
}
118+
}
119+
120+
struct SuggestionFeatureEnabledProjectListView_Preview: PreviewProvider {
121+
static var previews: some View {
122+
SuggestionFeatureEnabledProjectListView(
123+
isOpen: .constant(true),
124+
settings: .init(suggestionFeatureEnabledProjectList: .init(wrappedValue: [
125+
"hello/2",
126+
"hello/3",
127+
"hello/4",
128+
], "SuggestionFeatureEnabledProjectListView_Preview"))
129+
)
130+
}
131+
}
132+

0 commit comments

Comments
 (0)