async
This commit is contained in:
+16
-6
@@ -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<T: PartialEq> PartialEq<T> for State<T> {
|
||||
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
@@ -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,
|
||||
}
|
||||
async {
|
||||
sleep(3000);
|
||||
ersx!(text {
|
||||
"OK"
|
||||
})
|
||||
|
||||
%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" }
|
||||
}
|
||||
text { "Hello world!" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+10
-9
@@ -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<Instruction>,
|
||||
pub instructions: Vec<Instruction<'a>>,
|
||||
pub ghosts: Vec<usize>,
|
||||
}
|
||||
|
||||
@@ -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,9 +104,16 @@ 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
@@ -121,7 +122,7 @@ pub fn div<'a>(
|
||||
id: &'a str,
|
||||
children: Children,
|
||||
style: Style,
|
||||
instructions: Vec<Instruction>,
|
||||
instructions: Vec<Instruction<'a>>,
|
||||
ghosts: Vec<usize>,
|
||||
) -> Box<Div<'a>> {
|
||||
Box::new(Div {
|
||||
|
||||
+6
-7
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user