diff --git a/egui_demo_lib/src/apps/demo/dancing_strings.rs b/egui_demo_lib/src/apps/demo/dancing_strings.rs index 3578a006..296cea83 100644 --- a/egui_demo_lib/src/apps/demo/dancing_strings.rs +++ b/egui_demo_lib/src/apps/demo/dancing_strings.rs @@ -11,7 +11,7 @@ impl Default for DancingStrings { } impl super::Demo for DancingStrings { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "♫ Dancing Strings" } diff --git a/egui_demo_lib/src/apps/demo/demo_window.rs b/egui_demo_lib/src/apps/demo/demo_window.rs index e2a77a15..bdd70347 100644 --- a/egui_demo_lib/src/apps/demo/demo_window.rs +++ b/egui_demo_lib/src/apps/demo/demo_window.rs @@ -27,7 +27,7 @@ impl Default for DemoWindow { } impl Demo for DemoWindow { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "✨ Misc Demos" } diff --git a/egui_demo_lib/src/apps/demo/demo_windows.rs b/egui_demo_lib/src/apps/demo/demo_windows.rs index 3b85a934..6e156d80 100644 --- a/egui_demo_lib/src/apps/demo/demo_windows.rs +++ b/egui_demo_lib/src/apps/demo/demo_windows.rs @@ -1,14 +1,15 @@ use egui::{CtxRef, ScrollArea, Ui, Window}; +use std::collections::BTreeSet; // ---------------------------------------------------------------------------- #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "persistence", serde(default))] struct Demos { - open: Vec, - #[cfg_attr(feature = "persistence", serde(skip))] demos: Vec>, + + open: BTreeSet, } impl Default for Demos { fn default() -> Self { @@ -31,29 +32,48 @@ impl Default for Demos { Box::new(super::tests::ManualLayoutTest::default()), Box::new(super::tests::TableTest::default()), ]; - Self { - open: vec![false; demos.len()], - demos, - } + + use crate::apps::demo::Demo; + let mut open = BTreeSet::new(); + open.insert( + super::widget_gallery::WidgetGallery::default() + .name() + .to_owned(), + ); + + Self { open, demos } } } impl Demos { pub fn checkboxes(&mut self, ui: &mut Ui) { let Self { open, demos } = self; - for (ref mut open, demo) in open.iter_mut().zip(demos.iter()) { - ui.checkbox(open, demo.name()); + for demo in demos { + let mut is_open = open.contains(demo.name()); + ui.checkbox(&mut is_open, demo.name()); + set_open(open, demo.name(), is_open); } } pub fn show(&mut self, ctx: &CtxRef) { let Self { open, demos } = self; - open.resize(demos.len(), false); // Handle deserialization of old data. - for (ref mut open, demo) in open.iter_mut().zip(demos.iter_mut()) { - demo.show(ctx, open); + for demo in demos { + let mut is_open = open.contains(demo.name()); + demo.show(ctx, &mut is_open); + set_open(open, demo.name(), is_open); } } } +fn set_open(open: &mut BTreeSet, key: &'static str, is_open: bool) { + if is_open { + if !open.contains(key) { + open.insert(key.to_owned()); + } + } else { + open.remove(key); + } +} + // ---------------------------------------------------------------------------- /// A menu bar in which you can select different demo windows to show. diff --git a/egui_demo_lib/src/apps/demo/drag_and_drop.rs b/egui_demo_lib/src/apps/demo/drag_and_drop.rs index c0a2ba07..36c44240 100644 --- a/egui_demo_lib/src/apps/demo/drag_and_drop.rs +++ b/egui_demo_lib/src/apps/demo/drag_and_drop.rs @@ -94,7 +94,7 @@ impl Default for DragAndDropDemo { } impl super::Demo for DragAndDropDemo { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "✋ Drag and Drop" } diff --git a/egui_demo_lib/src/apps/demo/font_book.rs b/egui_demo_lib/src/apps/demo/font_book.rs index dcbbb8eb..45cff1c4 100644 --- a/egui_demo_lib/src/apps/demo/font_book.rs +++ b/egui_demo_lib/src/apps/demo/font_book.rs @@ -40,7 +40,7 @@ impl FontBook { } impl super::Demo for FontBook { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "🔤 Font Book" } diff --git a/egui_demo_lib/src/apps/demo/layout_test.rs b/egui_demo_lib/src/apps/demo/layout_test.rs index 808da7f3..d2427843 100644 --- a/egui_demo_lib/src/apps/demo/layout_test.rs +++ b/egui_demo_lib/src/apps/demo/layout_test.rs @@ -28,7 +28,7 @@ impl Default for LayoutTest { } impl super::Demo for LayoutTest { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "Layout Test" } diff --git a/egui_demo_lib/src/apps/demo/mod.rs b/egui_demo_lib/src/apps/demo/mod.rs index db42553e..42461645 100644 --- a/egui_demo_lib/src/apps/demo/mod.rs +++ b/egui_demo_lib/src/apps/demo/mod.rs @@ -34,7 +34,8 @@ pub trait View { /// Something to view pub trait Demo { - fn name(&self) -> &str; + /// `&'static` so we can also use it as a key to store open/close state. + fn name(&self) -> &'static str; /// Show windows, etc fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool); diff --git a/egui_demo_lib/src/apps/demo/painting.rs b/egui_demo_lib/src/apps/demo/painting.rs index 483bd9cc..61efed1c 100644 --- a/egui_demo_lib/src/apps/demo/painting.rs +++ b/egui_demo_lib/src/apps/demo/painting.rs @@ -64,7 +64,7 @@ impl Painting { } impl super::Demo for Painting { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "🖊 Painting" } diff --git a/egui_demo_lib/src/apps/demo/plot_demo.rs b/egui_demo_lib/src/apps/demo/plot_demo.rs index 0b04ee0f..37cbebe4 100644 --- a/egui_demo_lib/src/apps/demo/plot_demo.rs +++ b/egui_demo_lib/src/apps/demo/plot_demo.rs @@ -26,7 +26,7 @@ impl Default for PlotDemo { } impl super::Demo for PlotDemo { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "🗠 Plot" } diff --git a/egui_demo_lib/src/apps/demo/scrolling.rs b/egui_demo_lib/src/apps/demo/scrolling.rs index 71359f02..32f5369d 100644 --- a/egui_demo_lib/src/apps/demo/scrolling.rs +++ b/egui_demo_lib/src/apps/demo/scrolling.rs @@ -20,7 +20,7 @@ impl Default for Scrolling { } impl super::Demo for Scrolling { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "↕ Scrolling" } diff --git a/egui_demo_lib/src/apps/demo/sliders.rs b/egui_demo_lib/src/apps/demo/sliders.rs index cc4d5386..21e049ac 100644 --- a/egui_demo_lib/src/apps/demo/sliders.rs +++ b/egui_demo_lib/src/apps/demo/sliders.rs @@ -30,7 +30,7 @@ impl Default for Sliders { } impl super::Demo for Sliders { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "⬌ Sliders" } diff --git a/egui_demo_lib/src/apps/demo/tests.rs b/egui_demo_lib/src/apps/demo/tests.rs index 59a00efd..f62fddab 100644 --- a/egui_demo_lib/src/apps/demo/tests.rs +++ b/egui_demo_lib/src/apps/demo/tests.rs @@ -2,7 +2,7 @@ pub struct IdTest {} impl super::Demo for IdTest { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "ID Test" } @@ -79,7 +79,7 @@ impl Default for ManualLayoutTest { } impl super::Demo for ManualLayoutTest { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "Manual Layout Test" } @@ -156,7 +156,7 @@ impl Default for TableTest { } impl super::Demo for TableTest { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "Table Test" } @@ -221,7 +221,7 @@ pub struct InputTest { } impl super::Demo for InputTest { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "Input Test" } @@ -273,7 +273,7 @@ impl super::View for InputTest { pub struct WindowResizeTest {} impl super::Demo for WindowResizeTest { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "↔ Window Resize" } diff --git a/egui_demo_lib/src/apps/demo/widget_gallery.rs b/egui_demo_lib/src/apps/demo/widget_gallery.rs index 408b6b11..c316c1cf 100644 --- a/egui_demo_lib/src/apps/demo/widget_gallery.rs +++ b/egui_demo_lib/src/apps/demo/widget_gallery.rs @@ -30,7 +30,7 @@ impl Default for WidgetGallery { } impl super::Demo for WidgetGallery { - fn name(&self) -> &str { + fn name(&self) -> &'static str { "🗄 Widget Gallery" } diff --git a/egui_demo_lib/src/apps/demo/window_options.rs b/egui_demo_lib/src/apps/demo/window_options.rs index 8b32443d..94ca11bf 100644 --- a/egui_demo_lib/src/apps/demo/window_options.rs +++ b/egui_demo_lib/src/apps/demo/window_options.rs @@ -25,9 +25,8 @@ impl Default for WindowOptions { } impl super::Demo for WindowOptions { - fn name(&self) -> &str { - // "🗖 Window Options" - &self.title + fn name(&self) -> &'static str { + "🗖 Window Options" } fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {