diff --git a/examples/tour.rs b/examples/tour.rs index bae1f6f..8a85a9f 100644 --- a/examples/tour.rs +++ b/examples/tour.rs @@ -64,7 +64,7 @@ pub fn app(screen: Arc) -> Rsx { FlexRow { "Username" - @Transform::new().padding(1, 1).dimensions(40, 1); + @Transform::new().padding(1, 1).dimensions_const(40, 1); @Style { foreground: Some(0xffffff), background: Background::RoundedOutline(0xff0000) }; @Focused; Input { } @@ -73,7 +73,7 @@ pub fn app(screen: Arc) -> Rsx { FlexRow { "Password" - @Transform::new().padding(1, 1).dimensions(40, 1); + @Transform::new().padding(1, 1).dimensions_const(40, 1); @Style { foreground: Some(0xffffff), background: Background::RoundedOutline(0xffff00) }; Input { } } diff --git a/examples/velocity.rs b/examples/velocity.rs index 5cd8a5f..3d6043a 100644 --- a/examples/velocity.rs +++ b/examples/velocity.rs @@ -1,20 +1,52 @@ +use std::sync::Arc; + use osui::prelude::*; fn main() -> std::io::Result<()> { let screen = Screen::new(); + screen.extension(InputExtension); - app().draw(&screen); + app(screen.clone()).draw(&screen); screen.run() } -pub fn app() -> Rsx { +pub fn app(screen: Arc) -> Rsx { rsx! { + @Handler::new({ + let screen = screen.clone(); + move |_, e: &crossterm::event::Event| { + if let crossterm::event::Event::Key(e) = e { + if e.code == crossterm::event::KeyCode::Esc { + screen.close(); + } + } + } + }); @Style { foreground: None, background: Background::Outline(0xff0000) }; - @Transform::new().padding(1, 1); VelocityHandler { - @Velocity(200, 100); - "test" + @Velocity(700, 0); + @Handler::new({ + move |w, e: &crossterm::event::Event| { + if let crossterm::event::Event::Key(e) = e { + if e.code == crossterm::event::KeyCode::Char(' ') { + if let Some(Velocity(v, _)) = w.get() { + w.set_component(Velocity(-v, 0)); + } else { + w.component(Velocity(700, 0)); + } + } + } + } + }); + "(•_•)" + } + + @Transform::new().dimensions(Dimension::Full, Dimension::Full); + VelocityHandler { + @Velocity(0, -150); + @Transform::center().bottom(); + "^" } } } diff --git a/src/style.rs b/src/style.rs index c61d7d3..d1e1015 100644 --- a/src/style.rs +++ b/src/style.rs @@ -139,12 +139,19 @@ impl Transform { } /// Sets constant dimensions. - pub fn dimensions(mut self, width: u16, height: u16) -> Self { + pub fn dimensions_const(mut self, width: u16, height: u16) -> Self { self.width = Dimension::Const(width); self.height = Dimension::Const(height); self } + /// Sets constant dimensions. + pub fn dimensions(mut self, width: Dimension, height: Dimension) -> Self { + self.width = width; + self.height = height; + self + } + /// Resolves `Dimension` rules into absolute values for the given parent size. pub fn use_dimensions(&self, parent_transform: &RawTransform, raw: &mut RawTransform) { self.width