Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add close_button option to test_viewports #4907

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading