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:
33
src/ui/preview.rs
Normal file
33
src/ui/preview.rs
Normal 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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user