Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions _release-content/migration-guides/macos_app_activation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: "macOS app activation now follows `Window::focused` on startup and window creation"
pull_requests: [24702]
---

On macOS, apps now request activation (to become the active/frontmost app) on startup only if the `WindowPlugin::primary_window` (or any additional `Window` entities available before `WinitPlugin::build`) has `focused: true`. This allows apps to avoid stealing focus from the user by setting `focused: false`.

In addition, apps now request activation when a visible `Window` is created after startup with `focused: true`. Set `focused: false` if you'd prefer the app to remain inactive.

Apps that only use the default `WindowPlugin::primary_window`, which is initially `focused: true` by default, are unchanged.
15 changes: 15 additions & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ impl Plugin for WinitPlugin {
event_loop_builder.with_any_thread(self.run_on_any_thread);
}

#[cfg(target_os = "macos")]
{
use bevy_ecs::system::SystemState;
use winit::platform::macos::EventLoopBuilderExtMacOS;

// Don't request app activation on startup if all its windows should
// start unfocused. Otherwise, app activation would focus one of the
// windows.
let mut initial_windows_state =
SystemState::<Query<(Entity, &Window)>>::new(app.world_mut());
let initial_windows = initial_windows_state.get(app.world()).unwrap();
let initially_focused = initial_windows.iter().any(|(_, window)| window.focused);
event_loop_builder.with_activate_ignoring_other_apps(initially_focused);
}

#[cfg(target_os = "windows")]
{
use winit::platform::windows::EventLoopBuilderExtWindows;
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ pub fn create_windows(
}
}

#[cfg(target_os = "macos")]
{
// Request app activation via `focus_window()` if the window should start focused.
if window.focused {
winit_window.focus_window();
}
}

window_created_events.write(WindowCreated { window: entity });
}
});
Expand Down
Loading