webview_cocoa.h
The macOS webview backend (WKWebView).
#include <sonore/webview_cocoa.h>- 1type
- 8other declarations
- Guides: The web editor
On this page
Design notes
Written against the Objective-C RUNTIME rather than in Objective-C, so the whole SDK stays plain C++ and a generated project needs no .mm file, no mixed-language build, and no Xcode-specific CMake handling. Every Cocoa call here is objc_msgSend on a class looked up by name, which is exactly what the compiler emits for Objective-C anyway.
Shape mirrors the other backends: an NSView the host embeds, the bridge injected at document start, a ~30 Hz timer, and a message handler.
macOS specifics that shape this file:
- CLAP's cocoa API uses LOGICAL points, and the OS handles Retina scaling, so set_scale is (correctly) ignored, as it is on Windows.
- A script message handler is an Objective-C object with a required method. There is no such class in the frameworks, so one is REGISTERED at runtime with a C function as its implementation. That is the standard way to do this without writing Objective-C, and it is why the file needs objc/runtime.h.
- The webview is added as a subview of the host's NSView and pinned with an autoresizing mask, so a host resizing its window resizes the page without a round trip through set_size.
Types 1
A WKWebView inside an NSView the host embeds.
Functions 8
- fn
template <typename Ret, typename... Args> inline Ret msg(id target, SEL selector, Args... args)objc_msgSend must be called through a correctly-typed function pointer: it is a trampoline, and calling it with the wrong signature corrupts arguments on arm64 (where variadic and regular calls use different registers).cocoa::msg · webview_cocoa.h:50 - fn
inline id cls(const char* name)cocoa::cls · webview_cocoa.h:55 - fn
inline id nsString(const std::string& s)An autoreleased NSString from UTF-8.cocoa::nsString · webview_cocoa.h:58 - fn
inline std::string fromNsString(id str)cocoa::fromNsString · webview_cocoa.h:63 - fn
inline const char* uniqueClassName(const char* base, void* inThisImage, char* out, size_t cap)A runtime class name that is unique to THIS binary.cocoa::uniqueClassName · webview_cocoa.h:88More
The Objective-C runtime has one namespace per process. Two plugins built from different SDK versions, loaded into one host, would both register "SonoreScriptHandler": the second finds the first's class and uses it, with method implementations that live in the OTHER binary -- and when that binary unloads, every message into the class jumps into freed code. The address of a function in this image is unique per load, so the name is too (the Win32 window class does the same with its module handle).
- fn
inline Class messageHandlerClass()cocoa::messageHandlerClass · webview_cocoa.h:93 - fn
inline void handleScriptMessage(id self, SEL, id, id message)cocoa::handleScriptMessage · webview_cocoa.h:335