Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: When the program is minimized, the viewport stops working. #4487

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
88 changes: 47 additions & 41 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl GlowWinitRunning {
) -> EventResult {
crate::profile_function!();

let Some(viewport_id) = self
let Some(mut viewport_id) = self
.glutin
.borrow()
.viewport_from_window
Expand All @@ -520,24 +520,26 @@ impl GlowWinitRunning {
let mut frame_timer = crate::stopwatch::Stopwatch::new();
frame_timer.start();

{
let glutin = self.glutin.borrow();
let viewport = &glutin.viewports[&viewport_id];
let is_immediate = viewport.viewport_ui_cb.is_none();
if is_immediate && viewport_id != ViewportId::ROOT {
// This will only happen if this is an immediate viewport.
// That means that the viewport cannot be rendered by itself and needs his parent to be rendered.
if let Some(parent_viewport) = glutin.viewports.get(&viewport.ids.parent) {
if let Some(window) = parent_viewport.window.as_ref() {
return EventResult::RepaintNext(window.id());
}
let (raw_input, viewport_ui_cb) = {
crate::profile_scope!("Prepare");

let mut glutin = self.glutin.borrow_mut();
let original_viewport = &glutin.viewports[&viewport_id];

// This will only happens when a Immediate Viewport.
Copy link
Owner

Choose a reason for hiding this comment

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

Dead comment?

if original_viewport.class == ViewportClass::Immediate {
let Some(parent_viewport) = glutin.viewports.get(&original_viewport.ids.parent)
else {
return EventResult::Wait;
};

if parent_viewport.class == ViewportClass::Deferred {
viewport_id = parent_viewport.ids.this;
} else {
viewport_id = ViewportId::ROOT;
}
return EventResult::Wait;
}
}

let (raw_input, viewport_ui_cb) = {
let mut glutin = self.glutin.borrow_mut();
let egui_ctx = glutin.egui_ctx.clone();
let Some(viewport) = glutin.viewports.get_mut(&viewport_id) else {
return EventResult::Wait;
Expand Down Expand Up @@ -582,21 +584,21 @@ impl GlowWinitRunning {
..
} = &mut *glutin;
let viewport = &viewports[&viewport_id];
let Some(window) = viewport.window.as_ref() else {
return EventResult::Wait;
};
let Some(gl_surface) = viewport.gl_surface.as_ref() else {
return EventResult::Wait;
};

let screen_size_in_pixels: [u32; 2] = window.inner_size().into();

{
frame_timer.pause();
change_gl_context(current_gl_context, gl_surface);
frame_timer.resume();
}

let Some(window) = viewport.window.as_ref() else {
return EventResult::Wait;
};
let screen_size_in_pixels: [u32; 2] = window.inner_size().into();

self.painter
.borrow()
.clear(screen_size_in_pixels, clear_color);
Expand Down Expand Up @@ -642,13 +644,17 @@ impl GlowWinitRunning {
let Some(viewport) = viewports.get_mut(&viewport_id) else {
return EventResult::Wait;
};

viewport.info.events.clear(); // they should have been processed
let window = viewport.window.clone().unwrap();
let gl_surface = viewport.gl_surface.as_ref().unwrap();
let egui_winit = viewport.egui_winit.as_mut().unwrap();

egui_winit.handle_platform_output(&window, platform_output);
let (Some(egui_winit), Some(window), Some(gl_surface)) = (
&mut viewport.egui_winit,
&viewport.window.clone(),
&viewport.gl_surface,
) else {
return EventResult::Wait;
};

egui_winit.handle_platform_output(window, platform_output);

let clipped_primitives = integration.egui_ctx.tessellate(shapes, pixels_per_point);

Expand Down Expand Up @@ -705,7 +711,7 @@ impl GlowWinitRunning {
}
}

integration.post_rendering(&window);
integration.post_rendering(window);
}

{
Expand Down Expand Up @@ -734,7 +740,7 @@ impl GlowWinitRunning {

integration.report_frame_time(frame_timer.total_time_sec()); // don't count auto-save time as part of regular frame time

integration.maybe_autosave(app.as_mut(), Some(&window));
integration.maybe_autosave(app.as_mut(), Some(window));

if window.is_minimized() == Some(true) {
// On Mac, a minimized Window uses up all CPU:
Expand Down Expand Up @@ -861,31 +867,31 @@ fn change_gl_context(
) {
crate::profile_function!();

let Some(p_current_gl_context) = current_gl_context.take() else {
return;
};

if !cfg!(target_os = "windows") {
// According to https://github.com/emilk/egui/issues/4289
// we cannot do this early-out on Windows.
// TODO(emilk): optimize context switching on Windows too.
// See https://github.com/emilk/egui/issues/4173

if let Some(current_gl_context) = current_gl_context {
crate::profile_scope!("is_current");
if gl_surface.is_current(current_gl_context) {
return; // Early-out to save a lot of time.
}
crate::profile_scope!("is_current");
if gl_surface.is_current(&p_current_gl_context) {
return; // Early-out to save a lot of time.
}
}

let not_current = {
crate::profile_scope!("make_not_current");
current_gl_context
.take()
.unwrap()
.make_not_current()
.unwrap()
crate::profile_scope!("make_not_current");
let Ok(not_current) = p_current_gl_context.make_not_current() else {
return;
};

crate::profile_scope!("make_current");
*current_gl_context = Some(not_current.make_current(gl_surface).unwrap());
if let Ok(current) = not_current.make_current(gl_surface) {
*current_gl_context = Some(current);
}
}

impl GlutinWindowContext {
Expand Down
24 changes: 12 additions & 12 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ impl WgpuWinitRunning {
fn run_ui_and_paint(&mut self, window_id: WindowId) -> EventResult {
crate::profile_function!();

let Some(viewport_id) = self
let Some(mut viewport_id) = self
.shared
.borrow()
.viewport_from_window
Expand Down Expand Up @@ -555,20 +555,20 @@ impl WgpuWinitRunning {
viewports, painter, ..
} = &mut *shared_lock;

if viewport_id != ViewportId::ROOT {
let Some(viewport) = viewports.get(&viewport_id) else {
let Some(original_viewport) = viewports.get(&viewport_id) else {
return EventResult::Wait;
};

// This will only happens when a Immediate Viewport.
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
// This will only happens when a Immediate Viewport.

if original_viewport.class == ViewportClass::Immediate {
let Some(parent_viewport) = viewports.get(&original_viewport.ids.parent) else {
return EventResult::Wait;
};

if viewport.viewport_ui_cb.is_none() {
// This will only happen if this is an immediate viewport.
// That means that the viewport cannot be rendered by itself and needs his parent to be rendered.
if let Some(viewport) = viewports.get(&viewport.ids.parent) {
if let Some(window) = viewport.window.as_ref() {
return EventResult::RepaintNext(window.id());
}
}
return EventResult::Wait;
if parent_viewport.class == ViewportClass::Deferred {
viewport_id = parent_viewport.ids.this;
} else {
viewport_id = ViewportId::ROOT;
}
}

Expand Down
Loading