From 1c67eaa3a2dc5542261e8380661a3bce4b872384 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Wed, 18 Dec 2024 23:06:51 +0100 Subject: [PATCH] added instructions --- src/examples.rs | 4 +++- src/lib.rs | 9 +++++++-- src/main.rs | 7 ++----- src/rsx.rs | 5 +++++ src/ui/mod.rs | 16 ++++++++++++++++ src/ui/styles.rs | 2 -- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/examples.rs b/src/examples.rs index ee3647f..85bac1d 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -4,10 +4,12 @@ pub fn todo_app() -> Element { let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"]; rsx! { + @SetStyle(todo_styling()) + text { class: "title", static "Todo list app made with OSUI" } text { class: "title", "{}", "-".repeat(crossterm::terminal::size().unwrap().0 as usize) } for (t in todo) { - ersx!( button{ class: "todo", on_click: fn(btn: &mut Button, _, _) { + ersx!(button { class: "todo", on_click: fn(btn: &mut Button, _, _) { btn.class = if btn.class == "todo" { "complete" } else { diff --git a/src/lib.rs b/src/lib.rs index 088098b..726041b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,7 @@ 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::{self as osui, css, ersx, launch, rsx, ui::*, Handler}; pub use crate::{call, Children, Element, ElementCore, ElementWidget}; @@ -69,6 +70,9 @@ pub trait ElementWidget: ElementCore + std::fmt::Debug { fn event(&mut self, event: crossterm::event::Event, document: &Document) { _ = (event, document) } + fn initialize(&mut self, document: &mut Document) { + _ = document + } } ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -80,7 +84,7 @@ pub struct Handler(pub Arc, - css: ui::Css, + pub css: ui::Css, } #[derive(Debug)] @@ -168,9 +172,10 @@ impl Document { pub fn draw(&mut self, element: &mut Element) { self.element = element; } - pub fn run(&self) -> bool { + pub fn run(&mut self) -> bool { unsafe { *self.running = None } let element = unsafe { &mut *self.element }; + element.initialize(self); // Send initial event element.event(crossterm::event::Event::FocusGained, self); diff --git a/src/main.rs b/src/main.rs index b391d23..5cd4e91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,5 @@ -use osui::{ - examples::{todo_app, todo_styling}, - launch, -}; +use osui::{examples::todo_app, launch}; fn main() { - launch!(todo_app(), todo_styling()); + launch!(todo_app()); } diff --git a/src/rsx.rs b/src/rsx.rs index 3247413..988e9d8 100644 --- a/src/rsx.rs +++ b/src/rsx.rs @@ -12,6 +12,11 @@ macro_rules! check_expr { } #[macro_export] macro_rules! parse_rsx_param { + ($elem:expr, @ $($i:ident)::+($($inner:tt)*) $($rest:tt)*) => { + $elem.instructions.push($($i)::+($($inner)*)); + osui::parse_rsx_param!($elem, $($rest)*); + }; + ($elem:expr, static $text:expr) => { $elem.children.set_static_force(format!($text)) }; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 41d53b9..c80f894 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -20,11 +20,17 @@ impl ElementWidget for Text<'_> { } } +#[derive(Debug)] +pub enum Instruction { + SetStyle(Css), +} + #[element] #[elem_fn] #[derive(Default, Debug)] pub struct Div { pub styling: Option>, + pub instructions: Vec, } impl ElementWidget for Div<'_> { @@ -66,6 +72,16 @@ impl ElementWidget for Div<'_> { } } } + + fn initialize(&mut self, document: &mut Document) { + for inst in &self.instructions { + match inst { + Instruction::SetStyle(s) => { + document.css.extend(s.clone().into_iter()); + } + } + } + } } #[element] diff --git a/src/ui/styles.rs b/src/ui/styles.rs index 50dc162..d096e62 100644 --- a/src/ui/styles.rs +++ b/src/ui/styles.rs @@ -62,8 +62,6 @@ pub enum Number { #[derive(PartialEq, Eq, Hash, Debug, Clone)] pub enum StyleName { Class(String), - Id(String), - Component(String), ClassState(String, String), }