diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 26c5812a12a..179f4dd505f 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -1276,9 +1276,9 @@ mod glow_integration { if recreate { viewport.window = None; viewport.gl_surface = None; - viewport.viewport_ui_cb = viewport_ui_cb.clone(); viewport.id_pair.parent = *id; } + viewport.viewport_ui_cb = viewport_ui_cb.clone(); // always update the latest callback if let Some(w) = viewport.window.clone() { process_viewport_commands(commands, *id, None, &w.borrow()); } diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index d3849e76ac9..1c5c101c1da 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -1548,7 +1548,7 @@ impl Context { let mut viewports = Vec::new(); self.write(|ctx| { ctx.viewports.retain(|_, viewport| { - let retain = viewport.used; + let was_used = viewport.used; if viewport_id == viewport.id_pair.parent { viewport.used = false; @@ -1559,7 +1559,7 @@ impl Context { id_pair: viewport.id_pair, viewport_ui_cb: viewport.viewport_ui_cb.clone(), }); - (retain || viewport_id != viewport.id_pair.parent) + (was_used || viewport_id != viewport.id_pair.parent) && available_viewports.contains(&viewport.id_pair.parent) }); }); diff --git a/examples/test_viewports/src/main.rs b/examples/test_viewports/src/main.rs index f1a723f2a3f..81f78bd6fd3 100644 --- a/examples/test_viewports/src/main.rs +++ b/examples/test_viewports/src/main.rs @@ -57,17 +57,38 @@ impl ViewportState { })) } - pub fn show(slf: Arc>, ctx: &egui::Context) { - if !slf.read().visible { + pub fn show(vp_state: Arc>, ctx: &egui::Context) { + if !vp_state.read().visible { return; } - let id = slf.read().id; - let sync = slf.read().sync; + let vp_id = vp_state.read().id; + let sync = vp_state.read().sync; + let title = vp_state.read().title.clone(); + + let vp_builder = ViewportBuilder::new(vp_id) + .with_title(&title) + .with_inner_size(Some(egui::vec2(450.0, 400.0))); if sync { - show_sync_viewport(ctx, id, &mut slf.write()); + let mut vp_state = vp_state.write(); + ctx.create_viewport_sync(vp_builder, move |ctx| { + show_as_popup(ctx, &title, vp_id.into(), |ui: &mut egui::Ui| { + generic_child_ui(ui, &mut vp_state); + }); + }); } else { - show_async_viewport(ctx, id, slf); + let count = Arc::new(RwLock::new(0)); + ctx.create_viewport_async(vp_builder, move |ctx| { + let mut vp_state = vp_state.write(); + let count = count.clone(); + show_as_popup(ctx, &title, vp_id.into(), move |ui: &mut egui::Ui| { + let current_count = *count.read(); + ui.label(format!("Callback has been reused {current_count} times")); + *count.write() += 1; + + generic_child_ui(ui, &mut vp_state); + }); + }); } } } @@ -114,43 +135,6 @@ impl eframe::App for App { } } -fn show_async_viewport( - ctx: &egui::Context, - vp_id: ViewportId, - vp_state: Arc>, -) { - let id = Id::from(vp_id); - let title = vp_state.read().title.clone(); - - ctx.create_viewport_async( - ViewportBuilder::new(vp_id) - .with_title(&title) - .with_inner_size(Some(egui::vec2(450.0, 400.0))), - move |ctx| { - let mut vp_state = vp_state.write(); - show_as_popup(ctx, &title, id, move |ui: &mut egui::Ui| { - generic_child_ui(ui, &mut vp_state); - }); - }, - ); -} - -fn show_sync_viewport(ctx: &egui::Context, vp_id: ViewportId, vp_state: &mut ViewportState) { - let id = Id::from(vp_id); - let title = vp_state.title.clone(); - - ctx.create_viewport_sync( - ViewportBuilder::new(vp_id) - .with_title(&title) - .with_inner_size(Some(egui::vec2(450.0, 400.0))), - move |ctx| { - show_as_popup(ctx, &title, id, |ui: &mut egui::Ui| { - generic_child_ui(ui, vp_state); - }); - }, - ); -} - /// This will make the content as a popup if cannot has his own native window fn show_as_popup(ctx: &egui::Context, title: &str, id: Id, content: impl FnOnce(&mut egui::Ui)) { if ctx.viewport_id() == ctx.parent_viewport_id() {