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 non-mut egui::Window::open #4323

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 32 additions & 5 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::*;
#[must_use = "You should call .show()"]
pub struct Window<'open> {
title: WidgetText,
open: Option<&'open mut bool>,
open: Option<Open<'open>>,
area: Area,
frame: Option<Frame>,
resize: Resize,
Expand All @@ -42,6 +42,11 @@ pub struct Window<'open> {
with_title_bar: bool,
}

enum Open<'open> {
Mut(&'open mut bool),
NonMut(bool),
}

impl<'open> Window<'open> {
/// The window title is used as a unique [`Id`] and must be unique, and should not change.
/// This is true even if you disable the title bar with `.title_bar(false)`.
Expand Down Expand Up @@ -72,14 +77,31 @@ impl<'open> Window<'open> {
self
}

/// Unlike [`Window::open_mut`], calling this function does not display the close button.
/// It is functionally equivalent to the following code:
///
/// ``` ignore
/// if open {
/// // your window code
/// }
/// ```
///
/// The advantage of calling this function instead of using `if open` is that
/// this can display the close animation when the window is closed.
#[inline]
pub fn open(mut self, open: bool) -> Self {
self.open = Some(Open::NonMut(open));
self
}

/// Call this to add a close-button to the window title bar.
///
/// * If `*open == false`, the window will not be visible.
/// * If `*open == true`, the window will have a close button.
/// * If the close button is pressed, `*open` will be set to `false`.
#[inline]
pub fn open(mut self, open: &'open mut bool) -> Self {
self.open = Some(open);
pub fn open_mut(mut self, open: &'open mut bool) -> Self {
self.open = Some(Open::Mut(open));
self
}

Expand Down Expand Up @@ -394,7 +416,7 @@ impl<'open> Window<'open> {
) -> Option<InnerResponse<Option<R>>> {
let Window {
title,
open,
mut open,
area,
frame,
resize,
Expand All @@ -413,7 +435,7 @@ impl<'open> Window<'open> {
// Add border padding to the inner margin to prevent it from covering the contents
window_frame.inner_margin += border_padding;

let is_explicitly_closed = matches!(open, Some(false));
let is_explicitly_closed = matches!(open, Some(Open::Mut(false) | Open::NonMut(false)));
let is_open = !is_explicitly_closed || ctx.memory(|mem| mem.everything_is_visible());
area.show_open_close_animation(ctx, &window_frame, is_open);

Expand Down Expand Up @@ -483,6 +505,11 @@ impl<'open> Window<'open> {
let frame_stroke = window_frame.stroke;
let mut frame = window_frame.begin(&mut area_content_ui);

let open = match open.take() {
Some(Open::Mut(open)) => Some(open),
_ => None,
};

let show_close_button = open.is_some();

let where_to_put_header_background = &area_content_ui.painter().add(Shape::Noop);
Expand Down
8 changes: 4 additions & 4 deletions crates/egui_demo_app/src/backend_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,28 +394,28 @@ impl EguiWindows {
}

egui::Window::new("🔧 Settings")
.open(settings)
.open_mut(settings)
.vscroll(true)
.show(ctx, |ui| {
ctx.settings_ui(ui);
});

egui::Window::new("🔍 Inspection")
.open(inspection)
.open_mut(inspection)
.vscroll(true)
.show(ctx, |ui| {
ctx.inspection_ui(ui);
});

egui::Window::new("📝 Memory")
.open(memory)
.open_mut(memory)
.resizable(false)
.show(ctx, |ui| {
ctx.memory_ui(ui);
});

egui::Window::new("📤 Output Events")
.open(output_events)
.open_mut(output_events)
.resizable(true)
.default_width(520.0)
.show(ctx, |ui| {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl WrapApp {
if !self.dropped_files.is_empty() {
let mut open = true;
egui::Window::new("Dropped files")
.open(&mut open)
.open_mut(&mut open)
.show(ctx, |ui| {
for file in &self.dropped_files {
let mut info = if let Some(path) = &file.path {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl super::Demo for About {
egui::Window::new(self.name())
.default_width(320.0)
.default_height(480.0)
.open(open)
.open_mut(open)
.resizable([true, false])
.show(ctx, |ui| {
use super::View as _;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl super::Demo for CodeEditor {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use super::View as _;
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.default_height(500.0)
.show(ctx, |ui| self.ui(ui));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/code_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl super::Demo for CodeExample {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
use super::View;
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.min_width(375.0)
.default_size([390.0, 500.0])
.scroll(false)
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl super::Demo for ContextMenus {
egui::Window::new(self.name())
.vscroll(false)
.resizable(false)
.open(open)
.open_mut(open)
.show(ctx, |ui| self.ui(ui));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/dancing_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl super::Demo for DancingStrings {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.open_mut(open)
.default_size(vec2(512.0, 256.0))
.vscroll(false)
.show(ctx, |ui| self.ui(ui));
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl DemoWindows {
.default_width(default_width)
.default_height(ctx.available_rect().height() - 46.0)
.vscroll(true)
.open(&mut self.about_is_open)
.open_mut(&mut self.about_is_open)
.resizable(false)
.collapsible(false)
.show(ctx, |ui| {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/drag_and_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl super::Demo for DragAndDropDemo {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.open_mut(open)
.default_size(vec2(256.0, 256.0))
.vscroll(false)
.resizable(false)
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/extra_viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl super::Demo for ExtraViewport {
// Not a real viewport
egui::Window::new(self.name())
.id(id)
.open(open)
.open_mut(open)
.show(ctx, |ui| {
ui.label("This egui integration does not support multiple viewports");
});
Expand Down
10 changes: 6 additions & 4 deletions crates/egui_demo_lib/src/demo/font_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ impl super::Demo for FontBook {
}

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _;
self.ui(ui);
});
egui::Window::new(self.name())
.open_mut(open)
.show(ctx, |ui| {
use super::View as _;
self.ui(ui);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/frame_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl super::Demo for FrameDemo {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.resizable(false)
.show(ctx, |ui| {
use super::View as _;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl super::Demo for Highlighting {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.default_width(320.0)
.open(open)
.open_mut(open)
.show(ctx, |ui| {
use super::View as _;
self.ui(ui);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl super::Demo for LayoutTest {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.resizable(false)
.show(ctx, |ui| {
use super::View as _;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/misc_demo_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Demo for MiscDemoWindow {

fn show(&mut self, ctx: &Context, open: &mut bool) {
Window::new(self.name())
.open(open)
.open_mut(open)
.vscroll(true)
.hscroll(true)
.show(ctx, |ui| self.ui(ui));
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/multi_touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl super::Demo for MultiTouch {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.default_size(vec2(512.0, 512.0))
.resizable(true)
.show(ctx, |ui| {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/paint_bezier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl super::Demo for PaintBezier {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.open_mut(open)
.vscroll(false)
.resizable(false)
.default_size([300.0, 350.0])
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/painting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl super::Demo for Painting {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.open_mut(open)
.default_size(vec2(512.0, 512.0))
.vscroll(false)
.show(ctx, |ui| self.ui(ui));
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/pan_zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl super::Demo for PanZoom {
.default_width(300.0)
.default_height(300.0)
.vscroll(false)
.open(open);
.open_mut(open);
window.show(ctx, |ui| self.ui(ui));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl super::Demo for Panels {
.default_width(600.0)
.default_height(400.0)
.vscroll(false)
.open(open);
.open_mut(open);
window.show(ctx, |ui| self.ui(ui));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl super::Demo for PlotDemo {
fn show(&mut self, ctx: &Context, open: &mut bool) {
use super::View as _;
Window::new(self.name())
.open(open)
.open_mut(open)
.default_size(vec2(400.0, 400.0))
.vscroll(false)
.show(ctx, |ui| self.ui(ui));
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/scrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl super::Demo for Scrolling {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.resizable(true)
.hscroll(false)
.vscroll(false)
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/sliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl super::Demo for Sliders {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.resizable(false)
.show(ctx, |ui| {
use super::View as _;
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/strip_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl super::Demo for StripDemo {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.resizable(true)
.default_width(400.0)
.show(ctx, |ui| {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/table_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl super::Demo for TableDemo {

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.open_mut(open)
.default_width(400.0)
.show(ctx, |ui| {
use super::View as _;
Expand Down
Loading
Loading