SPB Git

Repository Public License

RegularBrowserView.swift 16.4 KB 388 lines swift

1import LunarDesign2import NavigatorCore3import SwiftUI45/// The obsidian field behind the chrome: the theme gradient, a faint glow and a still, sparse starfield.6struct BackdropView: View {7    @Environment(\.lunarTheme) private var theme8    var density = 0.79    var isAnimated = false1011    var body: some View {12        ZStack {13            Rectangle().fill(theme.windowGradient)14            // A faint lunar glow rising from the top edge, as in the app icon.15            GeometryReader { proxy in16                Rectangle().fill(RadialGradient(17                    colors: [theme.glow.withAlpha(theme.glow.alpha * 0.5).color, theme.glow.withAlpha(0).color],18                    center: UnitPoint(x: 0.5, y: 0), startRadius: 0, endRadius: max(240, proxy.size.height * 0.7)))19            }20            LunarStarfield(density: density, showsOrbits: true, isAnimated: isAnimated)21        }22        .ignoresSafeArea()23        .accessibilityHidden(true)24    }25}2627/// Glass circle that opens the window menu. Its entries are the commands placed in `.windowMenu`, followed —28/// on a phone, where the capsule does not exist — by the toolbar items features put in the capsule.29struct ToolbarMenuButton: View {30    @Environment(\.lunarTheme) private var theme31    @Environment(BrowserStore.self) private var store32    let window: WindowModel33    var diameter: CGFloat = LunarMetrics.controlHeight34    /// Compact width (phone dock): window and layout items are left out by the commands themselves.35    var isCompactWidth = false3637    var body: some View {38        let context = window.commandContext(source: .windowMenu, isCompactWidth: isCompactWidth)39        Menu {40            CommandMenuItems(groups: store.registry.windowMenuCommands, context: context, showsIcons: true)41            if isCompactWidth {42                let items = store.registry.toolbarItems(in: .capsule).filter { $0.isVisible(context) }43                if !items.isEmpty {44                    Divider()45                    ForEach(items) { item in46                        let state = item.state(context)47                        Button {48                            if case .popover = item.action { window.toolbarSheetItemID = item.id } else { window.perform(item.action, context: context) }49                        } label: {50                            if case .symbol(let name) = state.icon ?? item.icon {51                                Label(item.label.string, systemImage: name)52                            } else {53                                Text(item.label.string)54                            }55                        }56                        .disabled(!state.isEnabled || !window.isEnabled(item.action, context: context))57                    }58                }59            }60        } label: {61            LunarIconView(.symbol("ellipsis"), size: diameter * 0.5)62                .foregroundStyle(theme.textSecondary.mixed(with: theme.textPrimary, 0.5))63                .frame(width: diameter, height: diameter)64                .contentShape(Circle())65        }66        .menuStyle(.button)67        .buttonStyle(.plain)68        .menuIndicator(.hidden)69        .fixedSize()70        .accessibilityLabel(L("Menu"))71        .accessibilityIdentifier("toolbar.menu")72        .help(L("Menu"))73    }74}7576extension TabLayoutMode {77    var localizedName: String {78        switch self {79        case .vertical: L("Vertical")80        case .horizontal: L("Horizontal")81        case .compact: L("Compact")82        case .grid: L("Grid")83        case .orbit: L("Orbit")84        }85    }86}8788/// Bookmark star: reflects whether the page is bookmarked.89struct BookmarkButton: View {90    @Environment(BrowserStore.self) private var store91    let window: WindowModel92    var diameter: CGFloat = LunarMetrics.controlHeight93    @State private var isBookmarked = false9495    var body: some View {96        let url = window.selectedTab?.engine.url97        LunarIconButton(systemName: isBookmarked ? "bookmark.fill" : "bookmark",98                        label: isBookmarked ? L("Edit bookmark") : L("Bookmark this page"), isActive: isBookmarked,99                        diameter: diameter) {100            Task { await toggle(url) }101        }102        .disabled(!window.hasPage)103        .accessibilityIdentifier("toolbar.bookmark")104        .task(id: "\(url?.absoluteString ?? "")#\(store.bookmarksRevision)#\(window.dataProfileID)") {105            guard let url else { isBookmarked = false; return }106            isBookmarked = !((try? await store.database.bookmarks.bookmarks(for: url, profile: window.dataProfileID)) ?? []).isEmpty107        }108    }109110    private func toggle(_ url: URL?) async {111        guard let url else { return }112        if let existing = try? await store.database.bookmarks.bookmarks(for: url, profile: window.dataProfileID).first {113            window.bookmarkDraft = BookmarkDraft(id: existing.id, isNew: false, kind: .link, title: existing.title,114                                                 address: url.absoluteString, parentID: existing.parentID,115                                                 isFavorite: existing.isFavorite)116        } else {117            window.bookmarkCurrentPage()118        }119    }120}121122struct ShareButton: View {123    @Environment(\.lunarTheme) private var theme124    let window: WindowModel125    var diameter: CGFloat = LunarMetrics.controlHeight126127    var body: some View {128        if let tab = window.selectedTab, let url = tab.engine.url, !InternalAddress.isInternal(url) {129            ShareLink(item: url, subject: Text(tab.title)) {130                LunarIconView(.symbol("square.and.arrow.up"), size: diameter * 0.5)131                    .foregroundStyle(theme.textSecondary.mixed(with: theme.textPrimary, 0.5))132                    .frame(width: diameter, height: diameter)133                    .contentShape(Circle())134            }135            .buttonStyle(LunarPressStyle())136            .accessibilityLabel(L("Share"))137            .accessibilityIdentifier("toolbar.share")138            .help(L("Share"))139        }140    }141}142143/// The floating capsule: back, forward, address field and the page actions.144struct ToolbarCapsule: View {145    @Environment(BrowserStore.self) private var store146    let window: WindowModel147    var omniboxWidth: CGFloat148    var showsNewTab = false149150    /// Width the fixed controls take, for the row that sizes the address field.151    static func fixedWidth(itemCount: Int) -> CGFloat { 262 + CGFloat(itemCount) * (LunarMetrics.controlHeight + LunarSpacing.unit) }152153    var body: some View {154        LunarCapsuleBar {155            LunarIconButton(systemName: "chevron.left", label: L("Back")) { window.run(ShellCommandID.back, source: .toolbar) }156                .disabled(!window.canGoBack)157                .accessibilityIdentifier("toolbar.back")158            LunarIconButton(systemName: "chevron.right", label: L("Forward")) { window.run(ShellCommandID.forward, source: .toolbar) }159                .disabled(!window.canGoForward)160                .accessibilityIdentifier("toolbar.forward")161            OmniboxBar(window: window).frame(width: omniboxWidth)162            BookmarkButton(window: window)163            ShareButton(window: window)164            ForEach(store.registry.toolbarItems(in: .capsule)) { item in165                ContributedToolbarButton(window: window, item: item)166            }167            ToolbarMenuButton(window: window)168            if showsNewTab {169                LunarIconButton(systemName: "plus", label: L("New tab"), style: .accent) {170                    window.run(ShellCommandID.newTab, source: .toolbar)171                }172                .accessibilityIdentifier("toolbar.newTab")173            }174        }175    }176}177178/// The "+" of a tab strip.179public struct NewTabButton: View {180    private let window: WindowModel181182    public init(window: WindowModel) { self.window = window }183184    public var body: some View {185        LunarIconButton(systemName: "plus", label: L("New tab"), style: .glass, diameter: 32) {186            window.run(ShellCommandID.newTab, source: .toolbar)187        }188        .accessibilityIdentifier("strip.newTab")189    }190}191192/// The toolbar row of a regular-width window: the capsule (back, forward, address field, page actions, items193/// contributed by features), centred, with the button that brings the sidebar back when it is collapsed.194/// A building block for tab layouts.195public struct BrowserToolbarRow: View {196    @Environment(BrowserStore.self) private var store197    private let window: WindowModel198    private let leadingReserve: CGFloat199200    /// `leadingReserve`: width to keep free at the leading edge (window buttons on the Mac).201    public init(window: WindowModel, leadingReserve: CGFloat = 0) {202        self.window = window203        self.leadingReserve = leadingReserve204    }205206    public var body: some View {207        GeometryReader { proxy in208            let showsSidebarToggle = window.tabLayout == .vertical && window.isSidebarCollapsed209                && !window.hiddenChrome.contains(.sidebar)210            let showsBadges = !(window.tabLayout == .vertical && !window.isSidebarCollapsed) && !store.registry.badges(for: window).isEmpty211            let reserve = leadingReserve + (showsSidebarToggle ? 44 : 0) + (showsBadges ? 34 : 0)212            let context = window.commandContext(source: .toolbar)213            let items = store.registry.toolbarItems(in: .capsule).filter { $0.isVisible(context) }.count214            // The field gets what the fixed controls leave, up to its ideal width.215            let fieldWidth = min(430, max(160, proxy.size.width - 2 * reserve - ToolbarCapsule.fixedWidth(itemCount: items)))216            ZStack {217                ToolbarCapsule(window: window, omniboxWidth: fieldWidth, showsNewTab: Self.capsuleShowsNewTab && window.tabLayout == .vertical)218                HStack(spacing: LunarSpacing.xs) {219                    if showsSidebarToggle {220                        LunarIconButton(systemName: "sidebar.left", label: L("Show sidebar"), style: .glass, diameter: 32) {221                            window.isSidebarCollapsed = false222                        }223                        .accessibilityIdentifier("sidebar.toggle")224                    }225                    if showsBadges { WindowBadgesView(window: window, isCompact: true) }226                    Spacer()227                }228                .padding(.leading, leadingReserve)229            }230            .frame(width: proxy.size.width, height: proxy.size.height)231        }232        .frame(height: 52)233    }234235    /// On iPad the capsule carries the accent "new tab" button, as in the reference mockup.236    static var capsuleShowsNewTab: Bool {237        #if os(iOS)238        true239        #else240        false241        #endif242    }243}244245// MARK: - The shell's tab layouts246247/// Sidebar with the tab list, toolbar above the page.248struct VerticalTabLayout: View {249    let context: TabLayoutContext250251    var body: some View {252        let window = context.window253        let showsSidebar = !window.isSidebarCollapsed && !context.hiddenChrome.contains(.sidebar)254        let inset = context.windowControlsInset255        HStack(spacing: LunarSpacing.sm) {256            if showsSidebar {257                SidebarView(window: window)258                    .frame(width: RegularBrowserView.sidebarWidth)259                    .lunarGlass(.lg, style: .chrome, elevation: .floating)260                    .transition(.move(edge: .leading).combined(with: .opacity))261                    .accessibilityElement(children: .contain)262                    .accessibilityIdentifier("sidebar")263            }264            VStack(spacing: LunarSpacing.sm) {265                if !context.hiddenChrome.contains(.toolbar) {266                    // Without the sidebar the window buttons share this row: it moves up to their centre line.267                    BrowserToolbarRow(window: window, leadingReserve: showsSidebar ? 0 : inset)268                        .padding(.top, !showsSidebar && inset > 0 ? -LunarSpacing.sm : 0)269                } else if !showsSidebar, inset > 0 {270                    // Nothing but the page: keep the strip of the window buttons clear.271                    Color.clear.frame(height: 22)272                }273                WebAreaView(window: window)274            }275        }276        .lunarAnimation(.smooth, value: showsSidebar)277    }278}279280/// Tab strip on the first row, toolbar on the second.281struct HorizontalTabLayout: View {282    let context: TabLayoutContext283284    var body: some View {285        let window = context.window286        let inset = context.windowControlsInset287        let showsStrip = !context.hiddenChrome.contains(.tabStrip), showsToolbar = !context.hiddenChrome.contains(.toolbar)288        VStack(spacing: LunarSpacing.sm) {289            if showsStrip {290                HStack(spacing: LunarSpacing.xs) {291                    TabStripView(window: window)292                    NewTabButton(window: window)293                }294                .padding(.leading, inset)295                .frame(height: LunarMetrics.tabChipHeight + 4)296                .padding(.top, inset > 0 ? -6 : 0)297            }298            if showsToolbar {299                BrowserToolbarRow(window: window, leadingReserve: showsStrip ? 0 : inset)300                    .padding(.top, !showsStrip && inset > 0 ? -LunarSpacing.sm : 0)301            }302            if !showsStrip, !showsToolbar, inset > 0 { Color.clear.frame(height: 22) }303            WebAreaView(window: window)304        }305    }306}307308/// One row: the capsule, then the tabs as icons.309struct CompactTabLayout: View {310    let context: TabLayoutContext311312    var body: some View {313        let window = context.window314        let inset = context.windowControlsInset315        let showsStrip = !context.hiddenChrome.contains(.tabStrip), showsToolbar = !context.hiddenChrome.contains(.toolbar)316        VStack(spacing: LunarSpacing.sm) {317            if showsStrip || showsToolbar {318                GeometryReader { proxy in319                    HStack(spacing: LunarSpacing.xs) {320                        if showsToolbar {321                            ToolbarCapsule(window: window, omniboxWidth: min(360, max(180, proxy.size.width * 0.34)))322                        }323                        if showsStrip {324                            TabStripView(window: window, isCondensed: true)325                            NewTabButton(window: window)326                        }327                    }328                    .padding(.leading, inset)329                    .frame(height: proxy.size.height)330                }331                .frame(height: 52)332                .padding(.top, inset > 0 ? -LunarSpacing.sm : 0)333            } else if inset > 0 {334                Color.clear.frame(height: 22)335            }336            WebAreaView(window: window)337        }338    }339}340341/// Mac and iPad (regular width): the tab layout chosen for the window (sidebar or strip, toolbar, web area)342/// and, beside it, the side panel.343struct RegularBrowserView: View {344    @Environment(\.lunarTheme) private var theme345    @Environment(\.windowControlsInset) private var controlsInset346    @Environment(BrowserStore.self) private var store347    let window: WindowModel348349    static let sidebarWidth: CGFloat = 268350    static let panelWidth: CGFloat = 348351352    var body: some View {353        let hidden = window.hiddenChrome354        let panel = hidden.contains(.panel) ? nil : window.panel.flatMap { store.registry.panel($0) }355        ZStack {356            BackdropView()357                .dropDestination(for: URL.self) { urls, _ in358                    store.activeWindowID = window.id359                    store.open(urls: urls)360                    return !urls.isEmpty361                }362            HStack(spacing: LunarSpacing.sm) {363                layout(hidden: hidden)364                if let panel {365                    PanelContainer(window: window, panel: panel)366                        .frame(width: panel.preferredWidth ?? Self.panelWidth)367                        .lunarGlass(.lg, style: .chrome, elevation: .floating)368                        .transition(.move(edge: .trailing).combined(with: .opacity))369                }370            }371            .padding(LunarSpacing.sm)372        }373        .lunarAnimation(.smooth, value: window.isSidebarCollapsed)374        .lunarAnimation(.smooth, value: window.panel)375        .lunarAnimation(.smooth, value: window.tabLayout)376        .lunarAnimation(.smooth, value: hidden)377    }378379    @ViewBuilder private func layout(hidden: ChromeElements) -> some View {380        let context = TabLayoutContext(window: window, windowControlsInset: controlsInset, hiddenChrome: hidden)381        if let contribution = store.registry.tabLayout(window.tabLayout) {382            contribution.body(context)383        } else {384            VerticalTabLayout(context: context)385        }386    }387}388
Files main
Menu

Theme