From 6ef75e80935ee833f92c90635b0bbfbb9052b198 Mon Sep 17 00:00:00 2001 From: xylyxyrr <39663597+XyLyXyRR@users.noreply.github.com> Date: Sat, 4 Mar 2023 23:16:48 +0800 Subject: [PATCH 1/3] improve(code): use enum instead of string --- crates/egui_demo_app/src/wrap_app.rs | 81 ++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index 3f2f5a0a2f5..fce50ac61ca 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -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))] @@ -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 { + write!(f, "{self:?}") + } +} + +impl From 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). @@ -89,7 +121,7 @@ pub struct State { clock: FractalClockApp, color_test: ColorTestApp, - selected_anchor: String, + selected_anchor: Anchor, backend_panel: super::backend_panel::BackendPanel, } @@ -125,27 +157,27 @@ impl WrapApp { slf } - fn apps_iter_mut(&mut self) -> impl Iterator { + fn apps_iter_mut(&mut self) -> impl Iterator { 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, ), ]; @@ -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, )); @@ -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); + if let Some(v) = anchor { + self.state.selected_anchor = v; + } } #[cfg(not(target_arch = "wasm32"))] @@ -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) { @@ -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))); } @@ -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")); } From babe6ac611223345a69ceed6374ee9bb7cda4e7b Mon Sep 17 00:00:00 2001 From: xylyxyrr <39663597+XyLyXyRR@users.noreply.github.com> Date: Wed, 29 Mar 2023 22:02:32 +0800 Subject: [PATCH 2/3] update(code): optimize code --- crates/egui_demo_app/src/wrap_app.rs | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index fce50ac61ca..d7276633a31 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -5,7 +5,6 @@ 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))] @@ -89,8 +88,23 @@ enum Anchor { Colors, } -impl Display for Anchor { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { +impl Anchor { + fn all() -> Vec { + vec![ + Anchor::Demo, + Anchor::EasyMarkEditor, + #[cfg(feature = "http")] + Anchor::Http, + Anchor::Clock, + #[cfg(any(feature = "glow", feature = "wgpu"))] + Anchor::Custom3d, + Anchor::Colors, + ] + } +} + +impl std::fmt::Display for Anchor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{self:?}") } } @@ -214,17 +228,7 @@ 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('#') { - 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); + let anchor = Anchor::all().find(|x| x.to_string() == anchor); if let Some(v) = anchor { self.state.selected_anchor = v; } From ecd7f07278aecce2cfeb4b353ffb4f78879747b0 Mon Sep 17 00:00:00 2001 From: xylyxyrr <39663597+XyLyXyRR@users.noreply.github.com> Date: Thu, 30 Mar 2023 10:24:44 +0800 Subject: [PATCH 3/3] fix(code): lints --- crates/egui_demo_app/src/wrap_app.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index d7276633a31..3b2c09a796d 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -89,6 +89,7 @@ enum Anchor { } impl Anchor { + #[cfg(target_arch = "wasm32")] fn all() -> Vec { vec![ Anchor::Demo, @@ -228,7 +229,7 @@ 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('#') { - let anchor = Anchor::all().find(|x| x.to_string() == anchor); + let anchor = Anchor::all().into_iter().find(|x| x.to_string() == anchor); if let Some(v) = anchor { self.state.selected_anchor = v; }