some fine tuning
This commit is contained in:
@@ -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
@@ -30,6 +30,7 @@ use crossterm::event::Event;
|
|||||||
|
|
||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod css;
|
pub mod css;
|
||||||
|
pub mod examples;
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
pub mod rsx;
|
pub mod rsx;
|
||||||
pub mod ui;
|
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::{self as osui, css, rsx, rsx_elem, ui::*, Handler};
|
||||||
pub use crate::{style, Command, Document, Element, Value};
|
pub use crate::{style, Command, Document, Element, Value};
|
||||||
// useful for Element making
|
// 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 use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
|
||||||
pub fn sleep(ms: u64) {
|
pub fn sleep(ms: u64) {
|
||||||
std::thread::sleep(std::time::Duration::from_millis(ms));
|
std::thread::sleep(std::time::Duration::from_millis(ms));
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ macro_rules! val {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! run_handler {
|
macro_rules! call {
|
||||||
($self:ident.$handler:ident ($($inner:tt)*)) => {{
|
($self:ident.$handler:ident ($($inner:tt)*)) => {{
|
||||||
let a = std::sync::Arc::clone(&$self.$handler.0);
|
let a = std::sync::Arc::clone(&$self.$handler.0);
|
||||||
let mut o = a.lock().unwrap();
|
let mut o = a.lock().unwrap();
|
||||||
|
|||||||
+2
-25
@@ -1,29 +1,6 @@
|
|||||||
use osui::prelude::*;
|
use osui::prelude::*;
|
||||||
|
use osui::examples;
|
||||||
pub struct App {}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
while osui::run(&mut app()) {}
|
while osui::run(&mut examples::event_logger()) {}
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-2
@@ -26,7 +26,9 @@ use osui_element::{elem_fn, element};
|
|||||||
#[element]
|
#[element]
|
||||||
#[elem_fn]
|
#[elem_fn]
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
pub struct Text {}
|
pub struct Text {
|
||||||
|
pub on_event: Handler<Text<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
impl ElementWidget for Text<'_> {
|
impl ElementWidget for Text<'_> {
|
||||||
fn render(&self, _: bool) -> String {
|
fn render(&self, _: bool) -> String {
|
||||||
@@ -36,6 +38,9 @@ impl ElementWidget for Text<'_> {
|
|||||||
String::new()
|
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
|
/// 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 {
|
if key.code == KeyCode::Enter {
|
||||||
document.render();
|
document.render();
|
||||||
self.state = "clicked";
|
self.state = "clicked";
|
||||||
run_handler!(self.on_click(event, document));
|
call!(self.on_click(event, document));
|
||||||
document.render();
|
document.render();
|
||||||
sleep(100);
|
sleep(100);
|
||||||
self.state = "";
|
self.state = "";
|
||||||
|
|||||||
Reference in New Issue
Block a user