From def18fed65e13d8864d58d717a522c6f118b33a2 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 3 Feb 2025 13:41:13 +0100 Subject: [PATCH] a few improvements --- src/elements.rs | 10 +++---- src/lib.rs | 73 ++++++++++++++++++++++--------------------------- src/main.rs | 28 +++++-------------- 3 files changed, 45 insertions(+), 66 deletions(-) diff --git a/src/elements.rs b/src/elements.rs index a54b906..b832081 100644 --- a/src/elements.rs +++ b/src/elements.rs @@ -1,13 +1,13 @@ use crate::Element; impl Element for &str { - fn render(&self, frame: &mut dyn crate::Framing) { - frame.draw_str(self); + fn render(&self, frame: &mut String) { + *frame += self; } } -impl Element for crate::Ui { - fn render(&self, frame: &mut dyn crate::Framing) { - self(frame) +impl Element for String { + fn render(&self, frame: &mut String) { + *frame += self; } } diff --git a/src/lib.rs b/src/lib.rs index ea0dcfd..eb5f044 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ pub mod prelude { state::State, style::{Color, Style}, utils::init, - Framing, Props, Result, Ui, + Props, Result, Ui, }; #[cfg(feature = "state")] @@ -21,16 +21,8 @@ pub mod prelude { pub use crossterm::event::read; } -// Traits -pub trait Framing { - fn draw(&mut self, element: &dyn Element, props: &Props); - fn draw_str(&mut self, s: &str) { - _ = s - } -} - pub trait Element { - fn render(&self, frame: &mut dyn Framing); + fn render(&self, frame: &mut String); fn event(&self, event: crossterm::event::Event) { _ = event } @@ -38,7 +30,10 @@ pub trait Element { // Types pub type Handler = std::sync::Arc>; -pub type Ui = Box; +pub type Ui = Box; + +// Structs +pub struct Frame(Vec<(Box, Props)>); #[derive(Debug, Clone)] pub struct Props { @@ -57,6 +52,12 @@ pub struct Console { pub mouse_position: Option<(u16, u16)>, } +impl Frame { + pub fn draw(&mut self, element: Box, props: Props) { + self.0.push((element, props)); + } +} + impl Props { pub fn new(x: u16, y: u16) -> Self { Self { @@ -138,7 +139,27 @@ impl Props { } } -impl Framing for Console { +impl Console { + pub fn render(&mut self, ui: &Ui) -> Result<()> { + (self.width, self.height) = crossterm::terminal::size()?; + utils::clear()?; + let mut frame = Frame(Vec::new()); + ui(&mut frame); + for (e, _) in frame.0 { + self.draw(e.as_ref(), &Props::auto()); + } + Ok(()) + } + + pub fn close(self) -> Result<()> { + if self.mouse { + crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture)?; + } + crossterm::terminal::disable_raw_mode()?; + utils::show_cursor()?; + utils::clear() + } + fn draw(&mut self, element: &dyn Element, props: &Props) { let mut written = String::new(); element.render(&mut written); @@ -185,34 +206,6 @@ impl Framing for Console { } } -impl Console { - pub fn render(&mut self, ui: &Ui) -> Result<()> { - (self.width, self.height) = crossterm::terminal::size()?; - utils::clear()?; - ui(self); - Ok(()) - } - - pub fn close(self) -> Result<()> { - if self.mouse { - crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture)?; - } - crossterm::terminal::disable_raw_mode()?; - utils::show_cursor()?; - utils::clear() - } -} - -impl Framing for String { - fn draw(&mut self, element: &dyn Element, _props: &Props) { - element.render(self); - } - - fn draw_str(&mut self, s: &str) { - *self += s; - } -} - pub fn func(t: T) -> Handler { std::sync::Arc::new(std::sync::Mutex::new(t)) } diff --git a/src/main.rs b/src/main.rs index 84e6520..49cb68c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,9 @@ use osmium_engine::prelude::*; fn main() -> Result<()> { let mut console = init(true)?; - let (mut count, ui) = app(); + let mut count = use_state(0); + + let ui = app(count); loop { console.render(&ui)?; @@ -14,24 +16,8 @@ fn main() -> Result<()> { console.close() } -pub fn app() -> (State, Ui) { - let (count, count_ui) = counter(); - - ( - count, - Box::new(move |frame| { - frame.draw(&count_ui, &Props::center()); - }), - ) -} - -pub fn counter() -> (State, Ui) { - let count = use_state(0); - - ( - count, - Box::new(move |frame| { - frame.draw(&format!("Events: {count}").as_str(), &Props::center()); - }), - ) +pub fn app(count: State) -> Ui { + Box::new(move |frame| { + frame.draw(Box::new(format!("Events: {count}")), Props::center()); + }) }