OpenTalon / app · opentalon.ai / native / Sources / OpenTalon / Appearance.swift
| 1 | import CompanionCore |
| 2 | import SwiftUI |
| 3 | |
| 4 | extension Color { |
| 5 | init(hex: UInt32) { |
| 6 | self.init( |
| 7 | red: Double(hex >> 16 & 255) / 255, |
| 8 | green: Double(hex >> 8 & 255) / 255, |
| 9 | blue: Double(hex & 255) / 255 |
| 10 | ) |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | struct Palette { |
| 15 | var theme: Theme |
| 16 | var dark: Bool { theme == .graphite } |
| 17 | var paper: Bool { theme == .paper } |
| 18 | private var colors: OpenTalonColors { OpenTalonColors(theme: theme) } |
| 19 | var canvas: Color { colors.canvas } |
| 20 | var sidebar: Color { shade(0xeae3d9, 0x101114, 0xeef0f3) } |
| 21 | var card: Color { colors.card } |
| 22 | var panel: Color { shade(0xfaf7f1, 0x191a1e, 0xffffff) } |
| 23 | var text: Color { colors.text } |
| 24 | var muted: Color { colors.muted } |
| 25 | var scrollThumb: Color { shade(0x927969, 0x767985, 0x76818f) } |
| 26 | var line: Color { colors.line } |
| 27 | var well: Color { shade(0xece6dc, 0x18191d, 0xf0f2f5) } |
| 28 | var hover: Color { shade(0xe4dacd, 0x24252b, 0xe4e7ec) } |
| 29 | var active: Color { shade(0xe7cdbb, 0x303139, 0xdde1e7) } |
| 30 | var accent: Color { colors.accent } |
| 31 | var actionAccent: Color { colors.actionAccent } |
| 32 | var accentText: Color { shade(0xfffdf9, 0x1e1f25, 0xffffff) } |
| 33 | var danger: Color { colors.danger } |
| 34 | |
| 35 | private func shade(_ paperValue: UInt32, _ darkValue: UInt32, _ lightValue: UInt32) -> Color { |
| 36 | Color(hex: dark ? darkValue : paper ? paperValue : lightValue) |
| 37 | } |
| 38 | |
| 39 | func display(_ size: CGFloat, weight: Font.Weight = .semibold) -> Font { |
| 40 | .system(size: size, weight: weight, design: paper ? .serif : .default) |
| 41 | } |
| 42 | |
| 43 | func flow(_ status: WorkStatus) -> Color { |
| 44 | switch status { |
| 45 | case .todo: shade(0xcfc6b8, 0x4a4c55, 0xd3d7de) |
| 46 | case .sent: shade(0xc99b45, 0xc9a45c, 0xc79a3b) |
| 47 | case .working: shade(0xb9603a, 0x8ea4dc, 0x4f6fae) |
| 48 | case .done: colors.done |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private struct PaletteKey: EnvironmentKey { |
| 54 | static let defaultValue = Palette(theme: .light) |
| 55 | } |
| 56 | |
| 57 | extension EnvironmentValues { |
| 58 | var palette: Palette { |
| 59 | get { self[PaletteKey.self] } |
| 60 | set { self[PaletteKey.self] = newValue } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | struct Surface: ViewModifier { |
| 65 | @Environment(\.palette) private var palette |
| 66 | var radius: CGFloat |
| 67 | var fill: Color? |
| 68 | func body(content: Content) -> some View { |
| 69 | content |
| 70 | .background(fill ?? palette.card, in: .rect(cornerRadius: radius)) |
| 71 | .overlay(RoundedRectangle(cornerRadius: radius).strokeBorder(palette.line).allowsHitTesting(false)) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | extension View { |
| 76 | func surface(_ radius: CGFloat = 8, fill: Color? = nil) -> some View { |
| 77 | modifier(Surface(radius: radius, fill: fill)) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | struct GlassActionStyle: ButtonStyle { |
| 82 | @Environment(\.palette) private var palette |
| 83 | @Environment(\.isEnabled) private var enabled |
| 84 | var prominent = false |
| 85 | var height: CGFloat = 30 |
| 86 | |
| 87 | @ViewBuilder func makeBody(configuration: Configuration) -> some View { |
| 88 | let destructive = configuration.role == .destructive |
| 89 | let emphasized = prominent && !destructive |
| 90 | let label = configuration.label |
| 91 | .font(.system(size: 12, weight: .semibold)) |
| 92 | .padding(.horizontal, prominent ? 15 : 12) |
| 93 | .frame(minHeight: height) |
| 94 | if enabled { |
| 95 | label |
| 96 | .foregroundStyle(emphasized ? palette.accentText : destructive ? palette.danger : palette.text) |
| 97 | .glassControl( |
| 98 | in: Capsule(), |
| 99 | tint: emphasized ? palette.accent : nil, |
| 100 | fill: emphasized ? palette.accent : configuration.isPressed ? palette.hover : palette.card |
| 101 | ) |
| 102 | .contentShape(Capsule()) |
| 103 | .opacity(configuration.isPressed ? 0.8 : 1) |
| 104 | } else { |
| 105 | label.foregroundStyle(palette.muted) |
| 106 | .overlay { Capsule().strokeBorder(palette.line).allowsHitTesting(false) } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | struct GlassToggleStyle: ToggleStyle { |
| 112 | @Environment(\.palette) private var palette |
| 113 | @Environment(\.isEnabled) private var enabled |
| 114 | |
| 115 | func makeBody(configuration: Configuration) -> some View { |
| 116 | Button { configuration.isOn.toggle() } label: { |
| 117 | HStack(spacing: 9) { |
| 118 | Capsule() |
| 119 | .fill(configuration.isOn ? palette.accent : palette.well) |
| 120 | .overlay { Capsule().strokeBorder(palette.line.opacity(configuration.isOn ? 0 : 1)) } |
| 121 | .overlay(alignment: configuration.isOn ? .trailing : .leading) { |
| 122 | Circle() |
| 123 | .fill(configuration.isOn ? palette.accentText : palette.text.opacity(0.53)) |
| 124 | .frame(width: 12, height: 12) |
| 125 | .padding(3) |
| 126 | } |
| 127 | .frame(width: 31, height: 18) |
| 128 | configuration.label |
| 129 | .foregroundStyle(palette.text) |
| 130 | } |
| 131 | .frame(minHeight: 26) |
| 132 | .contentShape(Rectangle()) |
| 133 | } |
| 134 | .buttonStyle(.plain) |
| 135 | .opacity(enabled ? 1 : 0.46) |
| 136 | .accessibilityRepresentation { |
| 137 | Toggle(isOn: configuration.$isOn) { configuration.label } |
| 138 | .toggleStyle(.switch) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | struct IconButton: View { |
| 144 | var symbol: String |
| 145 | var label: String |
| 146 | var action: () -> Void |
| 147 | @Environment(\.palette) private var palette |
| 148 | @State private var hovered = false |
| 149 | |
| 150 | var body: some View { |
| 151 | Button(action: action) { |
| 152 | Image(systemName: symbol) |
| 153 | .font(.system(size: 13, weight: .medium)) |
| 154 | .frame(width: 28, height: 28) |
| 155 | .background(hovered ? palette.hover : .clear, in: .rect(cornerRadius: 6)) |
| 156 | .contentShape(Rectangle()) |
| 157 | } |
| 158 | .buttonStyle(.plain) |
| 159 | .onHover { hovered = $0 } |
| 160 | .help(label) |
| 161 | .accessibilityLabel(label) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | struct PriorityBadge: View { |
| 166 | @Environment(\.palette) private var palette |
| 167 | let priority: Priority |
| 168 | var body: some View { |
| 169 | let color = priority == .none ? palette.muted : priority.color(palette.dark) |
| 170 | Label(priority.label, systemImage: priority == .none ? "minus" : "chart.bar.fill") |
| 171 | .font(.system(size: 10, weight: .semibold)) |
| 172 | .padding(.horizontal, 6) |
| 173 | .padding(.vertical, 3) |
| 174 | .foregroundStyle(color) |
| 175 | .background(color.opacity(0.10), in: .rect(cornerRadius: 4)) |
| 176 | .fixedSize() |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | struct ProjectIcon: View { |
| 181 | let key: String? |
| 182 | var size: CGFloat = 14 |
| 183 | |
| 184 | var body: some View { |
| 185 | Group { |
| 186 | if key == "feather" { |
| 187 | FeatherOutline().stroke(style: StrokeStyle(lineWidth: size / 12, lineCap: .round, lineJoin: .round)) |
| 188 | } else { |
| 189 | Image(systemName: BoardIcons.symbol(key)).font(.system(size: size)) |
| 190 | } |
| 191 | } |
| 192 | .frame(width: size, height: size) |
| 193 | .accessibilityHidden(true) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private struct FeatherOutline: Shape { |
| 198 | func path(in rect: CGRect) -> Path { |
| 199 | var outline = Path() |
| 200 | outline.move(to: CGPoint(x: 20.2, y: 12.2)) |
| 201 | outline.addCurve(to: CGPoint(x: 11.8, y: 3.8), control1: CGPoint(x: 26, y: 6.4), control2: CGPoint(x: 17.6, y: -2)) |
| 202 | outline.addLines([CGPoint(x: 2, y: 13.5), CGPoint(x: 2, y: 22), CGPoint(x: 10.5, y: 22), CGPoint(x: 20.2, y: 12.2)]) |
| 203 | outline.move(to: CGPoint(x: 16, y: 8)) |
| 204 | outline.addLine(to: CGPoint(x: 2, y: 22)) |
| 205 | outline.move(to: CGPoint(x: 9, y: 15)) |
| 206 | outline.addLine(to: CGPoint(x: 17.4, y: 15)) |
| 207 | return outline.applying(CGAffineTransform(scaleX: rect.width / 24, y: rect.height / 24)) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | enum Artwork { |
| 212 | static let wordmark = Bundle.main.url(forResource: "wordmark", withExtension: "png").flatMap(NSImage.init(contentsOf:)) |
| 213 | } |
| 214 | |
| 215 | struct Wordmark: View { |
| 216 | @Environment(\.palette) private var palette |
| 217 | var body: some View { |
| 218 | Group { |
| 219 | if let wordmark = Artwork.wordmark { |
| 220 | Image(nsImage: wordmark).renderingMode(.template).resizable().scaledToFit() |
| 221 | .frame(width: 181, height: 38) |
| 222 | } else { |
| 223 | Text("OpenTalon").font(.system(size: 24, weight: .black)) |
| 224 | } |
| 225 | } |
| 226 | .foregroundStyle(palette.dark ? .white : .black) |
| 227 | .accessibilityElement(children: .ignore) |
| 228 | .accessibilityLabel("OpenTalon") |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | extension View { |
| 233 | func glassPrimary() -> some View { |
| 234 | buttonStyle(GlassActionStyle(prominent: true, height: 35)) |
| 235 | } |
| 236 | |
| 237 | func glassControl<S: Shape>(in shape: S, tint: Color? = nil, fill: Color, interactive: Bool = true) -> some View { |
| 238 | modifier(GlassControlSurface(shape: shape, tint: tint, fill: fill, interactive: interactive)) |
| 239 | } |
| 240 | |
| 241 | @ViewBuilder |
| 242 | func glassSearchSurface() -> some View { |
| 243 | if #available(macOS 26, *) { |
| 244 | glassEffect(.regular, in: .capsule) |
| 245 | } else { |
| 246 | surface(7) |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private struct GlassControlSurface<S: Shape>: ViewModifier { |
| 252 | @Environment(\.palette) private var palette |
| 253 | var shape: S |
| 254 | var tint: Color? |
| 255 | var fill: Color |
| 256 | var interactive: Bool |
| 257 | |
| 258 | @ViewBuilder |
| 259 | func body(content: Content) -> some View { |
| 260 | if #available(macOS 26, *) { |
| 261 | content.glassEffect(.regular.tint(tint).interactive(interactive), in: shape) |
| 262 | } else { |
| 263 | content.background(fill, in: shape) |
| 264 | .overlay { shape.stroke(palette.line, lineWidth: 1).allowsHitTesting(false) } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | struct GlassCommandGroup<Content: View>: View { |
| 270 | @ViewBuilder var content: () -> Content |
| 271 | var body: some View { |
| 272 | if #available(macOS 26, *) { |
| 273 | GlassEffectContainer(spacing: 5) { content() } |
| 274 | } else { |
| 275 | content() |
| 276 | } |
| 277 | } |
| 278 | } |