Skip to content

Commit fed4e3d

Browse files
pm100hacknus
authored andcommitted
Add close_button option to test_viewports (emilk#4907)
This make the test excercise the window recreation logic, that resulted in several bugs - see emilk#4862 Adds a check box that turns the close button on and off for child windows
1 parent dcafe2f commit fed4e3d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

tests/test_viewports/src/main.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl ViewportState {
6161
}))
6262
}
6363

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

7272
let viewport = ViewportBuilder::default()
7373
.with_title(&title)
74+
.with_close_button(close_button)
7475
.with_inner_size([500.0, 500.0]);
7576

7677
if immediate {
@@ -80,7 +81,7 @@ impl ViewportState {
8081
vp_state.visible = false;
8182
}
8283
show_as_popup(ctx, class, &title, vp_id.into(), |ui: &mut egui::Ui| {
83-
generic_child_ui(ui, &mut vp_state);
84+
generic_child_ui(ui, &mut vp_state, close_button);
8485
});
8586
});
8687
} else {
@@ -101,7 +102,7 @@ impl ViewportState {
101102
ui.label(format!("Callback has been reused {current_count} times"));
102103
*count.write() += 1;
103104

104-
generic_child_ui(ui, &mut vp_state);
105+
generic_child_ui(ui, &mut vp_state, close_button);
105106
},
106107
);
107108
});
@@ -118,6 +119,7 @@ impl ViewportState {
118119

119120
pub struct App {
120121
top: Vec<Arc<RwLock<ViewportState>>>,
122+
close_button: bool,
121123
}
122124

123125
impl Default for App {
@@ -151,6 +153,7 @@ impl Default for App {
151153
],
152154
),
153155
],
156+
close_button: true,
154157
}
155158
}
156159
}
@@ -169,8 +172,8 @@ impl eframe::App for App {
169172
}
170173
ctx.set_embed_viewports(embed_viewports);
171174
}
172-
173-
generic_ui(ui, &self.top);
175+
ui.checkbox(&mut self.close_button, "with close button");
176+
generic_ui(ui, &self.top, self.close_button);
174177
});
175178
}
176179
}
@@ -191,7 +194,7 @@ fn show_as_popup(
191194
}
192195
}
193196

194-
fn generic_child_ui(ui: &mut egui::Ui, vp_state: &mut ViewportState) {
197+
fn generic_child_ui(ui: &mut egui::Ui, vp_state: &mut ViewportState, close_button: bool) {
195198
ui.horizontal(|ui| {
196199
ui.label("Title:");
197200
if ui.text_edit_singleline(&mut vp_state.title).changed() {
@@ -203,10 +206,10 @@ fn generic_child_ui(ui: &mut egui::Ui, vp_state: &mut ViewportState) {
203206
}
204207
});
205208

206-
generic_ui(ui, &vp_state.children);
209+
generic_ui(ui, &vp_state.children, close_button);
207210
}
208211

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

212215
let ctx = ui.ctx().clone();
@@ -290,7 +293,7 @@ fn generic_ui(ui: &mut egui::Ui, children: &[Arc<RwLock<ViewportState>>]) {
290293
*visible
291294
};
292295
if visible {
293-
ViewportState::show(child.clone(), &ctx);
296+
ViewportState::show(child.clone(), &ctx, close_button);
294297
}
295298
}
296299
}

0 commit comments

Comments
 (0)