SPB Git

Repository Public License

FeatureContributions.swift 14.5 KB 316 lines swift

1import Foundation2import LunarDesign3import NavigatorCore4import SwiftUI5import WebRuntime67// MARK: - Shared pieces89/// What happens when the user activates a contributed control (toolbar item, sidebar footer item).10public enum ContributionAction {11    /// Runs a registered command; the control is disabled while the command is unavailable.12    case command(String)13    /// Opens the side panel, or closes it when it is already open.14    case panel(SidePanelID)15    /// Presents a window overlay.16    case overlay(String)17    case perform(@MainActor (CommandContext) -> Void)18    /// Shows content anchored to the control (a popover; a sheet when the control sits in the phone menu).19    case popover(@MainActor (WindowModel) -> AnyView)20}2122/// Live appearance of a contributed control.23public struct ContributionState: Equatable, Sendable {24    /// The "on" look (panel open, protection active).25    public var isActive: Bool26    /// A small count on the control; `nil` or 0 shows nothing.27    public var badge: Int?28    public var isEnabled: Bool29    /// Replaces the registered icon (a filled variant, a state glyph).30    public var icon: LunarIcon?31    /// Spoken after the label by assistive technologies ("3 trackers blocked").32    public var accessibilityValue: String?3334    public init(isActive: Bool = false, badge: Int? = nil, isEnabled: Bool = true, icon: LunarIcon? = nil,35                accessibilityValue: String? = nil) {36        self.isActive = isActive; self.badge = badge; self.isEnabled = isEnabled; self.icon = icon37        self.accessibilityValue = accessibilityValue38    }3940    public static let normal = ContributionState()41}4243// MARK: - Side panels4445/// Identifier of a panel shown beside the page (Mac, iPad) or in a sheet (iPhone).46public struct SidePanelID: RawRepresentable, Hashable, Sendable, Identifiable, ExpressibleByStringLiteral,47    CustomStringConvertible {48    public let rawValue: String49    public init(rawValue: String) { self.rawValue = rawValue }50    public init(_ rawValue: String) { self.rawValue = rawValue }51    public init(stringLiteral value: String) { rawValue = value }52    public var id: String { rawValue }53    public var description: String { rawValue }5455    public static let history: SidePanelID = "history"56    public static let bookmarks: SidePanelID = "bookmarks"57    public static let downloads: SidePanelID = "downloads"58    public static let settings: SidePanelID = "settings"59    public static let help: SidePanelID = "help"60}6162public struct SidePanelContribution: Identifiable {63    public let id: SidePanelID64    public var title: LocalizedText65    public var icon: LunarIcon66    /// Width beside the page on Mac and iPad; `nil` uses the standard width (348 pt).67    public var preferredWidth: CGFloat?68    public var content: @MainActor (WindowModel) -> AnyView6970    /// `content` fills the panel under the shell's header (title, icon, close button). It receives the store71    /// and the theme through the environment.72    public init<Content: View>(id: SidePanelID, title: LocalizedText, icon: LunarIcon, preferredWidth: CGFloat? = nil,73                               @ViewBuilder content: @escaping @MainActor (WindowModel) -> Content) {74        self.id = id; self.title = title; self.icon = icon; self.preferredWidth = preferredWidth75        self.content = { AnyView(content($0)) }76    }77}7879// MARK: - Toolbar items8081public struct ToolbarItemPlacement: OptionSet, Hashable, Sendable {82    public let rawValue: Int83    public init(rawValue: Int) { self.rawValue = rawValue }84    /// Inside the address field, before the reload button: page-scoped indicators (shield count, reader). Shown85    /// in the capsule's field on Mac and iPad and in the address pill on iPhone.86    public static let addressField = ToolbarItemPlacement(rawValue: 1)87    /// A button of the toolbar capsule, after Share (Mac, iPad). On iPhone the item becomes an entry of the dock88    /// menu, with its label; popover content is then presented as a sheet.89    public static let capsule = ToolbarItemPlacement(rawValue: 2)90}9192public struct ToolbarItemContribution: Identifiable {93    public let id: String94    public var label: LocalizedText95    public var icon: LunarIcon96    public var placement: ToolbarItemPlacement97    public var order: Int98    /// For example `{ $0.isWebPage }` for an item that only makes sense on an http(s) page.99    public var isVisible: @MainActor (CommandContext) -> Bool100    public var state: @MainActor (CommandContext) -> ContributionState101    public var action: ContributionAction102103    public init(id: String, label: LocalizedText, icon: LunarIcon, placement: ToolbarItemPlacement, order: Int = 100,104                isVisible: @escaping @MainActor (CommandContext) -> Bool = { _ in true },105                state: @escaping @MainActor (CommandContext) -> ContributionState = { _ in .normal },106                action: ContributionAction) {107        self.id = id; self.label = label; self.icon = icon; self.placement = placement; self.order = order108        self.isVisible = isVisible; self.state = state; self.action = action109    }110}111112// MARK: - Sidebar113114public struct SidebarSectionContribution: Identifiable {115    public enum Placement: Sendable { case aboveTabs, belowTabs }116117    public let id: String118    public var placement: Placement119    public var order: Int120    public var isVisible: @MainActor (WindowModel) -> Bool121    public var content: @MainActor (WindowModel) -> AnyView122123    /// A block of the vertical sidebar, above or below the tab list. The sidebar is 268 pt wide; the section124    /// draws its own header if it wants one.125    public init<Content: View>(id: String, placement: Placement, order: Int = 100,126                               isVisible: @escaping @MainActor (WindowModel) -> Bool = { _ in true },127                               @ViewBuilder content: @escaping @MainActor (WindowModel) -> Content) {128        self.id = id; self.placement = placement; self.order = order; self.isVisible = isVisible129        self.content = { AnyView(content($0)) }130    }131}132133/// An icon of the row at the bottom of the sidebar (History, Bookmarks, Downloads, Settings and what features134/// add). The row shows the first five by `order`; the others go into a "More" menu.135public struct SidebarFooterItem: Identifiable {136    public let id: String137    public var label: LocalizedText138    public var icon: LunarIcon139    public var order: Int140    public var isVisible: @MainActor (CommandContext) -> Bool141    public var state: @MainActor (CommandContext) -> ContributionState142    public var action: ContributionAction143144    public init(id: String, label: LocalizedText, icon: LunarIcon, order: Int = 100,145                isVisible: @escaping @MainActor (CommandContext) -> Bool = { _ in true },146                state: @escaping @MainActor (CommandContext) -> ContributionState = { _ in .normal },147                action: ContributionAction) {148        self.id = id; self.label = label; self.icon = icon; self.order = order149        self.isVisible = isVisible; self.state = state; self.action = action150    }151}152153/// A mark on the window chrome that tells what kind of window this is (private, focus session). Shown in the154/// sidebar header, at the leading edge of the toolbar when the sidebar is hidden, and in the phone dock.155public struct WindowBadge: Equatable, Sendable {156    public var label: String157    public var icon: LunarIcon158    public var status: LunarStatus159160    public init(label: String, icon: LunarIcon, status: LunarStatus = .neutral) {161        self.label = label; self.icon = icon; self.status = status162    }163}164165public struct WindowBadgeContribution: Identifiable {166    public let id: String167    public var order: Int168    /// `nil` when the window does not carry the badge.169    public var badge: @MainActor (WindowModel) -> WindowBadge?170171    public init(id: String, order: Int = 100, badge: @escaping @MainActor (WindowModel) -> WindowBadge?) {172        self.id = id; self.order = order; self.badge = badge173    }174}175176// MARK: - Window overlays177178public enum WindowOverlayStyle: Sendable {179    /// Floats over a dimmed window, like a command palette. The content brings its own surface (glass, card);180    /// `maxWidth` / `maxHeight` bound it and `alignment` places it (`.top` for a palette, `.center` for a dialog).181    case scrim(alignment: Alignment = .center, maxWidth: CGFloat? = 640, maxHeight: CGFloat? = nil)182    /// Covers the whole window, chrome included (the Orbit view).183    case fullCover184}185186public struct WindowOverlayContribution: Identifiable {187    public let id: String188    public var style: WindowOverlayStyle189    /// A click or tap on the scrim dismisses the overlay (`.scrim` only).190    public var dismissesOnOutsideTap: Bool191    public var content: @MainActor (WindowModel) -> AnyView192193    /// Present with `window.present(overlay:)`, dismiss with `window.dismissOverlay()`. Escape always dismisses;194    /// keyboard focus then returns to the page.195    public init<Content: View>(id: String, style: WindowOverlayStyle, dismissesOnOutsideTap: Bool = true,196                               @ViewBuilder content: @escaping @MainActor (WindowModel) -> Content) {197        self.id = id; self.style = style; self.dismissesOnOutsideTap = dismissesOnOutsideTap198        self.content = { AnyView(content($0)) }199    }200}201202// MARK: - Web area203204/// What a web-area provider is asked to draw into.205@MainActor206public struct WebAreaContext {207    public let window: WindowModel208    /// Insets and gestures the shell wants for web views in this layout (the phone dock's clearance…). Pass it209    /// to every `TabContentView` you create.210    public let webOptions: WebEngineViewOptions211    /// Room floating chrome covers at the bottom (phone dock); native content should pad by it.212    public let bottomChromeInset: CGFloat213    public let isCompactWidth: Bool214}215216/// Takes over what the web area shows for a window: several tabs side by side (Split), an article in place of217/// the page (Reader), a canvas. The shell asks the registered providers from the highest `priority` down and218/// uses the first whose `isActive(in:)` is true; with none, it shows the selected tab (`TabContentView`).219/// The shell keeps drawing what belongs to the window on top: banners, find bar, page prompts, toasts.220@MainActor221public protocol WebAreaContentProvider: AnyObject {222    var id: String { get }223    /// Higher wins. Shell default: 0. Suggested: Reader 100, Split 200, a full takeover (Canvas) 300.224    var priority: Int { get }225    /// Read observable state only (the window, its tabs, your per-window state): the web area re-evaluates it226    /// when that state changes.227    func isActive(in window: WindowModel) -> Bool228    func makeContent(_ context: WebAreaContext) -> AnyView229}230231/// Closure form of `WebAreaContentProvider`.232@MainActor233public final class WebAreaProvider: WebAreaContentProvider {234    public let id: String235    public let priority: Int236    private let active: @MainActor (WindowModel) -> Bool237    private let content: @MainActor (WebAreaContext) -> AnyView238239    public init<Content: View>(id: String, priority: Int, isActive: @escaping @MainActor (WindowModel) -> Bool,240                               @ViewBuilder content: @escaping @MainActor (WebAreaContext) -> Content) {241        self.id = id; self.priority = priority; self.active = isActive242        self.content = { AnyView(content($0)) }243    }244245    public func isActive(in window: WindowModel) -> Bool { active(window) }246    public func makeContent(_ context: WebAreaContext) -> AnyView { content(context) }247}248249/// Parts of the window chrome a feature can ask the shell to hide (Focus, a presentation mode).250public struct ChromeElements: OptionSet, Hashable, Sendable {251    public let rawValue: Int252    public init(rawValue: Int) { self.rawValue = rawValue }253    /// The vertical sidebar.254    public static let sidebar = ChromeElements(rawValue: 1)255    /// The horizontal or compact tab strip.256    public static let tabStrip = ChromeElements(rawValue: 2)257    /// The toolbar capsule (address field and buttons); on iPhone, the dock and the address pill.258    public static let toolbar = ChromeElements(rawValue: 4)259    /// The side panel, if one is open.260    public static let panel = ChromeElements(rawValue: 8)261    public static let all: ChromeElements = [.sidebar, .tabStrip, .toolbar, .panel]262}263264// MARK: - Tab layouts265266/// What a tab layout is asked to draw: everything of a regular-width window except the side panel.267@MainActor268public struct TabLayoutContext {269    public let window: WindowModel270    /// Width to keep free at the leading edge of the first row for the window buttons (Mac; 0 elsewhere and in271    /// full screen).272    public let windowControlsInset: CGFloat273    /// Chrome other features asked to hide; a layout honours it for its own tab chrome and toolbar.274    public let hiddenChrome: ChromeElements275}276277/// Renders one `TabLayoutMode` on Mac and iPad (the phone has its own layout). The shell ships vertical,278/// horizontal and compact; a feature registers `grid` and `orbit`. `supportedLayouts` is what is registered.279///280/// `body` composes the public building blocks: `SidebarView`, `TabStripView`, `BrowserToolbarRow`,281/// `WebAreaView`, `NewTabButton`.282public struct TabLayoutContribution: Identifiable {283    public let mode: TabLayoutMode284    public var title: LocalizedText285    public var icon: LunarIcon286    public var body: @MainActor (TabLayoutContext) -> AnyView287    public var id: String { mode.rawValue }288289    public init<Content: View>(mode: TabLayoutMode, title: LocalizedText, icon: LunarIcon,290                               @ViewBuilder body: @escaping @MainActor (TabLayoutContext) -> Content) {291        self.mode = mode; self.title = title; self.icon = icon292        self.body = { AnyView(body($0)) }293    }294}295296// MARK: - Settings297298public struct SettingsSectionContribution: Identifiable {299    public let id: String300    public var title: LocalizedText301    public var icon: LunarIcon302    /// Shell sections use 10…90 and 10 000 (About, always last); features start at 100.303    public var order: Int304    public var isVisible: @MainActor (WindowModel) -> Bool305    public var content: @MainActor (WindowModel) -> AnyView306307    /// `content` is laid out in a vertical stack inside the section's card, under its title. Use the Lunar308    /// components (`Toggle(...).toggleStyle(.lunar)`, `LunarSegmentedControl`, `LunarSlider`…).309    public init<Content: View>(id: String, title: LocalizedText, icon: LunarIcon, order: Int,310                               isVisible: @escaping @MainActor (WindowModel) -> Bool = { _ in true },311                               @ViewBuilder content: @escaping @MainActor (WindowModel) -> Content) {312        self.id = id; self.title = title; self.icon = icon; self.order = order; self.isVisible = isVisible313        self.content = { AnyView(content($0)) }314    }315}316
Files main
Menu

Theme