|
| 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