Skip to content

Commit

Permalink
Always use the latest async callback when rendering a viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 7, 2023
1 parent 6643713 commit 7bc5698
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
});
});
Expand Down
70 changes: 27 additions & 43 deletions examples/test_viewports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,38 @@ impl ViewportState {
}))
}

pub fn show(slf: Arc<RwLock<ViewportState>>, ctx: &egui::Context) {
if !slf.read().visible {
pub fn show(vp_state: Arc<RwLock<ViewportState>>, 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);
});
});
}
}
}
Expand Down Expand Up @@ -114,43 +135,6 @@ impl eframe::App for App {
}
}

fn show_async_viewport(
ctx: &egui::Context,
vp_id: ViewportId,
vp_state: Arc<RwLock<ViewportState>>,
) {
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() {
Expand Down

0 comments on commit 7bc5698

Please sign in to comment.