added instructions
This commit is contained in:
@@ -4,6 +4,8 @@ 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) {
|
||||
|
||||
+7
-2
@@ -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<T>(pub Arc<Mutex<dyn FnMut(&mut T, crossterm::event::Event, &
|
||||
pub struct Document {
|
||||
element: *mut Element,
|
||||
running: *mut Option<bool>,
|
||||
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);
|
||||
|
||||
+2
-5
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
};
|
||||
|
||||
@@ -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<std::collections::HashMap<StyleName, Style>>,
|
||||
pub instructions: Vec<Instruction>,
|
||||
}
|
||||
|
||||
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]
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user