1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Classify viewports using enum ViewportClass

This commit is contained in:
Emil Ernerfeldt
2023-11-16 09:09:41 +01:00
parent b0d64aaf0a
commit 26f0c2a222
6 changed files with 151 additions and 43 deletions

View File

@@ -55,10 +55,16 @@ impl eframe::App for MyApp {
egui::ViewportBuilder::default()
.with_title("Immediate Viewport")
.with_inner_size([200.0, 100.0]),
|ctx| {
|ctx, class| {
assert!(
class == egui::ViewportClass::Immediate,
"This egui backend doesn't support multiple viewports"
);
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Hello from immediate viewport");
});
if ctx.input(|i| i.raw.viewport.close_requested) {
// Tell parent viewport that we should not show next frame:
self.show_immediate_viewport = false;
@@ -75,12 +81,17 @@ impl eframe::App for MyApp {
egui::ViewportBuilder::default()
.with_title("Deferred Viewport")
.with_inner_size([200.0, 100.0]),
|ctx| {
|ctx, class| {
assert!(
class == egui::ViewportClass::Deferred,
"This egui backend doesn't support multiple viewports"
);
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Hello from deferred viewport");
});
if ctx.input(|i| i.raw.viewport.close_requested) {
// Tell parent to close use
// Tell parent to close us.
show_deferred_viewport.store(false, Ordering::Relaxed);
ctx.request_repaint(); // make sure there is a next frame
}

View File

@@ -71,23 +71,29 @@ impl ViewportState {
if immediate {
let mut vp_state = vp_state.write();
ctx.show_viewport_immediate(vp_id, viewport, move |ctx| {
show_as_popup(ctx, &title, vp_id.into(), |ui: &mut egui::Ui| {
ctx.show_viewport_immediate(vp_id, viewport, move |ctx, class| {
show_as_popup(ctx, class, &title, vp_id.into(), |ui: &mut egui::Ui| {
generic_child_ui(ui, &mut vp_state);
});
});
} else {
let count = Arc::new(RwLock::new(0));
ctx.show_viewport(vp_id, viewport, move |ctx| {
ctx.show_viewport(vp_id, viewport, move |ctx, class| {
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;
show_as_popup(
ctx,
class,
&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);
});
generic_child_ui(ui, &mut vp_state);
},
);
});
}
}
@@ -160,8 +166,15 @@ impl eframe::App for App {
}
/// 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() {
fn show_as_popup(
ctx: &egui::Context,
class: egui::ViewportClass,
title: &str,
id: Id,
content: impl FnOnce(&mut egui::Ui),
) {
if class == egui::ViewportClass::Embedded {
// Not a real viewport
egui::Window::new(title).id(id).show(ctx, content);
} else {
egui::CentralPanel::default().show(ctx, |ui| ui.push_id(id, content));