Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Resources/app_icon.icns
Binary file not shown.
56 changes: 56 additions & 0 deletions Sources/LadybirdEngine/BrowserCommandRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Lets the Settings window act on the browser without owning one.
*/

import AppKit
import Foundation

/// Routes a command from the Settings window to the browser window that is actually running an engine.
///
/// Settings must never create a `BrowserModel`: a second one would start a second `PortApp`, which the engine refuses
/// (`BrowserModel.start` guards on `processApp`). So the entry points that need to open a page register the model they
/// already have, and Settings calls through here. The reference is weak — a closed window unregisters itself by
/// deallocating, and a command with no live window does nothing.
@MainActor
public final class BrowserCommandRouter {
public static let shared = BrowserCommandRouter()

private weak var activeModel: BrowserModel?

public init() {}

/// Called by each window as it starts. The most recent window wins, which is the one the user was last looking at.
public func register(_ model: BrowserModel) {
activeModel = model
}

/// Opens the engine's own settings page, which owns search, privacy, network and blocking.
///
/// Opens in a new tab rather than navigating the current one: the user opened Settings from the app menu, so the
/// page they were reading is not theirs to discard.
@discardableResult
public func openEngineSettings() -> BrowserTab? {
activeModel?.openTab(url: Self.engineSettingsURL)
}

public static let engineSettingsURL = "about:settings"

/// Opens the engine's settings page and dismisses the Settings window that asked for it.
///
/// The close lives here because it needs AppKit, and `LadybirdUI` is deliberately not allowed to touch it.
public func openEngineSettingsAndDismissSettingsWindow() {
// Only dismiss once the page is actually opening. `openTab` returns nil when there is no live engine (startup
// failed) or the tab could not be created; closing Settings then would look like success and leave the user on
// the page they were already reading, with the window they came from gone.
guard openEngineSettings() != nil else { return }
// `keyWindow` is the Settings window: it is modal-free but key while the button is pressed. Guarded by class name
// so a future non-Settings window cannot be closed by a stale call.
if NSApp.keyWindow?.className.contains("Settings") == true {
NSApp.keyWindow?.performClose(nil)
}
}
}
2 changes: 2 additions & 0 deletions Sources/LadybirdMac/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct AvaApp: App {
Settings {
SettingsView()
}
// The starting size only. The view sets its own minimum, so this stays resizable.
.defaultSize(width: 700, height: 440)
}
}

Expand Down
10 changes: 10 additions & 0 deletions Sources/LadybirdUI/BrowserWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public struct BrowserWindow: View {
}
}
.modifier(SpaceActionModifier(model: model, actionState: spaceActionState))
// The width is not persisted here on purpose: SwiftUI's NavigationSplitView already writes the column's
// frame to `NSSplitView Subview Frames` in the app's preferences and restores it on launch. Tracking it
// ourselves duplicated that and could disagree with it. `ideal` only seeds a first launch.
.navigationSplitViewColumnWidth(min: 180, ideal: 240, max: 320)
} detail: {
content
Expand Down Expand Up @@ -107,6 +110,13 @@ public struct BrowserWindow: View {
homeURL: Self.launchURL,
overrideRestoredTab: Self.launchURLArgument != nil
)
// Settings needs a live window to open the engine's settings page; it must not make its own model.
// Only when this window really owns an engine: a second window trips the `processApp` guard in
// `start`, sets `failure` and returns, so registering it would replace a working model with a dead one
// and make "More settings" a no-op.
if model.failure == nil {
BrowserCommandRouter.shared.register(model)
}
addressState.handleTabSwitch(to: model.currentURL)
}
.onChange(of: model.selectedTabID) { _, _ in
Expand Down
Loading
Loading