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

Demo app: use enum instead of strings for demo-selector anchor #2781

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Changes from 1 commit
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
81 changes: 58 additions & 23 deletions crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use eframe::glow;

#[cfg(target_arch = "wasm32")]
use core::any::Any;
use std::fmt::{Display, Formatter};

#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
Expand Down Expand Up @@ -75,6 +76,37 @@ impl eframe::App for ColorTestApp {
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum Anchor {
Demo,
EasyMarkEditor,
#[cfg(feature = "http")]
Http,
Clock,
#[cfg(any(feature = "glow", feature = "wgpu"))]
Custom3d,
Colors,
}

impl Display for Anchor {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
XyLyXyRR marked this conversation as resolved.
Show resolved Hide resolved
write!(f, "{self:?}")
}
}

impl From<Anchor> for egui::WidgetText {
fn from(value: Anchor) -> Self {
Self::RichText(egui::RichText::new(value.to_string()))
}
}

impl Default for Anchor {
fn default() -> Self {
Self::Demo
}
}

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

/// The state that we persist (serialize).
Expand All @@ -89,7 +121,7 @@ pub struct State {
clock: FractalClockApp,
color_test: ColorTestApp,

selected_anchor: String,
selected_anchor: Anchor,
backend_panel: super::backend_panel::BackendPanel,
}

Expand Down Expand Up @@ -125,27 +157,27 @@ impl WrapApp {
slf
}

fn apps_iter_mut(&mut self) -> impl Iterator<Item = (&str, &str, &mut dyn eframe::App)> {
fn apps_iter_mut(&mut self) -> impl Iterator<Item = (&str, Anchor, &mut dyn eframe::App)> {
let mut vec = vec![
(
"✨ Demos",
"demo",
Anchor::Demo,
&mut self.state.demo as &mut dyn eframe::App,
),
(
"🖹 EasyMark editor",
"easymark",
Anchor::EasyMarkEditor,
&mut self.state.easy_mark_editor as &mut dyn eframe::App,
),
#[cfg(feature = "http")]
(
"⬇ HTTP",
"http",
Anchor::Http,
&mut self.state.http as &mut dyn eframe::App,
),
(
"🕑 Fractal Clock",
"clock",
Anchor::Clock,
&mut self.state.clock as &mut dyn eframe::App,
),
];
Expand All @@ -154,14 +186,14 @@ impl WrapApp {
if let Some(custom3d) = &mut self.custom3d {
vec.push((
"🔺 3D painting",
"custom3d",
Anchor::Custom3d,
custom3d as &mut dyn eframe::App,
));
}

vec.push((
"🎨 Color test",
"colors",
Anchor::Colors,
&mut self.state.color_test as &mut dyn eframe::App,
));

Expand All @@ -182,12 +214,20 @@ impl eframe::App for WrapApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
#[cfg(target_arch = "wasm32")]
if let Some(anchor) = frame.info().web_info.location.hash.strip_prefix('#') {
self.state.selected_anchor = anchor.to_owned();
}

if self.state.selected_anchor.is_empty() {
let selected_anchor = self.apps_iter_mut().next().unwrap().0.to_owned();
self.state.selected_anchor = selected_anchor;
let anchors = vec![
Anchor::Demo,
Anchor::EasyMarkEditor,
#[cfg(feature = "http")]
Anchor::Http,
Anchor::Clock,
#[cfg(any(feature = "glow", feature = "wgpu"))]
Anchor::Custom3d,
Anchor::Colors,
];
let anchor = anchors.into_iter().find(|x| x.to_string() == anchor);
XyLyXyRR marked this conversation as resolved.
Show resolved Hide resolved
if let Some(v) = anchor {
self.state.selected_anchor = v;
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -277,17 +317,12 @@ impl WrapApp {
}

fn show_selected_app(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
let mut found_anchor = false;
let selected_anchor = self.state.selected_anchor.clone();
let selected_anchor = self.state.selected_anchor;
for (_name, anchor, app) in self.apps_iter_mut() {
if anchor == selected_anchor || ctx.memory(|mem| mem.everything_is_visible()) {
app.update(ctx, frame);
found_anchor = true;
}
}
if !found_anchor {
self.state.selected_anchor = "demo".into();
}
}

fn bar_contents(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
Expand All @@ -306,13 +341,13 @@ impl WrapApp {

ui.separator();

let mut selected_anchor = self.state.selected_anchor.clone();
let mut selected_anchor = self.state.selected_anchor;
for (name, anchor, _app) in self.apps_iter_mut() {
if ui
.selectable_label(selected_anchor == anchor, name)
.clicked()
{
selected_anchor = anchor.to_owned();
selected_anchor = anchor;
if frame.is_web() {
ui.output_mut(|o| o.open_url(format!("#{}", anchor)));
}
Expand All @@ -324,7 +359,7 @@ impl WrapApp {
if false {
// TODO(emilk): fix the overlap on small screens
if clock_button(ui, crate::seconds_since_midnight()).clicked() {
self.state.selected_anchor = "clock".to_owned();
self.state.selected_anchor = Anchor::Clock;
if frame.is_web() {
ui.output_mut(|o| o.open_url("#clock"));
}
Expand Down