Repository navigation
feat(desktop): report a window's native handle - #2835
Merged
Merged
Conversation
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.
|
This pull request has no valid
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
desktop.Window.NativeHandle()anddesktop.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'sSCContentFilter, Windows Graphics Capture or an XDG portal'sparent_window. Both return adesktop.NativeHandlewith aKindand the handle:NativeHandleAppKit):Handleis theNSWindow'swindowNumber, theCGWindowIDthat ScreenCaptureKit andCGWindowListCopyWindowInfomatch, andObjectis theNSWindowitself, for AppKit calls in a native component on the main thread.NativeHandleWin32):Handleis the top-levelHWND.NativeHandleX11):Handleis the realizedGtkWindow's XID. GTK realizes a window as it first shows, so a window opened hidden reports none until then; this is documented.Window.NativeHandleis for a window or panel the app holds.desktop.NativeWindow(cx)is for the window a view is in, in the same way asdesktop.Frameanddesktop.Maximized. It also reaches the main window that Windows and Linux open fornsgo.Run(Root). That window is the backend's own primary surface (PlatformMain), so no*Windowstands for it anddesktop.MainWindowreturns nil there.Each backend reports the handle with the surface's environment (
Environment.NativeWindow), the same way it reportsMaximizedandWindowFrame, 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 thatSetWindowEnvironmentsets, 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 innsgotest'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
windowNumberappears askCGWindowNumberfor this process's PID inCGWindowListCopyWindowInfo; on WindowsIsWindowis true andGetAncestor(hwnd, GA_ROOT) == hwndfor 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
core,base,ui,tsx, thensgotool)Screenshots
None
Verification
b84a066ccf90ef44b7b478273e1b8ecc0ff0e78cb84aa6d55go tool nsgo check -target macos,windows,linux -jobs 6→ exit 0go tool nsgo test ./core ./desktop ./internal/driver ./internal/moduletest ./nsgotest→ exit 0go tool nsgo test -run TestNativeWindowHandle ./internal/platform/macos→ exit 0go run ./tools/changelog check→ exit 0TestNativeWindowHandleOfOpenedWindows,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
fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.changelog.d/fragment, not inCHANGELOG.md.