Skip to content

feat(desktop): report the application in front - #579

Merged
houko merged 2 commits into
mainfrom
feat/frontmost-app
Nov 17, 2026
Merged

houko merged 2 commits into
mainfrom
feat/frontmost-app

Conversation

@houko

@houko houko commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #570.

desktop.FrontmostApp(cx) returns the application in front on the desktop, the app itself included, as a desktop.FrontmostAppInfo (an alias of core.FrontmostAppInfo): its ID, name, process ID and whether its frontmost window is in full screen. desktop.CanReadFrontmostApp(cx) says whether the platform follows it. Both are part of the environment, so effects and bindings reading them run again as another app comes to the front or the one in front enters or leaves full screen. Both are held to the new core.AccessFrontmostApp capability, since they say what else the user runs: refused code reads zero and false, and asking CanReadFrontmostApp logs no refusal. FrontmostApp is zero wherever CanReadFrontmostApp is false. Tests set them through nsgotest.Environment(core.Environment{FrontmostApp: ..., CanReadFrontmostApp: true}), which the test host pins over the platform's values.

  • macOS: NSWorkspace.frontmostApplication, followed through its activation and active Space notifications; full screen from the window server's window list, which needs no permission. CanReadFrontmostApp is true.
  • Windows: the foreground window's process, followed through SetWinEventHook (foreground, and location changes of the process in front) on the UI thread, whose message loop receives the out-of-context events; the path from QueryFullProcessImageNameW with PROCESS_QUERY_LIMITED_INFORMATION, the name from the executable's file description. A process Windows will not open reports its PID with no ID or name. CanReadFrontmostApp is true.
  • Linux on X11: _NET_ACTIVE_WINDOW with _GTK_APPLICATION_ID, WM_CLASS, _NET_WM_PID and _NET_WM_STATE_FULLSCREEN, followed through PropertyNotify. CanReadFrontmostApp is true only when GDK draws on X11.
  • Zero, with CanReadFrontmostApp false, on Wayland, phones, the web and mini programs. The remaining gaps (Wayland, UWP apps reporting their frame host, macOS full screen that changes no Space) are recorded in the roadmap.

Targets

  • macOS: compiled, vetted and run on the Mac Studio. The native frontmost tests pass and do not skip, both under nsgo test -target macos and under plain go test over SSH.
  • Linux (X11): compiled, vetted and unit-tested (TestWMClass). The live X11 tracking was not run, because the build host has no GUI session.
  • Windows: compiled and vetted only (cross-compile). Not run, because there is no Windows machine.
  • Wayland, iOS, tvOS, watchOS, Android, web: report zero and CanReadFrontmostApp false by design. Android, web and iOS compiled and vetted.

Screenshots

None: this adds a non-visual API and changes no UI.

Verification

All commands run from the branch's worktree at the head commit.

  • ubuild mise exec [email protected] -- go tool nsgo check -target all: exit 0. Vetted macos_arm64/x64, windows_arm64/x64, linux_arm64/x64, android_arm64 and web; ios/tvos/watchos skipped there ("need macOS with Xcode").
  • rbuild mise exec [email protected] -- go tool nsgo check -target ios: exit 0.
  • ubuild ... go tool nsgo test ./desktop: 3 passed (TestFrontmostAppFollowsTheAppInFront, TestFrontmostAppIsHeldToAccess).
  • ubuild ... go tool nsgo test ./core: 662 passed, 0 failed (includes TestServicesGoThroughTheGate and TestGateRules).
  • ubuild ... go tool nsgo test ./nsgotest: 70 passed.
  • ubuild ... go tool nsgo test ./internal/testhost: 1 passed (TestPinSetsTheTestsDisplaySettings).
  • ubuild ... go tool nsgo test -run TestWMClass ./internal/platform/linux: 1 passed.
  • rbuild ... go tool nsgo test -target macos ./desktop: 3 passed, 0 skipped.
  • rbuild ... go tool nsgo test -target macos -run 'TestReadFrontmost|TestFrontmostFollows|TestCoversDisplay' ./internal/platform/macos: 3 passed, 0 skipped.
  • rbuild ... go test -run 'TestReadFrontmost|TestFrontmostFollows|TestCoversDisplay' -v ./internal/platform/macos: all three PASS, none skipped. The earlier statement that TestReadFrontmost skips over SSH on the Mac Studio does not hold at this head: run over SSH there, it reads the app in front and passes.
  • gofmt reports nothing for the changed files.

Not checked:

  • The Windows code and its tests were compiled only, never run.
  • FullScreen against a real full-screen player and a maximized window was not checked on any platform, and the live X11 tracking was not run.

Checklist

  • Conventional Commit title.
  • Changelog, architecture and public services documentation (en and zh), testing documentation and roadmap updated.
  • No new dependencies or third-party assets.

@github-actions github-actions Bot added size/L enhancement New feature or request documentation Improvements or additions to documentation macOS Windows Linux core conflict Conflicts with the base branch; resolve it first labels Nov 17, 2026
desktop.Frontmost returns the application in front on the desktop, the app itself included: its ID, name, process ID and whether its frontmost window is in full screen. It is part of the environment, so effects reading it run again as another app comes to the front, and it is held to the new core.AccessFrontmostApp capability.

- macOS: NSWorkspace's frontmost application, followed through its activation and active Space notifications; full screen from the window server's window list.
- Windows: the foreground window's process, followed through SetWinEventHook (foreground, and location changes of the process in front); the name from the executable's file description.
- Linux X11: _NET_ACTIVE_WINDOW with _GTK_APPLICATION_ID, WM_CLASS, _NET_WM_PID and _NET_WM_STATE_FULLSCREEN, followed through PropertyNotify; zero on Wayland.

Fixes #570
Rename desktop.Frontmost to desktop.FrontmostApp and its type to desktop.FrontmostAppInfo (core.FrontmostAppInfo, driver.FrontmostAppInfo), keeping Scope.FrontmostApp, as with DisplayInfo.

Add desktop.CanReadFrontmostApp, backed by Environment.CanReadFrontmostApp: true on macOS, Windows and Linux on X11, false on Wayland, phones, the web and mini programs, and for code whose Access refuses AccessFrontmostApp, without logging a refusal. Scope.FrontmostApp now reads zero wherever the platform does not follow the app in front, and the test host pins both fields.
@houko
houko force-pushed the feat/frontmost-app branch from 5a04a39 to bc135a3 Compare November 17, 2026 11:30
@houko
houko merged commit 7ccbe32 into main Nov 17, 2026
9 of 14 checks passed
@houko
houko deleted the feat/frontmost-app branch November 17, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflict Conflicts with the base branch; resolve it first core documentation Improvements or additions to documentation enhancement New feature or request Linux macOS size/L Windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Desktop: the frontmost application and its changes

1 participant