Added mut Docuemnt and example menu
This commit is contained in:
+19
-23
@@ -86,7 +86,7 @@ pub trait ElementCore {
|
||||
|
||||
pub trait ElementWidget: ElementCore + std::fmt::Debug {
|
||||
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)
|
||||
}
|
||||
fn initialize(&mut self, document: &mut Document) {
|
||||
@@ -103,11 +103,11 @@ pub trait Component: std::fmt::Debug {
|
||||
/// 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 {
|
||||
element: *mut Element,
|
||||
running: *mut Option<bool>,
|
||||
running: Option<bool>,
|
||||
pub css: ui::Css,
|
||||
}
|
||||
|
||||
@@ -142,23 +142,15 @@ impl Document {
|
||||
pub fn with_elem(element: &mut Element) -> Document {
|
||||
Document {
|
||||
element,
|
||||
running: &mut None,
|
||||
running: None,
|
||||
css: HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn exit(&self) {
|
||||
if !self.running.is_null() {
|
||||
unsafe {
|
||||
*self.running = Some(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn restart(&self) {
|
||||
if !self.running.is_null() {
|
||||
unsafe {
|
||||
*self.running = Some(true);
|
||||
}
|
||||
pub fn exit(&mut self) {
|
||||
self.running = Some(false);
|
||||
}
|
||||
pub fn restart(&mut self) {
|
||||
self.running = Some(true);
|
||||
}
|
||||
pub fn get_root<T>(&self) -> &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) {
|
||||
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 {
|
||||
*self.element = element;
|
||||
(*self.element).initialize(self);
|
||||
}
|
||||
}
|
||||
pub fn run(&mut self) -> bool {
|
||||
unsafe { *self.running = None }
|
||||
self.running = None;
|
||||
let element = unsafe { &mut *self.element };
|
||||
element.initialize(self);
|
||||
|
||||
@@ -212,7 +208,7 @@ impl Document {
|
||||
crossterm::terminal::enable_raw_mode().unwrap();
|
||||
utils::clear();
|
||||
|
||||
while unsafe { *self.running } == None {
|
||||
while self.running == None {
|
||||
self.render();
|
||||
element.event(crossterm::event::read().unwrap(), self);
|
||||
}
|
||||
@@ -221,7 +217,7 @@ impl Document {
|
||||
crossterm::terminal::disable_raw_mode().unwrap();
|
||||
utils::clear();
|
||||
|
||||
unsafe { (*self.running).unwrap() }
|
||||
self.running.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,12 +322,12 @@ impl Children {
|
||||
impl<T> Handler<T> {
|
||||
pub fn new<F>(handler_fn: F) -> Handler<T>
|
||||
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)))
|
||||
}
|
||||
|
||||
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();
|
||||
o(s, event, document);
|
||||
}
|
||||
@@ -460,7 +456,7 @@ impl Writer {
|
||||
impl<T> Default for Handler<T> {
|
||||
fn default() -> Self {
|
||||
Handler(Arc::new(Mutex::new(
|
||||
|_: &mut T, _: crossterm::event::Event, _: &Document| {},
|
||||
|_: &mut T, _: crossterm::event::Event, _: &mut Document| {},
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
+39
-1
@@ -1,4 +1,42 @@
|
||||
use osui::{examples::login_example::App, prelude::*};
|
||||
use osui::{examples, prelude::*};
|
||||
fn main() {
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ macro_rules! parse_rsx_param {
|
||||
$elem.instructions.push($($i)::+($($inner)*));
|
||||
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.children = $crate::Children::StaticText(format!($text $($other)*));
|
||||
|
||||
+3
-3
@@ -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() {
|
||||
match event {
|
||||
Event::Key(k) => match k.code {
|
||||
@@ -121,7 +121,7 @@ impl ElementWidget for Button<'_> {
|
||||
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 {
|
||||
Event::Key(key) => {
|
||||
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 {
|
||||
Event::Key(k) => match k.code {
|
||||
KeyCode::Backspace => {
|
||||
|
||||
Reference in New Issue
Block a user