Skip to content

Commit

Permalink
Add close_button option to test_viewports (#4907)
Browse files Browse the repository at this point in the history
This make the test excercise the window recreation logic, that resulted
in several bugs - see #4862

Adds a check box that turns the close button on and off for child
windows
  • Loading branch information
pm100 authored Aug 26, 2024
1 parent 9a1e358 commit 06f88e1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tests/test_viewports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ViewportState {
}))
}

pub fn show(vp_state: Arc<RwLock<Self>>, ctx: &egui::Context) {
pub fn show(vp_state: Arc<RwLock<Self>>, ctx: &egui::Context, close_button: bool) {
if !vp_state.read().visible {
return;
}
Expand All @@ -71,6 +71,7 @@ impl ViewportState {

let viewport = ViewportBuilder::default()
.with_title(&title)
.with_close_button(close_button)
.with_inner_size([500.0, 500.0]);

if immediate {
Expand All @@ -80,7 +81,7 @@ impl ViewportState {
vp_state.visible = false;
}
show_as_popup(ctx, class, &title, vp_id.into(), |ui: &mut egui::Ui| {
generic_child_ui(ui, &mut vp_state);
generic_child_ui(ui, &mut vp_state, close_button);
});
});
} else {
Expand All @@ -101,7 +102,7 @@ impl ViewportState {
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, close_button);
},
);
});
Expand All @@ -118,6 +119,7 @@ impl ViewportState {

pub struct App {
top: Vec<Arc<RwLock<ViewportState>>>,
close_button: bool,
}

impl Default for App {
Expand Down Expand Up @@ -151,6 +153,7 @@ impl Default for App {
],
),
],
close_button: true,
}
}
}
Expand All @@ -169,8 +172,8 @@ impl eframe::App for App {
}
ctx.set_embed_viewports(embed_viewports);
}

generic_ui(ui, &self.top);
ui.checkbox(&mut self.close_button, "with close button");
generic_ui(ui, &self.top, self.close_button);
});
}
}
Expand All @@ -191,7 +194,7 @@ fn show_as_popup(
}
}

fn generic_child_ui(ui: &mut egui::Ui, vp_state: &mut ViewportState) {
fn generic_child_ui(ui: &mut egui::Ui, vp_state: &mut ViewportState, close_button: bool) {
ui.horizontal(|ui| {
ui.label("Title:");
if ui.text_edit_singleline(&mut vp_state.title).changed() {
Expand All @@ -203,10 +206,10 @@ fn generic_child_ui(ui: &mut egui::Ui, vp_state: &mut ViewportState) {
}
});

generic_ui(ui, &vp_state.children);
generic_ui(ui, &vp_state.children, close_button);
}

fn generic_ui(ui: &mut egui::Ui, children: &[Arc<RwLock<ViewportState>>]) {
fn generic_ui(ui: &mut egui::Ui, children: &[Arc<RwLock<ViewportState>>], close_button: bool) {
let container_id = ui.id();

let ctx = ui.ctx().clone();
Expand Down Expand Up @@ -290,7 +293,7 @@ fn generic_ui(ui: &mut egui::Ui, children: &[Arc<RwLock<ViewportState>>]) {
*visible
};
if visible {
ViewportState::show(child.clone(), &ctx);
ViewportState::show(child.clone(), &ctx, close_button);
}
}
}
Expand Down

0 comments on commit 06f88e1

Please sign in to comment.