Skip to content

feat(desktop): report a window's native handle - #2835

Merged
houko merged 5 commits into
mainfrom
feat/2827-native-window-handle
Dec 1, 2026
Merged

houko merged 5 commits into
mainfrom
feat/2827-native-window-handle

Conversation

@houko

@houko houko commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add desktop.Window.NativeHandle() and desktop.NativeWindow(cx), which return a window's or panel's identity on its platform so apps can hand it to system APIs NSGo does not wrap, such as ScreenCaptureKit's SCContentFilter, Windows Graphics Capture or an XDG portal's parent_window. Both return a desktop.NativeHandle with a Kind and the handle:

  • macOS (NativeHandleAppKit): Handle is the NSWindow's windowNumber, the CGWindowID that ScreenCaptureKit and CGWindowListCopyWindowInfo match, and Object is the NSWindow itself, for AppKit calls in a native component on the main thread.
  • Windows (NativeHandleWin32): Handle is the top-level HWND.
  • Linux on X11 (NativeHandleX11): Handle is the realized GtkWindow's XID. GTK realizes a window as it first shows, so a window opened hidden reports none until then; this is documented.

Window.NativeHandle is for a window or panel the app holds. desktop.NativeWindow(cx) is for the window a view is in, in the same way as desktop.Frame and desktop.Maximized. It also reaches the main window that Windows and Linux open for nsgo.Run(Root). That window is the backend's own primary surface (PlatformMain), so no *Window stands for it and desktop.MainWindow returns nil there.

Each backend reports the handle with the surface's environment (Environment.NativeWindow), the same way it reports Maximized and WindowFrame, so the engine reads it without touching main-thread-only objects, as the issue asks. The core starts every new surface with no handle, so a window never reads the app's or another window's. On Windows and Linux, the hidden primary surface of an app that opens its windows itself reports none, and the primary that is the main window reports its handle. nsgotest's headless model gives no window the handle in the app's environment, only one that SetWindowEnvironment sets, so the handle stays per window. The accessors are tracked, so an effect sees the handle arrive. They return false for a nil or closed window, before the platform reports the handle, on Wayland, on phones, on the web and in nsgotest's headless model. An embedded view and a tray icon's surface report none on every platform.

Native tests check the value against each platform's own APIs: on macOS the windowNumber appears as kCGWindowNumber for this process's PID in CGWindowListCopyWindowInfo; on Windows IsWindow is true and GetAncestor(hwnd, GA_ROOT) == hwnd for an opened window, a panel and the platform main window; on X11 the X server places the reported XID once the window shows.

Wayland's exported xdg-foreign handle is recorded in docs/roadmap.md. Until then, Wayland reports none, and the docs say so.

Fixes #2827

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Screenshots

None

Verification

  • head: b84a066ccf90ef44b7b478273e1b8ecc0ff0e78c
  • base: b84aa6d55
  • commands:
    • go tool nsgo check -target macos,windows,linux -jobs 6 → exit 0
    • go tool nsgo test ./core ./desktop ./internal/driver ./internal/moduletest ./nsgotest → exit 0
    • go tool nsgo test -run TestNativeWindowHandle ./internal/platform/macos → exit 0
    • go run ./tools/changelog check → exit 0
  • result: pass
  • not covered: these ran on a darwin/amd64 host over SSH. The new Windows tests (TestNativeWindowHandleOfOpenedWindows, TestNativeWindowHandleOfPlatformMain) and the X11 test (TestNativeWindowHandleOnX11) were vetted for windows and linux but not run, so CI is their first run. The macOS test ran on amd64 only. iOS, Android, the web and the emulator jobs were not run.

Checklist

  • The title is a Conventional Commit, such as fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.
  • Docs whose facts change are updated, and a capability users can see has a line in a changelog.d/ fragment, not in CHANGELOG.md.
  • New third-party code, assets and dependencies name their source and license.

houko added 5 commits December 1, 2026 11:24
Add desktop.Window.NativeHandle, which returns the window's identity on its platform for system APIs NSGo does not wrap: the NSWindow's windowNumber (a CGWindowID) and the NSWindow on macOS, the top-level HWND on Windows and the XID on X11. Each backend reports it with the surface's environment, so the engine reads it without touching main-thread-only objects; the core starts a new surface with none, and the accessor returns false before the report, once the window closes, on Wayland, phones, the web and in nsgotest.

Fixes #2827
desktop.NativeWindow(cx) returns the handle of the window or panel a scope is in, which reaches the main window Windows and Linux open for nsgo.Run(Root), where MainWindow is nil. The hidden primary of an app that opens its windows itself reports no handle, and nsgotest's headless model keeps each window's handle its own.
macOS finds the windowNumber among the process's windows in CGWindowListCopyWindowInfo; Windows checks IsWindow and GetAncestor(GA_ROOT) for an opened window, a panel and the platform main window; X11 checks the XID the X server places once the window shows.
@github-actions github-actions Bot added size/L local-ci-skipped enhancement New feature or request documentation Improvements or additions to documentation macOS Windows Linux core and removed enhancement New feature or request labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

This pull request has no valid Local CI declaration for its head commit, so a maintainer audits it before merging:

  • commands gives no exit code, such as go tool nsgo check → 0

Run the checks on the head commit, fill the block in the template, then push or edit the body: this label and comment update on their own.

@houko
houko merged commit cc1221f into main Dec 1, 2026
48 checks passed
@houko
houko deleted the feat/2827-native-window-handle branch December 1, 2026 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

desktop: no way to get a Window's native handle (CGWindowID, HWND, X11 XID, Wayland handle) for system APIs such as capture filters

1 participant