1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 14:50:03 -04:00

Add Ui::close and Response::should_close (#5729)

This adds a generic way of telling containers to close from their child
`Ui`s.

* Part of #5727 
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
lucasmerlin
2025-02-20 17:59:29 +01:00
committed by GitHub
parent 264749b8af
commit f5b058b908
21 changed files with 233 additions and 52 deletions

View File

@@ -59,25 +59,25 @@ impl ContextMenus {
ui.set_max_width(200.0); // To make sure we wrap long text
if ui.button("Open…").clicked() {
ui.close_menu();
ui.close();
}
ui.menu_button("SubMenu", |ui| {
ui.menu_button("SubMenu", |ui| {
if ui.button("Open…").clicked() {
ui.close_menu();
ui.close();
}
let _ = ui.button("Item");
ui.menu_button("Recursive", Self::nested_menus)
});
ui.menu_button("SubMenu", |ui| {
if ui.button("Open…").clicked() {
ui.close_menu();
ui.close();
}
let _ = ui.button("Item");
});
let _ = ui.button("Item");
if ui.button("Open…").clicked() {
ui.close_menu();
ui.close();
}
});
ui.menu_button("SubMenu", |ui| {
@@ -86,7 +86,7 @@ impl ContextMenus {
let _ = ui.button("Item3");
let _ = ui.button("Item4");
if ui.button("Open…").clicked() {
ui.close_menu();
ui.close();
}
});
let _ = ui.button("Very long text for this item that should be wrapped");

View File

@@ -234,7 +234,7 @@ impl DemoWindows {
ui.set_style(ui.ctx().style()); // ignore the "menu" style set by `menu_button`.
self.demo_list_ui(ui);
if ui.ui_contains_pointer() && ui.input(|i| i.pointer.any_click()) {
ui.close_menu();
ui.close();
}
});
@@ -345,7 +345,7 @@ fn file_menu_button(ui: &mut Ui) {
.clicked()
{
ui.ctx().memory_mut(|mem| mem.reset_areas());
ui.close_menu();
ui.close();
}
if ui
@@ -357,7 +357,7 @@ fn file_menu_button(ui: &mut Ui) {
.clicked()
{
ui.ctx().memory_mut(|mem| *mem = Default::default());
ui.close_menu();
ui.close();
}
});
}

View File

@@ -96,7 +96,9 @@ impl crate::View for Modals {
*save_modal_open = true;
}
if ui.button("Cancel").clicked() {
*user_modal_open = false;
// You can call `ui.close()` to close the modal.
// (This causes the current modals `should_close` to return true)
ui.close();
}
},
);
@@ -123,7 +125,7 @@ impl crate::View for Modals {
}
if ui.button("No Thanks").clicked() {
*save_modal_open = false;
ui.close();
}
},
);

View File

@@ -153,6 +153,10 @@ impl crate::View for PopupsDemo {
.show(|ui| {
_ = ui.button("Menu item 1");
_ = ui.button("Menu item 2");
if ui.button("I always close the menu").clicked() {
ui.close();
}
});
self.apply_options(Popup::context_menu(&response).id(Id::new("context_menu")))

View File

@@ -1,4 +1,4 @@
use egui::Vec2b;
use egui::{UiKind, Vec2b};
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -149,6 +149,16 @@ impl crate::View for WindowOptions {
self.disabled_time = ui.input(|i| i.time);
}
egui::reset_button(ui, self, "Reset");
if ui
.button("Close")
.on_hover_text("You can collapse / close Windows via Ui::close")
.clicked()
{
// Calling close would close the collapsible within the window
// ui.close();
// Instead, we close the window itself
ui.close_kind(UiKind::Window);
}
ui.add(crate::egui_github_link_file!());
});
}

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3411a4a8939b7e731c9c1a6331921b0ac905f4e3e86a51af70bdb38d9446f5e1
size 35193
oid sha256:e67b1e676ff994cb9557939db3dca5ddd15c69d167afd96c0957a2a3b75c0fd8
size 36007