mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
[demo] Move Fractal Clock to WrapApp
This commit is contained in:
@@ -297,24 +297,6 @@ impl epi::App for DemoApp {
|
||||
self.frame_history
|
||||
.on_new_frame(ctx.input().time, frame.info().cpu_usage);
|
||||
|
||||
let web_location_hash = frame
|
||||
.info()
|
||||
.web_info
|
||||
.as_ref()
|
||||
.map(|info| info.web_location_hash.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
let link = if web_location_hash == "clock" {
|
||||
Some(super::DemoLink::Clock)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let demo_environment = super::DemoEnvironment {
|
||||
seconds_since_midnight: frame.info().seconds_since_midnight,
|
||||
link,
|
||||
};
|
||||
|
||||
let mean_frame_time = self.frame_history.mean_frame_time();
|
||||
|
||||
let Self {
|
||||
@@ -323,7 +305,7 @@ impl epi::App for DemoApp {
|
||||
..
|
||||
} = self;
|
||||
|
||||
demo_windows.ui(ctx, &demo_environment, frame.tex_allocator(), |ui| {
|
||||
demo_windows.ui(ctx, frame.tex_allocator(), |ui| {
|
||||
ui.separator();
|
||||
ui.checkbox(backend_window_open, "💻 Backend");
|
||||
|
||||
|
||||
@@ -2,24 +2,6 @@ use egui::{CtxRef, Resize, ScrollArea, Ui, Window};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Link to show a specific part of the demo app.
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub enum DemoLink {
|
||||
Clock,
|
||||
}
|
||||
|
||||
/// Special input to the demo-app.
|
||||
#[derive(Default)]
|
||||
pub struct DemoEnvironment {
|
||||
/// Local time. Used for the clock in the demo app.
|
||||
pub seconds_since_midnight: Option<f64>,
|
||||
|
||||
/// Set to `Some` to open a specific part of the demo app.
|
||||
pub link: Option<DemoLink>,
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
#[serde(default)]
|
||||
struct Demos {
|
||||
@@ -67,13 +49,8 @@ pub struct DemoWindows {
|
||||
#[serde(skip)]
|
||||
color_test: super::ColorTest,
|
||||
|
||||
fractal_clock: super::FractalClock,
|
||||
|
||||
/// open, title, view
|
||||
demos: Demos,
|
||||
|
||||
#[serde(skip)]
|
||||
previous_link: Option<DemoLink>,
|
||||
}
|
||||
|
||||
impl DemoWindows {
|
||||
@@ -82,26 +59,11 @@ impl DemoWindows {
|
||||
pub fn ui(
|
||||
&mut self,
|
||||
ctx: &CtxRef,
|
||||
env: &DemoEnvironment,
|
||||
tex_allocator: &mut Option<&mut dyn epi::TextureAllocator>,
|
||||
sidebar_ui: impl FnOnce(&mut Ui),
|
||||
) {
|
||||
if self.previous_link != env.link {
|
||||
match env.link {
|
||||
None => {}
|
||||
Some(DemoLink::Clock) => {
|
||||
self.open_windows = OpenWindows {
|
||||
fractal_clock: true,
|
||||
..OpenWindows::none()
|
||||
};
|
||||
}
|
||||
}
|
||||
self.previous_link = env.link;
|
||||
}
|
||||
|
||||
egui::SidePanel::left("side_panel", 190.0).show(ctx, |ui| {
|
||||
ui.heading("✒ Egui Demo");
|
||||
egui::warn_if_debug_build(ui);
|
||||
|
||||
ui.separator();
|
||||
|
||||
@@ -132,24 +94,22 @@ impl DemoWindows {
|
||||
});
|
||||
|
||||
egui::TopPanel::top("menu_bar").show(ctx, |ui| {
|
||||
show_menu_bar(ui, &mut self.open_windows, env.seconds_since_midnight);
|
||||
show_menu_bar(ui);
|
||||
});
|
||||
|
||||
self.windows(ctx, env, tex_allocator);
|
||||
self.windows(ctx, tex_allocator);
|
||||
}
|
||||
|
||||
/// Show the open windows.
|
||||
fn windows(
|
||||
&mut self,
|
||||
ctx: &CtxRef,
|
||||
env: &DemoEnvironment,
|
||||
tex_allocator: &mut Option<&mut dyn epi::TextureAllocator>,
|
||||
) {
|
||||
let Self {
|
||||
open_windows,
|
||||
demo_window,
|
||||
color_test,
|
||||
fractal_clock,
|
||||
demos,
|
||||
..
|
||||
} = self;
|
||||
@@ -191,12 +151,6 @@ impl DemoWindows {
|
||||
|
||||
demos.show(ctx);
|
||||
|
||||
fractal_clock.window(
|
||||
ctx,
|
||||
&mut open_windows.fractal_clock,
|
||||
env.seconds_since_midnight,
|
||||
);
|
||||
|
||||
self.resize_windows(ctx);
|
||||
}
|
||||
|
||||
@@ -259,7 +213,6 @@ impl DemoWindows {
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
struct OpenWindows {
|
||||
demo: bool,
|
||||
fractal_clock: bool,
|
||||
|
||||
// egui stuff:
|
||||
settings: bool,
|
||||
@@ -284,7 +237,6 @@ impl OpenWindows {
|
||||
fn none() -> Self {
|
||||
Self {
|
||||
demo: false,
|
||||
fractal_clock: false,
|
||||
|
||||
settings: false,
|
||||
inspection: false,
|
||||
@@ -298,7 +250,6 @@ impl OpenWindows {
|
||||
fn checkboxes(&mut self, ui: &mut Ui) {
|
||||
let Self {
|
||||
demo,
|
||||
fractal_clock,
|
||||
settings,
|
||||
inspection,
|
||||
memory,
|
||||
@@ -317,11 +268,10 @@ impl OpenWindows {
|
||||
.on_hover_text("For testing the integrations painter");
|
||||
ui.separator();
|
||||
ui.label("Misc:");
|
||||
ui.checkbox(fractal_clock, "🕑 Fractal Clock");
|
||||
}
|
||||
}
|
||||
|
||||
fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, seconds_since_midnight: Option<f64>) {
|
||||
fn show_menu_bar(ui: &mut Ui) {
|
||||
use egui::*;
|
||||
|
||||
menu::bar(ui, |ui| {
|
||||
@@ -337,24 +287,5 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, seconds_since_midnight:
|
||||
*ui.ctx().memory() = Default::default();
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(time) = seconds_since_midnight {
|
||||
let time = format!(
|
||||
"{:02}:{:02}:{:02}.{:02}",
|
||||
(time % (24.0 * 60.0 * 60.0) / 3600.0).floor(),
|
||||
(time % (60.0 * 60.0) / 60.0).floor(),
|
||||
(time % 60.0).floor(),
|
||||
(time % 1.0 * 100.0).floor()
|
||||
);
|
||||
|
||||
ui.with_layout(Layout::right_to_left(), |ui| {
|
||||
if ui
|
||||
.add(Button::new(time).text_style(TextStyle::Monospace))
|
||||
.clicked
|
||||
{
|
||||
windows.fractal_clock = !windows.fractal_clock;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ mod drag_and_drop;
|
||||
mod font_book;
|
||||
pub mod font_contents_emoji;
|
||||
pub mod font_contents_ubuntu;
|
||||
mod fractal_clock;
|
||||
mod painting;
|
||||
mod scrolls;
|
||||
mod sliders;
|
||||
@@ -23,8 +22,8 @@ mod widgets;
|
||||
|
||||
pub use {
|
||||
app::*, color_test::ColorTest, dancing_strings::DancingStrings, demo_window::DemoWindow,
|
||||
demo_windows::*, drag_and_drop::*, font_book::FontBook, fractal_clock::FractalClock,
|
||||
painting::Painting, scrolls::Scrolls, sliders::Sliders, tests::Tests, widgets::Widgets,
|
||||
demo_windows::*, drag_and_drop::*, font_book::FontBook, painting::Painting, scrolls::Scrolls,
|
||||
sliders::Sliders, tests::Tests, widgets::Widgets,
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -29,17 +29,19 @@ impl Default for FractalClock {
|
||||
}
|
||||
}
|
||||
|
||||
impl FractalClock {
|
||||
pub fn window(&mut self, ctx: &CtxRef, open: &mut bool, seconds_since_midnight: Option<f64>) {
|
||||
Window::new("🕑 Fractal Clock")
|
||||
.open(open)
|
||||
.default_size(vec2(512.0, 512.0))
|
||||
.scroll(false)
|
||||
// Dark background frame to make it pop:
|
||||
.frame(Frame::window(&ctx.style()).fill(Srgba::black_alpha(250)))
|
||||
.show(ctx, |ui| self.ui(ui, seconds_since_midnight));
|
||||
impl epi::App for FractalClock {
|
||||
fn name(&self) -> &str {
|
||||
"🕑 Fractal Clock"
|
||||
}
|
||||
|
||||
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut epi::Frame<'_>) {
|
||||
egui::CentralPanel::default()
|
||||
.frame(Frame::dark_canvas(&ctx.style()))
|
||||
.show(ctx, |ui| self.ui(ui, frame.info().seconds_since_midnight));
|
||||
}
|
||||
}
|
||||
|
||||
impl FractalClock {
|
||||
pub fn ui(&mut self, ui: &mut Ui, seconds_since_midnight: Option<f64>) {
|
||||
if !self.paused {
|
||||
self.time = seconds_since_midnight.unwrap_or_else(|| ui.input().time);
|
||||
@@ -1,7 +1,9 @@
|
||||
mod demo;
|
||||
mod fractal_clock;
|
||||
mod http_app;
|
||||
|
||||
pub use demo::DemoApp;
|
||||
pub use fractal_clock::FractalClock;
|
||||
pub use http_app::HttpApp;
|
||||
|
||||
pub use demo::DemoWindows; // used for tests
|
||||
|
||||
Reference in New Issue
Block a user