This commit is contained in:
2024-12-24 20:55:20 +01:00
parent ea7e9c1e1c
commit 7e2106cae5
5 changed files with 50 additions and 52 deletions
+16 -6
View File
@@ -55,10 +55,10 @@ 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::Instruction::*;
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::{style, Component, Document, State}; pub use crate::{style, Component, Document, Instruction, State};
pub use crate::{Children, Element, ElementCore, ElementWidget}; pub use crate::{Children, Element, ElementCore, ElementWidget};
pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind}; pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
pub use osui_element::component; pub use osui_element::component;
@@ -197,17 +197,19 @@ impl Document {
} }
} }
pub fn run(&mut self) -> bool { pub fn run(&mut self) -> bool {
// Set up the screen
utils::hide_cursor();
crossterm::terminal::enable_raw_mode().unwrap();
utils::clear();
self.running = None; self.running = None;
let element = unsafe { &mut *self.element }; let element = unsafe { &mut *self.element };
self.render();
element.initialize(self); element.initialize(self);
// Send initial event // Send initial event
element.event(crossterm::event::Event::FocusGained, self); element.event(crossterm::event::Event::FocusGained, self);
utils::hide_cursor();
crossterm::terminal::enable_raw_mode().unwrap();
utils::clear();
while self.running == None { while self.running == None {
self.render(); self.render();
element.event(crossterm::event::read().unwrap(), self); element.event(crossterm::event::read().unwrap(), self);
@@ -509,3 +511,11 @@ impl<T: PartialEq> PartialEq<T> for State<T> {
self.use_state().ne(other) self.use_state().ne(other)
} }
} }
#[derive(Debug)]
pub enum Instruction<'a> {
SetStyle(ui::Css),
SetChild(usize),
Ghost(usize),
Load(Handler<ui::Div<'a>>),
}
+8 -29
View File
@@ -1,4 +1,5 @@
use osui::{examples, prelude::*}; use osui::prelude::*;
fn main() { fn main() {
launch!(App); launch!(App);
} }
@@ -6,34 +7,12 @@ fn main() {
#[component] #[component]
pub fn App() { pub fn App() {
rsx! { rsx! {
@SetChild(1) async {
@SetStyle(css! { sleep(3000);
example { ersx!(text {
outline: true, "OK"
y: Auto,
x: 0 px,
}
example: "hover" {
color: Blue,
outline_color: Red,
}
}) })
}
%text { class: "red-bg", static "Select an example" } text { "Hello world!" }
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" }
} }
} }
+9
View File
@@ -12,6 +12,15 @@ macro_rules! check_expr {
} }
#[macro_export] #[macro_export]
macro_rules! parse_rsx_param { 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:expr, %$elem_path:path { $($inner:tt)* } $($rest:tt)*) => {
$elem.ghosts.push($elem.ghosts.len()); $elem.ghosts.push($elem.ghosts.len());
if let $crate::Children::Children(children, _) = &mut $elem.children { if let $crate::Children::Children(children, _) = &mut $elem.children {
+10 -9
View File
@@ -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] #[element]
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct Div { pub struct Div {
pub instructions: Vec<Instruction>, pub instructions: Vec<Instruction<'a>>,
pub ghosts: Vec<usize>, pub ghosts: Vec<usize>,
} }
@@ -99,6 +92,7 @@ impl ElementWidget for Div<'_> {
} }
fn initialize(&mut self, document: &mut Document) { fn initialize(&mut self, document: &mut Document) {
let mut load_funcs = Vec::new();
for inst in &self.instructions { for inst in &self.instructions {
match inst { match inst {
Instruction::SetStyle(s) => { Instruction::SetStyle(s) => {
@@ -110,9 +104,16 @@ impl ElementWidget for Div<'_> {
Instruction::Ghost(idx) => { Instruction::Ghost(idx) => {
self.ghosts.push(*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)
}
}
} }
#[component] #[component]
@@ -121,7 +122,7 @@ pub fn div<'a>(
id: &'a str, id: &'a str,
children: Children, children: Children,
style: Style, style: Style,
instructions: Vec<Instruction>, instructions: Vec<Instruction<'a>>,
ghosts: Vec<usize>, ghosts: Vec<usize>,
) -> Box<Div<'a>> { ) -> Box<Div<'a>> {
Box::new(Div { Box::new(Div {
+6 -7
View File
@@ -56,7 +56,6 @@ pub enum Number {
Pe(u16), Pe(u16),
Center, Center,
Auto, Auto,
Default,
} }
#[derive(PartialEq, Eq, Hash, Debug, Clone)] #[derive(PartialEq, Eq, Hash, Debug, Clone)]
@@ -152,10 +151,10 @@ impl Default for StyleElement {
cursor: (false, Color::NoColor), cursor: (false, Color::NoColor),
font: (false, Vec::new()), font: (false, Vec::new()),
outline_color: (false, Color::NoColor), outline_color: (false, Color::NoColor),
x: (false, Number::Default), x: (false, Number::Px(0)),
y: (false, Number::Px(0)), y: (false, Number::Auto),
width: (false, Number::Default), width: (false, Number::Auto),
height: (false, Number::Default), height: (false, Number::Auto),
visible: (false, true), visible: (false, true),
outline: (false, false), outline: (false, false),
caret: (false, String::from("█")), caret: (false, String::from("█")),
@@ -322,7 +321,7 @@ impl Number {
} }
} }
pub fn as_size(&self, written: u16, frame_size: u16, outline: bool) -> u16 { 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 written
} else if outline { } else if outline {
self.as_size_raw(frame_size) - 2 self.as_size_raw(frame_size) - 2
@@ -335,7 +334,7 @@ impl Number {
crate::ui::Number::Px(px) => *px, crate::ui::Number::Px(px) => *px,
crate::ui::Number::Pe(pe) => (frame_size * pe) / 100, crate::ui::Number::Pe(pe) => (frame_size * pe) / 100,
crate::ui::Number::Center => (frame_size) / 2, crate::ui::Number::Center => (frame_size) / 2,
crate::ui::Number::Auto | crate::ui::Number::Default => *used, crate::ui::Number::Auto => *used,
} }
} }
} }