Skip to content
CefSwift
Esc
↑↓navigate↵open⌘Jpreview
On this page

Tabs and the tab strip

Chromium's own tabbed windows, tabs and groups under a tab strip you draw in SwiftUI.

import CefChromium

let window = try await CefChromeWindow.open(url: URL(string: "https://swift.org")!)
window.showsChromeUI = false
let tab = try await window.openTab(URL(string: "https://developer.apple.com")!)
try await tab.pin()
let group = try await window.profile.tabs.group([tab])
try await group?.setTitle("Reading")

Gallery: TabStripDemo, AppShellDemo. Browser example: Core/TabRail is a complete tab rail: pinned tabs, groups, dragging, overflow, keyboard and VoiceOver.

Chromium’s tabs: windows, tabs and groups

Tabs that are Chromium’s own (what chrome.tabs, chrome.tabGroups, recently closed tabs and moving tabs between windows see) live in the windows Chrome draws. CefChromium models them in three pieces, each usable alone, all @Observable:

Type What it is
Window CefChromeWindow One of Chromium’s tabbed windows (a Browser of type normal): its tabs and groups in order, and the regions of the window the app fills
Tab CefTab A page’s place in Chromium’s tab model: its page, title, icon, loading, sound, pinning, group, selection; commands on itself
Group CefTabGroup A tab group: title, colour, collapsed, tabs

The profile’s CefTabs (profile.tabs) lists the windows and follows Chromium from first use, so a tab interface draws Chromium’s model instead of keeping one: changes made by the app, by Chrome’s commands and shortcuts and by extensions all show up the same way.

CefTabs follows the events CEF and AppKit deliver anyway (a page created or closed, each page’s title, address, icon and loading, the page a window shows, every Chrome command) and reads Chromium’s tab model, in a hidden service host, only when one of them reports a change to its structure; the host closes a few seconds after. While nothing changes no host runs. Two things CEF does not report (sound starting or stopping in a tab, and an extension moving, pinning or grouping tabs) show at the next read, or at once with strip.observesChromiumEvents = true, which keeps one host listening to chrome.tabs’ events (one renderer, about 30 MB). The Browser example turns it on while it is the active app.

import CefChromium

let window = try await CefChromeWindow.open(url: URL(string: "https://swift.org")!)
let tab = try await window.openTab(URL(string: "https://developer.apple.com")!)
try await tab.pin()
let group = try await window.profile.tabs.group([tab])
try await group?.setTitle("Reading")
try await tab.move(to: 0, in: window)
for tab in window.tabs {
    print(tab.title, tab.isPinned, tab.group?.title ?? "-", tab.page?.identifier ?? -1)
}

Each command asks Chromium; the properties change when Chromium reports the change, a moment later. Commands: select, close, pin, move(to:in:) (to another window too: the tab keeps its page, its history and its CefPage), duplicate, moveToNewWindow, setMuted, discard; on the strip group, ungroup, select(_:) for several tabs (Chromium’s multiple selection), pin/move/close for several, reopenClosed(in:) (⌘⇧T); on a group add, ungroup, close, move, moveToNewWindow, setTitle, setColor. recentlyClosed lists what Chromium can reopen (Restoring tabs and sessions).

The app’s own tab interface. Set showsChromeUI to false and Chrome’s tab strip and toolbar are covered by the app’s topBar (and, with tabs at the side, sidebar): they get no clicks, no keyboard focus (focus that reaches them moves to the app’s views) and no place in the accessibility tree. contentFrame is where the page is, as it moves with resizing, full screen, the bookmarks bar, infobars and the side panel; the covered height is Chromium’s (topBarHeight), since Chromium lays out its window itself. strip.layout = .vertical(width:) turns on Chrome’s vertical tabs, which the sidebar covers.

import CefChromium
import SwiftUI

/// A tab bar drawn from the window's tabs: each button is a command to
/// Chromium, and the bar redraws when Chromium reports the change.
struct TabBar: View {
    let window: CefChromeWindow

    var body: some View {
        HStack {
            ForEach(window.tabs) { tab in
                Button(tab.title) { Task { try? await tab.select() } }
                    .fontWeight(tab.isActive ? .semibold : .regular)
            }
        }
        .padding(.leading, 80)  // the window buttons
    }
}

func dress(_ window: CefChromeWindow) {
    window.showsChromeUI = false
    window.setTopBar { TabBar(window: window) }
}

Chromium’s surfaces. Bubbles, dialogs, the find bar and the status bubble are surfaces of the window, with the policy, anchors and custom presentations of a CefWindow’s (Chromium’s own interface): chromiumSurfacePolicy, setCustomPresentation(for:), setFindBar, setStatusBubble, onChromiumSurface. Each surface belongs to the tab it opened in (surface.page): switching tabs closes the bubbles about the tab left, yours included, and keeps a tab’s find bar for when it comes back. With Chrome’s UI covered, bubbles that hang from Chrome’s toolbar go under the app’s top bar at the page’s trailing edge unless an anchor says otherwise (chromiumSurfaceAnchor on a view of the top bar). A tabbed window adds kinds: .tabSearch (⇧⌘A, replaceable), .tabGroupEditor and .tabHoverCard, and an extension’s action popup (.extensionPopup) opens here, which it does not in a CefWindow. pageBar holds a bar of the app’s along the top of the page, which is where setFindBar puts it.

import CefChromium
import SwiftUI

func dressSurfaces(_ window: CefChromeWindow) {
    window.setFindBar { session in
        TextField("Find on Page", text: Bindable(session).text)
            .onSubmit(session.next)
            .padding(6)
    }
    window.setCustomPresentation(for: .qrCode) { surface in
        Text(surface.url?.absoluteString ?? "").padding()
    }
}

Tabs and pages. Each tab is matched with its CefPage exactly: a tab the app opens with openTab is known from its creation; a window’s active tab is the page shown in its window; a background tab is the one page in its window with its address, and when several share one, CefChromium mutes one of them through CEF for a moment and asks Chromium which tab became muted. openTab makes the page with the app’s configuration and delegate (it starts in a window of its own, kept off screen, and Chromium moves the tab in); tabs Chromium makes itself (an extension’s chrome.tabs.create, a duplicate, a reopened tab) have CefKit’s default configuration and reach CefRuntime.onPageCreated.

Previews. tab.capturePreview() captures a tab’s page, a background tab’s too, as a small picture into tab.preview (with capturedAt): about 80 ms each through the DevTools Protocol, so take them when an overview opens or a tab is hovered, not continuously.

Dragging tabs. CefTab.Transfer is Transferable (the tab inside the app, its address in Finder and other apps) and tab.pasteboardItem is the same for AppKit dragging; the drop lands with move(to:in:) or group.add(_:).

What Chromium does not allow. Collapsing a group: Chromium grants chrome.tabGroups to extensions only, and its group header takes no accessibility action, click or key press sent to it (setCollapsed throws .unsupported; isCollapsed follows an extension’s collapsing). Renaming and recolouring go through Chrome’s own group editor, kept off screen. Tabs cannot move between profiles: open the page in the other profile’s window and close it here. chrome.tabs.remove from a WebUI page ended the browser process, so tabs close through CEF.

Which host when.

You want Use
A page inside your own SwiftUI or AppKit views CefWebView (Alloy style; not a Chromium tab)
One page in a window, Chrome’s pages and extensions working CefWindow (a Chromium window of one tab, of type popup)
Several tabs Chromium knows, with Chrome’s own UI CefChromeWindow (or CefRuntime.openChromeWindow)
Your own tab interface over Chromium’s tabs CefChromeWindow with showsChromeUI = false and a topBar
Pages without a window (rendering, automation) CefWebView offscreen

The design notes record the measurements.

Tabs

Each CefWindow holds one page, a Chromium window of one tab. For tabs that are Chromium’s own, use CefChromeWindow (Chromium’s tabs). With CefWindow, a tabbed interface is one window per tab, grouped by a shared tabbingIdentifier. To show the “+” button in the native tab bar, implement newWindowForTab(_:) in your app delegate. To put a new tab in a specific group, call addTabbedWindow(_:ordered:) on a window of that group with the new window.window.

Tabs Chromium opens itself

Chromium opens some pages in tabbed windows of its own design, with Chrome’s tab strip and toolbar:

Path Opener Disposition
A link from chrome://settings, such as Autofill and passwords → Passkeys none .unknown
Chrome commands such as .newTab and .newWindow that the delegate does not handle none .unknown
Tabs restored from an earlier session none .unknown
A ⌘-click (.newBackgroundTab) or ⇧-click (.newWindow) in a Chrome-style page whose delegate answers .allowNativePopup the clicked page’s document as clicked

CefRuntime.shared.onChromeTab decides what happens to each. The request carries the page Chromium created (live, and in CefRuntime.shared.pages), the URL it opens, its CefProfile (nil for one Chromium made itself, such as its own incognito profile), the page that asked for it, and the disposition. Open the URL in your own UI and answer .close, or answer .showInChromeWindow:

CefRuntime.shared.onChromeTab = { request in
    // Your tab UI; open it in request.profile, next to request.opener.
    openTab(request.url, inBackground: request.disposition == .newBackgroundTab)
    return .close
}

Chromium’s window, the one holding that page’s view, stays off screen until the handler answers; other windows are left alone, and so is a Chromium window already on screen that the tab joined. .close closes the page, and its window when that window shows no other tab; .showInChromeWindow shows the window and announces the page through onPageCreated. CEF cannot move a page out of Chromium’s tab strip, so the tab you open is a new page that starts from the URL: it has no back-forward list, and a form submission opened in a new tab loads again as a plain request. When onChromeTab is nil, Chromium shows these pages itself. Leave it nil in apps that use openChromeWindow, whose new tabs Chromium opens the same way.

Popups and links with target=_blank from a CefWindow open in a browser view of their own, which CefWindow.onPopup adopts with its opener and history intact; they never reach onChromeTab.

Every other browser Chromium creates on its own, popups and DevTools included, is passed to CefRuntime.shared.onPageCreated. Assign a delegate there to manage it. Pages that onChromeTab closes do not reach onPageCreated. CefRuntime.shared.pages lists every live page.

Chrome commands

page.execute(_:) runs one of Chrome’s own commands (CefChromeCommand): .find, .print, .showHistory, .showDownloads, .showSettings, .createQRCode, .zoomIn, .bookmarkThisTab, .showDevTools and more. canExecute(_:) says whether one is available. Chrome-style pages only. zoomLevel follows .zoomIn, .zoomOut and .resetZoom, whether you run them or the user chooses them in Chrome’s UI.

Before Chrome runs a command chosen from its menus or keyboard shortcuts, it calls the delegate’s page(_:handleChromeCommand:disposition:). Return true to handle the command yourself, for example .newTab or .focusLocation.

configuration.chromeToolbar trims Chrome’s own UI in Chrome-style windows: hiddenAppMenuCommands, disabledAppMenuCommands, showsToolbarButtons and showsPageActionIcons.

Tabs: Chromium’s tab strip under the app’s own

A tabbed browser on CefSwift wants its own tab interface over Chromium’s own tab model, so that chrome.tabs, chrome.tabGroups, recently closed tabs and moving tabs between windows behave as in Chrome. Five ways to host many tabs in one window were tried on CEF 156 (SelfTest’s tabHosting group, and the exploratory probe CEFSWIFT_SELFTEST_PROBE=chromeTabStrip):

Hosting What Chromium sees Result
(a) Several Chrome-style cef_browser_view_t in one cef_window_t — CEF refuses: CefBrowserViewImpl::AddedToWidget logs “Cannot add multiple Chrome style BrowserViews” and never creates the second page; the first keeps working. Chromium’s BrowserWidget holds one BrowserView.
(b) One CefWindow per tab, attached as borderless child windows over the page area of one app window One popup window of one tab per app tab Works as AppKit (frame tracking, full screen, minimizing, the page taking the keyboard while the app window stays main, with an override of Chromium’s canBecomeMainWindow), but Chromium’s model is N one-tab windows: no indices, no groups. Built, measured and removed.
(c) Native window tabbing with the system tab bar hidden As (b) macOS keeps the tab bar of a group of two or more tabs shown; toggleTabBar(_:) does nothing. No custom rail can replace it.
(d) Alloy-style views in an ordinary NSWindow Not tabs at all chrome.tabs does not list Alloy pages; no chrome:// pages, extensions UI or history recording.
(e) The window Chrome draws (openChromeWindow) A normal window with a real TabStripModel Real tabs, pinning, groups, moves between windows. Chrome draws its own tab strip and toolbar.

Every Chrome-style CefWindow is its own Chromium Browser with one tab (canExecute(.selectNextTab) stays false even while macOS shows two of them as one tab group; CEF’s ChromeBrowserHostImpl says views-hosted pages hold a single tab), and chrome.windows reports it as a popup, so chrome.tabs.group and move refuse its tabs. Only (e) makes the app’s tabs Chromium’s.

In a window Chrome draws, measured:

  • chrome.tabs.create({windowId}) adds tabs to it; each reaches the app as a CefPage (onPageCreated).
  • From the chrome://bookmarks WebUI, which Chromium grants chrome.tabs and chrome.windows (not chrome.tabGroups): update (active, pinned), move (within and between windows), group (with createProperties.windowId: the default target is the caller’s window, a popup), ungroup, duplicate and windows.create({tabId}) work. A move between windows keeps the CefPage, its identifier and its page.
  • chrome.tabs.remove and update({muted}) from that page ended the browser process (status 70, nothing logged); CefTabs closes tabs through CEF instead and leaves muting to CefPage.isAudioMuted.
  • chrome://tab-strip-internals loads, and tab_strip_internals.mojom‘s GetTabStripData() returns every window’s tree: pinned and unpinned collections, groups with their title, colour and collapsed state, tabs with title, address, activity and alert states, and OnTabStripUpdated on changes; its window identifiers are chrome.windows’. It reports neither a new window nor a group renamed or recoloured (an extension’s chrome.tabGroups.update) by itself.
  • chrome://tab-strip.top-chrome (the WebUI tab strip) is not built on macOS; chrome://tab-search.top-chrome loads, and its bundle (tab_search.js) exports TabSearchApiProxyImpl, whose getProfileData() lists windows (without identifiers), tabs, groups and recently closed tabs and groups, with tabsChanged, tabUpdated and tabsRemoved events and openRecentlyClosedEntry; tabs_api.mojom.TabStripService is bound only to the experimental WebUI browser window.
  • Chrome’s tab strip and toolbar take the top of the window (121 of 1006 points in the probe; the page is the WebContentsViewCocoa). Chromium’s BridgedContentView answers every hit test inside itself, so a view added to it gets no clicks; a view added to the window’s frame view above the content view does, and the window buttons still work. An app can cover Chrome’s strip and toolbar with its own this way.

CefChromium’s CefChromeWindow, CefTab, CefTabGroup and CefTabs are the result. Measured further (SelfTest tabs, and the extensions probe’s “tab strip and extensions” checks):

  • No way to have Chromium leave the strip and toolbar out. A normal window always has both; popup and app windows have neither but are not tabbed. Chrome’s vertical tabs (vertical_tabs.enabled, width vertical_tabs.uncollapsed_width) move the strip to the side and leave a toolbar of 46 points at the top. So the app’s views cover them, at Chromium’s height (87 points with tabs at the top).
  • Covered controls out of the way. The accessibility tree loses them when AXPlatformNodeCocoa’s children leave out elements wholly under the cover (checked: no AXTabGroup or AXToolbar of Chromium’s in the window’s tree, the app’s top bar in it). Keyboard focus Chromium moves to them (IDC_FOCUS_LOCATION, Tab past the page) does not change AppKit’s first responder when it is already Chromium’s content view, so CefChromium also looks at Chromium’s focused accessibility element after each window update and moves focus to the app’s views.
  • Matching tabs with pages exactly. Neither chrome.tabs ids nor TabHandles are visible to CEF, and CEF’s page identifier is not the tab id. DevTools’ Target.getTargetInfo gives a target id no tab API shows; Browser.getWindowForTarget gives the window, but needs a DevTools session on every tab. What is exact: a page’s view is in its tab’s window (nativeView.window, background tabs’ views included, hidden), shown only for the active tab; and chrome.tabs reports mutedInfo for every tab, which CEF sets (set_audio_muted). So: the active tab is the page shown in the window, a background tab the one page in the window with its address, and among background tabs of one address a page is muted for a moment to see which tab reports it. Matches are kept by chrome.tabs id, which survives moves between windows (the CefPage does too).
  • One listener crashes Chromium: chrome.windows.onFocusChanged. Each chrome.tabs and chrome.windows event was listened to alone on the chrome://bookmarks host, then an incognito window opened and an extension was disabled and enabled (tabResearch probe, listener=<name>): chrome.tabs’ onUpdated, onCreated, onRemoved, onMoved, onActivated, onAttached and chrome.windows’ onCreated and onRemoved all survived; with onFocusChanged, Chromium ended with SIGTRAP as the incognito window opened.
  • Following without polling, and without a host at idle. CEF and AppKit already report most of what a tab strip shows: a page created (a new tab or window) or closed (CefRuntime.pages), each page’s title, address, icon and loading (CefPage), the page a window shows (the shown WebContentsViewCocoa changes: the active tab), every Chrome command (on_chrome_command, Chrome’s shortcuts included). CefTabs applies a closed tab and a new active tab at once, reads chrome.windows.getAll({populate: true}) only after a new page, a window or tab command (IDC_* 34000–34999) or a change the app makes, and the internal page only for groups’ names and colours and what Chromium can reopen. Each host closes 3 s after its last call (a per-call idle time in CefServiceHosts). Not reported by CEF: sound starting or stopping (an NSWindow title carries no alert state on macOS) and an extension’s moves, pinning and grouping; with observesChromiumEvents the bookmarks host stays open listening to every event above but onFocusChanged. chrome://tab-strip-internals could not serve both: its renderer can reach chrome.tabs no more than the bookmarks renderer can bind its Mojo interface (WebUI hosts bind by host), and chrome://tab-search.top-chrome has no window identifiers.
  • What a window Chrome draws cost, and why. The Browser example on CefChromeWindow measured 10 processes and 410 MiB (relaunch, median of three, alternating with the new build on one Mac), against 8 and 215 MiB now. The two hosts were two renderers (about 45 MiB). The rest was Chrome’s WebUI omnibox popup (chrome://omnibox-popup.top-chrome, “Tool: Omnibox” in the task manager): preloaded with every window in a renderer of its own (about 50 MiB) and drawn off screen, 120 MB of IOSurfaces in the GPU process (footprint -p: 127 IOSurfaces against 16 without it), under a covered omnibox no one types in. The Browser turns WebUIOmniboxPopup, WebUIOmniboxAimPopup and WebUIOmniboxFullPopup off; Chromium falls back to its views popup. Disabling PreloadTopChromeWebUI did not remove it. What a window Chrome draws still costs, measured in one process: about 12 MB more in the GPU process than a CefWindow of the same size (35 against 23 MiB), for Chrome’s frame, tab strip and toolbar, which are views without an NSView each, so nothing outside Chromium can hide them.
  • Group names and colours change through Chrome’s own group editor: the header’s AXShowMenu opens it, kept transparent, the name is typed through its NSTextInputClient and a colour pressed through accessibility. Collapsing has no route: the header offers no AXPress or expanded state, the editor no collapse item, and neither a click sent through the window at the header (with the cover set aside) nor a Space key collapsed it, with the window key. setCollapsed throws .unsupported; the Browser example collapses groups in its own rail.
  • Tabs the app opens keep its configuration. Chromium adds tabs to its windows only itself (chrome.tabs.create, its commands), with CefKit’s default client and configuration. CefChromeWindow.openTab creates the page with the app’s configuration in a window of its own, kept off screen and transparent, then chrome.tabs.moves its tab in: the page, its bridges and user scripts come along.
  • Surfaces in these windows. Chromium attaches its bubbles to a window it draws as it does to a CefWindow (child windows of BrowserNativeWidgetWindow), so the same controller follows both; CefChromium overrides that class’s addChildWindow:ordered: and removeChildWindow: as it does CefNSWindow’s. Commands come through CEF’s command handler for every tab’s page. Tab search is a WebUIBubbleDialogView, recognized by its page’s host when its accessibility tree has it and otherwise by the command that opened it; the group editor is TabGroupEditorBubbleView. “Sharing this tab” is an infobar inside the window’s views, not a surface.
  • Reopening and the previous session. CEF runs no session restore: after a clean quit and after a kill, with session.restore_on_startup at 1 or at its default, the second launch opened nothing, Chromium’s tab restore service listed nothing from a killed run, and no “Restore pages?” bubble appeared (tabResearch probe, session1/session2, crash1/crash2). The tab restore service is saved and read at the next launch: a window closed with IDC_CLOSE_WINDOW is one entry with its tabs and their history; a tab closed through CEF, as CefKit’s quit closes every page, is a loose tab. tab_search.mojom‘s OpenRecentlyClosedEntry reopens a given entry into the window of the page that asks (a CefWindow’s is a popup, where a tab cannot stay), so CefChromium asks from a page in a Chrome window kept off screen and moves a reopened tab to where it was; only IDC_RESTORE_TAB puts the newest entry back by itself. At quit CefChromium closes each of Chromium’s windows as a window first, and saves the windows’ tabs (a small file in the profile’s directory) as they change, marked running until the quit, which is what tells a crash.
  • Previews. Chromium’s hover-card thumbnails come from ThumbnailTabHelper, which only Chrome’s views tab strip and the WebUI tab strip (not built on macOS) read; no route to them was found. The DevTools Protocol’s Page.captureScreenshot does capture a background tab (Chromium draws a frame for it): a quarter-size JPEG of a 1200 × 900 page took 73 ms for the active tab and 94 ms for a background one, the first capture attaching DevTools (tabResearch probe, preview); a full-size PNG took 135 ms and 560 KB. CefTab.capturePreview() takes the small one on demand; nothing captures continuously.

Was this page helpful?