diff --git a/src/lib.rs b/src/lib.rs index 961ff8d..8715c5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,10 +55,10 @@ pub mod utils; pub mod prelude { pub use crate::ui::Color::*; pub use crate::ui::Font::*; - pub use crate::ui::Instruction::*; pub use crate::ui::Number::{Auto, Center}; + pub use crate::Instruction::*; pub use crate::{self as osui, css, ersx, launch, rsx, ui::*, Handler}; - pub use crate::{style, Component, Document, State}; + pub use crate::{style, Component, Document, Instruction, State}; pub use crate::{Children, Element, ElementCore, ElementWidget}; pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind}; pub use osui_element::component; @@ -197,17 +197,19 @@ impl Document { } } pub fn run(&mut self) -> bool { + // Set up the screen + utils::hide_cursor(); + crossterm::terminal::enable_raw_mode().unwrap(); + utils::clear(); + self.running = None; let element = unsafe { &mut *self.element }; + self.render(); element.initialize(self); // Send initial event element.event(crossterm::event::Event::FocusGained, self); - utils::hide_cursor(); - crossterm::terminal::enable_raw_mode().unwrap(); - utils::clear(); - while self.running == None { self.render(); element.event(crossterm::event::read().unwrap(), self); @@ -509,3 +511,11 @@ impl PartialEq for State { self.use_state().ne(other) } } + +#[derive(Debug)] +pub enum Instruction<'a> { + SetStyle(ui::Css), + SetChild(usize), + Ghost(usize), + Load(Handler>), +} diff --git a/src/main.rs b/src/main.rs index ef3cb7f..2349358 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use osui::{examples, prelude::*}; +use osui::prelude::*; + fn main() { launch!(App); } @@ -6,34 +7,12 @@ fn main() { #[component] pub fn App() { rsx! { - @SetChild(1) - @SetStyle(css! { - example { - outline: true, - y: Auto, - x: 0 px, - } - example: "hover" { - color: Blue, - outline_color: Red, - } - }) - - %text { class: "red-bg", static "Select an example" } - - button { class: "example", on_click: fn(_, _, document) { - document.clear_css(); - document.draw(examples::counter_example::App::default().create_element()); - }, static "Counter" } - - button { class: "example", on_click: fn(_, _, document) { - document.clear_css(); - document.draw(examples::todo_example::App::default().create_element()); - }, static "Todo" } - - button { class: "example", on_click: fn(_, _, document) { - document.clear_css(); - document.draw(examples::login_example::App::default().create_element()); - }, static "Login" } + async { + sleep(3000); + ersx!(text { + "OK" + }) + } + text { "Hello world!" } } } diff --git a/src/rsx.rs b/src/rsx.rs index 9fb8d59..2de3f33 100644 --- a/src/rsx.rs +++ b/src/rsx.rs @@ -12,6 +12,15 @@ macro_rules! check_expr { } #[macro_export] macro_rules! parse_rsx_param { + ($elem:expr, async $code:block $($rest:tt)*) => { + $elem.instructions.push($crate::Instruction::Load(Handler::new( + |d: &mut Div, _, _| { + d.children.add_child($code); + } + ))); + osui::parse_rsx_param!($elem, $($rest)*); + }; + ($elem:expr, %$elem_path:path { $($inner:tt)* } $($rest:tt)*) => { $elem.ghosts.push($elem.ghosts.len()); if let $crate::Children::Children(children, _) = &mut $elem.children { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 5134e53..c22e733 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -29,17 +29,10 @@ pub fn text<'a>(class: &'a str, id: &'a str, children: Children, style: Style) - }) } -#[derive(Debug)] -pub enum Instruction { - SetStyle(Css), - SetChild(usize), - Ghost(usize), -} - #[element] #[derive(Default, Debug)] pub struct Div { - pub instructions: Vec, + pub instructions: Vec>, pub ghosts: Vec, } @@ -99,6 +92,7 @@ impl ElementWidget for Div<'_> { } fn initialize(&mut self, document: &mut Document) { + let mut load_funcs = Vec::new(); for inst in &self.instructions { match inst { Instruction::SetStyle(s) => { @@ -110,8 +104,15 @@ impl ElementWidget for Div<'_> { Instruction::Ghost(idx) => { self.ghosts.push(*idx); } + Instruction::Load(f) => { + load_funcs.push(f.clone()); + } } } + self.instructions.clear(); + for l in load_funcs { + l.call(self, Event::FocusGained, document) + } } } @@ -121,7 +122,7 @@ pub fn div<'a>( id: &'a str, children: Children, style: Style, - instructions: Vec, + instructions: Vec>, ghosts: Vec, ) -> Box> { Box::new(Div { diff --git a/src/ui/styles.rs b/src/ui/styles.rs index b6b6c1c..8a00e89 100644 --- a/src/ui/styles.rs +++ b/src/ui/styles.rs @@ -56,7 +56,6 @@ pub enum Number { Pe(u16), Center, Auto, - Default, } #[derive(PartialEq, Eq, Hash, Debug, Clone)] @@ -152,10 +151,10 @@ impl Default for StyleElement { cursor: (false, Color::NoColor), font: (false, Vec::new()), outline_color: (false, Color::NoColor), - x: (false, Number::Default), - y: (false, Number::Px(0)), - width: (false, Number::Default), - height: (false, Number::Default), + x: (false, Number::Px(0)), + y: (false, Number::Auto), + width: (false, Number::Auto), + height: (false, Number::Auto), visible: (false, true), outline: (false, false), caret: (false, String::from("█")), @@ -322,7 +321,7 @@ impl Number { } } pub fn as_size(&self, written: u16, frame_size: u16, outline: bool) -> u16 { - if *self == Number::Auto || *self == Number::Default { + if *self == Number::Auto { written } else if outline { self.as_size_raw(frame_size) - 2 @@ -335,7 +334,7 @@ impl Number { crate::ui::Number::Px(px) => *px, crate::ui::Number::Pe(pe) => (frame_size * pe) / 100, crate::ui::Number::Center => (frame_size) / 2, - crate::ui::Number::Auto | crate::ui::Number::Default => *used, + crate::ui::Number::Auto => *used, } } }