introduce View and Previews traits

In this commit we refactor the preview mechanism, and switch to
responsive views by default.

To create a preview, your view now has to implement the Preview trait.
This is very similar to SwiftUI's preview mechanism.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-04-19 14:09:19 -07:00
parent a5e1fbf328
commit a71e8206fb
11 changed files with 313 additions and 284 deletions

33
src/ui/preview.rs Normal file
View File

@@ -0,0 +1,33 @@
use crate::ui::View;
pub trait Preview {
type Prev: View;
fn preview() -> Self::Prev;
}
pub struct PreviewApp {
view: Box<dyn View>,
}
impl<V> From<V> for PreviewApp
where
V: View + 'static,
{
fn from(v: V) -> Self {
PreviewApp::new(v)
}
}
impl PreviewApp {
pub fn new(view: impl View + 'static) -> PreviewApp {
let view = Box::new(view);
Self { view }
}
}
impl eframe::App for PreviewApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| self.view.ui(ui));
}
}