Fix eframe window not being focused on mac on startup (#7593)

* Workaround for https://github.com/rust-windowing/winit/issues/4371
* Closes https://github.com/emilk/egui/issues/7588

Tested manually
This commit is contained in:
Emil Ernerfeldt 2025-10-07 14:21:10 +02:00 committed by GitHub
parent 78fffc7707
commit ab461f4115
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 6 deletions

View File

@ -775,8 +775,21 @@ impl GlowWinitRunning<'_> {
let mut repaint_asap = false; let mut repaint_asap = false;
match event { match event {
winit::event::WindowEvent::Focused(new_focused) => { winit::event::WindowEvent::Focused(focused) => {
glutin.focused_viewport = new_focused.then(|| viewport_id).flatten(); let focused = if cfg!(target_os = "macos")
&& let Some(viewport_id) = viewport_id
&& let Some(viewport) = glutin.viewports.get(&viewport_id)
&& let Some(window) = &viewport.window
{
// TODO(emilk): remove this work-around once we update winit
// https://github.com/rust-windowing/winit/issues/4371
// https://github.com/emilk/egui/issues/7588
window.has_focus()
} else {
*focused
};
glutin.focused_viewport = focused.then_some(viewport_id).flatten();
} }
winit::event::WindowEvent::Resized(physical_size) => { winit::event::WindowEvent::Resized(physical_size) => {

View File

@ -775,8 +775,21 @@ impl WgpuWinitRunning<'_> {
let mut repaint_asap = false; let mut repaint_asap = false;
match event { match event {
winit::event::WindowEvent::Focused(new_focused) => { winit::event::WindowEvent::Focused(focused) => {
shared.focused_viewport = new_focused.then(|| viewport_id).flatten(); let focused = if cfg!(target_os = "macos")
&& let Some(viewport_id) = viewport_id
&& let Some(viewport) = shared.viewports.get(&viewport_id)
&& let Some(window) = &viewport.window
{
// TODO(emilk): remove this work-around once we update winit
// https://github.com/rust-windowing/winit/issues/4371
// https://github.com/emilk/egui/issues/7588
window.has_focus()
} else {
*focused
};
shared.focused_viewport = focused.then_some(viewport_id).flatten();
} }
winit::event::WindowEvent::Resized(physical_size) => { winit::event::WindowEvent::Resized(physical_size) => {

View File

@ -423,10 +423,19 @@ impl State {
} }
} }
WindowEvent::Focused(focused) => { WindowEvent::Focused(focused) => {
self.egui_input.focused = *focused; let focused = if cfg!(target_os = "macos") {
// TODO(emilk): remove this work-around once we update winit
// https://github.com/rust-windowing/winit/issues/4371
// https://github.com/emilk/egui/issues/7588
window.has_focus()
} else {
*focused
};
self.egui_input.focused = focused;
self.egui_input self.egui_input
.events .events
.push(egui::Event::WindowFocused(*focused)); .push(egui::Event::WindowFocused(focused));
EventResponse { EventResponse {
repaint: true, repaint: true,
consumed: false, consumed: false,