// Copyright 2026 The Clair Authors // You can use, redistribute, and/or modify this source code under // the terms of the GPL-3.0 license that can be found in the LICENSE file. import AppKit import ClairHost import SwiftUI /// What the sidebar asks the window to do. @MainActor protocol SidebarActions: SpaceActions, PinActions, SplitActions { // Tabs. func activate(tab: TabID) func close(tab: TabID) func closeTabs(below tab: TabID) func duplicate(tab: TabID) func setMuted(_ muted: Bool, tab: TabID) func copyLink(of tab: TabID) func move(tab: TabID, to index: Int) func newTab() // The top row and the URL field. func goBack() func goForward() /// Starts the back or forward history menu, or nil when the tab can't go /// that way (arc-navigation-feedback design D3). func historyMenu(_ direction: HistoryDirection) -> HistoryMenuBuilder? func reloadOrStop() func reloadButtonHovered(_ hovering: Bool) func toggleSidebar() /// A double-click on the top row's empty space, as on a title bar. func titleBarDoubleClicked() func editURL() func copyURL() func showSiteControls(indicator: TabIndicators) func openInNewTab(_ url: URL) // Extensions and downloads. func runExtension(_ extensionID: String, pinned: Bool) func setExtension(_ extensionID: String, pinned: Bool) func contextMenu(forExtension extensionID: String) -> NSMenu? func openDownload(_ downloadID: String) func showDownloadInFinder(_ downloadID: String) func cancelDownload(_ downloadID: String) func resumeDownload(_ downloadID: String) func keepDownload(_ downloadID: String) func discardDownload(_ downloadID: String) func downloadsListOpened() func openDownloadsPage() // Where the controls are, for Chromium's bubbles. func controlFramesChanged(_ frames: [AnchorControl: CGRect]) /// The toolbar's controls, in its own coordinate space (add-page-toolbar /// D6). func toolbarControlFramesChanged(_ frames: [AnchorControl: CGRect], pane: Int) /// Shows or hides the toolbar in every window (add-page-toolbar D8). func setToolbarShown(_ shown: Bool) // The update banner (add-update-banners). /// The banner's Restart and Update, with no version alert (D2). func restartToUpdate() /// Closes "Update Complete!" in every window (D3). func closeUpdateComplete() /// Opens the running version's release page and closes the card (D6). func updateWhatsNewChosen() } /// The sidebar's coordinate space, which is its hosting view's. let sidebarCoordinateSpace = "clair.sidebar" /// Records a control's frame for `AnchorResolver`. struct AnchorFramesKey: PreferenceKey { static let defaultValue: [AnchorControl: CGRect] = [:] static func reduce(value: inout [AnchorControl: CGRect], nextValue: () -> [AnchorControl: CGRect]) { value.merge(nextValue()) { $1 } } } extension EnvironmentValues { /// The coordinate space controls report their frames in: the sidebar's, /// or the toolbar's (add-page-toolbar D6). @Entry var anchorCoordinateSpace = sidebarCoordinateSpace } extension View { func anchorFrame(_ control: AnchorControl) -> some View { modifier(AnchorFrameReporter(control: control)) } } private struct AnchorFrameReporter: ViewModifier { let control: AnchorControl @Environment(\.anchorCoordinateSpace) private var space func body(content: Content) -> some View { content.background( GeometryReader { proxy in Color.clear.preference(key: AnchorFramesKey.self, value: [control: proxy.frame(in: .named(space))]) }) } } /// The sidebar (design D12): the top row, the URL field, extensions and /// downloads, then the "New Tab" row and the tab list. struct SidebarView: View { let model: BrowserWindowModel weak var actions: (any SidebarActions)? /// The URL field's buttons show while the pointer is anywhere over the /// sidebar of the key window (design D18). @State private var isHovered = false /// The tab list's height, as laid out. @State private var tabListHeight: CGFloat = 0 /// The height of what's above the favorites: the top row, the URL field, /// the extensions and the incognito badge. @State private var topHeight: CGFloat = 0 /// Whether a drag above the Space header opened the favorites' drop zone /// (show-favorites-drop-zone D1). It closes when the drag ends (D2). @State private var showsFavoritesDropZone = false @Environment(\.colorScheme) private var colorScheme @Environment(\.accessibilityReduceMotion) private var reduceMotion /// Arc's line between a list that scrolls and what's below it: 1 pt across /// the sidebar, about 4% black in Light Mode and 8% white in Dark Mode, as /// measured. It sits above the update banner while one shows. @ViewBuilder private var overflowLine: some View { if TabList.overflows(height: tabListHeight, rows: TabList.rowsHeight(model: model)) { Rectangle().fill(Color.primary.opacity(colorScheme == .dark ? 0.08 : 0.04)).frame(height: 1) } } var body: some View { // Arc's metrics: the top row at y+8, the URL field at y+46, the list // from y+90 with rows the sidebar's width less 8 pt each side. VStack(alignment: .leading, spacing: 0) { VStack(alignment: .leading, spacing: 0) { TopRow(model: model, actions: actions) // The row's empty space acts as the window's title bar. .background { TitleBarArea { actions?.titleBarDoubleClicked() } } // The New Space form takes the URL field's place too, as in Arc. While // the toolbar shows, the field and the extensions are in it // (add-page-toolbar D5). if model.newSpaceForm == nil && model.sidebarShowsControls { URLField( model: model, actions: actions, showsButtons: (isHovered && model.isWindowKey) || model.isSiteControlCenterOpen) .padding(.horizontal, 8) .padding(.top, 6) } if !model.pinnedExtensionActions.isEmpty && model.newSpaceForm == nil && model.sidebarShowsControls { ToolsRow(model: model, actions: actions) .padding(.horizontal, 8) .padding(.top, 8) } if model.profileKind == .incognito && model.newSpaceForm == nil { // Arc's row: 37 pt from 8 pt below the URL field. IncognitoBadge() .padding(.leading, 14) .frame(height: 37) .padding(.top, 8) } } .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { topHeight = $0 } // The profile's favorites, which stay put as the Spaces of the profile // slide under them (add-pinned-tabs-and-favorites D11). if model.showsPins && model.newSpaceForm == nil { if !model.favorites.isEmpty { FavoritesGrid(model: model, actions: actions) } else if model.showsFavoritesHint { FavoritesHintBox(model: model, actions: actions) } else if showsFavoritesDropZone && canOpenFavoritesDropZone { FavoritesHintBox(model: model, actions: actions, isDropZone: true) } } // The Space's header and tabs, or the New Space form, between the URL // field and the footer: the part that slides between Spaces // (add-spaces D10). The footer stays at the bottom however short the // list is; the space between moves the window like the rest of the // sidebar. SpacePager(model: model, actions: actions, isSidebarHovered: isHovered, tabListHeight: $tabListHeight) .padding(.top, tabListTopPadding) .frame(maxHeight: .infinity, alignment: .top) // The update banner's slot, between the list and the footer; its cards // open upward over the list (add-update-banners D4). Later in the stack, // it draws above the list. if model.updateBanner.banner != .none { UpdateBannerView(banner: model.updateBanner.banner, actions: actions) .background(alignment: .top) { overflowLine } .transition(UpdateBannerView.slotTransition(reduceMotion: reduceMotion)) } SidebarFooter(model: model, actions: actions) .overlay(alignment: .top) { if model.updateBanner.banner == .none { overflowLine } } } // The pill shows, and the complete card closes, by sliding down 15 pt and // fading (add-update-banners D5). .animation(.easeIn(duration: 0.12), value: model.updateBanner.banner == .none) // The explicit minimum keeps the sidebar at the width it's given; without // one, a child too wide to shrink widens it and the host centres the excess. .frame(minWidth: 0, maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) // The sidebar's empty areas move the window, as in Arc (design D18). .background { Color.clear .contentShape(Rectangle()) .gesture(WindowDragGesture()) .allowsWindowActivationEvents(true) } // Above the Space header, a drag opens the favorites' drop zone and a // drop makes the first favorite, as in Arc (show-favorites-drop-zone D1). .overlay(alignment: .top) { if canOpenFavoritesDropZone { FavoritesDropTrigger( onEnter: { showsFavoritesDropZone = true }, onDrop: { [actions] item in showsFavoritesDropZone = false actions?.drop(item, on: .favorites(0)) } ) .frame(height: topHeight + (showsFavoritesDropZone ? FavoritesHintBox.topInset : tabListTopPadding)) } } // The drags start in SwiftUI, which reports no end, so the zone closes // once the mouse button is up (D2). .task(id: showsFavoritesDropZone) { while showsFavoritesDropZone && !Task.isCancelled { try? await Task.sleep(for: .milliseconds(100)) if NSEvent.pressedMouseButtons & 1 == 0 { showsFavoritesDropZone = false } } } .onChange(of: canOpenFavoritesDropZone) { _, canOpen in if !canOpen { showsFavoritesDropZone = false } } .onHover { hovering in withAnimation(sidebarHoverFade) { isHovered = hovering } } .environment(\.isWindowKey, model.isWindowKey) .coordinateSpace(name: sidebarCoordinateSpace) .onPreferenceChange(AnchorFramesKey.self) { frames in MainActor.assumeIsolated { actions?.controlFramesChanged(frames) } } } /// Under the URL field or the pinned extensions: the Space name's text /// starts 17 pt below, as in Arc, or the list 8 pt below in a guest window. private var tabListTopPadding: CGFloat { switch model.profileKind { case .incognito: 0 // Below the favorites, or their empty box, the name's centre sits // 18.5 pt down, as in Arc. case .regular: showsFavoritesSection ? 2.5 : 9 default: 8 } } private var showsFavoritesSection: Bool { model.showsPins && model.newSpaceForm == nil && (!model.favorites.isEmpty || model.showsFavoritesHint || (showsFavoritesDropZone && canOpenFavoritesDropZone)) } /// Whether a drag above the Space header may open the favorites' drop /// zone: the profile has no favorites, and their box doesn't show. private var canOpenFavoritesDropZone: Bool { model.showsPins && model.newSpaceForm == nil && model.favorites.isEmpty && !model.showsFavoritesHint } } /// Arc's incognito badge, where a normal window shows its Space, as measured: /// a barely lighter 22 pt capsule, a near-white crossed-out eye 17 pt wide /// and 6.5 pt in, and "Incognito" in dim grey from 27 pt in. struct IncognitoBadge: View { var body: some View { HStack(spacing: 3.5) { Image(systemName: "eye.slash") .font(.system(size: 11.5, weight: .medium)) .foregroundStyle(Color.primary.opacity(0.94)) .frame(width: 17) Text("Incognito") .font(.system(size: 11, weight: .medium)) .foregroundStyle(Color.primary.opacity(0.4)) } .padding(.leading, 6.5) .padding(.trailing, 6) .frame(height: 22) .background(Color.primary.opacity(0.03), in: .capsule) .accessibilityElement(children: .combine) .accessibilityLabel("Incognito") } } /// The sidebar's fills, from Arc in Light and Dark Mode (design D18). enum SidebarFill { /// The URL field: a flat fill, no border. static func field(_ scheme: ColorScheme, hovered: Bool) -> Color { scheme == .dark ? Color.white.opacity(hovered ? 0.2 : 0.15) : Color.black.opacity(hovered ? 0.09 : 0.045) } /// The active tab's pill, lighter while the window isn't key. static func activePill(_ scheme: ColorScheme, isWindowKey: Bool) -> Color { scheme == .dark ? Color.white.opacity(isWindowKey ? 0.24 : 0.16) : Color(nsColor: .controlBackgroundColor).opacity(isWindowKey ? 0.9 : 0.7) } /// The active tab's shadow, the same whether the window is key or not. static let activeShadow = Color.black.opacity(0.12) /// A hovered row or button. static func hover(_ scheme: ColorScheme) -> Color { scheme == .dark ? Color.white.opacity(0.09) : Color.primary.opacity(0.06) } } /// Whether the sidebar's window is key; glyphs dim when it isn't, as in Arc. extension EnvironmentValues { @Entry var isWindowKey = true } /// How fast the sidebar's hover backgrounds and buttons fade (design D18). let sidebarHoverFade = Animation.easeOut(duration: 0.15) /// The top row, as in Arc: room for the window controls, hide-sidebar, then /// back, forward and reload or stop at the trailing edge. A narrow sidebar /// gets the compact row: back, forward and a menu with reload and Hide /// Sidebar. struct TopRow: View { let model: BrowserWindowModel weak var actions: (any SidebarActions)? @Environment(\.accessibilityReduceMotion) private var reduceMotion /// The trailing margin, and the gap between back, forward and reload. static let trailing: CGFloat = 6 static let gap: CGFloat = 2 /// The row's height while the toolbar holds its buttons. static let strippedHeight: CGFloat = 27.5 /// Where hide-sidebar starts: after the window controls, or up to 3 pt /// closer when the sidebar is only just wide enough, as in Arc. static func hideSidebarX(width: CGFloat, controls: WindowControlsMetrics) -> CGFloat { min(controls.topRowLeading, width - trailing - 3 * (32 + gap) - 32) } var body: some View { // The button's own state, which holds stop under the pointer (design D2); // the tab rows' spinners read the tab's `isLoading`. let reloadStop = model.reloadStop.mode let isLoading = reloadStop.visible == .stop let controls = model.isWindowFullscreen ? WindowControlsMetrics.fullScreen : model.windowControls // While the toolbar shows, the row keeps only the window controls' room, // at its height, so they stay at Arc's place (add-page-toolbar D5). let isCompact = model.sidebarShowsControls && controls.hasCompactTopRow(width: model.sidebar.width) let size = isCompact ? IconButtonSize.small : IconButtonSize.regular let back = IconButton( systemImage: "arrow.left", help: "Go back", shortcut: ["⌘", "["], isEnabled: model.canGoBack, size: size ) { actions?.goBack() } .overlay( HistoryButtonCatcher( isEnabled: model.canGoBack, makeMenu: { actions?.historyMenu(.back) })) .accessibilityIdentifier("clair.back") let forward = IconButton( systemImage: "arrow.right", help: "Go forward", shortcut: ["⌘", "]"], isEnabled: model.canGoForward, size: size ) { actions?.goForward() } .overlay( HistoryButtonCatcher( isEnabled: model.canGoForward, makeMenu: { actions?.historyMenu(.forward) })) .accessibilityIdentifier("clair.forward") // The row is centred on the window controls (design D3b). if !model.sidebarShowsControls { // Arc's favorites start 44 pt from the window's top with the toolbar // on (toolbar-on.jpg), 4.5 pt higher than below the full row. Color.clear .frame(height: Self.strippedHeight) .padding(.top, max(0, controls.centreY - 16)) } else if isCompact { HStack(spacing: 0) { Spacer(minLength: 0) back forward IconMenu(help: "More options", size: IconButtonSize.small) { Group { if isLoading { Button("Stop") { actions?.reloadOrStop() }.keyboardShortcut(.escape, modifiers: []) } else { Button("Reload") { actions?.reloadOrStop() }.keyboardShortcut("r", modifiers: .command) } } .disabled(!model.canReload || !reloadStop.isEnabled) Button("Hide Sidebar") { actions?.toggleSidebar() }.keyboardShortcut("s", modifiers: .command) } label: { SymbolGlyph(systemImage: "ellipsis") } .accessibilityIdentifier("clair.topRowMenu") } .padding(.trailing, Self.trailing) .frame(height: 28) .padding(.top, max(0, controls.centreY - 14)) .padding(.bottom, 2) } else { HStack(spacing: 0) { // The window controls sit here (design D4). Color.clear.frame(width: Self.hideSidebarX(width: model.sidebar.width, controls: controls)) // Arc's wording: a revealed sidebar's button docks it. IconButton( systemImage: "sidebar.left", help: model.sidebar.isHidden && model.sidebar.isRevealed ? "Lock sidebar" : "Hide sidebar", shortcut: ["⌘", "S"] ) { actions?.toggleSidebar() } .accessibilityIdentifier("clair.hideSidebar") Spacer(minLength: 0) HStack(spacing: Self.gap) { back forward IconButton( help: isLoading ? "Stop loading this page" : "Reload this page", shortcut: isLoading ? ["esc"] : ["⌘", "R"], isEnabled: model.canReload && reloadStop.isEnabled ) { actions?.reloadOrStop() } label: { // Arc crossfades the two glyphs in about 100 ms. ZStack { SymbolGlyph(systemImage: "arrow.clockwise").opacity(isLoading ? 0 : 1) SymbolGlyph(systemImage: "xmark").opacity(isLoading ? 1 : 0) } .animation(reduceMotion ? nil : .easeInOut(duration: 0.1), value: isLoading) } .onHover { actions?.reloadButtonHovered($0) } .accessibilityIdentifier("clair.reload") } } .padding(.trailing, Self.trailing) .frame(height: 32) .padding(.top, max(0, controls.centreY - 16)) } } } /// The URL field, as in Arc: the host or full URL, or a magnifier and the /// placeholder on a new tab page; the indicators; and, under the pointer, copy /// and site-controls buttons at its trailing end. A click opens the command /// bar on the current URL. struct URLField: View { @Bindable var model: BrowserWindowModel weak var actions: (any SidebarActions)? /// Whether the copy and site-controls buttons show: while the pointer is /// over the key window's sidebar. var showsButtons = false @State private var isHovered = false @Environment(\.colorScheme) private var colorScheme static let height: CGFloat = 36 /// Arc's URL-field buttons: 28 pt, side by side. static let buttonSize = IconButtonSize.small static let indicatorSymbols: [(TabIndicators, String, String)] = [ (.camera, "video.fill", "Camera"), (.microphone, "mic.fill", "Microphone"), (.location, "location.fill", "Location"), (.screenCapture, "rectangle.inset.filled.on.rectangle", "Screen sharing"), ] var body: some View { let tab = model.activeTab let display = model.urlFieldText let hasSiteControls = tab != nil let showsNotSecure = URLDisplay.showsNotSecure(for: tab) HStack(spacing: 0) { if showsNotSecure { Image(systemName: "lock.slash.fill") .font(.system(size: 11)) .foregroundStyle(Color.red) .padding(.leading, 12) .tooltip("Site is not secure") .accessibilityLabel("Site is not secure") .accessibilityIdentifier("clair.notSecure") } if display == nil { Image(systemName: "magnifyingglass") .font(.system(size: 13, weight: .medium)) .foregroundStyle(.secondary) .frame(width: 24, height: 24) .padding(.leading, 6) .padding(.trailing, 3) .frame(maxHeight: .infinity) .contentShape(Rectangle()) .onTapGesture { actions?.editURL() } } Text(display ?? URLDisplay.placeholder) .font(.system(size: 13)) .lineLimit(1) .truncationMode(.tail) .foregroundStyle(display == nil ? Color.secondary : Color.primary.opacity(0.85)) .padding(.leading, display == nil ? 0 : (showsNotSecure ? 5 : 12)) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) .contentShape(Rectangle()) .onTapGesture { actions?.editURL() } .accessibilityElement() .accessibilityLabel(display ?? URLDisplay.placeholder) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("clair.urlField") ForEach(Self.indicatorSymbols, id: \.0.rawValue) { indicator, symbol, label in if tab?.indicators.contains(indicator) == true { Button { actions?.showSiteControls(indicator: indicator) } label: { Image(systemName: symbol) .font(.system(size: 11)) .foregroundStyle(Color.red) .frame(width: 22, height: Self.buttonSize) .contentShape(Rectangle()) } .buttonStyle(.plain) .tooltip(label) .accessibilityLabel(label) .anchorFrame(.indicator(indicator.rawValue)) } } if showsButtons, display != nil { URLFieldButton( systemImage: "link", help: "Copy URL", shortcut: ["⇧", "⌘", "C"], glyphSize: URLFieldButton.linkGlyphSize ) { actions?.copyURL() } .accessibilityIdentifier("clair.copyURL") .transition(.opacity) } if showsButtons, hasSiteControls { URLFieldButton( systemImage: "slider.horizontal.2.square", help: "Site Control Center", glyphSize: URLFieldButton.siteControlsGlyphSize, glyphWeight: .regular ) { model.isSiteControlCenterOpen = true } // A click while the popover is open only closes it: the transient // popover takes the mouse-down, and the button must not then open it // again (arc-site-control-center design D2). .allowsHitTesting(!model.isSiteControlCenterOpen) .accessibilityIdentifier("clair.siteControls") .transition(.opacity) } } .padding(.trailing, showsButtons || tab?.indicators.isEmpty == false ? 5 : 12) .frame(height: Self.height) // Chromium's page info and the Site Control Center attach where the // site-controls button shows on hover, whether or not it shows now. .overlay(alignment: .trailing) { Color.clear .frame(width: Self.buttonSize, height: Self.buttonSize) .allowsHitTesting(false) .accessibilityHidden(true) .popover(isPresented: $model.isSiteControlCenterOpen, arrowEdge: .bottom) { SiteControlCenter(model: model, actions: actions) { model.isSiteControlCenterOpen = false } } .padding(.trailing, 5) .anchorFrame(.siteControls) } // Arc's flat fill, darker under the pointer (design D18). .background(SidebarFill.field(colorScheme, hovered: isHovered), in: .rect(cornerRadius: 10, style: .continuous)) .onHover { hovering in withAnimation(sidebarHoverFade) { isHovered = hovering } } .contextMenu { if display != nil { Button("Copy URL") { actions?.copyURL() } Divider() } // The field shows only while the toolbar is off (add-page-toolbar D8). Button("Show as Toolbar") { actions?.setToolbarShown(true) } } } } /// A button at the URL field's trailing end. struct URLFieldButton: View { /// Point sizes that draw the glyphs as large as Arc's, as measured: the /// link 14.5 pt across and the square 16 pt. The square draws smaller than /// the link at the same size. static let linkGlyphSize: CGFloat = 14 static let siteControlsGlyphSize: CGFloat = 18 let systemImage: String let help: String var shortcut: [String] = [] var glyphSize: CGFloat = 13 /// The square draws heavier than the link at the same weight. var glyphWeight: Font.Weight = .medium let action: () -> Void var body: some View { IconButton( systemImage: systemImage, help: help, shortcut: shortcut, size: URLField.buttonSize, cornerRadius: 7, glyphSize: glyphSize, glyphWeight: glyphWeight, action: action) } } /// The pinned extensions, shown while any is pinned. struct ToolsRow: View { let model: BrowserWindowModel weak var actions: (any SidebarActions)? var body: some View { PinnedExtensionsLayout { ForEach(model.pinnedExtensionActions, id: \.extensionID) { action in PinnedExtensionTile( action: action, contextMenu: { actions?.contextMenu(forExtension: action.extensionID) } ) { actions?.runExtension(action.extensionID, pinned: true) } .anchorFrame(.extensionAction(action.extensionID)) } } } } /// The sidebar's footer, as in Arc: a 50 pt band at the bottom of the sidebar, /// with the downloads button always where Arc has its library button and the /// plus menu at the other end, each 8 pt in from the side and 9 pt from the /// bottom. struct SidebarFooter: View { @Bindable var model: BrowserWindowModel weak var actions: (any SidebarActions)? static let height: CGFloat = 50 /// The footer's glyphs: medium weight like Arc's footer symbols (17 pt /// medium), 18 pt, so a circled symbol is 18 pt across. The grey is the /// buttons' `.secondary`, which matches Arc's. static let glyphSize: CGFloat = 18 static let glyphWeight: Font.Weight = .medium var body: some View { // While the New Space form shows, the footer shows only the Spaces and a // dot for the new one, as in Arc. let creating = model.newSpaceForm != nil // The form's dot follows every Space's, even the only one's. let items = model.showsSpaces ? SpacesLogic.switcherItems( model.spaces.spaces, shown: model.shownSpaceID, includingOne: creating) : [] HStack(spacing: 0) { DownloadsButton(progress: model.downloadProgress, hasUnseen: model.hasUnseenDownloads) { model.isDownloadsListOpen.toggle() if model.isDownloadsListOpen { actions?.downloadsListOpened() } } .anchorFrame(.downloads) .popover(isPresented: $model.isDownloadsListOpen, arrowEdge: .trailing) { DownloadsList(model: model, actions: actions) } .opacity(creating ? 0 : 1) .allowsHitTesting(!creating) // Arc's strip runs from 2 pt after the downloads button to 2 pt before // the plus. Spacer(minLength: 2) if !items.isEmpty || (creating && model.showsSpaces) { SpaceSwitcher( items: creating ? items.map { $0.notCurrent } : items, showsNewSpaceDot: creating, actions: actions) Spacer(minLength: 2) } // The plus menu (sidebar spec, "Plus menu"): Arc's also offers Easels, // Boosts and Split View, which Clair hasn't. IconMenu(help: "New") { ForEach(Array(SpacesLogic.plusMenu(showsSpaces: model.showsSpaces).enumerated()), id: \.offset) { _, item in switch item { case .newSpace: Button("New Space…") { actions?.beginNewSpace() } case .newFolder: Button("New Folder") { actions?.newFolder() } case .divider: Divider() case .newTab: Button("New Tab") { actions?.newTab() }.keyboardShortcut("t", modifiers: .command) } } } label: { SymbolGlyph(systemImage: "plus", size: Self.glyphSize, weight: Self.glyphWeight) } .accessibilityIdentifier("clair.footerNew") .opacity(creating ? 0 : 1) .allowsHitTesting(!creating) } .padding(.horizontal, 8) .frame(height: Self.height) } } /// The sidebar's content between the URL field and the footer: the Space's /// header and tab list, sliding sideways to another Space's as in Arc, or the /// New Space form (add-spaces D10). struct SpacePager: View { let model: BrowserWindowModel weak var actions: (any SidebarActions)? /// Whether the pointer is over the sidebar, which shows Clear. var isSidebarHovered = false @Binding var tabListHeight: CGFloat @State private var width: CGFloat = 0 /// 0 as a slide starts, 1 once the new Space is in place. @State private var progress: CGFloat = 1 /// The slide `progress` animates; a newer one starts from 0 before its /// first frame. @State private var animatedSlide: UUID? /// Arc's slide, recorded at 60 fps on an M1 Pro (add-spaces task 3.4): /// about 90% of the way within two frames, settled by about 100 ms. A /// critically damped spring of 0.08 s is 90% there at 50 ms and settled by /// 85 ms. static let slide = Animation.spring(response: 0.08, dampingFraction: 1) var body: some View { ZStack(alignment: .topLeading) { if model.newSpaceForm != nil { NewSpaceFormView(model: model, actions: actions) } else { let slide = model.spaceSlide let direction: CGFloat = slide?.fromTrailing == false ? -1 : 1 let progress = slide != nil && slide?.id != animatedSlide ? 0 : self.progress // The earlier Space stays for the whole slide, and goes without a // transition of its own once it's out: removed inside the slide's // animation, it would fade where it was instead of sliding out. if let slide { SpacePage(model: model, space: slide.fromSpace, tabs: slide.fromTabs, items: slide.fromItems, actions: nil) .offset(x: -direction * width * progress) .allowsHitTesting(false) .accessibilityHidden(true) .transition(.identity) } SpacePage( model: model, space: model.shownSpace, tabs: model.tabs, items: model.pinnedItems, actions: actions, isSidebarHovered: isSidebarHovered, tabListHeight: $tabListHeight ) .offset(x: slide == nil ? 0 : direction * width * (1 - progress)) } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) .clipped() .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width = $0 } // From the first appearance too: a window a group swaps in may draw its // sidebar for the first time with the slide already set (add-profiles // D12), and a slide that never starts would leave the page offset. .onChange(of: model.spaceSlide?.id, initial: true) { _, id in guard id != nil else { return } animatedSlide = id progress = 0 withAnimation(Self.slide) { progress = 1 } completion: { if model.spaceSlide?.id == id { var done = Transaction() done.disablesAnimations = true withTransaction(done) { model.spaceSlide = nil } } } } } } /// One Space's header and tab list. struct SpacePage: View { let model: BrowserWindowModel let space: (any ClairSpace)? let tabs: [any ClairTab] /// Its pinned items, pinned tabs and folders (add-pinned-tabs-and-favorites /// D11, add-tab-folders D4). var items: [any ClairPinnedItem] = [] weak var actions: (any SidebarActions)? var isSidebarHovered = false var tabListHeight: Binding? /// The list's height and the space below it, which together are the room /// the list may grow into. @State private var listHeight: CGFloat = 0 @State private var spaceBelow: CGFloat = 0 @State private var isHeaderTargeted = false var body: some View { VStack(alignment: .leading, spacing: 0) { if model.showsSpaces { let isShown = space?.spaceID == model.shownSpaceID let collapsed = isShown ? model.isPinnedSectionCollapsed : false SpaceHeaderView( model: model, space: space, actions: actions, pinsCollapsed: collapsed, // No chevron or hover pill without pinned tabs or folders, as in // Arc. onToggleCollapse: PinsLogic.offersCollapse(pinCount: items.count, collapsed: collapsed) ? { [weak actions] in actions?.togglePinnedSection() } : nil, isDropTarget: isHeaderTargeted) // A row or tile dropped on the header is pinned last, as Pin Tab // pins, whether the section is empty or collapsed. .onDrop( of: [SidebarDragItem.type], delegate: PinningDropDelegate( isEnabled: isShown && model.showsPins, target: .pinned(items.count), isTargeted: $isHeaderTargeted, actions: actions)) } TabList( model: model, tabs: tabs, items: items, isSidebarHovered: isSidebarHovered, room: listHeight + spaceBelow, actions: actions ) .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { height in listHeight = height tabListHeight?.wrappedValue = height } // Ahead of the space below, which would otherwise take half the room. .layoutPriority(1) Spacer(minLength: 0) .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { spaceBelow = $0 } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) // add-tab-folders D10: right-clicking the sidebar where there's no row // or control, as below the list, opens the header's menu; other clicks // go through, so the sidebar still moves the window. .background { if model.showsSpaces, actions != nil, space?.spaceID == model.shownSpaceID { RowClickCatcher(onMiddleClick: {}, menuItems: sidebarMenuItems) .accessibilityHidden(true) } } } /// The sidebar's own menu: the Space header's. private var sidebarMenuItems: [RowClickCatcher.Item] { guard let space else { return [] } return SpaceHeaderView.menuItems(for: space, model: model, actions: actions) } } struct ExtensionIcon: View { let action: any ClairExtensionAction var size: CGFloat = 16 var body: some View { if let icon = action.icon { Image(nsImage: icon).resizable().interpolation(.high).frame(width: size, height: size) } else { Image(systemName: "puzzlepiece").frame(width: size, height: size) } } } /// Slides the tab list's rows down from under the Space header while the /// pinned section expands: `height` is the pinned rows it brings back. struct PinnedSectionReveal: ViewModifier, Animatable { var progress: CGFloat let isCollapsed: Bool let height: CGFloat var animatableData: CGFloat { get { progress } set { progress = newValue } } func body(content: Content) -> some View { content.offset(y: isCollapsed ? 0 : -(1 - progress) * height) } } /// The downloads button, 32 pt as in Arc's footer, with the running /// downloads' progress on the glyph's circle and a dot for unseen downloads. struct DownloadsButton: View { let progress: Double? let hasUnseen: Bool let action: () -> Void @Environment(\.isWindowKey) private var isWindowKey var body: some View { IconButton(help: "Downloads", action: action) { glyph .frame(width: IconButtonSize.regular, height: IconButtonSize.regular) .overlay(alignment: .topTrailing) { if hasUnseen { Circle().fill(Color.accentColor).frame(width: 7, height: 7).padding(2) } } } .accessibilityValue(progress.map { "\(Int($0 * 100))%" } ?? (hasUnseen ? "New" : "")) .accessibilityIdentifier("clair.downloads") } @ViewBuilder private var glyph: some View { if let progress { if #available(macOS 26, *) { // The symbol draws its own ring up to the progress (Variable Draw): // the arrow in the button's grey, the ring in the accent colour. Image(systemName: "arrow.down.circle", variableValue: progress) .symbolRenderingMode(.palette) .symbolVariableValueMode(.draw) .font(.system(size: SidebarFooter.glyphSize, weight: SidebarFooter.glyphWeight)) .foregroundStyle(arrowColor, Color.accentColor) } else { // Variable Draw needs macOS 26, and the symbol has no variable // layers before it, so macOS 15 draws the progress over the ring: // 18 pt across with a stroke of about 1.75 pt. footerGlyph .overlay { Circle() .trim(from: 0, to: progress) .stroke(Color.accentColor, style: StrokeStyle(lineWidth: 2, lineCap: .round)) .rotationEffect(.degrees(-90)) .frame(width: SidebarFooter.glyphSize - 1.75, height: SidebarFooter.glyphSize - 1.75) } } } else { footerGlyph } } private var footerGlyph: SymbolGlyph { SymbolGlyph(systemImage: "arrow.down.circle", size: SidebarFooter.glyphSize, weight: SidebarFooter.glyphWeight) } /// The palette replaces the button's grey, so the arrow dims itself. private var arrowColor: Color { Color(nsColor: isWindowKey ? .secondaryLabelColor : .tertiaryLabelColor) } } /// Recent downloads, with their actions. struct DownloadsList: View { let model: BrowserWindowModel weak var actions: (any SidebarActions)? var body: some View { VStack(alignment: .leading, spacing: 8) { if model.downloads.isEmpty { Text("No recent downloads") .foregroundStyle(.secondary) } else { ForEach(model.downloads.prefix(8), id: \.downloadID) { download in DownloadRow(download: download, actions: actions) } } Divider() Button("Show All Downloads") { actions?.openDownloadsPage() } .buttonStyle(.plain) .foregroundStyle(Color.accentColor) } .padding(12) .frame(width: 300) } } struct DownloadRow: View { let download: any ClairDownload weak var actions: (any SidebarActions)? var status: String { switch download.state { case .inProgress: if download.totalBytes > 0 { let format = ByteCountFormatter() return "\(format.string(fromByteCount: download.receivedBytes)) of \(format.string(fromByteCount: download.totalBytes))" } return "Downloading…" case .paused: return "Paused" case .complete: return ByteCountFormatter.string(fromByteCount: download.totalBytes, countStyle: .file) case .cancelled: return "Cancelled" case .interrupted: return "Failed" @unknown default: return "" } } var body: some View { HStack(spacing: 8) { Image(systemName: download.needsDecision ? "exclamationmark.triangle.fill" : "doc") .foregroundStyle(download.needsDecision ? Color.orange : Color.secondary) VStack(alignment: .leading, spacing: 2) { Text(download.fileName).lineLimit(1).truncationMode(.middle) Text(download.warningText ?? status).font(.caption).foregroundStyle(.secondary) if download.state == .inProgress, download.totalBytes > 0 { ProgressView(value: Double(download.receivedBytes), total: Double(download.totalBytes)) } } Spacer(minLength: 0) if download.needsDecision { Button("Keep") { actions?.keepDownload(download.downloadID) } Button("Delete") { actions?.discardDownload(download.downloadID) } } else { switch download.state { case .inProgress, .paused: Button { actions?.cancelDownload(download.downloadID) } label: { Image(systemName: "xmark.circle") } .buttonStyle(.plain).tooltip("Cancel") case .interrupted where download.canResume: Button { actions?.resumeDownload(download.downloadID) } label: { Image(systemName: "arrow.clockwise") } .buttonStyle(.plain).tooltip("Retry") case .complete: Button { actions?.showDownloadInFinder(download.downloadID) } label: { Image(systemName: "magnifyingglass") } .buttonStyle(.plain).tooltip("Show in Finder") default: EmptyView() } } } .contentShape(Rectangle()) .onTapGesture { if download.state == .complete { actions?.openDownload(download.downloadID) } } .accessibilityElement(children: .combine) .accessibilityIdentifier("clair.downloadRow") } } /// The pinned section, the divider, the "New Tab" row and the Today tabs, in /// one list that scrolls (add-pinned-tabs-and-favorites D11). Rows drag /// between the pinned section and Today, and to and from the favorites. struct TabList: View { let model: BrowserWindowModel /// The Today rows' tabs: the window's in its Space, or the Space sliding /// out's. let tabs: [any ClairTab] /// The pinned section's items, pinned tabs and folders. var items: [any ClairPinnedItem] = [] /// Whether the pointer is over the sidebar, which shows Clear. var isSidebarHovered = false /// The height the list may grow to, 0 until it's laid out. var room: CGFloat = 0 weak var actions: (any SidebarActions)? /// Room beyond the rows, so a list that fits never scrolls. static let slack: CGFloat = 8 /// One of the list's items. enum Item: Identifiable { /// A pinned tab or a folder (add-tab-folders D4). case pinned(PinsLogic.PinnedRow) /// The divider, when it shows, and "New Tab", as one row, so the list /// offers no place between them. case boundary(showsDivider: Bool) case tab(any ClairTab) var id: String { switch self { case .pinned(let row): row.id case .boundary: "boundary" case .tab(let tab): "tab-\(tab.tabID)" } } } /// The height of the list's rows, which the list grows to. static func contentHeight(rows: CGFloat) -> CGFloat { rows + slack } /// The rows' height: "New Tab", the pinned and Today rows, and the divider /// when it shows. static func rowsHeight(pinCount: Int, tabCount: Int, showsPins: Bool) -> CGFloat { let divider = PinsLogic.showsDivider(todayRowCount: tabCount, showsPins: showsPins) return TabRow.pitch * CGFloat(pinCount + tabCount + 1) + (divider ? TodayDivider.height : 0) } /// How far the rows start above their places as the pinned section expands /// in `room`: the pinned rows it brings back, or none when the expanded /// rows won't fit. Such a list scrolls, so sliding it would leave a gap at /// its bottom, or move rows other than the ones the user sees. static func revealHeight(hiddenPins: Int, expandedRows: CGFloat, room: CGFloat) -> CGFloat { guard room > 0, contentHeight(rows: expandedRows) <= room + 0.5 else { return 0 } return TabRow.pitch * CGFloat(hiddenPins) } static func rowsHeight(model: BrowserWindowModel) -> CGFloat { let todayRows = TabListLogic.rowCount(model.tabs) let pinned = PinsLogic.pinnedRows( model.pinnedItems, collapsed: model.isPinnedSectionCollapsed, highlighted: model.highlightedTab) return rowsHeight(pinCount: pinned.count, tabCount: todayRows, showsPins: model.showsPins) } /// Whether a list this tall is shorter than its rows, so it scrolls. It /// follows the rows as well as the height: a list capped at the room left /// keeps its height while tabs come and go. static func overflows(height: CGFloat, rows: CGFloat) -> Bool { height > 0 && height + 0.5 < contentHeight(rows: rows) } var body: some View { // A Peek has no row; its parent's row stands for both (add-peek D4). // Worked out once per update, not per row. let rows = TabListLogic.rows(tabs) let peeks = model.rowPeeks(in: tabs) let highlighted = model.highlightedTab let moveTargets = model.showsSpaces ? SpacesLogic.moveTargets(from: model.shownSpaceID, in: model.spaces.spaces) : [] let collapsed = model.isPinnedSectionCollapsed let pinned = PinsLogic.pinnedRows(items, collapsed: collapsed, highlighted: highlighted) let expanded = collapsed ? PinsLogic.pinnedRows(items, collapsed: false, highlighted: highlighted) : pinned // Under the Space header, pinned tabs or not, as in Arc. let showsDivider = PinsLogic.showsDivider(todayRowCount: rows.count, showsPins: model.showsPins) let boundary = 1 let listItems: [Item] = pinned.map { .pinned($0) } + [.boundary(showsDivider: showsDivider)] + rows.map { .tab($0) } let items = items List { ForEach(listItems) { item in row(item, peeks: peeks, highlighted: highlighted, moveTargets: moveTargets) .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: -1)) .listRowSeparator(.hidden) .listRowBackground(Color.clear) } .onMove { source, destination in guard let from = source.first, listItems.indices.contains(from) else { return } let drop: PinsLogic.ListDrop let dragged: SidebarDragItem switch listItems[from] { case .pinned(let row): switch row { case .pin(let pin, _): dragged = .pin(pin.pinID) // add-split-view D10: a pinned split moves as one. case .split(let split, _): dragged = .pin(split.pins.first?.pinID ?? "") case .folder(let folder): dragged = .folder(folder.folderID) } drop = PinsLogic.listDrop( destination: destination, rows: pinned, items: items, collapsed: collapsed, boundaryCount: boundary, today: tabs, movingPinnedRow: from) case .tab(let tab): dragged = .tab(tab.tabID) drop = PinsLogic.listDrop( destination: destination, rows: pinned, items: items, collapsed: collapsed, boundaryCount: boundary, today: tabs, movingTodayRow: from - pinned.count - boundary) // The divider and "New Tab" can be lifted but move nothing. They // aren't `moveDisabled`: the list then offers no place beside them // to a row dragged from above, such as a pinned tab to the top of // Today, nor to any row while the pinned section is empty. case .boundary: return } actions?.drop(dragged, on: Self.target(of: drop)) } .onInsert(of: [SidebarDragItem.type]) { index, providers in let drop = PinsLogic.listDrop( destination: index, rows: pinned, items: items, collapsed: collapsed, boundaryCount: boundary, today: tabs) let target = Self.target(of: drop) let actions = actions SidebarDragItem.load(from: providers) { item in actions?.drop(item, on: target) } } } .listStyle(.plain) .scrollContentBackground(.hidden) .environment(\.defaultMinListRowHeight, 1) // No taller than its rows, so the space below the last tab is the // sidebar's, which moves the window (design D18); the list scrolls once // the rows don't fit. .frame( maxHeight: Self.contentHeight( rows: Self.rowsHeight(pinCount: pinned.count, tabCount: rows.count, showsPins: model.showsPins))) // Collapsing, the list slides the rows below over the pinned rows that go // and shrinks with them. Expanding, it adds the rows at once, so it takes // its new height at once too, and its rows slide down from under the // header instead. .transaction(value: model.isPinnedSectionCollapsed) { transaction in if !model.isPinnedSectionCollapsed { transaction.animation = nil } } .modifier( PinnedSectionReveal( progress: model.isPinnedSectionCollapsed ? 0 : 1, isCollapsed: model.isPinnedSectionCollapsed, height: Self.revealHeight( hiddenPins: expanded.count - PinsLogic.pinnedRows(items, collapsed: true, highlighted: highlighted).count, expandedRows: Self.rowsHeight(pinCount: expanded.count, tabCount: rows.count, showsPins: model.showsPins), room: room))) .clipped() .accessibilityIdentifier("clair.tabList") } /// The sidebar's own menu: the Space header's. private var sidebarMenuItems: [RowClickCatcher.Item] { guard let space = model.shownSpace else { return [] } return SpaceHeaderView.menuItems(for: space, model: model, actions: actions) } /// Where a drop in the list goes. static func target(of drop: PinsLogic.ListDrop) -> SidebarDropTarget { switch drop { case .today(let index): .today(index) case .pinned(let index): .pinned(index) case .folder(let folderID, let index): .folder(folderID, index) } } @ViewBuilder private func row( _ item: Item, peeks: [TabID: any ClairTab], highlighted: TabID, moveTargets: [any ClairSpace] ) -> some View { switch item { case .pinned(.pin(let pin, let folder)): let tab = model.tab(ofPin: pin) PinRow( pin: pin, tab: tab, peek: tab.flatMap { $0.peekID != ClairTabIDNone ? model.pinTabs[$0.peekID] : nil }, isActive: pin.tabID != ClairTabIDNone && pin.tabID == highlighted, isWindowKey: model.isWindowKey, moveTargets: moveTargets, rename: model.renaming?.row == .pin(pin.pinID) ? model.renaming : nil, actions: actions ) // add-tab-folders D4: a folder's tabs are indented under it. .padding(.leading, folder == nil ? 0 : FolderRow.indent) .itemProvider { SidebarDragItem.pin(pin.pinID).itemProvider() } case .pinned(.split(let split, let folder)): SplitRows.pinned( split, model: model, highlighted: highlighted, moveTargets: moveTargets, actions: actions ) .padding(.leading, folder == nil ? 0 : FolderRow.indent) .itemProvider { split.pins.first.map { SidebarDragItem.pin($0.pinID).itemProvider() } } case .pinned(.folder(let folder)): FolderRow( folder: folder, moveTargets: moveTargets, rename: model.renaming?.row == .folder(folder.folderID) ? model.renaming : nil, actions: actions ) .itemProvider { SidebarDragItem.folder(folder.folderID).itemProvider() } case .boundary(let showsDivider): VStack(spacing: 0) { if showsDivider { TodayDivider( showsClear: PinsLogic.showsClear( todayRowCount: TabListLogic.rowCount(tabs), showsPins: model.showsPins, isSidebarHovered: isSidebarHovered) ) { actions?.clearToday() } } NewTabRow(isHighlighted: model.commandBar?.mode == .newTab) { actions?.newTab() } // From inside a row, where it can reach the list's scroll view. .background(ScrollerControlSize(size: .small)) // add-tab-folders D10: right-clicking the list where there's no // row opens the header's menu. .background(TableMenuInstaller(items: model.showsSpaces && actions != nil ? sidebarMenuItems : nil)) // add-tab-folders D10: right-clicking "New Tab" opens the header's // menu, as in Arc. .overlay { if model.showsSpaces && actions != nil { RowClickCatcher(onMiddleClick: {}, menuItems: sidebarMenuItems) } } } case .tab(let tab) where tab.splitPartnerID != ClairTabIDNone: // add-split-view D2: a split's row, its chips in pane order. SplitRows.today(tab, model: model, highlighted: highlighted, moveTargets: moveTargets, actions: actions) .itemProvider { model.showsPins ? SidebarDragItem.tab(tab.tabID).itemProvider() : nil } case .tab(let tab): TabRow( tab: tab, isActive: tab.tabID == highlighted, isWindowKey: model.isWindowKey, isIncognito: model.profileKind == .incognito, canPin: model.showsPins, peek: peeks[tab.tabID], moveTargets: moveTargets, rename: model.renaming?.row == .tab(tab.tabID) ? model.renaming : nil, actions: actions ) .itemProvider { model.showsPins ? SidebarDragItem.tab(tab.tabID).itemProvider() : nil } } } } /// Sets the scrollers of the scroll view around it to a control size, for a /// thinner scroll bar: SwiftUI has no way to, but a `List` scrolls in an /// AppKit scroll view. struct ScrollerControlSize: NSViewRepresentable { let size: NSControl.ControlSize func makeNSView(context: Context) -> SizingView { let view = SizingView() view.size = size return view } func updateNSView(_ view: SizingView, context: Context) { view.size = size view.apply() } final class SizingView: NSView { var size: NSControl.ControlSize = .regular override func viewDidMoveToWindow() { super.viewDidMoveToWindow() apply() } // Clicks go to the row. override func hitTest(_ point: NSPoint) -> NSView? { nil } func apply() { enclosingScrollView?.verticalScroller?.controlSize = size } } } struct NewTabRow: View { let isHighlighted: Bool let action: () -> Void @State private var isHovered = false @Environment(\.colorScheme) private var colorScheme var body: some View { Button(action: action) { HStack(spacing: TabRow.titleGap) { Image(systemName: "plus") .font(.system(size: 15, weight: .medium)) .frame(width: TabRow.iconSize, height: TabRow.iconSize) Text("New Tab").font(TabRow.titleFont) Spacer(minLength: 0) } .foregroundStyle(.secondary) .padding(.horizontal, TabRow.horizontalPadding) .frame(height: TabRow.pillHeight) .background( isHighlighted ? Color.accentColor.opacity(0.18) : (isHovered ? SidebarFill.hover(colorScheme) : .clear), in: .rect(cornerRadius: TabRow.cornerRadius, style: .continuous) ) .padding(.top, TabRow.pillInset) .padding(.bottom, TabRow.pillInset + TabRow.spacing) .contentShape(Rectangle()) } .buttonStyle(.plain) .onHover { isHovered = $0 } .accessibilityIdentifier("clair.newTabRow") } } /// One tab: favicon, title, the speaker while it plays or is muted, and the /// close button on hover. struct TabRow: View { let tab: any ClairTab let isActive: Bool let isWindowKey: Bool var isIncognito = false /// Whether its menu offers the pin items: not in incognito and guest /// windows (add-pinned-tabs-and-favorites). var canPin = false /// The tab's Peek, whose favicon the row shows as a badge (add-peek D4). var peek: (any ClairTab)? /// The Spaces "Move to Space" lists: every other one, or none with one /// Space (add-spaces D7). var moveTargets: [any ClairSpace] = [] /// The rename in progress on this row, if any (tab-names spec). var rename: RowRename? weak var actions: (any SidebarActions)? /// What the row reads: the tab's custom name, or its title. var shownTitle: String { tab.name.flatMap { $0.isEmpty ? nil : $0 } ?? tab.title } @State private var isHovered = false @State private var showsSpeaker = false @Environment(\.colorScheme) private var colorScheme /// How long the pointer rests on a row before its full title shows. static let titleTooltipDelay: Duration = .seconds(2) // Arc's metrics: 40 pt rows on a 41 pt pitch, an 18 pt favicon frame 9 pt // into the pill with the favicon drawn at 16 pt, the title 8 pt after it, // and a 36 pt pill. The close button is 26 pt, 5 pt from the pill's end. static let pillHeight: CGFloat = 36 static let pillInset: CGFloat = 2 static let spacing: CGFloat = 1 static let pitch = pillHeight + 2 * pillInset + spacing static let horizontalPadding: CGFloat = 9 static let iconSize: CGFloat = 18 static let faviconSize: CGFloat = 16 static let closeSize = IconButtonSize.tab static let speakerLeading: CGFloat = 4.5 static let speakerTrailing: CGFloat = 5.5 static let closeTrailing: CGFloat = 5 static let titleGap: CGFloat = 8 static let cornerRadius: CGFloat = 10 static let titleFont = Font.system(size: 14) var body: some View { HStack(spacing: Self.titleGap) { // The favicon, the speaker and the title, with their own gaps: the // speaker sits between the other two, as in Arc, and collapses to // nothing while there's no sound. HStack(spacing: 0) { Group { if tab.isLoading && tab.favicon == nil { ProgressView().controlSize(.small) } else if isIncognito && tab.isNewTabPage { // Chromium gives the incognito new tab its goggles as a favicon. Show the // crossed-out eye instead, near-white like the header's badge, as in Arc. Image(systemName: "eye.slash").foregroundStyle(Color.primary.opacity(0.94)) } else if let favicon = tab.favicon { Image(nsImage: favicon).resizable().interpolation(.high) .frame(width: Self.faviconSize, height: Self.faviconSize) } else { Image(systemName: "globe").foregroundStyle(.secondary) } } .frame(width: Self.iconSize, height: Self.iconSize) .accessibilityElement() .accessibilityIdentifier("clair.tabFavicon") // The speaker's glyph keeps the same visible gap from the favicon and // from the title as the title has from the favicon without it (10 pt // drawn to drawn): its 26 pt button pads the glyph by about 4 pt, and // the drawn favicon sits 1 pt inside its frame. It stays in the row, // collapsed, so it grows and fades in and out; the list's rows don't // animate an inserted view. TabSpeakerButton(isMuted: tab.isMuted) { muted in actions?.setMuted(muted, tab: tab.tabID) } .scaleEffect(showsSpeaker ? 1 : 0.5) .opacity(showsSpeaker ? 1 : 0) .frame(width: showsSpeaker ? TabRow.closeSize : 0) .padding(.leading, showsSpeaker ? Self.speakerLeading : 0) .padding(.trailing, showsSpeaker ? Self.speakerTrailing : Self.titleGap) .allowsHitTesting(showsSpeaker) .accessibilityHidden(!showsSpeaker) if let rename { RenameField(text: shownTitle, placeholder: tab.title, rename: rename, current: tab.name) { change in actions?.endRenaming(.tab(tab.tabID), change: change) } } else { Text(shownTitle).font(Self.titleFont).lineLimit(1) .accessibilityIdentifier("clair.tabTitle") } } Spacer(minLength: 0) if let peek { PeekBadge(peek: peek) .padding(.trailing, isHovered ? 0 : PeekBadge.trailing - Self.horizontalPadding) } // A double-click's field leaves the button, as Arc's; Rename…'s takes // its room. if isHovered && rename?.selectsAll != true { TabCloseButton { actions?.close(tab: tab.tabID) } .padding(.trailing, Self.closeTrailing - Self.horizontalPadding) } } .padding(.horizontal, Self.horizontalPadding) .frame(height: Self.pillHeight) .background { if isActive { // Lighter while the window isn't key, as in Arc. RoundedRectangle(cornerRadius: Self.cornerRadius, style: .continuous) .fill(SidebarFill.activePill(colorScheme, isWindowKey: isWindowKey)) .shadow(color: SidebarFill.activeShadow, radius: 2, y: 1) } else if isHovered { RoundedRectangle(cornerRadius: Self.cornerRadius, style: .continuous).fill(SidebarFill.hover(colorScheme)) } } .padding(.top, Self.pillInset) .padding(.bottom, Self.pillInset + Self.spacing) .contentShape(Rectangle()) .onTapGesture { actions?.activate(tab: tab.tabID) } // tab-names: a double-click renames, with the caret where it clicked. .simultaneousGesture( SpatialTapGesture(count: 2).onEnded { value in guard canPin else { return } let titleStart = Self.horizontalPadding + Self.iconSize + Self.titleGap actions?.beginRenaming( RowRename( row: .tab(tab.tabID), selectsAll: false, caret: RenameField.caret(in: shownTitle, at: value.location.x - titleStart))) }) // AppKit's context menu rather than SwiftUI's, which makes the list draw // a selection ring around the row (design D18). .overlay(RowClickCatcher(onMiddleClick: { actions?.close(tab: tab.tabID) }, menuItems: menuItems)) // The hover background and the close button come and go at once. .onHover { isHovered = $0 } // The name, or the full title and the hint, once the pointer has rested // on the row (design D18, tab-names spec). .tooltip( canPin ? PinsLogic.tooltip(title: tab.title, name: tab.name) : (tab.title.isEmpty ? nil : tab.title), delay: Self.titleTooltipDelay) // The speaker comes and goes with the sound, the title making room. The // list's rows ignore implicit animations, so the row animates its own // state. .onAppear { showsSpeaker = tab.isAudible || tab.isMuted } .onChange(of: tab.isAudible || tab.isMuted) { _, shows in withAnimation(.snappy(duration: 0.25)) { showsSpeaker = shows } } .accessibilityElement(children: .contain) .accessibilityLabel(shownTitle) .accessibilityIdentifier("clair.tabRow") .accessibilityAddTraits(isActive ? .isSelected : []) } } /// The favicon of a tab's Peek, at the trailing end of the tab's row /// (add-peek design D4): the page's favicon on a small rounded tile. struct PeekBadge: View { let peek: any ClairTab // Arc's, measured: a 24 pt tile holding a 14 pt favicon, 6 pt from the // pill's end. static let size: CGFloat = 24 static let faviconSize: CGFloat = 14 /// From the badge to the pill's end while the close button is hidden. static let trailing: CGFloat = 6 @Environment(\.colorScheme) private var colorScheme var body: some View { Group { if let favicon = peek.favicon { Image(nsImage: favicon).resizable().interpolation(.high) } else { Image(systemName: "globe").resizable().foregroundStyle(.secondary) } } .frame(width: Self.faviconSize, height: Self.faviconSize) .frame(width: Self.size, height: Self.size) .background( Color.primary.opacity(colorScheme == .dark ? 0.12 : 0.07), in: .rect(cornerRadius: 7, style: .continuous)) .accessibilityElement() .accessibilityLabel(peek.title.isEmpty ? "Peek" : "Peek: \(peek.title)") .accessibilityIdentifier("clair.peekBadge") } } /// The tab's speaker, as in Arc: the outline speaker with three waves, or /// crossed out while muted, 17.5 pt wide in mid-grey on a 26 pt button /// with a hover background. SF Symbols animates the change. struct TabSpeakerButton: View { let isMuted: Bool /// Asks for the tab to be muted (true) or unmuted. let setMuted: (Bool) -> Void /// What Chromium last reported, kept here so the change animates: the /// list's rows ignore implicit animations. The button only ever shows what /// Chromium reports, so it can't fall out of step with the tab. @State private var shownMuted: Bool? var body: some View { let muted = shownMuted ?? isMuted // Arc's grey, which measures about 125 on the light pill. IconButton( help: muted ? "Unmute this tab" : "Mute this tab", size: TabRow.closeSize, dimsWhenInactive: false, foreground: Color.primary.opacity(0.68) ) { setMuted(!isMuted) } label: { Image(systemName: muted ? "speaker.slash" : "speaker.wave.3") .font(.system(size: 13.5, weight: .medium)) .contentTransition(.symbolEffect(.replace.magic(fallback: .replace), options: .speed(1.5))) } .onChange(of: isMuted) { _, reported in withAnimation(.snappy(duration: 0.2)) { shownMuted = reported } } .accessibilityIdentifier("clair.tabSpeaker") } } /// The tab's close button, as in Arc: 26 pt with a 10 pt glyph and its own /// hover background. struct TabCloseButton: View { let action: () -> Void var body: some View { IconButton( systemImage: "xmark", help: "Close tab", shortcut: ["⌘", "W"], size: TabRow.closeSize, glyphSize: 12, glyphWeight: .bold, dimsWhenInactive: false, action: action ) .accessibilityIdentifier("clair.tabClose") } } extension TabRow { /// The row's context menu (sidebar spec, "Today tab menu"): Rename… and /// the pin items only where the window shows pins. var menuItems: [RowClickCatcher.Item] { Self.menuItems(tab: tab, canPin: canPin, moveTargets: moveTargets, actions: actions) } /// A Today tab's menu, or a Today split's chip's with `inSplit` /// (add-split-view, "The split row's menu"). static func menuItems( tab: any ClairTab, canPin: Bool, moveTargets: [any ClairSpace], actions: (any SidebarActions)?, inSplit: Bool = false ) -> [RowClickCatcher.Item] { let tabID = tab.tabID, isMuted = tab.isMuted let menu = PinsLogic.todayMenu( isMuted: isMuted, canMoveToSpace: !moveTargets.isEmpty, canPin: canPin, inSplit: inSplit) return menu.compactMap { item -> RowClickCatcher.Item? in guard let item else { return .separator } switch item.command { case .copyLink: return .action(item.title) { actions?.copyLink(of: tabID) } case .rename: guard canPin else { return nil } return .action(item.title) { actions?.beginRenaming(RowRename(row: .tab(tabID), selectsAll: true, caret: nil)) } case .duplicate: return .action(item.title) { actions?.duplicate(tab: tabID) } case .mute, .unmute: return .action(item.title) { actions?.setMuted(!isMuted, tab: tabID) } case .pin: return .action(item.title) { actions?.pin(tab: tabID) } case .pinToFavorites: return .action(item.title) { actions?.pinToFavorites(tab: tabID) } case .moveToSpace: // add-spaces D7: every other Space, and no item with one Space. return .submenu( item.title, moveTargets.map { space in let spaceID = space.spaceID return .action(SpacesLogic.label(of: space)) { actions?.moveTab(tabID, toSpace: spaceID) } }) case .closeTab: return .action(item.title) { actions?.close(tab: tabID) } case .closeTabsBelow: return .action(item.title) { actions?.closeTabs(below: tabID) } case .splitWithTab: return .submenu(item.title, actions?.splitWithTabItems(tab: tabID, pin: nil) ?? []) case .separateTabs: return .action(item.title) { actions?.separateSplit(of: tabID) } default: return nil } } } } /// Catches middle clicks and context-menu clicks (a right click or a /// Control-click), and lets every other click through. Opening the menu here /// keeps the press from the list, which would otherwise ring the row. struct RowClickCatcher: NSViewRepresentable { enum Item { case action(String, () -> Void) /// An action with a checkmark, as the Space's profile in Set Profile. case checked(String, () -> Void) /// Shown, but can't be chosen. case disabled(String) case submenu(String, [Item]) /// An action with an icon, as a tab with its favicon (add-split-view D9). case icon(String, NSImage?, () -> Void) /// A disabled submenu. case disabledSubmenu(String) case separator /// The item's title, or nil for a separator. var title: String? { switch self { case .action(let title, _), .checked(let title, _), .disabled(let title), .submenu(let title, _), .icon(let title, _, _), .disabledSubmenu(let title): title case .separator: nil } } } let onMiddleClick: () -> Void let menuItems: [Item] /// A ready-made menu, used instead of `menuItems`. var menuProvider: (() -> NSMenu?)? func makeNSView(context: Context) -> CatcherView { let view = CatcherView() updateNSView(view, context: context) return view } func updateNSView(_ view: CatcherView, context: Context) { view.onMiddleClick = onMiddleClick view.menuItems = menuItems view.menuProvider = menuProvider } final class CatcherView: NSView { var onMiddleClick: (() -> Void)? var menuItems: [Item] = [] var menuProvider: (() -> NSMenu?)? override func hitTest(_ point: NSPoint) -> NSView? { guard let event = NSApp.currentEvent else { return nil } switch event.type { case .otherMouseDown, .otherMouseUp, .rightMouseDown, .rightMouseUp: return super.hitTest(point) case .leftMouseDown where event.modifierFlags.contains(.control): return super.hitTest(point) default: return nil } } override func otherMouseUp(with event: NSEvent) { if event.buttonNumber == 2 { onMiddleClick?() } } override func mouseDown(with event: NSEvent) { // Only a Control-click gets here. if let menu = makeMenu() { NSMenu.popUpContextMenu(menu, with: event, for: self) } } override func menu(for event: NSEvent) -> NSMenu? { makeMenu() } private func makeMenu() -> NSMenu? { if let menuProvider { return menuProvider() } return Self.menu(menuItems, target: self) } /// The menu for `items`, whose actions `target` runs. static func menu(_ items: [Item], target: CatcherView) -> NSMenu { let menu = NSMenu() menu.autoenablesItems = false for item in items { switch item { case .separator: menu.addItem(.separator()) case .action(let title, let action), .checked(let title, let action): let menuItem = NSMenuItem(title: title, action: #selector(runItem(_:)), keyEquivalent: "") menuItem.target = target menuItem.representedObject = ItemAction(run: action) if case .checked = item { menuItem.state = .on } menu.addItem(menuItem) case .disabled(let title): let menuItem = NSMenuItem(title: title, action: nil, keyEquivalent: "") menuItem.isEnabled = false menu.addItem(menuItem) case .disabledSubmenu(let title): let menuItem = NSMenuItem(title: title, action: nil, keyEquivalent: "") menuItem.submenu = NSMenu() menuItem.isEnabled = false menu.addItem(menuItem) case .icon(let title, let image, let action): let menuItem = NSMenuItem(title: title, action: #selector(runItem(_:)), keyEquivalent: "") menuItem.target = target menuItem.representedObject = ItemAction(run: action) menuItem.image = image.map(SplitRows.menuIcon) ?? NSImage(systemSymbolName: "globe", accessibilityDescription: nil) menu.addItem(menuItem) case .submenu(let title, let items): let menuItem = NSMenuItem(title: title, action: nil, keyEquivalent: "") menuItem.submenu = Self.menu(items, target: target) menu.addItem(menuItem) } } return menu } @objc private func runItem(_ sender: NSMenuItem) { (sender.representedObject as? ItemAction)?.run() } } /// A menu item's action, boxed for `representedObject`. final class ItemAction: NSObject { let run: () -> Void init(run: @escaping () -> Void) { self.run = run } } }