Repository Public License
1import SwiftUI23/// The three button roles of the system.4///5/// ```swift6/// Button("Open") { … }.buttonStyle(.lunar(.primary))7/// ```8public struct LunarButtonStyle: ButtonStyle {9 public enum Kind: String, Sendable, CaseIterable { case primary, secondary, destructive }10 public enum Size: String, Sendable, CaseIterable {11 case compact, regular, large1213 var height: CGFloat {14 switch self {15 case .compact: LunarMetrics.controlHeightCompact16 case .regular: LunarMetrics.controlHeight17 case .large: LunarMetrics.touchTarget18 }19 }20 var horizontalPadding: CGFloat { self == .compact ? LunarSpacing.sm : LunarSpacing.md + LunarSpacing.unit }21 var textStyle: LunarTextStyle { self == .compact ? .subheadline : .bodyEmphasized }22 }2324 let kind: Kind25 let size: Size26 let fullWidth: Bool2728 public init(_ kind: Kind = .primary, size: Size = .regular, fullWidth: Bool = false) {29 self.kind = kind30 self.size = size31 self.fullWidth = fullWidth32 }3334 public func makeBody(configuration: Configuration) -> some View {35 LunarButtonBody(configuration: configuration, kind: kind, size: size, fullWidth: fullWidth)36 }37}3839extension ButtonStyle where Self == LunarButtonStyle {40 /// A Lunar button: `.primary` (accent fill), `.secondary` (glass) or `.destructive`.41 public static func lunar(_ kind: LunarButtonStyle.Kind = .primary, size: LunarButtonStyle.Size = .regular,42 fullWidth: Bool = false) -> LunarButtonStyle {43 LunarButtonStyle(kind, size: size, fullWidth: fullWidth)44 }45}4647private struct LunarButtonBody: View {48 @Environment(\.lunarTheme) private var theme49 @Environment(\.isEnabled) private var isEnabled50 @Environment(\.accessibilityReduceMotion) private var reduceMotion51 let configuration: ButtonStyleConfiguration52 let kind: LunarButtonStyle.Kind53 let size: LunarButtonStyle.Size54 let fullWidth: Bool5556 var body: some View {57 LunarHoverReader { hovering in58 configuration.label59 .lunarFont(size.textStyle)60 .lineLimit(1)61 .foregroundStyle(foreground)62 .padding(.horizontal, size.horizontalPadding)63 .frame(maxWidth: fullWidth ? .infinity : nil, minHeight: size.height)64 .background { background(hovering: hovering) }65 .lunarFocusRing(.capsule)66 .lunarHitTarget(.capsule)67 .opacity(isEnabled ? 1 : 0.45)68 .scaleEffect(configuration.isPressed && !reduceMotion ? 0.975 : 1)69 .animation(LunarMotion.quick.animation(reduceMotion: reduceMotion), value: configuration.isPressed)70 .animation(LunarMotion.quick.animation(reduceMotion: reduceMotion), value: hovering)71 }72 }7374 private var foreground: LunarRGBA {75 switch kind {76 case .primary: theme.onAccent77 case .secondary: theme.textPrimary78 case .destructive: theme.danger79 }80 }8182 @ViewBuilder private func background(hovering: Bool) -> some View {83 let lift = configuration.isPressed ? -0.06 : (hovering ? 0.07 : 0)84 switch kind {85 case .primary:86 let top = theme.accent.mixed(with: .white, 0.10 + max(0, lift))87 let bottom = theme.accent.mixed(with: .black, lift < 0 ? 0.12 : 0)88 Capsule(style: .continuous)89 .fill(LinearGradient(colors: [top.color, bottom.color], startPoint: .top, endPoint: .bottom))90 .overlay {91 Capsule(style: .continuous)92 .strokeBorder(LinearGradient(colors: [Color.white.opacity(0.42), Color.white.opacity(0.04)],93 startPoint: .top, endPoint: .bottom), lineWidth: 1)94 }95 .shadow(color: theme.accent.withAlpha(theme.isDark ? 0.28 : 0.22).color, radius: 10, y: 4)96 case .secondary:97 Color.clear98 .lunarGlass(.capsule, style: .control, elevation: .flat)99 .overlay { Capsule(style: .continuous).fill(theme.textPrimary.withAlpha(max(0, lift) * 0.8)) }100 case .destructive:101 Capsule(style: .continuous)102 .fill(theme.danger.withAlpha((theme.isDark ? 0.16 : 0.10) + max(0, lift)))103 .overlay { Capsule(style: .continuous).strokeBorder(theme.danger.withAlpha(0.42), lineWidth: 1) }104 }105 }106}107108/// A round icon-only button with a tooltip and an accessibility label.109public struct LunarIconButton: View {110 public enum Style: String, Sendable, CaseIterable {111 /// No surface until hovered or active. For toolbars that already sit on glass.112 case plain113 /// A small glass disc. For buttons floating over content.114 case glass115 /// Accent-filled. One per view at most.116 case accent117 }118119 @Environment(\.lunarTheme) private var theme120 @Environment(\.isEnabled) private var isEnabled121 @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor122 private let icon: LunarIcon123 private let label: String124 private let style: Style125 private let isActive: Bool126 private let diameter: CGFloat127 private let action: () -> Void128129 /// - Parameters:130 /// - label: accessibility label, also shown as the tooltip on the Mac.131 /// - isActive: the "on" state of a toggling toolbar button (sidebar shown, shield enabled…).132 public init(_ icon: LunarIcon, label: String, style: Style = .plain, isActive: Bool = false,133 diameter: CGFloat = LunarMetrics.controlHeight, action: @escaping () -> Void) {134 self.icon = icon135 self.label = label136 self.style = style137 self.isActive = isActive138 self.diameter = diameter139 self.action = action140 }141142 public init(systemName: String, label: String, style: Style = .plain, isActive: Bool = false,143 diameter: CGFloat = LunarMetrics.controlHeight, action: @escaping () -> Void) {144 self.init(.symbol(systemName), label: label, style: style, isActive: isActive, diameter: diameter,145 action: action)146 }147148 public init(glyph: LunarGlyph, label: String, style: Style = .plain, isActive: Bool = false,149 diameter: CGFloat = LunarMetrics.controlHeight, action: @escaping () -> Void) {150 self.init(.glyph(glyph), label: label, style: style, isActive: isActive, diameter: diameter, action: action)151 }152153 public var body: some View {154 Button(action: action) {155 LunarHoverReader { hovering in156 LunarIconView(icon, size: diameter * 0.5)157 .foregroundStyle(foreground)158 .frame(width: diameter, height: diameter)159 .background { surface(hovering: hovering) }160 .overlay(alignment: .bottom) {161 // A shape cue for the active state, so it does not rely on colour alone.162 if isActive && differentiateWithoutColor {163 Capsule().fill(foreground).frame(width: diameter * 0.3, height: 2).offset(y: -3)164 }165 }166 .lunarFocusRing(.circle)167 .lunarHitTarget(.circle)168 }169 }170 .buttonStyle(LunarPressStyle())171 .opacity(isEnabled ? 1 : 0.4)172 .help(label)173 .accessibilityLabel(label)174 .accessibilityAddTraits(isActive ? .isSelected : [])175 }176177 private var foreground: LunarRGBA {178 switch style {179 case .accent: theme.onAccent180 case .plain, .glass: isActive ? theme.accent : theme.textSecondary.mixed(with: theme.textPrimary, 0.5)181 }182 }183184 @ViewBuilder private func surface(hovering: Bool) -> some View {185 switch style {186 case .plain:187 Circle().fill(isActive ? theme.selection : theme.textPrimary.withAlpha(hovering ? 0.08 : 0))188 case .glass:189 Color.clear.lunarGlass(.circle, style: .control, elevation: .raised, interactive: true)190 .overlay { Circle().fill(isActive ? theme.selection : theme.textPrimary.withAlpha(hovering ? 0.06 : 0)) }191 case .accent:192 Circle()193 .fill(LinearGradient(colors: [theme.accent.mixed(with: .white, hovering ? 0.2 : 0.1).color,194 theme.accent.color], startPoint: .top, endPoint: .bottom))195 .overlay { Circle().strokeBorder(Color.white.opacity(0.3), lineWidth: 1) }196 }197 }198}199200/// Press feedback shared by the system's custom controls: a slight dim and scale, no scale under Reduce Motion.201public struct LunarPressStyle: ButtonStyle {202 public init() {}203204 public func makeBody(configuration: Configuration) -> some View {205 LunarPressBody(configuration: configuration)206 }207208 private struct LunarPressBody: View {209 @Environment(\.accessibilityReduceMotion) private var reduceMotion210 let configuration: ButtonStyleConfiguration211212 var body: some View {213 configuration.label214 .opacity(configuration.isPressed ? 0.72 : 1)215 .scaleEffect(configuration.isPressed && !reduceMotion ? 0.96 : 1)216 .animation(LunarMotion.quick.animation(reduceMotion: reduceMotion), value: configuration.isPressed)217 }218 }219}220221/// A pill that toggles: filters, modes, quick options.222public struct LunarChip: View {223 @Environment(\.lunarTheme) private var theme224 @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor225 private let title: String226 private let icon: LunarIcon?227 private let isSelected: Bool228 private let action: () -> Void229230 /// A chip bound to a Boolean.231 public init(_ title: String, icon: LunarIcon? = nil, isOn: Binding<Bool>) {232 self.init(title, icon: icon, isSelected: isOn.wrappedValue) { isOn.wrappedValue.toggle() }233 }234235 /// A chip whose selection is owned by the caller (single-choice groups).236 public init(_ title: String, icon: LunarIcon? = nil, isSelected: Bool, action: @escaping () -> Void) {237 self.title = title238 self.icon = icon239 self.isSelected = isSelected240 self.action = action241 }242243 public var body: some View {244 Button(action: action) {245 HStack(spacing: LunarSpacing.unit + 2) {246 if isSelected && differentiateWithoutColor {247 LunarIconView(.symbol("checkmark"), size: 14)248 } else if let icon {249 LunarIconView(icon, size: 16)250 }251 Text(title).lunarFont(.subheadline).lineLimit(1)252 }253 .foregroundStyle(isSelected ? theme.accent : theme.textSecondary)254 .padding(.horizontal, LunarSpacing.sm)255 .frame(minHeight: LunarMetrics.controlHeightCompact + LunarSpacing.unit)256 .background {257 Capsule(style: .continuous).fill(isSelected ? theme.selection : theme.textPrimary.withAlpha(0.05))258 }259 .overlay {260 Capsule(style: .continuous)261 .strokeBorder(isSelected ? theme.accent.withAlpha(0.55) : theme.hairline, lineWidth: 1)262 }263 .lunarFocusRing(.capsule)264 .lunarHitTarget(.capsule)265 }266 .buttonStyle(LunarPressStyle())267 .lunarAnimation(.snappy, value: isSelected)268 .accessibilityAddTraits(isSelected ? .isSelected : [])269 }270}271272#Preview("Buttons") {273 VStack(spacing: LunarSpacing.md) {274 Button("Continue") {}.buttonStyle(.lunar(.primary))275 Button("Not now") {}.buttonStyle(.lunar(.secondary))276 Button("Clear history") {}.buttonStyle(.lunar(.destructive))277 HStack {278 LunarIconButton(glyph: .orbit, label: "Orbit") {}279 LunarIconButton(glyph: .shield, label: "Shield", style: .glass, isActive: true) {}280 LunarIconButton(systemName: "plus", label: "New tab", style: .accent) {}281 }282 LunarChip("Private", icon: .glyph(.ghost), isSelected: true) {}283 }284 .padding(LunarSpacing.xl)285 .background(LunarWindowBackground())286 .lunarTheme(LunarThemes.lunar.dark)287}288