Skip to content

Fix intermittent window not coming to front on macOS startup#174

Merged
treeform merged 1 commit intomasterfrom
fix/macos-window-focus-on-startup
Apr 7, 2026
Merged

Fix intermittent window not coming to front on macOS startup#174
treeform merged 1 commit intomasterfrom
fix/macos-window-focus-on-startup

Conversation

@treeform
Copy link
Copy Markdown
Owner

@treeform treeform commented Apr 7, 2026

Summary

  • Moves setActivationPolicy(NSApplicationActivationPolicyRegular) from applicationDidFinishLaunching to before finishLaunching() in init(), fixing intermittent failure of the window to come to front on startup.

Problem

When a process starts (especially from terminal / without a .app bundle), macOS classifies it as a background process. setActivationPolicy(Regular) tells the window server to promote it to a regular GUI app, but the window server processes this asynchronously via IPC.

The old code set the policy inside applicationDidFinishLaunching and immediately called activateIgnoringOtherApps(true) — a race condition with the window server. Sometimes the promotion finished in time (window comes to front), sometimes it didn't (window stays behind).

Fix

Set the activation policy as early as possible — before finishLaunching() — so the window server has time to process the policy change before any window is created or activation is attempted. This is the same approach used by GLFW and SDL.

Test plan

  • Launch a windy app repeatedly from the terminal on macOS and verify the window consistently comes to front
  • Verify window focus callbacks (onFocusChange) still fire correctly
  • Test on both Intel and Apple Silicon Macs

🤖 Generated with Claude Code

Move setActivationPolicy(Regular) from applicationDidFinishLaunching
to before finishLaunching() in init(). The window server processes
activation policy changes asynchronously via IPC, so calling it inside
the launch callback and immediately trying to activate creates a race
condition — sometimes the promotion completes in time, sometimes it
doesn't, causing the window to stay behind other apps.

This matches what GLFW and SDL do: set the policy as early as possible
so the window server has time to promote the process before any window
is created or activation is attempted.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@monofuel monofuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@treeform treeform merged commit 45f58a2 into master Apr 7, 2026
3 checks passed
nishu-builder pushed a commit to Metta-AI/mettagrid that referenced this pull request Apr 9, 2026
## Summary

- Updates the windy nimby lock hash from `d87b6ad` to `45f58a2` (latest master)

## What changed in windy

The new windy includes [treeform/windy#174](treeform/windy#174) which fixes an intermittent bug where the mettascope window would not come to the front on macOS startup.

**Root cause:** `setActivationPolicy(NSApplicationActivationPolicyRegular)` was called inside the `applicationDidFinishLaunching` callback and then immediately tried to activate the app. This policy change tells the macOS window server (a separate process) to promote the app from a background process to a regular GUI app, but the window server processes this asynchronously via IPC. Sometimes the promotion completed before the activation request arrived (window comes to front), sometimes it didn't (window stays behind other apps).

**Fix:** Moved `setActivationPolicy` to before `finishLaunching()` in `init()`, giving the window server more time to process the policy change before any window is created or activation is attempted. This is the same approach used by GLFW and SDL.

Also includes:

- [treeform/windy#172](treeform/windy#172): Unify `pollHttp` behavior across platforms
- [treeform/windy#173](treeform/windy#173): Fix readme spelling and grammar

## Test plan

- [x] Launch mettascope repeatedly on macOS and verify the window consistently comes to front

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants