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

Swipe navigation

Two-finger swipes back and forward, your own indicator, and the back/forward list.

var pageConfiguration = CefPage.Configuration()
pageConfiguration.allowsBackForwardNavigationGestures = true
let page = CefPage(url: url, configuration: pageConfiguration)
// page.navigationGestureProgress runs from 0 toward -1 (back) or 1 (forward).

Gallery: SwipeNavigationDemo. Browser example: Features/SwipeIndicator draws the browser’s own indicator.

Swipe navigation

With CefPage.Configuration.allowsBackForwardNavigationGestures, a two-finger swipe that starts sideways (when the system’s “Swipe between pages” setting allows it) and the dedicated navigation swipe go back and forward, in CefWebView, both renderings, and in CefWindow, where Chrome’s own swipe handling does the work. It is off by default everywhere.

While a swipe is in progress, page.navigationGestureProgress moves from 0 toward -1 (back) or 1 (forward); at ±1 and beyond, releasing navigates. Draw an indicator of your own from it. In a CefWindow, Chromium shows its own arrow unless CefPage.Configuration.showsChromiumNavigationGestureIndicator is turned off; lay yours over the page with pageOverlay (or setPageOverlay(_:) in SwiftUI). Over a CefWebView, an .overlay does it, in both renderings:

CefWebView(page)
    .overlay {
        let progress = page.navigationGestureProgress
        Image(systemName: progress < 0 ? "chevron.left" : "chevron.right")
            .padding(12)
            .background(.regularMaterial, in: .circle)
            .frame(maxWidth: .infinity, alignment: progress < 0 ? .leading : .trailing)
            .opacity(min(1, abs(progress)))
            .allowsHitTesting(false)
    }

pageConfiguration.showsChromiumNavigationGestureIndicator = false   // a CefWindow
window.setPageOverlay { MyIndicator(page: window.page) }

In AppKit, place a view of your own over CefWindowedPageView or CefOffscreenPageView, as a sibling above it, and have it return nil from hitTest(_:). The Browser example’s SwipeIndicator is a complete one: a chevron that slides in from the edge, resists past the commit point and fills with the accent color once releasing navigates.

A view claims a swipe as it begins, when there is history in its direction, so the page does not also scroll sideways; in a CefWindow, Chrome lets the page scroll first and swipes only at its edge.

Back and forward

page.backForwardList is a snapshot of Chromium’s navigation entries, shaped like WKBackForwardList: items oldest first, currentIndex, currentItem, backItem, forwardItem, backList, forwardList and item(at:). Each item has its url, displayURL, originalURL (before redirects), title, httpStatusCode, completionDate, hasPostData and transition (its source, such as .link, .explicit, .formSubmit or .reload, and whether it isBackForward, isRedirect, isDirectLoad). Reading the list in a SwiftUI view updates the view as the page navigates, so a back button’s menu is a ForEach over backList:

Menu("Back") {
    ForEach(page.backForwardList.backList.reversed()) { item in
        Button(item.title.isEmpty ? item.url?.absoluteString ?? "" : item.title) { page.go(to: item) }
    }
} primaryAction: {
    page.goBack()
}

go(to:) goes to an item however many steps away in one navigation: Chromium counts each step from the one still pending, so the pages in between are never loaded. It returns false and does nothing when the list has changed since the item was read.

Was this page helpful?