Find bar and Chromium's bubbles
Replace Chromium's find bar, status bubble, QR code and other bubbles with SwiftUI, or anchor them under your controls.
import CefChromium
import SwiftUI
func addFindBar(to window: CefChromeWindow) {
window.setFindBar { session in
TextField("Find on Page", text: Bindable(session).text)
.onSubmit(session.next)
.padding(6)
}
}
Gallery: FindInPageDemo, ChromiumSurfacesDemo.
Browser example: Features/FindBar is a whole find-bar feature; Features/StatusBubble and Features/ChromiumPanels replace the others.
Chromium’s own interface
Chromium shows some of its interface over a CefWindow in windows
of its own: bubbles such as the QR code generator and Cast, the HTTP
sign-in dialog, the find bar, the status bubble with a hovered link’s
address. Without Chrome’s toolbar, Chromium hangs the toolbar’s bubbles
from the top right of the page. That is all CefKit does with them.
The optional CefChromium library
(.product(name: "CefChromium", package: "CefSwift")) lets the app take
them over. They are surfaces, CefChromiumSurface, of a Kind, and
window.chromiumSurfacePolicy decides, per kind, how each is presented:
| Presentation | Effect |
|---|---|
.native (default) |
Chromium draws it, under its anchor if it has one. |
.custom |
Chromium’s is kept off screen; your interface shows instead. |
.hidden |
Nothing shows; a dialog that asks something is cancelled. |
Anchors put Chromium’s surfaces under your controls:
import CefChromium
window.chromiumSurfacePolicy.anchor = .view(addressField, alignment: .trailing) // every toolbar surface
window.chromiumSurfacePolicy.anchors[.qrCode] = .view(qrButton, alignment: .trailing)
window.chromiumSurfacePolicy[.statusBubble] = .hidden
CefChromium recognizes surfaces by overriding methods of Chromium’s
private AppKit classes (CefNSWindow, NativeWidgetMacNSWindow) and by
the class names Chromium’s views report through accessibility, so it is
bound to the pinned CEF; SelfTest fails when an update renames them. It
starts following surfaces, in every CefWindow, the first time the app
uses any of its window API (chromiumSurfacePolicy, configuration.chromiumSurfacePolicy,
chromiumSurfaces, setCustomPresentation(for:_:), setStatusBubble(_:),
chromiumSurfaceAnchor(_:alignment:), …); surfaces already on screen then
are taken up as they are. Set chromiumSurfacePolicy before showing the window, so
nothing shows before it applies. Until then Chromium’s interface shows as
it does without the library.
A status bubble or zoom bubble hidden or custom in the window’s configuration is
switched off in Chromium when the page is created
(cef_browser_settings_t’s chrome_status_bubble and
chrome_zoom_bubble), so Chromium never creates its window, and
page.statusText still follows the pointer. Hidden later, through
window.chromiumSurfacePolicy, Chromium’s window is created and kept invisible:
import CefChromium
var configuration = CefWindow.Configuration()
var policy = CefChromiumSurface.Policy()
policy[.statusBubble] = .hidden
configuration.chromiumSurfacePolicy = policy // window.chromiumSurfacePolicy starts as this
let window = CefWindow(page: page, configuration: configuration)
In SwiftUI, mark the views in your toolbar:
AddressField(page: page)
.chromiumSurfaceAnchor(alignment: .trailing)
Button("QR Code", systemImage: "qrcode") { page.execute(.createQRCode) }
.chromiumSurfaceAnchor([.qrCode])
CefChromiumSurface.Anchoris.view(_:alignment:spacing:)or.rect(_:alignment:spacing:), a rectangle in window coordinates. The surface hangs below it,spacingpoints away, lined up with its leading or trailing edge or centered, and kept inside the window. Surfaces follow their anchor as the window moves or resizes, and when Chromium lays them out again.anchorapplies to the kinds that hang from Chrome’s toolbar (kind.hangsFromToolbar). The find bar and the sign-in dialog belong to the page and move only with an anchor of their own inanchors. Untitled widgets attached to the page, such as autofill suggestions, never move.
Your own interface replaces a kind with setCustomPresentation(for:_:).
The presenter receives the surface whenever Chromium would show it: from
its command (.find, .createQRCode, .castMedia, .showSharingHub),
its keyboard shortcut or menu item, or anything else, in which case
Chromium’s window is hidden before it is seen. In SwiftUI:
window.setFindBar { session in FindBar(session: session) } // under the toolbar, as in Safari
window.setStatusBubble { text in StatusBubble(text: text) } // over the page, as its pageOverlay
window.setCustomPresentation(for: .qrCode) { surface in // in a panel at the kind's anchor
QRCodeView(url: surface.url).frame(width: 200, height: 200).padding()
}
FindBar, StatusBubble and QRCodeView are views of your own; the
Browser example has all three (QRCodeView draws the code with Core
Image’s CIFilter.qrCodeGenerator()).
A surface carries what a replacement needs: kind, title, page,
url (the address a QR code encodes, or the page’s), anchorRect,
position(_:) to place a window of yours as Chromium would, and
dismiss(). A panel from the SwiftUI method closes on Esc, on a click
elsewhere, and when the surface is dismissed, and gives keyboard focus
back to the window. presentationForChromiumSurface decides per surface
as it appears, chromiumSurfaces lists those presented, and
onChromiumSurface hears about every one, whatever its presentation:
custom ones without a presenter, hidden ones (already dismissed), and
those CefChromium has no kind for, as .other(identifier), so that new
Chromium interface is never invisible to the app.
A surface’s state is .presented until it turns .dismissed(_:), once,
with the reason:
| Dismissal | When |
|---|---|
.app |
dismiss(); for a custom find bar, also findSession.end(). |
.chromium |
Chromium ended it: Esc, a click outside or a button of its bubble, the page navigating to another document (custom surfaces follow Chromium’s bubbles here), the status text going away. |
.replaced |
A newer surface of the kind took its place: a second ⌘D, QR code or zoom command while a custom one is open. ⌘F with a custom find bar open keeps the bar and its search instead. |
.presentation |
The kind’s presentation changed while it was open (native → hidden or custom, custom → native), or it was hidden as it appeared. |
.window |
The window, and with it the page, closed. Nothing is announced afterwards. |
Changing chromiumSurfacePolicy applies at once: Chromium’s surface of a
kind turned .custom is hidden and handed to your presenter with the same
url; your interface for a kind turned .native is dismissed, and
Chromium shows the next one. Native surfaces follow their anchor as the
window moves, resizes, enters full screen or changes screen, and as an
anchor view moves within the window; .leading and .trailing follow the
window’s layout direction.
What CefChromium can do differs by kind, and kind.capabilities says it:
every kind is observed (announced, listed, dismissed with a reason);
.anchor moves Chromium’s surface, .hide keeps it off screen (cancelling
what it asks), .dismiss closes it on request, and .replace lets your
interface take its place, which only makes sense where the app can get
what the surface shows and do what it offers. setCustomPresentation(for:_:)
does nothing for a kind without .replace, and a .custom or .hidden
presentation a kind does not allow stays native.
| Surface | Kind | Anchor | Hide | Dismiss | Replace: what your interface uses |
|---|---|---|---|---|---|
| Find bar | .findBar |
Yes (own anchor) | Yes | Yes | page.findSession: text, isCaseSensitive, matchCount, activeMatch, next(), previous(), end(); ⌘G and ⇧⌘G move through the matches. |
| QR code | .qrCode |
Yes | Yes | Yes | url, to encode with Core Image’s CIFilter.qrCodeGenerator(). |
| Cast | .cast |
Yes | Yes | Yes | url; the profile’s mediaRouter (each profile has its own) lists sinks and routes and casts Cast apps and presentation URLs. Mirroring a tab needs Chromium’s dialog. |
| Share | .share |
Yes | Yes | Yes | url, title. Chromium’s share menu needs its toolbar, so by default the command opens macOS’s share menu at the anchor. |
| Zoom bubble | .zoom |
Yes | Yes | Yes | page.zoomLevel (Chromium scales by 1.2 to its power). The zoom commands still zoom; hidden, only the bubble goes. |
| Bookmark added (⌘D) | .bookmark |
Yes | Yes | Yes | url, title; CefBookmarks adds, edits and removes the bookmark. |
| Page info | .pageInfo |
Yes | Yes | Yes | page.connectionSecurity and the profile’s siteSettings. |
| Downloads | .downloads |
Yes | Yes | Yes | page.downloads and the delegate’s download decisions. |
| Status bubble | .statusBubble |
No | Yes | No | page.statusText: .custom presents a surface while it is not empty; setStatusBubble(_:) draws it over the page. |
| Fullscreen notice | .fullscreenNotice |
Yes | Yes | No | page.isFullscreen. Also shown for keyboard and pointer lock. |
| HTTP sign-in | .signIn |
Yes (own anchor) | Yes | Yes | None in Chromium’s dialog: with the default authenticationPrompt = .delegate, page(_:credentialFor:) answers instead and no dialog shows. Hidden or dismissed, the sign-in is cancelled. |
| Permission prompt | .permissionPrompt |
Yes | Yes | Yes | Never shown in CefSwift: every request (camera, microphone, location, notifications, clipboard, MIDI, window management, protocol handlers, multiple downloads, screen capture) goes to page(_:decidePermissionFor:), so there is no prompt, quiet chip or indicator to hide. |
| Translate | .translate |
Yes | Yes | Yes | None: CEF has no translation API. |
| Save password | .savePassword |
Yes | Yes | Yes | None: CEF exposes no password store. |
| Device chooser (USB, serial, HID, Bluetooth) | .deviceChooser |
Yes | Yes | Yes | None: CEF has no device chooser API. Hidden or dismissed, the page’s request fails. |
| Autofill suggestions | .autofillSuggestions |
No | Yes | No | None: CEF exposes no autofill data. Placed by Chromium at the field. |
| Payment sheet | .paymentRequest |
No | Yes | Yes | None. |
| Print preview | .printPreview |
No | Yes | Yes | None; page.printToPDF prints without it. Hidden or dismissed, printing is cancelled and the page takes input again. |
| Anything else | .other(title), .other("view:Class") |
Titled ones | Yes | Titled ones | None. Identifiers are Chromium’s and change between versions. |
CEF 156 shows the bookmark, translate, page info and save-password
bubbles neither without Chrome’s toolbar nor with it in a CefWindow, as
SelfTest’s surface inventory found; CefChromium recognizes them should a
later CEF show them. IDC_MANAGE_PASSWORDS_FOR_PAGE does nothing in
CefKit: in CEF 156 it ends the browser process.
Other interface Chromium shows lives elsewhere: JavaScript dialogs, the
beforeunload question, file choosers, HTTP authentication answers,
client certificates and certificate errors go through the
delegate; context menus through CefKit’s
context menu; tooltips, <select> menus, date
pickers and the color panel are AppKit or page popups Chromium places at
the page element; picture in picture, the task manager and DevTools are
windows of their own, not attached to the CefWindow; crash pages and Safe
Browsing warnings are pages.