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:
@@ -1,11 +1,17 @@
|
||||
pub mod note;
|
||||
pub mod preview;
|
||||
pub mod username;
|
||||
|
||||
pub use note::Note;
|
||||
pub use preview::{Preview, PreviewApp};
|
||||
pub use username::Username;
|
||||
|
||||
use egui::Margin;
|
||||
|
||||
pub trait View {
|
||||
fn ui(&mut self, ui: &mut egui::Ui);
|
||||
}
|
||||
|
||||
pub fn padding<R>(
|
||||
amount: impl Into<Margin>,
|
||||
ui: &mut egui::Ui,
|
||||
@@ -15,3 +21,9 @@ pub fn padding<R>(
|
||||
.inner_margin(amount)
|
||||
.show(ui, add_contents)
|
||||
}
|
||||
|
||||
pub fn is_mobile(ctx: &egui::Context) -> bool {
|
||||
//true
|
||||
let screen_size = ctx.screen_rect().size();
|
||||
screen_size.x < 550.0
|
||||
}
|
||||
|
||||
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