Repository Public License
1import SwiftUI23/// Audio state of a tab.4public enum LunarTabAudio: String, Sendable, CaseIterable {5 case silent, playing, muted6}78/// A tab in the tab strip or the vertical tab list: favicon slot, title, state marks and a close button.9public struct LunarTabChip: View {10 public enum Layout: String, Sendable, CaseIterable {11 /// A pill in a horizontal strip.12 case horizontal13 /// A full-width row in the sidebar.14 case vertical15 }1617 @Environment(\.lunarTheme) private var theme18 @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor19 private let title: String20 private let favicon: Image?21 private let tint: LunarTint?22 private let isSelected: Bool23 private let isPinned: Bool24 private let isLoading: Bool25 private let audio: LunarTabAudio26 private let layout: Layout27 private let onSelect: () -> Void28 private let onClose: (() -> Void)?29 private let onToggleMute: (() -> Void)?3031 /// - Parameters:32 /// - favicon: the site icon; when `nil` a monogram disc is drawn from the title.33 /// - tint: workspace tint of the monogram disc and the selection mark.34 /// - isPinned: pinned tabs collapse to their favicon in the horizontal layout and show a pin in the vertical.35 /// - onClose: `nil` hides the close button (pinned tabs, single-tab windows).36 /// - onToggleMute: when set, the audio mark is a button.37 public init(title: String, favicon: Image? = nil, tint: LunarTint? = nil, isSelected: Bool = false,38 isPinned: Bool = false, isLoading: Bool = false, audio: LunarTabAudio = .silent,39 layout: Layout = .horizontal, onSelect: @escaping () -> Void, onClose: (() -> Void)? = nil,40 onToggleMute: (() -> Void)? = nil) {41 self.title = title42 self.favicon = favicon43 self.tint = tint44 self.isSelected = isSelected45 self.isPinned = isPinned46 self.isLoading = isLoading47 self.audio = audio48 self.layout = layout49 self.onSelect = onSelect50 self.onClose = onClose51 self.onToggleMute = onToggleMute52 }5354 private var collapsed: Bool { isPinned && layout == .horizontal }55 private var shape: LunarShape { layout == .horizontal ? .capsule : .xs }5657 public var body: some View {58 LunarHoverReader { hovering in59 HStack(spacing: 0) {60 Button(action: onSelect) {61 HStack(spacing: LunarSpacing.xs) {62 faviconSlot63 if !collapsed {64 Text(title)65 .lunarFont(isSelected ? .subheadline : .callout)66 .foregroundStyle(isSelected ? theme.textPrimary : theme.textSecondary)67 .lineLimit(1)68 .frame(maxWidth: .infinity, alignment: .leading)69 if isPinned {70 LunarIconView(.symbol("pin.fill"), size: 11).foregroundStyle(theme.textTertiary)71 }72 }73 }74 .padding(.leading, LunarSpacing.xs + 2)75 .padding(.trailing, collapsed ? LunarSpacing.xs + 2 : LunarSpacing.unit)76 .frame(minHeight: LunarMetrics.tabChipHeight)77 .contentShape(Rectangle())78 }79 .buttonStyle(LunarPressStyle())80 .accessibilityLabel(title)81 .accessibilityValue(stateDescription)82 .accessibilityAddTraits(isSelected ? .isSelected : [])8384 if !collapsed {85 audioMark86 if let onClose, isSelected || hovering || layout == .vertical {87 Button(action: onClose) {88 LunarIconView(.symbol("xmark"), size: 12)89 .foregroundStyle(theme.textSecondary)90 .frame(width: 24, height: 24)91 .background { Circle().fill(theme.textPrimary.withAlpha(hovering ? 0.08 : 0)) }92 .lunarFocusRing(.circle)93 .contentShape(Circle())94 }95 .buttonStyle(LunarPressStyle())96 .help("Close tab")97 .accessibilityLabel("Close \(title)")98 .opacity(layout == .vertical && !isSelected && !hovering ? 0.55 : 1)99 }100 }101 }102 .padding(.trailing, collapsed ? 0 : LunarSpacing.unit + 2)103 .frame(minWidth: collapsed ? LunarMetrics.tabChipHeight : (layout == .horizontal ? 132 : nil),104 maxWidth: collapsed ? LunarMetrics.tabChipHeight : (layout == .horizontal ? 220 : .infinity),105 minHeight: LunarMetrics.hitTarget > LunarMetrics.tabChipHeight ? LunarMetrics.hitTarget106 : LunarMetrics.tabChipHeight)107 .background { background(hovering: hovering) }108 .lunarFocusRing(shape)109 .overlay(alignment: layout == .horizontal ? .bottom : .leading) {110 if isSelected && differentiateWithoutColor {111 Capsule().fill(theme.accent)112 .frame(width: layout == .horizontal ? 18 : 3, height: layout == .horizontal ? 3 : 18)113 .padding(2)114 }115 }116 }117 .lunarAnimation(.snappy, value: isSelected)118 .accessibilityElement(children: .contain)119 }120121 private var stateDescription: String {122 var parts: [String] = []123 if isPinned { parts.append("Pinned") }124 if isLoading { parts.append("Loading") }125 switch audio {126 case .playing: parts.append("Playing audio")127 case .muted: parts.append("Muted")128 case .silent: break129 }130 return parts.joined(separator: ", ")131 }132133 @ViewBuilder private func background(hovering: Bool) -> some View {134 if isSelected {135 Color.clear.lunarGlass(shape, style: .control, elevation: .raised, tint: tint)136 } else {137 shape.fill(theme.textPrimary.withAlpha(hovering ? 0.06 : 0))138 }139 }140141 private var faviconSlot: some View {142 ZStack {143 if let favicon {144 favicon.resizable().interpolation(.high).scaledToFit()145 .frame(width: 16, height: 16)146 .clipShape(RoundedRectangle(cornerRadius: 4, style: .continuous))147 } else {148 LunarMonogram(title, tint: tint, size: 18)149 }150 if isLoading {151 LunarProgressRing(value: nil, label: "Loading", size: 24, lineWidth: 1.6)152 }153 }154 .frame(width: 24, height: 24)155 .accessibilityHidden(true)156 }157158 @ViewBuilder private var audioMark: some View {159 if audio != .silent {160 let symbol = audio == .playing ? "speaker.wave.2.fill" : "speaker.slash.fill"161 let mark = LunarIconView(.symbol(symbol), size: 13)162 .foregroundStyle(audio == .playing ? theme.accent : theme.textTertiary)163 .frame(width: 22, height: 24)164 if let onToggleMute {165 Button(action: onToggleMute) { mark.contentShape(Rectangle()) }166 .buttonStyle(LunarPressStyle())167 .help(audio == .playing ? "Mute tab" : "Unmute tab")168 .accessibilityLabel(audio == .playing ? "Mute \(title)" : "Unmute \(title)")169 } else {170 mark171 }172 }173 }174}175176/// The favicon fallback: the first letter of a title on a tinted disc.177public struct LunarMonogram: View {178 @Environment(\.lunarTheme) private var theme179 private let text: String180 private let tint: LunarTint?181 private let size: CGFloat182183 public init(_ text: String, tint: LunarTint? = nil, size: CGFloat = 18) {184 self.text = text185 self.tint = tint186 self.size = size187 }188189 public var body: some View {190 let color = tint.map(theme.color(for:)) ?? theme.accent191 let letter = text.first(where: { $0.isLetter || $0.isNumber }).map { String($0).uppercased() } ?? "•"192 RoundedRectangle(cornerRadius: size * 0.3, style: .continuous)193 .fill(color.withAlpha(theme.isDark ? 0.22 : 0.16))194 .overlay {195 RoundedRectangle(cornerRadius: size * 0.3, style: .continuous)196 .strokeBorder(color.withAlpha(0.45), lineWidth: 1)197 }198 .overlay {199 Text(letter)200 .font(.system(size: size * 0.56, weight: .bold, design: .rounded))201 .foregroundStyle(color)202 }203 .frame(width: size, height: size)204 .accessibilityHidden(true)205 }206}207208/// A workspace drawn as a small planet in its tint: a lit sphere, optionally ringed, glowing when active.209public struct LunarPlanet: View {210 @Environment(\.lunarTheme) private var theme211 private let tint: LunarTint212 private let size: CGFloat213 private let isActive: Bool214 private let hasRing: Bool215216 public init(tint: LunarTint, size: CGFloat = 20, isActive: Bool = false, hasRing: Bool = false) {217 self.tint = tint218 self.size = size219 self.isActive = isActive220 self.hasRing = hasRing221 }222223 public var body: some View {224 let color = theme.color(for: tint)225 let ring = Ellipse().size(width: size * 1.7, height: size * 0.5)226 .offset(x: -size * 0.35, y: size * 0.25)227 ZStack {228 if isActive {229 Circle()230 .fill(RadialGradient(colors: [color.withAlpha(theme.isDark ? 0.5 : 0.32).color,231 color.withAlpha(0).color],232 center: .center, startRadius: size * 0.3, endRadius: size * 0.95))233 .frame(width: size * 1.9, height: size * 1.9)234 }235 if hasRing {236 ring.stroke(color.withAlpha(0.55), lineWidth: max(1, size / 22))237 .frame(width: size, height: size).rotationEffect(.degrees(-18))238 }239 Circle()240 .fill(RadialGradient(241 stops: [.init(color: color.mixed(with: .white, 0.62).color, location: 0),242 .init(color: color.color, location: 0.45),243 .init(color: color.mixed(with: theme.isDark ? theme.windowBackground : .black,244 theme.isDark ? 0.62 : 0.38).color, location: 1)],245 center: UnitPoint(x: 0.32, y: 0.26), startRadius: 0, endRadius: size * 0.82))246 .overlay { Circle().strokeBorder(Color.white.opacity(theme.isDark ? 0.22 : 0.4), lineWidth: 0.75) }247 .frame(width: size, height: size)248 if hasRing {249 // The near half of the ring passes in front of the sphere.250 ring.trim(from: 0, to: 0.5)251 .stroke(color.mixed(with: .white, 0.35), lineWidth: max(1, size / 22))252 .frame(width: size, height: size).rotationEffect(.degrees(-18))253 }254 }255 .frame(width: size, height: size)256 .accessibilityHidden(true)257 }258}259260/// A satellite: a workspace (or a group of pages) shown as a planet with its label.261public struct LunarSatelliteChip: View {262 @Environment(\.lunarTheme) private var theme263 @Environment(\.accessibilityDifferentiateWithoutColor) private var differentiateWithoutColor264 private let label: String265 private let tint: LunarTint266 private let isActive: Bool267 private let count: Int?268 private let action: (() -> Void)?269270 /// - Parameter count: number of pages in the satellite, shown with tabular digits.271 public init(_ label: String, tint: LunarTint, isActive: Bool = false, count: Int? = nil,272 action: (() -> Void)? = nil) {273 self.label = label274 self.tint = tint275 self.isActive = isActive276 self.count = count277 self.action = action278 }279280 public var body: some View {281 if let action {282 Button(action: action) { chip.lunarFocusRing(.capsule).lunarHitTarget(.capsule) }283 .buttonStyle(LunarPressStyle())284 .accessibilityLabel(accessibilityText)285 .accessibilityAddTraits(isActive ? .isSelected : [])286 } else {287 chip.accessibilityElement(children: .ignore).accessibilityLabel(accessibilityText)288 }289 }290291 private var accessibilityText: String {292 var text = "\(label), \(tint.displayName)"293 if let count { text += ", \(count) \(count == 1 ? "page" : "pages")" }294 return text295 }296297 private var chip: some View {298 let color = theme.color(for: tint)299 return HStack(spacing: LunarSpacing.xs) {300 LunarPlanet(tint: tint, size: 16, isActive: isActive)301 Text(label)302 .lunarFont(.subheadline)303 .foregroundStyle(isActive ? theme.textPrimary : theme.textSecondary)304 .lineLimit(1)305 if let count {306 Text("\(count)").lunarFont(.caption).lunarTabularDigits().foregroundStyle(theme.textTertiary)307 }308 }309 .padding(.leading, LunarSpacing.xs)310 .padding(.trailing, LunarSpacing.sm)311 .frame(minHeight: LunarMetrics.controlHeightCompact + 2)312 .background { Capsule(style: .continuous).fill(isActive ? theme.softFill(for: tint) : theme.chromeSurface.withAlpha(0.72)) }313 .overlay {314 Capsule(style: .continuous)315 .strokeBorder(isActive ? color.withAlpha(0.6) : theme.hairline,316 lineWidth: isActive && differentiateWithoutColor ? 2 : 1)317 }318 }319}320321#Preview("Tabs") {322 VStack(alignment: .leading, spacing: LunarSpacing.md) {323 HStack {324 LunarTabChip(title: "Mail", tint: .rose, isPinned: true, onSelect: {})325 LunarTabChip(title: "ORIX Lab", tint: .lunarBlue, isSelected: true, onSelect: {}, onClose: {})326 LunarTabChip(title: "Night sounds", tint: .auroraMint, audio: .playing, onSelect: {}, onClose: {})327 LunarTabChip(title: "Loading page", isLoading: true, onSelect: {}, onClose: {})328 }329 HStack {330 LunarSatelliteChip("Research", tint: .spectralViolet, isActive: true, count: 8) {}331 LunarSatelliteChip("Travel", tint: .solar, count: 3) {}332 LunarPlanet(tint: .ocean, size: 36, isActive: true, hasRing: true)333 }334 }335 .padding(LunarSpacing.xl)336 .background(LunarWindowBackground())337 .lunarTheme(LunarThemes.lunar.dark)338}339