1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

eframe app creation refactor (#1363)

* Change how eframe apps are created
* eframe: re-export epi::* so users don't need to care about what epi is
This commit is contained in:
Emil Ernerfeldt
2022-03-16 15:39:48 +01:00
committed by GitHub
parent c768d1d48e
commit c8f6cae362
26 changed files with 387 additions and 444 deletions

View File

@@ -30,10 +30,6 @@ impl Default for ColorTest {
}
impl epi::App for ColorTest {
fn name(&self) -> &str {
"🎨 Color test"
}
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
if frame.is_web() {

View File

@@ -1,7 +1,3 @@
/// Demonstrates how to make an app using egui.
///
/// Implements `epi::App` so it can be used with
/// [`egui_glium`](https://github.com/emilk/egui/tree/master/egui_glium), [`egui_glow`](https://github.com/emilk/egui/tree/master/egui_glow) and [`egui_web`](https://github.com/emilk/egui/tree/master/egui_web).
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
@@ -10,28 +6,6 @@ pub struct DemoApp {
}
impl epi::App for DemoApp {
fn name(&self) -> &str {
"✨ Demos"
}
fn setup(
&mut self,
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {
*self = epi::get_value(storage, epi::APP_KEY).unwrap_or_default();
}
}
#[cfg(feature = "persistence")]
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
}
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
self.demo_windows.ui(ctx);
}

View File

@@ -33,10 +33,6 @@ impl Default for FractalClock {
}
impl epi::App for FractalClock {
fn name(&self) -> &str {
"🕑 Fractal Clock"
}
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
egui::CentralPanel::default()
.frame(Frame::dark_canvas(&ctx.style()))

View File

@@ -54,10 +54,6 @@ impl Default for HttpApp {
}
impl epi::App for HttpApp {
fn name(&self) -> &str {
"⬇ HTTP"
}
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
egui::TopBottomPanel::bottom("http_bottom").show(ctx, |ui| {
let layout = egui::Layout::top_down(egui::Align::Center).with_main_justify(true);

View File

@@ -30,10 +30,6 @@ impl Default for EasyMarkEditor {
}
impl epi::App for EasyMarkEditor {
fn name(&self) -> &str {
"🖹 EasyMark editor"
}
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
egui::TopBottomPanel::bottom("easy_mark_bottom").show(ctx, |ui| {
let layout = egui::Layout::top_down(egui::Align::Center).with_main_justify(true);

View File

@@ -12,14 +12,26 @@ pub struct Apps {
}
impl Apps {
fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut dyn epi::App)> {
fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &str, &mut dyn epi::App)> {
vec![
("demo", &mut self.demo as &mut dyn epi::App),
("easymark", &mut self.easy_mark_editor as &mut dyn epi::App),
("✨ Demos", "demo", &mut self.demo as &mut dyn epi::App),
(
"🖹 EasyMark editor",
"easymark",
&mut self.easy_mark_editor as &mut dyn epi::App,
),
#[cfg(feature = "http")]
("http", &mut self.http as &mut dyn epi::App),
("clock", &mut self.clock as &mut dyn epi::App),
("colors", &mut self.color_test as &mut dyn epi::App),
("⬇ HTTP", "http", &mut self.http as &mut dyn epi::App),
(
"🕑 Fractal Clock",
"clock",
&mut self.clock as &mut dyn epi::App,
),
(
"🎨 Color test",
"colors",
&mut self.color_test as &mut dyn epi::App,
),
]
.into_iter()
}
@@ -37,24 +49,17 @@ pub struct WrapApp {
dropped_files: Vec<egui::DroppedFile>,
}
impl epi::App for WrapApp {
fn name(&self) -> &str {
"egui demo apps"
}
fn setup(
&mut self,
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
impl WrapApp {
pub fn new(_cc: &epi::CreationContext<'_>) -> Self {
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {
*self = epi::get_value(storage, epi::APP_KEY).unwrap_or_default();
if let Some(storage) = _cc.storage {
return epi::get_value(storage, epi::APP_KEY).unwrap_or_default();
}
Self::default()
}
}
impl epi::App for WrapApp {
#[cfg(feature = "persistence")]
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
@@ -111,7 +116,7 @@ impl epi::App for WrapApp {
let mut found_anchor = false;
for (anchor, app) in self.apps.iter_mut() {
for (_name, anchor, app) in self.apps.iter_mut() {
if anchor == self.selected_anchor || ctx.memory().everything_is_visible() {
app.update(ctx, frame);
found_anchor = true;
@@ -138,9 +143,9 @@ impl WrapApp {
ui.checkbox(&mut self.backend_panel.open, "💻 Backend");
ui.separator();
for (anchor, app) in self.apps.iter_mut() {
for (name, anchor, _app) in self.apps.iter_mut() {
if ui
.selectable_label(self.selected_anchor == anchor, app.name())
.selectable_label(self.selected_anchor == anchor, name)
.clicked()
{
self.selected_anchor = anchor.to_owned();