Choosing a host
CefWebView, the AppKit views, CefWindow, CefChromeWindow or Chrome's own window: which host shows your pages, and what each gives you.
// One Chrome-style page in a window of its own, with a SwiftUI toolbar.
let page = CefPage(url: URL(string: "https://swift.org")!)
let window = CefWindow(page: page)
window.setToolbar(principal: { Text(page.title) })
window.show()
Gallery: ChromeWindowDemo, AppShellDemo, TabStripDemo.
| You want | Configuration | Host |
|---|---|---|
| Your own or trusted content inside your views | .embedded |
CefWebView, or CefWindowedPageView and CefOffscreenPageView in AppKit |
| One page in a window, Chrome’s pages and extensions working | .chrome |
CefWindow |
| Several tabs Chromium knows, with your own tab interface | .chrome |
CefChromeWindow (CefChromium) with showsChromeUI = false and a topBar |
| Chrome’s complete UI | .chrome |
CefRuntime.shared.openChromeWindow(url:) |
| Pages without a window: rendering, automation | .embedded + offscreen |
CefWebView offscreen |
| Your own or trusted content | Arbitrary sites, with Chrome’s services | |
|---|---|---|
chrome:// pages, extensions, history |
No | Yes |
| Background services | Off | On |
CefWebView shows a page in SwiftUI:
struct Reader: View {
@State private var page = CefPage(url: URL(string: "https://swift.org")!)
var body: some View {
CefWebView(page)
.navigationTitle(page.title)
.toolbar {
Button("Back", systemImage: "chevron.left", action: page.goBack)
.disabled(!page.canGoBack)
}
}
}
CefWebView(_:) never closes the page you give it, so the page
survives the view leaving and re-entering the hierarchy and can move to
another view. CefWebView(url:configuration:) creates and owns its page,
loads a new URL when url changes, and closes the page when the view
goes away.
CefPage.Configuration.rendering picks how the page draws:
| Rendering | How it draws | Use |
|---|---|---|
.windowed (default) |
Chromium’s own NSView inside yours. Chromium handles input, IME and accessibility. |
Most embedding. The app cannot read or redraw the page’s pixels. |
.offscreen or .offscreen(frameRate:) |
Frames composited in a layer of the app’s. | SwiftUI content and effects over the page, transforms with input that follows, thumbnails; see Animating a page. |
Views embed pages in the Alloy runtime style, which has no chrome://
pages, extensions or history. For those, use a
CefWindow.
Chrome-style windows
CefWindow is a CEF Views window (cef_window_t) with one Chrome-style
page: the full Chrome runtime with chrome://settings, chrome://extensions, history,
downloads, the password manager, autofill, extensions and PDF viewing.
Chromium provides Chrome style only in windows it creates, so the window owns
its NSWindow instead of living in a SwiftUI scene.
let page = CefPage(url: home, delegate: controller)
var configuration = CefWindow.Configuration()
configuration.tabbingIdentifier = "tabs"
let window = CefWindow(page: page, configuration: configuration)
window.setToolbar {
NavigationButtons(page: page) // beside the window buttons
} principal: {
AddressBar(page: page) // centered
} trailing: {
Button("New Tab", systemImage: "plus") { controller.openTab() }
}
window.setTabAccessory { TabIcon(page: page) } // in the native tab
window.show()
A tab accessory is a small view shown in the window’s native tab, such as the page’s icon:
struct TabIcon: View {
let page: CefPage
var body: some View {
if let favicon = page.favicon {
Image(nsImage: favicon).resizable().frame(width: 16, height: 16)
} else {
Image(systemName: "globe")
}
}
}
The window starts hidden; call show(). CefWindow.all lists the
open windows and keeps them alive, close() closes one (its page’s
beforeunload handler may keep it open), and onClose runs after it is
destroyed. window.window is the NSWindow: Chromium owns its lifetime,
and its frame, title, ordering and tabbing are yours.
CefWindow.Configuration:
| Property | Effect |
|---|---|
toolbar |
.none (default) shows only the page; .chrome shows Chrome’s toolbar and omnibox; .location shows Chrome’s read-only location bar. |
frame |
Initial frame in screen coordinates. nil centers a 1200 × 800 window. |
tabbingIdentifier |
Windows that share one are grouped as native tabs. nil keeps the window out of tab groups. |
titleFollowsPage |
Keeps the window title in sync with the page, as title makes it. On by default. |
title |
Makes the window title from the page: by default its page title, or the host while the page has none. Re-run whenever the observable state it reads changes, so { $0.title.isEmpty ? "New Tab" : $0.title } titles blank tabs. |
isFrameless |
Removes the title bar and window buttons. |
CefRuntime.shared.openChromeWindow(url:frame:configuration:delegate:) opens a
window drawn entirely by Chrome instead, with its tab strip, omnibox and
menus.
Toolbar and window
setToolbar(leading:principal:trailing:) gives the window a unified
title-bar toolbar laid out as Safari’s is: leading beside the window
buttons, principal centered, trailing at the far edge. Each is a
native toolbar item, whose views sit side by side, and the native tab bar
appears beneath the toolbar when the window joins a tab group.
setToolbar(principal:) sets only the centered view. Installing a toolbar
keeps the window’s frame.
The builder closures run once, when you call the method, and the views they
build stay live: SwiftUI updates them as the observable state they read
changes. Put views that read page state in a View type of their own,
such as NavigationButtons above, so they read the state in body; a value
read in the closure itself is captured once. Call setToolbar again to
replace the toolbar.
setToolbar, setTabAccessory(_:) and topAccessory take effect as soon
as the NSWindow exists, which is immediately for a window you create. For
other setup that needs the NSWindow, withWindow(_:) runs a closure at
the same moment. All of them do nothing once the window has closed.
Around the page:
window.topAccessory = statusBanner // an NSView between title bar and page
window.contentInsets = NSEdgeInsets(top: 0, left: 6, bottom: 6, right: 6)
window.contentCornerRadius = 10 // the page as an inset, rounded card
topAccessory sits outside Chromium’s view hierarchy, so focus, key
equivalents and menus behave as in any AppKit window. contentInsets
reserves space around the page for native views; the window background
shows around a page with insets or rounded corners.
Over the page, pageOverlay takes one view of your own (setPageOverlay(_:)
from SwiftUI): a swipe indicator, a status bubble, a banner. It is sized to
the page, clipped to its corners, and never receives events, so clicks and
scrolls reach the page. To show several, put them in one view:
window.setPageOverlay {
ZStack {
LinkStatus(page: window.page) // views that read the page's state in body
MyIndicator(page: window.page)
}
}
As with the toolbar, the closure runs once: views in it that show page
state read it in their own body.
Hosting
| Host | Style | How it draws |
|---|---|---|
CefWebView, .windowed |
Alloy | Chromium’s native NSView as a subview of CefWindowedPageView |
CefWebView, .offscreen |
Alloy | Frames in a shared IOSurface, copied into a surface of its own and set as layer contents by CefOffscreenPageView, which forwards input |
CefWindow |
Chrome | A CEF Views window; your toolbar is an NSToolbar and your accessory a title-bar accessory view |
CefRuntime.openChromeWindow |
Chrome | Chrome’s own window and UI |
CefChromeWindow (CefChromium) |
Chrome | Chrome’s own window, with the app’s views laid over Chrome’s tab strip and toolbar |
macOS provides Chrome style only in windows Chromium creates, which is why
the Chrome-style hosts own their NSWindow. A CefWindow draws the
whole content area in one layer, so contentInsets and
contentCornerRadius are drawn by a native view laid over the page that
paints everything except the page’s rectangle and passes events through.
Windows, tabs and views
Three things are kept apart, each usable alone:
- Content:
CefPage, one page’s model, whatever shows it. - View: where content shows.
CefWebView(CefWindowedPageView,CefOffscreenPageView) puts a page anywhere in the app’s own view tree, Alloy style: such pages are not Chromium tabs. A Chrome-style window’s content area shows its active tab’s page. - Window and tab: Chromium’s model of tabs. A
CefWindowis a CEF Views window (cef_window_t) holding onecef_browser_view_t; Chromium makes it aBrowserof typepopupwith one tab, and CEF refuses a second Chrome-style browser view in it. Tabbed windows are the windows Chromium draws itself (cef_browser_host_create_browserwith Chrome style and no CEF Views): CefChromium’sCefChromeWindow, with itsCefTabs andCefTabGroups, all listed by the profile’sCefTabs.
CefWindow and CefChromeWindow stay two types because they are two
different things in CEF: the first is a CEF Views object CefKit creates and
lays out (toolbar, insets, popups through onPopup, Chromium’s surfaces
recognised per window); the second has no CEF object at all, only the
NSWindow Chromium made and the tab model Chromium keeps, reached through
Chromium’s WebUI hosts (chrome://bookmarks’ chrome.tabs,
tab_strip_internals.mojom, tab_search.mojom). CefTabs follows
the events CEF and AppKit deliver for every page and window, and asks a
host only when they report a change to the tab model’s structure; hosts
close seconds after their last call. Chromium’s surfaces are one
controller for both window kinds (ChromiumSurfaceHost: the window’s
NSWindow and the page it shows, a CefChromeWindow‘s active tab’s).
A one-tab CefChromeWindow is a different
Chromium window from a CefWindow: a normal one, with a toolbar for
extensions’ action popups and a tab strip extensions can group and move
tabs in.
CefChromeWindow covers Chrome’s tab strip and toolbar with a view in the
window’s frame view, above Chromium’s BridgedContentView (which answers
every hit test inside itself) and below the title bar’s window buttons. No
preference, command-line switch or CEF callback takes them out of a normal
window. Under the cover, Chrome’s controls are kept out of the way through
AppKit: AXPlatformNodeCocoa’s accessibilityChildren leaves out elements
lying wholly under the cover (only in covered windows; the method is
replaced only if the class defines it), and keyboard focus Chromium moves
to them (Tab past the page, ⌘L, F6) moves on to the app’s views. The page
is the visible WebContentsViewCocoa, whose frame the window follows.