some fine tuning

This commit is contained in:
2024-11-22 12:42:03 +01:00
parent 7f1437e779
commit 129725dce2
5 changed files with 22 additions and 30 deletions
+9
View File
@@ -0,0 +1,9 @@
use crate::prelude::*;
pub fn event_logger() -> Element {
rsx_elem! {
text { on_event: Handler::new(|txt: &mut Text, e, _| {
txt.children.set_text(&format!("current event: {e:?}"));
}), "current event: None" }
}
}
+2 -1
View File
@@ -30,6 +30,7 @@ use crossterm::event::Event;
pub mod app;
pub mod css;
pub mod examples;
pub mod macros;
pub mod rsx;
pub mod ui;
@@ -45,7 +46,7 @@ pub mod prelude {
pub use crate::{self as osui, css, rsx, rsx_elem, ui::*, Handler};
pub use crate::{style, Command, Document, Element, Value};
// useful for Element making
pub use crate::{run_handler, Children, ElementCore, ElementWidget};
pub use crate::{call, Children, ElementCore, ElementWidget};
pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
pub fn sleep(ms: u64) {
std::thread::sleep(std::time::Duration::from_millis(ms));
+2 -2
View File
@@ -6,7 +6,7 @@ macro_rules! val {
}
#[macro_export]
macro_rules! run_handler {
macro_rules! call {
($self:ident.$handler:ident ($($inner:tt)*)) => {{
let a = std::sync::Arc::clone(&$self.$handler.0);
let mut o = a.lock().unwrap();
@@ -15,4 +15,4 @@ macro_rules! run_handler {
($self:ident.$handler:ident) => {{
&$self.$handler;
}};
}
}
+2 -25
View File
@@ -1,29 +1,6 @@
use osui::prelude::*;
pub struct App {}
use osui::examples;
fn main() {
while osui::run(&mut app()) {}
}
fn app() -> Element {
rsx! { styling: Some(styles()),
button { class: "btn", on_click: Handler::new(|_, _, document| {
if let Some(txt) = document.get_element_by_id::<Text>("txt") {
txt.children.set_text("edited");
}
}), "Hello, World!" }
text { id: "txt", y: 2, "Some text!" }
}
}
fn styles() -> Css {
css! {
.btn: clicked {
color: Blue
}
.btn: hover {
color: Red
}
}
while osui::run(&mut examples::event_logger()) {}
}
+7 -2
View File
@@ -26,7 +26,9 @@ use osui_element::{elem_fn, element};
#[element]
#[elem_fn]
#[derive(Default, Debug)]
pub struct Text {}
pub struct Text {
pub on_event: Handler<Text<'a>>,
}
impl ElementWidget for Text<'_> {
fn render(&self, _: bool) -> String {
@@ -36,6 +38,9 @@ impl ElementWidget for Text<'_> {
String::new()
}
}
fn event(&mut self, event: Event, document: &Document) {
call!(self.on_event(event, document));
}
}
/// The `Div` element represents a container for other UI elements. It can hold child components
@@ -161,7 +166,7 @@ impl ElementWidget for Button<'_> {
if key.code == KeyCode::Enter {
document.render();
self.state = "clicked";
run_handler!(self.on_click(event, document));
call!(self.on_click(event, document));
document.render();
sleep(100);
self.state = "";