Merge pull request #16 from osui-rs/demos-module

Demos module
This commit is contained in:
Leo Codes
2025-08-02 06:17:27 -05:00
committed by GitHub
5 changed files with 61 additions and 19 deletions
+40
View File
@@ -0,0 +1,40 @@
use osui::prelude::*;
pub fn app() -> Rsx {
let count = use_state(0);
std::thread::spawn({
let count = count.clone();
move || loop {
**count.get() += 1;
std::thread::sleep(std::time::Duration::from_secs(1));
}
});
rsx! {
FlexRow {
FlexCol {
@Transform::new().padding(2, 2);
@Style { foreground: None, background: Background::RoundedOutline(0x00ff00) };
Div {
"This is text inside a div"
}
@Transform::new().padding(2, 2);
@Style { foreground: None, background: Background::Outline(0x00ff00) };
Div {
"This is text inside a div with square outlines"
}
// TODO: div with width full
} (1)
FlexCol {
Div {
%count
"This will increment every second: {count}"
}
} (2)
} (0)
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ impl Div {
impl Element for Div { impl Element for Div {
fn render(&mut self, scope: &mut crate::render_scope::RenderScope) { fn render(&mut self, scope: &mut crate::render_scope::RenderScope) {
let (width, height) = scope.get_size_or(self.size.0, self.size.1); let (width, height) = scope.get_size_or(self.size.0, self.size.1);
scope.draw_rect(width, height, 0); scope.use_area(width, height);
} }
fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) { fn after_render(&mut self, scope: &mut crate::render_scope::RenderScope) {
+2 -2
View File
@@ -67,7 +67,7 @@ impl Element for FlexRow {
transform.height = transform.height.max(v + t.height); transform.height = transform.height.max(v + t.height);
t.x += transform.x; t.x += transform.x;
t.y += transform.y + v; t.y += transform.y + v;
v += t.height + self.gap; v += t.height + self.gap + (t.py * 2);
t.px += transform.px; t.px += transform.px;
t.py += transform.py; t.py += transform.py;
@@ -123,7 +123,7 @@ impl Element for FlexCol {
transform.height = transform.height.max(t.height + (t.py * 2)); transform.height = transform.height.max(t.height + (t.py * 2));
t.x += transform.x + v; t.x += transform.x + v;
t.y += transform.y; t.y += transform.y;
v += t.width + self.gap; v += t.width + self.gap + (t.px * 2);
t.px += transform.px; t.px += transform.px;
t.py += transform.py; t.py += transform.py;
+2 -14
View File
@@ -1,22 +1,10 @@
mod demos;
use osui::prelude::*; use osui::prelude::*;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let screen = Screen::new(); let screen = Screen::new();
screen.extension(VelocityExtension);
let count = use_state(0); demos::app().draw(&screen);
rsx! {
@Velocity(20, 0);
@Transform::new();
static "Count: {count}"
}
.draw(&screen);
std::thread::spawn(move || loop {
**count.get() += 1;
std::thread::sleep(std::time::Duration::from_millis(100));
});
screen.run() screen.run()
} }
+16 -2
View File
@@ -105,8 +105,22 @@ impl RenderScope {
match self.style.background { match self.style.background {
crate::style::Background::NoBackground => {} crate::style::Background::NoBackground => {}
crate::style::Background::Outline(_c) => { crate::style::Background::Outline(c) => {
// TODO: Implement basic outline drawing if needed let width = width + self.transform.px * 2;
let height = height + self.transform.py * 2;
if width > 1 && height > 1 {
let d = "─".repeat(width as usize - 2);
utils::print_liner(
self.transform.x,
self.transform.y,
&utils::hex_ansi(c),
&format!(
"┌{d}┐{}\n└{d}┘",
format!("\n│{}│", " ".repeat(width as usize - 2))
.repeat(height as usize - 2),
),
);
}
} }
crate::style::Background::RoundedOutline(c) => { crate::style::Background::RoundedOutline(c) => {
let width = width + self.transform.px * 2; let width = width + self.transform.px * 2;