Skip to content

Commit

Permalink
Update serial window example (#2756)
Browse files Browse the repository at this point in the history
* Update to make it more clear what will happen

* Provide an overview in readme of expectations

* Update screenshot to match new code
  • Loading branch information
c-git authored Mar 29, 2023
1 parent 8eeb3e0 commit 2946ed7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
7 changes: 7 additions & 0 deletions examples/serial_windows/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Demonstrates how to open several windows after each other.

Expected order of execution:

- When the example runs a first window will be shown.
- Once the first window is closed after a delay a second window will be shown.
- Similarly, when the second window is closed after a delay a third will be shown.
- Once the third is closed the program will stop.

NOTE: this doesn't work on Mac due to <https://github.com/rust-windowing/winit/issues/2431>.
See also <https://github.com/emilk/egui/issues/1918>.

Expand Down
Binary file modified examples/serial_windows/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 12 additions & 5 deletions examples/serial_windows/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_native(
"First Window",
options.clone(),
Box::new(|_cc| Box::new(MyApp::default())),
Box::new(|_cc| Box::new(MyApp { has_next: true })),
)?;

std::thread::sleep(std::time::Duration::from_secs(2));
Expand All @@ -26,7 +26,7 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_native(
"Second Window",
options.clone(),
Box::new(|_cc| Box::new(MyApp::default())),
Box::new(|_cc| Box::new(MyApp { has_next: true })),
)?;

std::thread::sleep(std::time::Duration::from_secs(2));
Expand All @@ -35,16 +35,23 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_native(
"Third Window",
options,
Box::new(|_cc| Box::new(MyApp::default())),
Box::new(|_cc| Box::new(MyApp { has_next: false })),
)
}

#[derive(Default)]
struct MyApp {}
struct MyApp {
pub(crate) has_next: bool,
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
let label_text = if self.has_next {
"When this window is closed the next will be opened after a short delay"
} else {
"This is the last window. Program will end when closed"
};
ui.label(label_text);
if ui.button("Close").clicked() {
eprintln!("Pressed Close button");
frame.close();
Expand Down

0 comments on commit 2946ed7

Please sign in to comment.