added instructions

This commit is contained in:
2024-12-18 23:06:51 +01:00
parent 8182e70ca4
commit 1c67eaa3a2
6 changed files with 33 additions and 10 deletions
+3 -1
View File
@@ -4,10 +4,12 @@ pub fn todo_app() -> Element {
let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"]; let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"];
rsx! { rsx! {
@SetStyle(todo_styling())
text { class: "title", static "Todo list app made with OSUI" } text { class: "title", static "Todo list app made with OSUI" }
text { class: "title", "{}", "-".repeat(crossterm::terminal::size().unwrap().0 as usize) } text { class: "title", "{}", "-".repeat(crossterm::terminal::size().unwrap().0 as usize) }
for (t in todo) { 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" { btn.class = if btn.class == "todo" {
"complete" "complete"
} else { } else {
+7 -2
View File
@@ -37,6 +37,7 @@ pub mod utils;
pub mod prelude { pub mod prelude {
pub use crate::ui::Color::*; pub use crate::ui::Color::*;
pub use crate::ui::Font::*; pub use crate::ui::Font::*;
pub use crate::ui::Instruction::*;
pub use crate::ui::Number::{Auto, Center}; pub use crate::ui::Number::{Auto, Center};
pub use crate::{self as osui, css, ersx, launch, rsx, ui::*, Handler}; pub use crate::{self as osui, css, ersx, launch, rsx, ui::*, Handler};
pub use crate::{call, Children, Element, ElementCore, ElementWidget}; 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) { fn event(&mut self, event: crossterm::event::Event, document: &Document) {
_ = (event, document) _ = (event, document)
} }
fn initialize(&mut self, document: &mut Document) {
_ = document
}
} }
////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////
@@ -80,7 +84,7 @@ pub struct Handler<T>(pub Arc<Mutex<dyn FnMut(&mut T, crossterm::event::Event, &
pub struct Document { pub struct Document {
element: *mut Element, element: *mut Element,
running: *mut Option<bool>, running: *mut Option<bool>,
css: ui::Css, pub css: ui::Css,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -168,9 +172,10 @@ impl Document {
pub fn draw(&mut self, element: &mut Element) { pub fn draw(&mut self, element: &mut Element) {
self.element = element; self.element = element;
} }
pub fn run(&self) -> bool { pub fn run(&mut self) -> bool {
unsafe { *self.running = None } unsafe { *self.running = None }
let element = unsafe { &mut *self.element }; let element = unsafe { &mut *self.element };
element.initialize(self);
// Send initial event // Send initial event
element.event(crossterm::event::Event::FocusGained, self); element.event(crossterm::event::Event::FocusGained, self);
+2 -5
View File
@@ -1,8 +1,5 @@
use osui::{ use osui::{examples::todo_app, launch};
examples::{todo_app, todo_styling},
launch,
};
fn main() { fn main() {
launch!(todo_app(), todo_styling()); launch!(todo_app());
} }
+5
View File
@@ -12,6 +12,11 @@ macro_rules! check_expr {
} }
#[macro_export] #[macro_export]
macro_rules! parse_rsx_param { 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:expr, static $text:expr) => {
$elem.children.set_static_force(format!($text)) $elem.children.set_static_force(format!($text))
}; };
+16
View File
@@ -20,11 +20,17 @@ impl ElementWidget for Text<'_> {
} }
} }
#[derive(Debug)]
pub enum Instruction {
SetStyle(Css),
}
#[element] #[element]
#[elem_fn] #[elem_fn]
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct Div { pub struct Div {
pub styling: Option<std::collections::HashMap<StyleName, Style>>, pub styling: Option<std::collections::HashMap<StyleName, Style>>,
pub instructions: Vec<Instruction>,
} }
impl ElementWidget for Div<'_> { 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] #[element]
-2
View File
@@ -62,8 +62,6 @@ pub enum Number {
#[derive(PartialEq, Eq, Hash, Debug, Clone)] #[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum StyleName { pub enum StyleName {
Class(String), Class(String),
Id(String),
Component(String),
ClassState(String, String), ClassState(String, String),
} }