Simple velocity example

This commit is contained in:
2025-11-02 10:49:25 +01:00
parent f5d1b42db4
commit 68d00be96c
3 changed files with 47 additions and 8 deletions
+2 -2
View File
@@ -64,7 +64,7 @@ pub fn app(screen: Arc<Screen>) -> 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<Screen>) -> 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 { }
}
+37 -5
View File
@@ -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<Screen>) -> 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();
"^"
}
}
}
+8 -1
View File
@@ -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