Skip to content

WebView: no pinch-to-zoom on macOS, no Ref command to load a URL, and Apple reports cancelled loads as errors (WebError has no code) #202

Closed

Description

@houko

Context

Three small gaps in core.WebView, all found while porting the same in-app browser. Line numbers are on main at 2345b2f.

1. No pinch-to-zoom. WebView (core/webview.go:19-64) has no field for magnification. The only gesture option is BackForwardGestures (core/webview.go:40), which Apple applies with setAllowsBackForwardNavigationGestures: (internal/platform/apple/web.go:277), Windows with ICoreWebView2Settings6 (internal/platform/windows/web.go:564-565) and Linux with webkit_settings_set_enable_back_forward_navigation_gestures (internal/platform/linux/web.go:315). Nothing sets WKWebView.allowsMagnification, whose default is NO, so a trackpad pinch on macOS does nothing. WebView2's IsPinchZoomEnabled defaults to true and NSGo never sets it, so the same app zooms on Windows and not on macOS, and cannot turn it off on Windows.

2. No way to load a URL again. A page loads only when SourceFunc's source changes: core compares it with the current config and touches the node only on a difference (core/webview.go:127-134), and each backend loads only when c.Source != old.Source (Apple internal/platform/apple/web.go:283, Linux internal/platform/linux/web.go:318, Windows internal/platform/windows/web.go:595). After the user follows links away from the source, returning the same source from SourceFunc does nothing, and a Signal[WebSource] set to the same value does not even run the effect (NewSignal's Set returns early on an equal value). The Ref commands are Reload, GoBack, GoForward, StopLoading, PostMessage, InjectJavaScript, SendInput and Evaluate (core/webview.go:231-297, internal/driver/webview.go:151-167). Reload reloads the page the view is on now, not a given address.

3. Apple reports cancelled loads as errors, and WebError cannot tell them apart. WebError is only URL and Message (core/webview.go:78-85), and core builds it from the event's text (core/webview.go:186-189). The Linux backend drops loads that were stopped or replaced by another navigation, WEBKIT_NETWORK_ERROR_CANCELLED and WEBKIT_POLICY_ERROR_FRAME_LOAD_INTERRUPTED_BY_POLICY_CHANGE (internal/platform/linux/web.go:776-779, constants at :193 and :196). Windows drops COREWEBVIEW2_WEB_ERROR_STATUS_OPERATION_CANCELED, for which webError(14) returns "" (internal/platform/windows/web.go:526-527, checked at :420). The Apple handler dispatches every didFailNavigation and didFailProvisionalNavigation unconditionally (internal/platform/apple/web.go:68-79, registered at :90-91), including NSURLErrorCancelled (-999, "cancelled") and WebKitErrorFrameLoadInterruptedByPolicyChange (102, "Frame load interrupted"), which WebKit reports when a navigation's policy changes, for example a response the view turns into a download (internal/platform/apple/web.go:156-164). On macOS and iOS, OnError therefore fires for StopLoading, for a new source set while the previous one loads, and for a page that redirects itself during load. The app has only a localized message to look at.

Minimal repro of 2 and 3 (type-checks against main):

package main

import (
	"log"

	"github.com/nsgo-dev/nsgo"
	"github.com/nsgo-dev/nsgo/core"
	"github.com/nsgo-dev/nsgo/ui"
)

func main() {
	nsgo.Run(func(cx *core.Scope) core.Node {
		home := core.WebSrc("https://example.com/")
		src := core.NewSignal(cx, home)
		ref := &core.Ref{}
		return core.View{Style: core.Style{FlexGrow: 1}, Children: []core.Node{
			ui.Wrap{Children: []core.Node{
				// Follow the page's link first, then press Home: nothing loads.
				ui.Button{Text: "Home", OnPress: func() { src.Set(home) }},
				// Press while the slow page loads: macOS logs "error: cancelled".
				ui.Button{Text: "Slow page", OnPress: func() { src.Set(core.WebSrc("https://httpbin.org/delay/5")) }},
				ui.Button{Text: "Stop", OnPress: ref.StopLoading},
			}},
			core.WebView{Ref: ref, SourceFunc: src.Get, Style: core.Style{FlexGrow: 1},
				OnLoadStart: func(n core.WebNavigation) { log.Printf("load start %s", n.URL) },
				OnError:     func(err error) { log.Printf("error: %v", err) }},
		}}
	})
}

What happens:

  • Click the "More information..." link on example.com, then press Home. No load start is logged on any desktop and the view stays on the other site.
  • Press Slow page, then Stop (or Home) before it finishes. On macOS OnError logs cancelled. On Linux and Windows nothing is logged.
  • Pinch on a macOS trackpad over the page: nothing zooms. On Windows the page zooms.

Why it matters for real apps

Kando (github.com/yldm-tech/kando) is a desktop video player with a built-in browser: the user signs in to a video site, browses to a video and hands its page to the downloader. Its browser does all three of these on its previous toolkit (apps/kando/state_browse.go: browseLoad, browseOpenWebView and browseLoadFailed):

  • Pinch-to-zoom. The page is a normal website shown at the window's size, and on a MacBook pinching is how users enlarge small text and thumbnails, as in Safari. The view sets PinchZoom: true today. On NSGo there is no equivalent, and CSS zoom injected through InjectJavaScript is not a gesture and reflows the page.
  • Load a URL. The toolbar has an address field and the start page has site shortcuts. Typing the address the view started from, or picking the shortcut for the site the user is already browsing, has to load it, as every browser does. The port works around it with ref.InjectJavaScript("location.assign(" + jsonURL + ")"). That goes through the page: a beforeunload handler can hold it, it does not carry WebView.Headers, and core cannot apply Access to it as it does to a Source (core/webview.go:137-156), because to the platform it is the page's own navigation.
  • Load failures. A load that fails shows a message bar with the reason. On NSGo's Apple backend every address the user types while a page is loading, every Stop and every site that redirects with location.replace during load shows "cancelled" or "Frame load interrupted" as a failure. The port drops failures of loads it cut short itself and failures while its start page shows. A cancellation the page caused still shows until the next load starts, because a message string cannot be matched reliably across languages and OS versions.

Proposal

  1. PinchZoom, opt-in like BackForwardGestures:
// PinchZoom lets a pinch, or the platform's zoom gesture, magnify the
// page where the platform has one (macOS trackpads, touch screens).
PinchZoom bool
  • macOS: setAllowsMagnification: next to setAllowsBackForwardNavigationGestures: in Configure.
  • iOS: WKWebView already zooms by the page's viewport, so it can be documented as having no effect there.
  • Windows: ICoreWebView2Settings5::put_IsPinchZoomEnabled(c.PinchZoom). This changes today's default (on), which the release notes should say.
  • Linux: whatever WebKitGTK exposes, with the field's doc stating the behaviour there.
  1. Ref.Load, a command that loads a source even when it equals the current one:
// Load loads src in the web view, as a new Source would, even when it is
// the source the view already has. Access, Headers and OnError apply as
// they do to Source. SourceFunc keeps its own source: the next change it
// makes loads again.
func (r *Ref) Load(src WebSource)

This needs a driver.WebLoad command that carries a WebSource (in MountItem.Data, as WebSendInput carries its *WebInput). Core runs it through the same refuseSource check that restrictWeb uses before sending it. Each backend calls the load it already has (Apple load, Linux loadWeb, Windows' navigation in apply).

  1. Error codes, and the same filtering on Apple.
  • In the Apple failed handler, skip NSURLErrorDomain -999 and WebKitErrorDomain 102, as Linux and Windows already skip their equivalents. This is a bug fix, not new API.
  • Add the platform's code to WebError so an app can tell offline from DNS from TLS without parsing localized text:
type WebError struct {
	URL     string
	Message string
	// Domain and Code are the platform's: NSError's domain and code on
	// Apple platforms, the GError domain's name and code on Linux,
	// "COREWEBVIEW2_WEB_ERROR_STATUS" and its value on Windows. Both are
	// empty for a navigation NSGo itself refused.
	Domain string
	Code   int
}

driver.Event would carry them for EventError (for example Data: driver.WebFailure{Domain, Code}, as other events use Data), and core would copy them at core/webview.go:188.

Acceptance

  • With PinchZoom: true, a trackpad pinch magnifies the page on macOS. With it false, a pinch does nothing on macOS and Windows. The field's doc states the iOS and Linux behaviour.
  • After following a link away from Source, Ref.Load(Source) fires OnLoadStart and shows the source again on all three desktops. So does Ref.Load with the current URL.
  • Ref.Load of a source that Access refuses arrives as OnError and loads nothing, as the same Source would. Headers are sent with the request.
  • Ref.StopLoading during a load, a new source set during a load, and a page that calls location.replace before it finishes do not fire OnError on macOS and iOS, matching Linux and Windows.
  • A link that OnDownload saves does not fire OnError ("Frame load interrupted") on macOS.
  • An unreachable host fires OnError with Domain/Code set: NSURLErrorDomain/-1003 on Apple, the WebKitGTK network error on Linux, the WebView2 status on Windows. A refused navigation has both empty.
  • The gallery's site browser (examples/gallery/media.go, next to its Back, Forward and Reload buttons) gets a Home button that uses Ref.Load, and docs/architecture/native-views.md records PinchZoom per target.

Note

None of this is covered by #33 (script results, downloads, headers, Incognito, custom schemes, trusted input), #112 (cookie store), #113 (new windows, same-document navigations) or #114 (muting, clearing website data, user agent suffix). #113 makes the address field follow the page. This issue is about the other direction: loading what the user typed into the address field.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions