[demo] Alwyas start with widget gallery

This commit is contained in:
Emil Ernerfeldt 2021-02-20 09:18:23 +01:00
parent 0f13fff24b
commit 9e38674d13
14 changed files with 50 additions and 30 deletions

View file

@ -11,7 +11,7 @@ impl Default for DancingStrings {
} }
impl super::Demo for DancingStrings { impl super::Demo for DancingStrings {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"♫ Dancing Strings" "♫ Dancing Strings"
} }

View file

@ -27,7 +27,7 @@ impl Default for DemoWindow {
} }
impl Demo for DemoWindow { impl Demo for DemoWindow {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"✨ Misc Demos" "✨ Misc Demos"
} }

View file

@ -1,14 +1,15 @@
use egui::{CtxRef, ScrollArea, Ui, Window}; use egui::{CtxRef, ScrollArea, Ui, Window};
use std::collections::BTreeSet;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))] #[cfg_attr(feature = "persistence", serde(default))]
struct Demos { struct Demos {
open: Vec<bool>,
#[cfg_attr(feature = "persistence", serde(skip))] #[cfg_attr(feature = "persistence", serde(skip))]
demos: Vec<Box<dyn super::Demo>>, demos: Vec<Box<dyn super::Demo>>,
open: BTreeSet<String>,
} }
impl Default for Demos { impl Default for Demos {
fn default() -> Self { fn default() -> Self {
@ -31,29 +32,48 @@ impl Default for Demos {
Box::new(super::tests::ManualLayoutTest::default()), Box::new(super::tests::ManualLayoutTest::default()),
Box::new(super::tests::TableTest::default()), Box::new(super::tests::TableTest::default()),
]; ];
Self {
open: vec![false; demos.len()], use crate::apps::demo::Demo;
demos, let mut open = BTreeSet::new();
} open.insert(
super::widget_gallery::WidgetGallery::default()
.name()
.to_owned(),
);
Self { open, demos }
} }
} }
impl Demos { impl Demos {
pub fn checkboxes(&mut self, ui: &mut Ui) { pub fn checkboxes(&mut self, ui: &mut Ui) {
let Self { open, demos } = self; let Self { open, demos } = self;
for (ref mut open, demo) in open.iter_mut().zip(demos.iter()) { for demo in demos {
ui.checkbox(open, demo.name()); 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) { pub fn show(&mut self, ctx: &CtxRef) {
let Self { open, demos } = self; let Self { open, demos } = self;
open.resize(demos.len(), false); // Handle deserialization of old data. for demo in demos {
for (ref mut open, demo) in open.iter_mut().zip(demos.iter_mut()) { let mut is_open = open.contains(demo.name());
demo.show(ctx, open); demo.show(ctx, &mut is_open);
set_open(open, demo.name(), is_open);
} }
} }
} }
fn set_open(open: &mut BTreeSet<String>, 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. /// A menu bar in which you can select different demo windows to show.

View file

@ -94,7 +94,7 @@ impl Default for DragAndDropDemo {
} }
impl super::Demo for DragAndDropDemo { impl super::Demo for DragAndDropDemo {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"✋ Drag and Drop" "✋ Drag and Drop"
} }

View file

@ -40,7 +40,7 @@ impl FontBook {
} }
impl super::Demo for FontBook { impl super::Demo for FontBook {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"🔤 Font Book" "🔤 Font Book"
} }

View file

@ -28,7 +28,7 @@ impl Default for LayoutTest {
} }
impl super::Demo for LayoutTest { impl super::Demo for LayoutTest {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"Layout Test" "Layout Test"
} }

View file

@ -34,7 +34,8 @@ pub trait View {
/// Something to view /// Something to view
pub trait Demo { 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 /// Show windows, etc
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool); fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool);

View file

@ -64,7 +64,7 @@ impl Painting {
} }
impl super::Demo for Painting { impl super::Demo for Painting {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"🖊 Painting" "🖊 Painting"
} }

View file

@ -26,7 +26,7 @@ impl Default for PlotDemo {
} }
impl super::Demo for PlotDemo { impl super::Demo for PlotDemo {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"🗠 Plot" "🗠 Plot"
} }

View file

@ -20,7 +20,7 @@ impl Default for Scrolling {
} }
impl super::Demo for Scrolling { impl super::Demo for Scrolling {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"↕ Scrolling" "↕ Scrolling"
} }

View file

@ -30,7 +30,7 @@ impl Default for Sliders {
} }
impl super::Demo for Sliders { impl super::Demo for Sliders {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"⬌ Sliders" "⬌ Sliders"
} }

View file

@ -2,7 +2,7 @@
pub struct IdTest {} pub struct IdTest {}
impl super::Demo for IdTest { impl super::Demo for IdTest {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"ID Test" "ID Test"
} }
@ -79,7 +79,7 @@ impl Default for ManualLayoutTest {
} }
impl super::Demo for ManualLayoutTest { impl super::Demo for ManualLayoutTest {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"Manual Layout Test" "Manual Layout Test"
} }
@ -156,7 +156,7 @@ impl Default for TableTest {
} }
impl super::Demo for TableTest { impl super::Demo for TableTest {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"Table Test" "Table Test"
} }
@ -221,7 +221,7 @@ pub struct InputTest {
} }
impl super::Demo for InputTest { impl super::Demo for InputTest {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"Input Test" "Input Test"
} }
@ -273,7 +273,7 @@ impl super::View for InputTest {
pub struct WindowResizeTest {} pub struct WindowResizeTest {}
impl super::Demo for WindowResizeTest { impl super::Demo for WindowResizeTest {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"↔ Window Resize" "↔ Window Resize"
} }

View file

@ -30,7 +30,7 @@ impl Default for WidgetGallery {
} }
impl super::Demo for WidgetGallery { impl super::Demo for WidgetGallery {
fn name(&self) -> &str { fn name(&self) -> &'static str {
"🗄 Widget Gallery" "🗄 Widget Gallery"
} }

View file

@ -25,9 +25,8 @@ impl Default for WindowOptions {
} }
impl super::Demo for WindowOptions { impl super::Demo for WindowOptions {
fn name(&self) -> &str { fn name(&self) -> &'static str {
// "🗖 Window Options" "🗖 Window Options"
&self.title
} }
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) { fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {