Repository Public License
1import SwiftUI23/// One option of a `LunarSegmentedControl`.4public struct LunarSegment<Value: Hashable>: Identifiable {5 public let value: Value6 public let title: String7 public let icon: LunarIcon?89 public var id: Value { value }1011 public init(_ title: String, value: Value, icon: LunarIcon? = nil) {12 self.title = title13 self.value = value14 self.icon = icon15 }16}1718/// A capsule segmented control whose thumb glides between options.19public struct LunarSegmentedControl<Value: Hashable>: View {20 @Environment(\.lunarTheme) private var theme21 @Environment(\.accessibilityReduceMotion) private var reduceMotion22 @Namespace private var namespace23 @Binding private var selection: Value24 private let segments: [LunarSegment<Value>]25 private let label: String2627 public init(_ label: String, selection: Binding<Value>, segments: [LunarSegment<Value>]) {28 self.label = label29 self._selection = selection30 self.segments = segments31 }3233 public var body: some View {34 HStack(spacing: 2) {35 ForEach(segments) { segment in36 let isSelected = segment.value == selection37 Button {38 withAnimation(LunarMotion.snappy.animation(reduceMotion: reduceMotion)) {39 selection = segment.value40 }41 } label: {42 HStack(spacing: LunarSpacing.unit + 1) {43 if let icon = segment.icon { LunarIconView(icon, size: 15) }44 Text(segment.title).lunarFont(.subheadline).lineLimit(1)45 }46 .foregroundStyle(isSelected ? theme.textPrimary : theme.textSecondary)47 .padding(.horizontal, LunarSpacing.sm)48 .frame(maxWidth: .infinity, minHeight: LunarMetrics.controlHeightCompact)49 .background {50 if isSelected {51 Capsule(style: .continuous)52 .fill(theme.elevatedSurface.mixed(with: theme.textPrimary, theme.isDark ? 0.10 : 0))53 .overlay { Capsule(style: .continuous).strokeBorder(theme.hairlineStrong, lineWidth: 1) }54 .shadow(color: theme.shadow.withAlpha(theme.shadow.alpha * 0.5).color, radius: 4, y: 1)55 .matchedGeometryEffect(id: "thumb", in: namespace)56 }57 }58 .lunarFocusRing(.capsule)59 .lunarHitTarget(.capsule)60 }61 .buttonStyle(LunarPressStyle())62 .accessibilityAddTraits(isSelected ? .isSelected : [])63 }64 }65 .padding(3)66 .background { Capsule(style: .continuous).fill(theme.sunkenSurface) }67 .overlay { Capsule(style: .continuous).strokeBorder(theme.hairline, lineWidth: 1) }68 .accessibilityElement(children: .contain)69 .accessibilityLabel(label)70 }71}7273/// A slider with a thin gradient track and a glass thumb. Fully adjustable with VoiceOver and the arrow keys.74public struct LunarSlider: View {75 @Environment(\.lunarTheme) private var theme76 @Environment(\.isEnabled) private var isEnabled77 @FocusState private var focused: Bool78 @Binding private var value: Double79 private let range: ClosedRange<Double>80 private let step: Double?81 private let label: String8283 public init(_ label: String, value: Binding<Double>, in range: ClosedRange<Double> = 0...1, step: Double? = nil) {84 self.label = label85 self._value = value86 self.range = range87 self.step = step88 }8990 private let thumb: CGFloat = 209192 public var body: some View {93 GeometryReader { proxy in94 let usable = max(1, proxy.size.width - thumb)95 let fraction = (min(max(value, range.lowerBound), range.upperBound) - range.lowerBound)96 / max(.leastNonzeroMagnitude, range.upperBound - range.lowerBound)97 ZStack(alignment: .leading) {98 Capsule().fill(theme.textPrimary.withAlpha(theme.isDark ? 0.12 : 0.10)).frame(height: 4)99 Capsule()100 .fill(LinearGradient(colors: [theme.accentSecondary.color, theme.accent.color],101 startPoint: .leading, endPoint: .trailing))102 .frame(width: thumb / 2 + usable * fraction, height: 4)103 Circle()104 .fill(theme.isDark ? theme.textPrimary : LunarRGBA.white)105 .overlay { Circle().strokeBorder(theme.accent.withAlpha(0.55), lineWidth: 1) }106 .shadow(color: theme.shadow.color, radius: 4, y: 1)107 .frame(width: thumb, height: thumb)108 .lunarFocusRing(.circle, isFocused: focused)109 .offset(x: usable * fraction)110 }111 .frame(maxHeight: .infinity)112 .contentShape(Rectangle())113 .gesture(DragGesture(minimumDistance: 0).onChanged { drag in114 set(fraction: (drag.location.x - thumb / 2) / usable)115 })116 }117 .frame(height: LunarMetrics.hitTarget)118 .opacity(isEnabled ? 1 : 0.45)119 .focusable()120 .focused($focused)121 .onKeyPress(.leftArrow) { nudge(-1); return .handled }122 .onKeyPress(.rightArrow) { nudge(1); return .handled }123 .accessibilityRepresentation {124 if let step {125 Slider(value: $value, in: range, step: step) { Text(label) }126 } else {127 Slider(value: $value, in: range) { Text(label) }128 }129 }130 }131132 private func set(fraction: Double) {133 var next = range.lowerBound + min(1, max(0, fraction)) * (range.upperBound - range.lowerBound)134 if let step, step > 0 { next = (next / step).rounded() * step }135 value = min(range.upperBound, max(range.lowerBound, next))136 }137138 private func nudge(_ direction: Double) {139 let increment = step ?? (range.upperBound - range.lowerBound) / 20140 value = min(range.upperBound, max(range.lowerBound, value + direction * increment))141 }142}143144/// The Lunar switch.145///146/// ```swift147/// Toggle("Block trackers", isOn: $on).toggleStyle(.lunar)148/// ```149public struct LunarToggleStyle: ToggleStyle {150 public init() {}151152 public func makeBody(configuration: Configuration) -> some View {153 LunarToggleBody(configuration: configuration)154 }155}156157extension ToggleStyle where Self == LunarToggleStyle {158 public static var lunar: LunarToggleStyle { LunarToggleStyle() }159}160161private struct LunarToggleBody: View {162 @Environment(\.lunarTheme) private var theme163 @Environment(\.isEnabled) private var isEnabled164 @Environment(\.accessibilityReduceMotion) private var reduceMotion165 @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor166 let configuration: ToggleStyleConfiguration167168 var body: some View {169 let isOn = configuration.isOn170 Button {171 withAnimation(LunarMotion.snappy.animation(reduceMotion: reduceMotion)) { configuration.isOn.toggle() }172 } label: {173 HStack(spacing: LunarSpacing.sm) {174 configuration.label175 .lunarFont(.body)176 .foregroundStyle(theme.textPrimary)177 .frame(maxWidth: .infinity, alignment: .leading)178 ZStack(alignment: isOn ? .trailing : .leading) {179 Capsule(style: .continuous)180 .fill(isOn ? AnyShapeStyle(LinearGradient(colors: [theme.accentSecondary.color,181 theme.accent.color],182 startPoint: .leading, endPoint: .trailing))183 : AnyShapeStyle(theme.textPrimary.withAlpha(theme.isDark ? 0.14 : 0.16)))184 .overlay { Capsule(style: .continuous).strokeBorder(theme.hairline, lineWidth: 1) }185 if differentiateWithoutColor {186 // I / O marks, as in the system switch with on/off labels.187 HStack {188 Capsule().fill(theme.onAccent).frame(width: 1.5, height: 9).opacity(isOn ? 1 : 0)189 Spacer()190 Circle().strokeBorder(theme.textSecondary, lineWidth: 1.5).frame(width: 8, height: 8)191 .opacity(isOn ? 0 : 1)192 }193 .padding(.horizontal, 8)194 }195 Circle()196 .fill(isOn ? theme.onAccent : (theme.isDark ? theme.textPrimary : LunarRGBA.white))197 .shadow(color: theme.shadow.color, radius: 3, y: 1)198 .padding(3)199 }200 .frame(width: 44, height: 26)201 .lunarFocusRing(.capsule)202 }203 .frame(minHeight: LunarMetrics.hitTarget)204 .contentShape(Rectangle())205 }206 .buttonStyle(.plain)207 .opacity(isEnabled ? 1 : 0.45)208 .accessibilityRepresentation {209 Toggle(isOn: configuration.$isOn) { configuration.label }210 }211 }212}213214#Preview("Controls") {215 @Previewable @State var mode = 0216 @Previewable @State var amount = 0.4217 @Previewable @State var on = true218 VStack(spacing: LunarSpacing.md) {219 LunarSegmentedControl("View", selection: $mode, segments: [220 LunarSegment("Tabs", value: 0), LunarSegment("Orbit", value: 1, icon: .glyph(.orbit)),221 LunarSegment("Memory", value: 2),222 ])223 LunarSlider("Zoom", value: $amount)224 Toggle("Block trackers", isOn: $on).toggleStyle(.lunar)225 }226 .padding(LunarSpacing.xl)227 .frame(width: 420)228 .background(LunarWindowBackground())229 .lunarTheme(LunarThemes.lunar.light)230}231