Added mut Docuemnt and example menu

This commit is contained in:
2024-12-24 01:51:17 +01:00
parent 4823288026
commit e98285a360
4 changed files with 68 additions and 30 deletions
+22 -26
View File
@@ -9,7 +9,7 @@
//! //!
//! ```rust //! ```rust
//! use osui::prelude::*; //! use osui::prelude::*;
//! //!
//! #[component] //! #[component]
//! pub fn App() -> Element { //! pub fn App() -> Element {
//! let count = State::new(0); //! let count = State::new(0);
@@ -17,11 +17,11 @@
//! rsx! { //! rsx! {
//! button { //! button {
//! class: "btn", //! class: "btn",
//! //!
//! on_click: fn(_, _, _) @count { //! on_click: fn(_, _, _) @count {
//! count += 1; //! count += 1;
//! }, //! },
//! //!
//! "The current count is: {count}" //! "The current count is: {count}"
//! } //! }
//! } //! }
@@ -86,7 +86,7 @@ pub trait ElementCore {
pub trait ElementWidget: ElementCore + std::fmt::Debug { pub trait ElementWidget: ElementCore + std::fmt::Debug {
fn render(&self, writer: &mut Writer); fn render(&self, writer: &mut Writer);
fn event(&mut self, event: crossterm::event::Event, document: &Document) { fn event(&mut self, event: crossterm::event::Event, document: &mut Document) {
_ = (event, document) _ = (event, document)
} }
fn initialize(&mut self, document: &mut Document) { fn initialize(&mut self, document: &mut Document) {
@@ -103,11 +103,11 @@ pub trait Component: std::fmt::Debug {
/// Structs /// Structs
////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////
pub struct Handler<T>(pub Arc<Mutex<dyn FnMut(&mut T, crossterm::event::Event, &Document)>>); pub struct Handler<T>(pub Arc<Mutex<dyn FnMut(&mut T, crossterm::event::Event, &mut Document)>>);
pub struct Document { pub struct Document {
element: *mut Element, element: *mut Element,
running: *mut Option<bool>, running: Option<bool>,
pub css: ui::Css, pub css: ui::Css,
} }
@@ -142,23 +142,15 @@ impl Document {
pub fn with_elem(element: &mut Element) -> Document { pub fn with_elem(element: &mut Element) -> Document {
Document { Document {
element, element,
running: &mut None, running: None,
css: HashMap::new(), css: HashMap::new(),
} }
} }
pub fn exit(&self) { pub fn exit(&mut self) {
if !self.running.is_null() { self.running = Some(false);
unsafe {
*self.running = Some(false);
}
}
} }
pub fn restart(&self) { pub fn restart(&mut self) {
if !self.running.is_null() { self.running = Some(true);
unsafe {
*self.running = Some(true);
}
}
} }
pub fn get_root<T>(&self) -> &mut Box<T> { pub fn get_root<T>(&self) -> &mut Box<T> {
unsafe { &mut *(self.element as *mut Element as *mut Box<T>) } unsafe { &mut *(self.element as *mut Element as *mut Box<T>) }
@@ -195,13 +187,17 @@ impl Document {
pub fn set_css(&mut self, css: ui::Css) { pub fn set_css(&mut self, css: ui::Css) {
self.css = css; self.css = css;
} }
pub fn draw(&self, element: Element) { pub fn clear_css(&mut self) {
self.css.clear();
}
pub fn draw(&mut self, element: Element) {
unsafe { unsafe {
*self.element = element; *self.element = element;
(*self.element).initialize(self);
} }
} }
pub fn run(&mut self) -> bool { pub fn run(&mut self) -> bool {
unsafe { *self.running = None } self.running = None;
let element = unsafe { &mut *self.element }; let element = unsafe { &mut *self.element };
element.initialize(self); element.initialize(self);
@@ -212,7 +208,7 @@ impl Document {
crossterm::terminal::enable_raw_mode().unwrap(); crossterm::terminal::enable_raw_mode().unwrap();
utils::clear(); utils::clear();
while unsafe { *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);
} }
@@ -221,7 +217,7 @@ impl Document {
crossterm::terminal::disable_raw_mode().unwrap(); crossterm::terminal::disable_raw_mode().unwrap();
utils::clear(); utils::clear();
unsafe { (*self.running).unwrap() } self.running.unwrap()
} }
} }
@@ -326,12 +322,12 @@ impl Children {
impl<T> Handler<T> { impl<T> Handler<T> {
pub fn new<F>(handler_fn: F) -> Handler<T> pub fn new<F>(handler_fn: F) -> Handler<T>
where where
F: FnMut(&mut T, crossterm::event::Event, &Document) + 'static, F: FnMut(&mut T, crossterm::event::Event, &mut Document) + 'static,
{ {
Handler(Arc::new(Mutex::new(handler_fn))) Handler(Arc::new(Mutex::new(handler_fn)))
} }
pub fn call(&self, s: &mut T, event: Event, document: &Document) { pub fn call(&self, s: &mut T, event: Event, document: &mut Document) {
let mut o = self.0.lock().unwrap(); let mut o = self.0.lock().unwrap();
o(s, event, document); o(s, event, document);
} }
@@ -460,7 +456,7 @@ impl Writer {
impl<T> Default for Handler<T> { impl<T> Default for Handler<T> {
fn default() -> Self { fn default() -> Self {
Handler(Arc::new(Mutex::new( Handler(Arc::new(Mutex::new(
|_: &mut T, _: crossterm::event::Event, _: &Document| {}, |_: &mut T, _: crossterm::event::Event, _: &mut Document| {},
))) )))
} }
} }
+39 -1
View File
@@ -1,4 +1,42 @@
use osui::{examples::login_example::App, prelude::*}; use osui::{examples, prelude::*};
fn main() { fn main() {
launch!(App); launch!(App);
} }
#[component]
pub fn App() {
rsx! {
@SetStyle(app_style())
text { 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" }
}
}
pub fn app_style() -> Css {
css! {
"example" {
outline: true,
y: Auto,
x: 0 px,
}
"example": "hover" {
color: Blue,
outline_color: Red,
}
}
}
+4
View File
@@ -16,6 +16,10 @@ macro_rules! parse_rsx_param {
$elem.instructions.push($($i)::+($($inner)*)); $elem.instructions.push($($i)::+($($inner)*));
osui::parse_rsx_param!($elem, $($rest)*); osui::parse_rsx_param!($elem, $($rest)*);
}; };
($elem:expr, @ $($i:ident)::+ $($rest:tt)*) => {
$elem.instructions.push($($i)::+);
osui::parse_rsx_param!($elem, $($rest)*);
};
($elem:expr, static $text:literal $($other:tt)*) => { ($elem:expr, static $text:literal $($other:tt)*) => {
$elem.children = $crate::Children::StaticText(format!($text $($other)*)); $elem.children = $crate::Children::StaticText(format!($text $($other)*));
+3 -3
View File
@@ -52,7 +52,7 @@ impl ElementWidget for Div<'_> {
} }
} }
fn event(&mut self, event: Event, document: &Document) { fn event(&mut self, event: Event, document: &mut Document) {
if let (Some((child, i)), l) = self.children.get_child_idx() { if let (Some((child, i)), l) = self.children.get_child_idx() {
match event { match event {
Event::Key(k) => match k.code { Event::Key(k) => match k.code {
@@ -121,7 +121,7 @@ impl ElementWidget for Button<'_> {
writer.write(&self.children.get_text()); writer.write(&self.children.get_text());
} }
fn event(&mut self, event: Event, document: &Document) { fn event(&mut self, event: Event, document: &mut Document) {
match event { match event {
Event::Key(key) => { Event::Key(key) => {
if key.code == KeyCode::Enter { if key.code == KeyCode::Enter {
@@ -187,7 +187,7 @@ impl ElementWidget for Input<'_> {
} }
} }
fn event(&mut self, event: crossterm::event::Event, document: &Document) { fn event(&mut self, event: crossterm::event::Event, document: &mut Document) {
match event { match event {
Event::Key(k) => match k.code { Event::Key(k) => match k.code {
KeyCode::Backspace => { KeyCode::Backspace => {