Branchwell

Jotley / app / macOS / Views / SettingsView+ThemeColorDropdown.swift

1import SwiftUI
2
3struct SettingsDropdown<Value: Hashable>: View {
4 let title: String
5 @Binding var selection: Value
6 let options: [Value]
7 var placeholder = ""
8 let optionTitle: (Value) -> String
9 var swatch: (Value) -> Color? = { _ in nil }
10
11 @State private var isShowing = false
12 @State private var hoveredOption: Value?
13 @FocusState private var focusedOption: Value?
14
15 private let width: CGFloat = 220
16 private let rowHeight: CGFloat = 34
17
18 private var selectedTitle: String {
19 options.contains(selection) ? optionTitle(selection) : placeholder
20 }
21
22 var body: some View {
23 HStack {
24 Text(title)
25 .frame(width: 120, alignment: .leading)
26 .accessibilityHidden(true)
27 Button {
28 isShowing.toggle()
29 } label: {
30 HStack(spacing: 8) {
31 if options.contains(selection) { colorSwatch(selection) }
32 Text(selectedTitle).lineLimit(1)
33 Spacer(minLength: 4)
34 Image(systemName: "chevron.up.chevron.down")
35 .font(.system(size: 10))
36 .foregroundStyle(.secondary)
37 .accessibilityHidden(true)
38 }
39 .font(.system(size: 13))
40 .foregroundStyle(.primary)
41 .padding(.horizontal, 10)
42 .padding(.vertical, 6)
43 .frame(width: width)
44 .background(Color(nsColor: .controlBackgroundColor), in: RoundedRectangle(cornerRadius: 6))
45 .overlay {
46 RoundedRectangle(cornerRadius: 6).strokeBorder(.gray.opacity(0.3))
47 }
48 .contentShape(Rectangle())
49 }
50 .buttonStyle(.plain)
51 .accessibilityLabel(title)
52 .accessibilityValue(selectedTitle)
53 .disabled(options.isEmpty)
54 .popover(isPresented: $isShowing, arrowEdge: .bottom) {
55 optionList
56 }
57 }
58 }
59
60 private var optionList: some View {
61 ScrollViewReader { proxy in
62 ScrollView {
63 VStack(spacing: 0) {
64 ForEach(options, id: \.self) { option in
65 optionRow(option)
66 .id(option)
67 }
68 }
69 .padding(6)
70 }
71 .scrollBounceBehavior(.basedOnSize)
72 .frame(width: width, height: rowHeight * CGFloat(min(options.count, 8)) + 12)
73 .onAppear {
74 hoveredOption = nil
75 focusedOption = options.contains(selection) ? selection : options.first
76 }
77 .onChange(of: focusedOption, initial: true) { _, option in
78 if let option { proxy.scrollTo(option) }
79 }
80 .onMoveCommand { direction in
81 guard direction == .up || direction == .down,
82 let option = focusedOption,
83 let index = options.firstIndex(of: option) else { return }
84 let next = index + (direction == .up ? -1 : 1)
85 if options.indices.contains(next) { focusedOption = options[next] }
86 }
87 }
88 }
89
90 private func optionRow(_ option: Value) -> some View {
91 let accent = Color.themePrimaryAccent
92 return Button {
93 selection = option
94 isShowing = false
95 } label: {
96 HStack(spacing: 10) {
97 colorSwatch(option)
98 Text(optionTitle(option)).lineLimit(1)
99 Spacer(minLength: 4)
100 if option == selection {
101 Image(systemName: "checkmark")
102 .font(.system(size: 12, weight: .medium))
103 .foregroundStyle(accent)
104 .accessibilityHidden(true)
105 }
106 }
107 .font(.system(size: 13))
108 .foregroundStyle(.primary)
109 .padding(.horizontal, 12)
110 .frame(height: rowHeight)
111 .background(
112 hoveredOption == option || focusedOption == option ? Color.themeSelectionBackground : .clear,
113 in: RoundedRectangle(cornerRadius: 6)
114 )
115 .contentShape(Rectangle())
116 }
117 .buttonStyle(.plain)
118 .focused($focusedOption, equals: option)
119 .accessibilityAddTraits(option == selection ? .isSelected : [])
120 .onHover { hovering in
121 if hovering { hoveredOption = option }
122 else if hoveredOption == option { hoveredOption = nil }
123 }
124 }
125
126 @ViewBuilder
127 private func colorSwatch(_ option: Value) -> some View {
128 if let color = swatch(option) {
129 Circle()
130 .fill(color)
131 .frame(width: 12, height: 12)
132 .accessibilityHidden(true)
133 }
134 }
135}