Skip to content

Commit

Permalink
Refactor the demo/test groups
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 28, 2024
1 parent 07d7b86 commit 27b4ac8
Showing 1 changed file with 66 additions and 121 deletions.
187 changes: 66 additions & 121 deletions crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,24 @@ use crate::View;
// ----------------------------------------------------------------------------

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
struct Demos {
struct DemoGroup {
#[cfg_attr(feature = "serde", serde(skip))]
demos: Vec<Box<dyn Demo>>,

open: BTreeSet<String>,
}

impl Default for Demos {
fn default() -> Self {
Self::from_demos(vec![
Box::<super::paint_bezier::PaintBezier>::default(),
Box::<super::code_editor::CodeEditor>::default(),
Box::<super::code_example::CodeExample>::default(),
Box::<super::context_menu::ContextMenus>::default(),
Box::<super::dancing_strings::DancingStrings>::default(),
Box::<super::drag_and_drop::DragAndDropDemo>::default(),
Box::<super::extra_viewport::ExtraViewport>::default(),
Box::<super::font_book::FontBook>::default(),
Box::<super::frame_demo::FrameDemo>::default(),
Box::<super::highlighting::Highlighting>::default(),
Box::<super::interactive_container::InteractiveContainerDemo>::default(),
Box::<super::MiscDemoWindow>::default(),
Box::<super::modals::Modals>::default(),
Box::<super::multi_touch::MultiTouch>::default(),
Box::<super::painting::Painting>::default(),
Box::<super::pan_zoom::PanZoom>::default(),
Box::<super::panels::Panels>::default(),
Box::<super::screenshot::Screenshot>::default(),
Box::<super::scrolling::Scrolling>::default(),
Box::<super::sliders::Sliders>::default(),
Box::<super::strip_demo::StripDemo>::default(),
Box::<super::table_demo::TableDemo>::default(),
Box::<super::text_edit::TextEditDemo>::default(),
Box::<super::text_layout::TextLayoutDemo>::default(),
Box::<super::tooltips::Tooltips>::default(),
Box::<super::undo_redo::UndoRedoDemo>::default(),
Box::<super::widget_gallery::WidgetGallery>::default(),
Box::<super::window_options::WindowOptions>::default(),
])
impl DemoGroup {
pub fn new(demos: Vec<Box<dyn Demo>>) -> Self {
Self {
demos,
open: Default::default(),
}
}
}

impl Demos {
pub fn from_demos(demos: Vec<Box<dyn Demo>>) -> Self {
let mut open = BTreeSet::new();

// Explains egui very well
open.insert(
super::code_example::CodeExample::default()
.name()
.to_owned(),
);

// Shows off the features
open.insert(
super::widget_gallery::WidgetGallery::default()
.name()
.to_owned(),
);

Self { demos, open }
#[allow(clippy::needless_pass_by_value)]
pub fn set_open(&mut self, demo: impl Demo + 'static, is_open: bool) {
set_open(&mut self.open, demo.name(), is_open);
}

pub fn checkboxes(&mut self, ui: &mut Ui) {
Expand All @@ -95,65 +51,6 @@ impl Demos {
}
}

// ----------------------------------------------------------------------------

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
struct Tests {
#[cfg_attr(feature = "serde", serde(skip))]
demos: Vec<Box<dyn Demo>>,

open: BTreeSet<String>,
}

impl Default for Tests {
fn default() -> Self {
Self::from_demos(vec![
Box::<super::tests::CursorTest>::default(),
Box::<super::tests::GridTest>::default(),
Box::<super::tests::IdTest>::default(),
Box::<super::tests::InputEventHistory>::default(),
Box::<super::tests::InputTest>::default(),
Box::<super::tests::LayoutTest>::default(),
Box::<super::tests::ManualLayoutTest>::default(),
Box::<super::tests::WindowResizeTest>::default(),
])
}
}

impl Tests {
pub fn from_demos(demos: Vec<Box<dyn Demo>>) -> Self {
let mut open = BTreeSet::new();
open.insert(
super::widget_gallery::WidgetGallery::default()
.name()
.to_owned(),
);

Self { demos, open }
}

pub fn checkboxes(&mut self, ui: &mut Ui) {
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
ui.toggle_value(&mut is_open, demo.name());
set_open(open, demo.name(), is_open);
}
}

pub fn windows(&mut self, ctx: &Context) {
let Self { demos, open } = self;
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) {
Expand All @@ -172,17 +69,65 @@ fn set_open(open: &mut BTreeSet<String>, key: &'static str, is_open: bool) {
pub struct DemoWindows {
about_is_open: bool,
about: About,
demos: Demos,
tests: Tests,
demos: DemoGroup,
tests: DemoGroup,
}

impl Default for DemoWindows {
fn default() -> Self {
let mut demos = DemoGroup::new(vec![
Box::<super::paint_bezier::PaintBezier>::default(),
Box::<super::code_editor::CodeEditor>::default(),
Box::<super::code_example::CodeExample>::default(),
Box::<super::context_menu::ContextMenus>::default(),
Box::<super::dancing_strings::DancingStrings>::default(),
Box::<super::drag_and_drop::DragAndDropDemo>::default(),
Box::<super::extra_viewport::ExtraViewport>::default(),
Box::<super::font_book::FontBook>::default(),
Box::<super::frame_demo::FrameDemo>::default(),
Box::<super::highlighting::Highlighting>::default(),
Box::<super::interactive_container::InteractiveContainerDemo>::default(),
Box::<super::MiscDemoWindow>::default(),
Box::<super::modals::Modals>::default(),
Box::<super::multi_touch::MultiTouch>::default(),
Box::<super::painting::Painting>::default(),
Box::<super::pan_zoom::PanZoom>::default(),
Box::<super::panels::Panels>::default(),
Box::<super::screenshot::Screenshot>::default(),
Box::<super::scrolling::Scrolling>::default(),
Box::<super::sliders::Sliders>::default(),
Box::<super::strip_demo::StripDemo>::default(),
Box::<super::table_demo::TableDemo>::default(),
Box::<super::text_edit::TextEditDemo>::default(),
Box::<super::text_layout::TextLayoutDemo>::default(),
Box::<super::tooltips::Tooltips>::default(),
Box::<super::undo_redo::UndoRedoDemo>::default(),
Box::<super::widget_gallery::WidgetGallery>::default(),
Box::<super::window_options::WindowOptions>::default(),
]);

// Explains egui very well
demos.set_open(super::code_example::CodeExample::default(), true);

// Shows off the features
demos.set_open(super::widget_gallery::WidgetGallery::default(), true);

let tests = DemoGroup::new(vec![
Box::<super::tests::CursorTest>::default(),
Box::<super::tests::GridTest>::default(),
Box::<super::tests::IdTest>::default(),
Box::<super::tests::InputEventHistory>::default(),
Box::<super::tests::InputTest>::default(),
Box::<super::tests::LayoutTest>::default(),
Box::<super::tests::ManualLayoutTest>::default(),
Box::<super::tests::WindowResizeTest>::default(),
]);

Self {
about_is_open: true,
about: Default::default(),
demos: Default::default(),
tests: Default::default(),
demos,
tests,
}
}
}
Expand Down Expand Up @@ -375,26 +320,26 @@ fn file_menu_button(ui: &mut Ui) {

#[cfg(test)]
mod tests {
use crate::demo::demo_app_windows::Demos;
use crate::{demo::demo_app_windows::DemoWindows, Demo};
use egui::Vec2;
use egui_kittest::kittest::Queryable;
use egui_kittest::{Harness, SnapshotOptions};

#[test]
fn demos_should_match_snapshot() {
let demos = Demos::default();
let demos = DemoWindows::default();

let mut errors = Vec::new();

for mut demo in demos.demos {
for mut demo in demos.demos.demos {
// Remove the emoji from the demo name
let name = demo
.name()
.split_once(' ')
.map_or(demo.name(), |(_, name)| name);

// Widget Gallery needs to be customized (to set a specific date) and has its own test
if name == "Widget Gallery" {
if name == crate::WidgetGallery::default().name() {
continue;
}

Expand Down

0 comments on commit 27b4ac8

Please sign in to comment.