[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 {
fn name(&self) -> &str {
fn name(&self) -> &'static str {
"♫ Dancing Strings"
}

View file

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

View file

@ -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<bool>,
#[cfg_attr(feature = "persistence", serde(skip))]
demos: Vec<Box<dyn super::Demo>>,
open: BTreeSet<String>,
}
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<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.

View file

@ -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"
}

View file

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

View file

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

View file

@ -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);

View file

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

View file

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

View file

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

View file

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

View file

@ -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"
}

View file

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

View file

@ -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) {