mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 14:20:04 -04:00
Make viewports example simpler
This commit is contained in:
@@ -3,7 +3,6 @@ use std::sync::RwLock;
|
|||||||
|
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use eframe::egui::ViewportBuilder;
|
use eframe::egui::ViewportBuilder;
|
||||||
use eframe::egui::ViewportId;
|
|
||||||
use eframe::NativeOptions;
|
use eframe::NativeOptions;
|
||||||
|
|
||||||
#[cfg(feature = "wgpu")]
|
#[cfg(feature = "wgpu")]
|
||||||
@@ -19,11 +18,17 @@ pub struct App {
|
|||||||
async_viewport_state: Arc<RwLock<usize>>,
|
async_viewport_state: Arc<RwLock<usize>>,
|
||||||
sync_viewport_state: usize,
|
sync_viewport_state: usize,
|
||||||
|
|
||||||
show_async_viewport2: Arc<RwLock<bool>>,
|
async_show_async_viewport: Arc<RwLock<bool>>,
|
||||||
show_sync_viewport2: Arc<RwLock<bool>>,
|
async_show_sync_viewport: Arc<RwLock<bool>>,
|
||||||
|
|
||||||
async_viewport_state2: Arc<RwLock<usize>>,
|
async_async_viewport_state: Arc<RwLock<usize>>,
|
||||||
sync_viewport_state2: Arc<RwLock<usize>>,
|
async_sync_viewport_state: Arc<RwLock<usize>>,
|
||||||
|
|
||||||
|
sync_show_async_viewport: bool,
|
||||||
|
sync_show_sync_viewport: bool,
|
||||||
|
|
||||||
|
sync_async_viewport_state: Arc<RwLock<usize>>,
|
||||||
|
sync_sync_viewport_state: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl eframe::App for App {
|
impl eframe::App for App {
|
||||||
@@ -41,14 +46,7 @@ impl eframe::App for App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
ui_info(ui);
|
||||||
ui.label(format!("Current Viewport Id: {}", ctx.get_viewport_id()));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!("Pos: {:?}", ctx.viewport_outer_pos()));
|
|
||||||
ui.label(format!("Size: {:?}", ctx.viewport_inner_size()));
|
|
||||||
ui.label("Look at the \"Frame: \" will tell you, what viewport is rendering!");
|
ui.label("Look at the \"Frame: \" will tell you, what viewport is rendering!");
|
||||||
{
|
{
|
||||||
let mut force_embedding = ctx.force_embedding();
|
let mut force_embedding = ctx.force_embedding();
|
||||||
@@ -63,11 +61,11 @@ impl eframe::App for App {
|
|||||||
if self.show_async_viewport {
|
if self.show_async_viewport {
|
||||||
let state = self.async_viewport_state.clone();
|
let state = self.async_viewport_state.clone();
|
||||||
|
|
||||||
let show_async_viewport2 = self.show_async_viewport2.clone();
|
let show_async_viewport2 = self.async_show_async_viewport.clone();
|
||||||
let show_sync_viewport2 = self.show_sync_viewport2.clone();
|
let show_sync_viewport2 = self.async_show_sync_viewport.clone();
|
||||||
|
|
||||||
let async_viewport_state2 = self.async_viewport_state2.clone();
|
let async_viewport_state2 = self.async_async_viewport_state.clone();
|
||||||
let sync_viewport_state2 = self.sync_viewport_state2.clone();
|
let sync_viewport_state2 = self.async_sync_viewport_state.clone();
|
||||||
|
|
||||||
ctx.create_viewport(
|
ctx.create_viewport(
|
||||||
ViewportBuilder::new("Async Viewport").with_title("Async Viewport"),
|
ViewportBuilder::new("Async Viewport").with_title("Async Viewport"),
|
||||||
@@ -81,14 +79,7 @@ impl eframe::App for App {
|
|||||||
let sync_viewport_state2 = sync_viewport_state2.clone();
|
let sync_viewport_state2 = sync_viewport_state2.clone();
|
||||||
|
|
||||||
let content = move |ui: &mut egui::Ui| {
|
let content = move |ui: &mut egui::Ui| {
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
ui_info(ui);
|
||||||
ui.label(format!("Current Viewport Id: {}", ctx.get_viewport_id()));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!("Pos: {:?}", ctx.viewport_outer_pos()));
|
|
||||||
ui.label(format!("Size: {:?}", ctx.viewport_inner_size()));
|
|
||||||
|
|
||||||
ui.checkbox(&mut show_async_viewport2, "Show Async Viewport 2");
|
ui.checkbox(&mut show_async_viewport2, "Show Async Viewport 2");
|
||||||
ui.checkbox(&mut show_sync_viewport2, "Show Sync Viewport 2");
|
ui.checkbox(&mut show_sync_viewport2, "Show Sync Viewport 2");
|
||||||
@@ -106,23 +97,7 @@ impl eframe::App for App {
|
|||||||
let mut state = async_viewport_state2.write().unwrap();
|
let mut state = async_viewport_state2.write().unwrap();
|
||||||
|
|
||||||
let content = move |ui: &mut egui::Ui| {
|
let content = move |ui: &mut egui::Ui| {
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
ui_info(ui);
|
||||||
ui.label(format!(
|
|
||||||
"Current Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Pos: {:?}",
|
|
||||||
ctx.viewport_outer_pos()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Size: {:?}",
|
|
||||||
ctx.viewport_inner_size()
|
|
||||||
));
|
|
||||||
|
|
||||||
ui.label(format!("Count: {state}"));
|
ui.label(format!("Count: {state}"));
|
||||||
if ui.button("Add").clicked() {
|
if ui.button("Add").clicked() {
|
||||||
@@ -130,16 +105,11 @@ impl eframe::App for App {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This will make the viewport content, a popup if is in the main window
|
show_as_popup(
|
||||||
if ctx.get_viewport_id() == ViewportId::MAIN {
|
ctx,
|
||||||
egui::Area::new("Async Viewport in Async Viewport")
|
"Async Viewport in Async Viewport",
|
||||||
.show(ctx, |ui| {
|
content,
|
||||||
egui::Frame::popup(ui.style())
|
);
|
||||||
.show(ui, content);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
egui::CentralPanel::default().show(ctx, content);
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -152,23 +122,7 @@ impl eframe::App for App {
|
|||||||
let mut state = sync_viewport_state2.write().unwrap();
|
let mut state = sync_viewport_state2.write().unwrap();
|
||||||
|
|
||||||
let content = move |ui: &mut egui::Ui| {
|
let content = move |ui: &mut egui::Ui| {
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
ui_info(ui);
|
||||||
ui.label(format!(
|
|
||||||
"Current Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Pos: {:?}",
|
|
||||||
ctx.viewport_outer_pos()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Size: {:?}",
|
|
||||||
ctx.viewport_inner_size()
|
|
||||||
));
|
|
||||||
|
|
||||||
ui.label(format!("Count: {state}"));
|
ui.label(format!("Count: {state}"));
|
||||||
if ui.button("Add").clicked() {
|
if ui.button("Add").clicked() {
|
||||||
@@ -176,29 +130,17 @@ impl eframe::App for App {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This will make the viewport content, a popup if is in the main window
|
show_as_popup(
|
||||||
if ctx.get_viewport_id() == ViewportId::MAIN {
|
ctx,
|
||||||
egui::Area::new("Sync Viewport in Async Viewport")
|
"Sync Viewport in Async Viewport",
|
||||||
.show(ctx, |ui| {
|
content,
|
||||||
egui::Frame::popup(ui.style())
|
);
|
||||||
.show(ui, content);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
egui::CentralPanel::default().show(ctx, content);
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This will make the viewport content, a popup if is in the main window
|
show_as_popup(ctx, "Async Viewport", content);
|
||||||
if ctx.get_viewport_id() == ViewportId::MAIN {
|
|
||||||
egui::Area::new("Async Viewport").show(ctx, |ui| {
|
|
||||||
egui::Frame::popup(ui.style()).show(ui, content);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
egui::CentralPanel::default().show(ctx, content);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -208,132 +150,71 @@ impl eframe::App for App {
|
|||||||
ctx.create_viewport_sync(
|
ctx.create_viewport_sync(
|
||||||
ViewportBuilder::new("Sync Viewport").with_title("Sync Viewport"),
|
ViewportBuilder::new("Sync Viewport").with_title("Sync Viewport"),
|
||||||
|ctx| {
|
|ctx| {
|
||||||
let mut show_async_viewport2 = self.show_async_viewport2.write().unwrap();
|
let async_viewport_state3 = self.sync_async_viewport_state.clone();
|
||||||
let mut show_sync_viewport2 = self.show_sync_viewport2.write().unwrap();
|
|
||||||
|
|
||||||
let async_viewport_state2 = self.async_viewport_state2.clone();
|
let content = |ui: &mut egui::Ui| {
|
||||||
let sync_viewport_state2 = self.sync_viewport_state2.clone();
|
ui_info(ui);
|
||||||
|
|
||||||
let content =
|
ui.checkbox(&mut self.sync_show_async_viewport, "Show Async Viewport");
|
||||||
|ui: &mut egui::Ui| {
|
ui.checkbox(&mut self.sync_show_sync_viewport, "Show Sync Viewport");
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
|
||||||
ui.label(format!("Current Viewport Id: {}", ctx.get_viewport_id()));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!("Pos: {:?}", ctx.viewport_outer_pos()));
|
|
||||||
ui.label(format!("Size: {:?}", ctx.viewport_inner_size()));
|
|
||||||
|
|
||||||
ui.checkbox(&mut show_async_viewport2, "Show Async Viewport");
|
ui.label(format!("Count: {}", self.sync_viewport_state));
|
||||||
ui.checkbox(&mut show_sync_viewport2, "Show Sync Viewport");
|
if ui.button("Add").clicked() {
|
||||||
|
self.sync_viewport_state += 1;
|
||||||
|
}
|
||||||
|
|
||||||
ui.label(format!("Count: {}", self.sync_viewport_state));
|
if self.sync_show_async_viewport {
|
||||||
if ui.button("Add").clicked() {
|
ctx.create_viewport(
|
||||||
self.sync_viewport_state += 1;
|
ViewportBuilder::new("Async Viewport in Sync Viewport")
|
||||||
}
|
.with_title("Async Viewport in Sync Viewport"),
|
||||||
|
move |ctx| {
|
||||||
|
let mut state = async_viewport_state3.write().unwrap();
|
||||||
|
|
||||||
if *show_async_viewport2 {
|
let content = move |ui: &mut egui::Ui| {
|
||||||
ctx.create_viewport(
|
ui_info(ui);
|
||||||
ViewportBuilder::new("Async Viewport in Sync Viewport")
|
|
||||||
.with_title("Async Viewport in Sync Viewport"),
|
|
||||||
move |ctx| {
|
|
||||||
let mut state = async_viewport_state2.write().unwrap();
|
|
||||||
|
|
||||||
let content = move |ui: &mut egui::Ui| {
|
ui.label(format!("Count: {state}"));
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
if ui.button("Add").clicked() {
|
||||||
ui.label(format!(
|
*state += 1;
|
||||||
"Current Viewport Id: {}",
|
}
|
||||||
ctx.get_viewport_id()
|
};
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Pos: {:?}",
|
|
||||||
ctx.viewport_outer_pos()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Size: {:?}",
|
|
||||||
ctx.viewport_inner_size()
|
|
||||||
));
|
|
||||||
|
|
||||||
ui.label(format!("Count: {state}"));
|
show_as_popup(
|
||||||
if ui.button("Add").clicked() {
|
ctx,
|
||||||
*state += 1;
|
"Async Viewport in Sync Viewport",
|
||||||
}
|
content,
|
||||||
};
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// This will make the viewport content, a popup if is in the main window
|
if self.sync_show_sync_viewport {
|
||||||
if ctx.get_viewport_id() == ViewportId::MAIN {
|
ctx.create_viewport_sync(
|
||||||
egui::Area::new("Async Viewport in Sync Viewport")
|
ViewportBuilder::new("Sync Viewport in Sync Viewport")
|
||||||
.show(ctx, |ui| {
|
.with_title("Sync Viewport in Sync Viewport"),
|
||||||
egui::Frame::popup(ui.style())
|
move |ctx| {
|
||||||
.show(ui, content);
|
let state = &mut self.sync_sync_viewport_state;
|
||||||
});
|
|
||||||
} else {
|
|
||||||
egui::CentralPanel::default().show(ctx, content);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if *show_sync_viewport2 {
|
let content = move |ui: &mut egui::Ui| {
|
||||||
ctx.create_viewport_sync(
|
ui_info(ui);
|
||||||
ViewportBuilder::new("Sync Viewport in Sync Viewport")
|
|
||||||
.with_title("Sync Viewport in Sync Viewport"),
|
|
||||||
move |ctx| {
|
|
||||||
let mut state = sync_viewport_state2.write().unwrap();
|
|
||||||
|
|
||||||
let content = move |ui: &mut egui::Ui| {
|
ui.label(format!("Count: {state}"));
|
||||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
if ui.button("Add").clicked() {
|
||||||
ui.label(format!(
|
*state += 1;
|
||||||
"Current Viewport Id: {}",
|
}
|
||||||
ctx.get_viewport_id()
|
};
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Current Parent Viewport Id: {}",
|
|
||||||
ctx.get_viewport_id()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Pos: {:?}",
|
|
||||||
ctx.viewport_outer_pos()
|
|
||||||
));
|
|
||||||
ui.label(format!(
|
|
||||||
"Size: {:?}",
|
|
||||||
ctx.viewport_inner_size()
|
|
||||||
));
|
|
||||||
|
|
||||||
ui.label(format!("Count: {state}"));
|
show_as_popup(
|
||||||
if ui.button("Add").clicked() {
|
ctx,
|
||||||
*state += 1;
|
"Sync Viewport in Sync Viewport",
|
||||||
}
|
content,
|
||||||
};
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// This will make the viewport content, a popup if is in the main window
|
show_as_popup(ctx, "Sync Viewport", content);
|
||||||
if ctx.get_viewport_id() == ViewportId::MAIN {
|
|
||||||
egui::Area::new("Sync Viewport in Sync Viewport")
|
|
||||||
.show(ctx, |ui| {
|
|
||||||
egui::Frame::popup(ui.style())
|
|
||||||
.show(ui, content);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
egui::CentralPanel::default().show(ctx, content);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// This will make the viewport content, a popup if is in the main window
|
|
||||||
if ctx.get_viewport_id() == ViewportId::MAIN {
|
|
||||||
egui::Area::new("Sync Viewport").show(ctx, |ui| {
|
|
||||||
egui::Frame::popup(ui.style()).show(ui, content);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
egui::CentralPanel::default().show(ctx, content);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -341,6 +222,27 @@ 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, name: &str, content: impl FnOnce(&mut egui::Ui)) {
|
||||||
|
if ctx.get_viewport_id() == ctx.get_parent_viewport_id() {
|
||||||
|
egui::Window::new(name).show(ctx, content);
|
||||||
|
} else {
|
||||||
|
egui::CentralPanel::default().show(ctx, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ui_info(ui: &mut egui::Ui) {
|
||||||
|
let ctx = ui.ctx().clone();
|
||||||
|
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
||||||
|
ui.label(format!("Current Viewport Id: {}", ctx.get_viewport_id()));
|
||||||
|
ui.label(format!(
|
||||||
|
"Current Parent Viewport Id: {}",
|
||||||
|
ctx.get_viewport_id()
|
||||||
|
));
|
||||||
|
ui.label(format!("Pos: {:?}", ctx.viewport_outer_pos()));
|
||||||
|
ui.label(format!("Size: {:?}", ctx.viewport_inner_size()));
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init(); // Use `RUST_LOG=debug` to see logs.
|
env_logger::init(); // Use `RUST_LOG=debug` to see logs.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user