From 290ecca9bd04ffbddbd1861ff5723cf4a4b29a6c Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 7 Nov 2023 15:20:35 +0100 Subject: [PATCH] Add a simple example of the viewports API --- examples/multiple_viewports/Cargo.toml | 15 +++++++++ examples/multiple_viewports/README.md | 7 ++++ examples/multiple_viewports/src/main.rs | 43 +++++++++++++++++++++++++ examples/test_viewports/README.md | 2 ++ 4 files changed, 67 insertions(+) create mode 100644 examples/multiple_viewports/Cargo.toml create mode 100644 examples/multiple_viewports/README.md create mode 100644 examples/multiple_viewports/src/main.rs diff --git a/examples/multiple_viewports/Cargo.toml b/examples/multiple_viewports/Cargo.toml new file mode 100644 index 00000000000..fb0fa3371b9 --- /dev/null +++ b/examples/multiple_viewports/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "multiple_viewports" +version = "0.1.0" +authors = ["Emil Ernerfeldt "] +license = "MIT OR Apache-2.0" +edition = "2021" +rust-version = "1.70" +publish = false + + +[dependencies] +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO +] } +env_logger = "0.10" diff --git a/examples/multiple_viewports/README.md b/examples/multiple_viewports/README.md new file mode 100644 index 00000000000..1ce0a2b98b0 --- /dev/null +++ b/examples/multiple_viewports/README.md @@ -0,0 +1,7 @@ +Example how to show multiple viewports (native windows) can be created in `egui` when using the `eframe` backend. + +```sh +cargo run -p multiple_viewports +``` + +For a more advanced example, see [../test_viewports]. diff --git a/examples/multiple_viewports/src/main.rs b/examples/multiple_viewports/src/main.rs new file mode 100644 index 00000000000..2de2751770c --- /dev/null +++ b/examples/multiple_viewports/src/main.rs @@ -0,0 +1,43 @@ +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release + +use eframe::egui; + +fn main() -> Result<(), eframe::Error> { + env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). + let options = eframe::NativeOptions { + initial_window_size: Some(egui::vec2(320.0, 240.0)), + ..Default::default() + }; + eframe::run_native( + "Confirm exit", + options, + Box::new(|_cc| Box::::default()), + ) +} + +#[derive(Default)] +struct MyApp { + show_child_viewport: bool, +} + +impl eframe::App for MyApp { + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + egui::CentralPanel::default().show(ctx, |ui| { + ui.label("Hello from the root viewport"); + + ui.checkbox(&mut self.show_child_viewport, "Show secondary viewport"); + }); + + if self.show_child_viewport { + ctx.show_viewport( + egui::ViewportBuilder::new(egui::ViewportId::from_hash_of("secondary_viewport")) + .with_title("Secondary Viewport"), + |ctx| { + egui::CentralPanel::default().show(ctx, |ui| { + ui.label("Hello from secondary viewport"); + }); + }, + ); + } + } +} diff --git a/examples/test_viewports/README.md b/examples/test_viewports/README.md index 4f34ac3db83..9e7b4fa6c37 100644 --- a/examples/test_viewports/README.md +++ b/examples/test_viewports/README.md @@ -1 +1,3 @@ This is a test of the viewports feature of eframe and egui, where we show off using multiple windows. + +For a simple example, see [../multiple_viewports].