OpenTalon / app · opentalon.ai / native / Sources / OpenTalon / BoardDropdown.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | struct BoardDropdown<Value: Hashable>: View { |
| 4 | @Environment(\.palette) private var palette |
| 5 | @Environment(\.isEnabled) private var enabled |
| 6 | @Binding var selection: Value |
| 7 | let title: String |
| 8 | let symbol: String |
| 9 | let choices: [Value] |
| 10 | var compact = false |
| 11 | var symbolColor: Color? = nil |
| 12 | var agentBrand: ((Value) -> Agent)? = nil |
| 13 | let label: (Value) -> String |
| 14 | |
| 15 | @State private var isOpen = false |
| 16 | @State private var highlighted: Int? |
| 17 | @State private var hovered: Int? |
| 18 | @State private var triggerHovered = false |
| 19 | @FocusState private var menuFocused: Bool |
| 20 | |
| 21 | var body: some View { |
| 22 | Button { |
| 23 | highlighted = choices.firstIndex(of: selection) |
| 24 | isOpen.toggle() |
| 25 | } label: { |
| 26 | HStack(spacing: 10) { |
| 27 | Group { |
| 28 | if let agentBrand { |
| 29 | AgentBrandIcon(agent: agentBrand(selection)) |
| 30 | } else { |
| 31 | Image(systemName: symbol) |
| 32 | .font(.system(size: 13, weight: .medium)) |
| 33 | .foregroundStyle(symbolColor ?? palette.accent) |
| 34 | } |
| 35 | } |
| 36 | .frame(width: compact ? 16 : 29, height: compact ? 16 : 29) |
| 37 | .background(palette.accent.opacity(compact ? 0 : 0.11), in: .rect(cornerRadius: 8)) |
| 38 | if compact { |
| 39 | valueLabel |
| 40 | } else { |
| 41 | VStack(alignment: .leading, spacing: 3) { |
| 42 | Text(title) |
| 43 | .font(.system(size: 10, weight: .medium)) |
| 44 | .foregroundStyle(palette.muted) |
| 45 | valueLabel |
| 46 | } |
| 47 | } |
| 48 | Spacer(minLength: 0) |
| 49 | Image(systemName: "chevron.down") |
| 50 | .font(.system(size: 10, weight: .semibold)) |
| 51 | .rotationEffect(.degrees(isOpen ? 180 : 0)) |
| 52 | .foregroundStyle(palette.muted) |
| 53 | } |
| 54 | .padding(.horizontal, compact ? 12 : 10) |
| 55 | .frame(height: compact ? 34 : 50) |
| 56 | .glassControl( |
| 57 | in: RoundedRectangle(cornerRadius: compact ? 17 : 13), |
| 58 | tint: triggerHovered || isOpen ? palette.accent.opacity(0.16) : nil, |
| 59 | fill: triggerHovered || isOpen ? palette.hover : palette.card, |
| 60 | interactive: enabled |
| 61 | ) |
| 62 | .contentShape(.rect(cornerRadius: compact ? 17 : 13)) |
| 63 | } |
| 64 | .buttonStyle(.plain) |
| 65 | .onHover { triggerHovered = $0 } |
| 66 | .accessibilityLabel(title) |
| 67 | .accessibilityValue(label(selection)) |
| 68 | .disabled(choices.isEmpty) |
| 69 | .opacity(enabled && !choices.isEmpty ? 1 : 0.46) |
| 70 | .popover(isPresented: $isOpen, arrowEdge: .bottom) { options } |
| 71 | .onChange(of: isOpen) { _, open in |
| 72 | if !open { hovered = nil } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private var valueLabel: some View { |
| 77 | Text(label(selection)) |
| 78 | .font(.system(size: 13, weight: .semibold)) |
| 79 | .foregroundStyle(palette.text) |
| 80 | .lineLimit(1) |
| 81 | } |
| 82 | |
| 83 | private var options: some View { |
| 84 | ScrollViewReader { scroll in |
| 85 | ThemedScrollView(track: palette.card) { |
| 86 | VStack(spacing: 3) { |
| 87 | ForEach(choices.indices, id: \.self) { index in |
| 88 | choiceRow(index) |
| 89 | .id(index) |
| 90 | } |
| 91 | } |
| 92 | .padding(7) |
| 93 | } |
| 94 | .frame(width: 236, height: min(CGFloat(choices.count * 37 + 11), 310)) |
| 95 | .scrollBounceBehavior(.basedOnSize) |
| 96 | .surface(12) |
| 97 | .focusable() |
| 98 | .focused($menuFocused) |
| 99 | .focusEffectDisabled() |
| 100 | .onAppear { |
| 101 | menuFocused = true |
| 102 | if let highlighted { scroll.scrollTo(highlighted) } |
| 103 | } |
| 104 | .onKeyPress(.upArrow) { moveHighlight(by: -1, scroll: scroll) } |
| 105 | .onKeyPress(.downArrow) { moveHighlight(by: 1, scroll: scroll) } |
| 106 | .onKeyPress(.return) { |
| 107 | guard let highlighted else { return .ignored } |
| 108 | choose(highlighted) |
| 109 | return .handled |
| 110 | } |
| 111 | .onExitCommand { isOpen = false } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private func choiceRow(_ index: Int) -> some View { |
| 116 | let selected = choices[index] == selection |
| 117 | return Button { choose(index) } label: { |
| 118 | HStack(spacing: 9) { |
| 119 | if let agentBrand { AgentBrandIcon(agent: agentBrand(choices[index])) } |
| 120 | Text(label(choices[index])) |
| 121 | .font(.system(size: 13, weight: selected ? .semibold : .regular)) |
| 122 | .foregroundStyle(palette.text) |
| 123 | .lineLimit(1) |
| 124 | Spacer(minLength: 0) |
| 125 | if selected { |
| 126 | Image(systemName: "checkmark") |
| 127 | .font(.system(size: 11, weight: .semibold)) |
| 128 | .foregroundStyle(palette.accent) |
| 129 | .accessibilityHidden(true) |
| 130 | } |
| 131 | } |
| 132 | .padding(.horizontal, 11) |
| 133 | .frame(height: 34) |
| 134 | .background(rowFill(index, selected: selected), in: .rect(cornerRadius: 7)) |
| 135 | .contentShape(Rectangle()) |
| 136 | } |
| 137 | .buttonStyle(.plain) |
| 138 | .onHover { inside in |
| 139 | if inside { hovered = index } |
| 140 | else if hovered == index { hovered = nil } |
| 141 | } |
| 142 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 143 | } |
| 144 | |
| 145 | private func rowFill(_ index: Int, selected: Bool) -> Color { |
| 146 | if (hovered ?? highlighted) == index { return palette.hover } |
| 147 | return selected ? palette.active.opacity(0.5) : .clear |
| 148 | } |
| 149 | |
| 150 | private func moveHighlight(by offset: Int, scroll: ScrollViewProxy) -> KeyPress.Result { |
| 151 | guard !choices.isEmpty else { return .ignored } |
| 152 | let next = min(max((highlighted ?? 0) + offset, 0), choices.count - 1) |
| 153 | hovered = nil |
| 154 | highlighted = next |
| 155 | scroll.scrollTo(next) |
| 156 | return .handled |
| 157 | } |
| 158 | |
| 159 | private func choose(_ index: Int) { |
| 160 | guard choices.indices.contains(index) else { return } |
| 161 | selection = choices[index] |
| 162 | isOpen = false |
| 163 | } |
| 164 | } |