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

Offscreen rendering and overlays

Pages drawn into your own layer, so SwiftUI content, effects and transforms go over and around them.

// With runtimeConfiguration.isOffscreenRenderingEnabled = true:
var pageConfiguration = CefPage.Configuration()
pageConfiguration.rendering = .offscreen(frameRate: 120)
let page = CefPage(url: url, configuration: pageConfiguration)
let card = CefWebView(page)
    .clipShape(.rect(cornerRadius: 16))
    .overlay(alignment: .bottomTrailing) { Button("Close") { page.close() }.padding() }

Gallery: OffscreenOverlayDemo.

Offscreen rendering

Offscreen pages need configuration.isOffscreenRenderingEnabled = true.

var pageConfiguration = CefPage.Configuration()
pageConfiguration.rendering = .offscreen(frameRate: 120)
pageConfiguration.allowsBackForwardNavigationGestures = true
let page = CefPage(url: url, configuration: pageConfiguration)

Pinch and smart magnify change zoomLevel. Set CefPage.Configuration.forwardsRawTouchEvents = true to send trackpad touches to the page instead of interpreting them as gestures.

page.offscreenFrameRate starts as the rendering’s rate and can change while the page runs, from 1 to 240: a game’s menu at 30 and its play at 120. While Low Power Mode or the Mac’s heat lowers offscreen rates (CefRuntime.shared.energyPolicy), the lower of the two applies. setNeedsDisplay() asks Chromium to paint the whole view again (invalidate), with the next frame:

page.offscreenFrameRate = 30
page.setNeedsDisplay()

Offscreen pages cannot open native popup windows: a request that would open one loads in the same page instead (see Navigation and new windows).

To draw offscreen frames in a view of your own, adopt CefOffscreenHost and call page.host(offscreenIn:size:). The host receives frames, cursors, IME ranges and drags; CefKit shows context menus over the host view (see Context menu). A frame’s IOSurface comes from Chromium’s pool and is valid only during the call: Chromium renders later frames into it, even while a layer still shows it, so copy it into a surface of your own before returning, as CefOffscreenPageView does with a Metal blit (see Frame ownership). In return the host drives the page as AppKit drives a view:

  • Lifecycle: hostViewDidResize(), hostViewDidHide(), hostViewDidUnhide(), hostViewDidBecomeKey(), hostViewDidResignKey(), hostViewDidChangeBackingProperties(), and hostViewDidMove() when the view’s window moves.
  • Input: sendMouseMove(to:modifiers:), sendMouseDown(at:button:clickCount:modifiers:), sendMouseUp(at:button:clickCount:modifiers:), sendMouseExit(at:modifiers:), sendScrollWheel(at:delta:modifiers:), sendKeyEvent(_:), sendTouchEvent(_:).
  • Text input: setMarkedText(_:selectedRange:replacementRange:), insertText(_:replacementRange:), unmarkText(keepingSelection:), cancelComposition().
  • Drag and drop: draggingEntered(…), draggingUpdated(at:modifiers:allowedOperations:), draggingExited(), performDrop(at:modifiers:) and draggingSessionEnded(at:operation:), which mirror NSDraggingDestination and NSDraggingSource.

With CEF 158, sendTouchEvent(_:) also delivers touch sequences to web content in a focused windowed page. Use coordinates relative to the top-left of the page view and keep each contact’s ID stable until it ends or is cancelled.

The page’s tooltips (a title under the mouse) reach the host through offscreenDidChangeToolTip(_:); CefOffscreenPageView shows them as its own toolTip, as AppKit does for any view (see Tooltips).

Was this page helpful?