working event loop
This commit is contained in:
+2
-2
@@ -5,7 +5,7 @@ use crossterm::{
|
||||
|
||||
use crate::{
|
||||
utils::{clear, create_frame, flush, get_term_size, hide_cursor, render_to_frame, show_cursor},
|
||||
Element, StateChanger,
|
||||
Element,
|
||||
};
|
||||
use std::{ffi::c_void, ptr::null};
|
||||
|
||||
@@ -49,7 +49,7 @@ fn event(element: &mut Element, event: Event) {
|
||||
if let crossterm::event::Event::Resize(width, height) = event {
|
||||
element.update_data(width as usize, height as usize);
|
||||
}
|
||||
element.event(event, &StateChanger(&mut 0));
|
||||
element.event(event);
|
||||
}
|
||||
|
||||
pub fn run(element: &mut Element) -> bool {
|
||||
|
||||
+6
-57
@@ -41,7 +41,7 @@ pub use app::run;
|
||||
pub mod prelude {
|
||||
pub use crate::ui::Color::*;
|
||||
pub use crate::{self as osui, css, rsx, rsx_elem, ui::*, Handler};
|
||||
pub use crate::{Element, State, StateChanger, Value};
|
||||
pub use crate::{Element, Value};
|
||||
// useful for Element making
|
||||
pub use crate::{run_handler, Children, Component, ElementCore};
|
||||
pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
|
||||
@@ -99,8 +99,8 @@ pub trait Component: ElementCore + std::fmt::Debug {
|
||||
String::new()
|
||||
}
|
||||
|
||||
fn event(&mut self, event: Event, state: &StateChanger) {
|
||||
(_, _) = (event, state)
|
||||
fn event(&mut self, event: Event) {
|
||||
_ = event
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,12 +110,12 @@ pub type Element = Box<dyn Component>;
|
||||
/// Handler Struct
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct Handler<T>(pub Arc<Mutex<dyn FnMut(&mut T, Event, &StateChanger) + Send + Sync>>);
|
||||
pub struct Handler<T>(pub Arc<Mutex<dyn FnMut(&mut T, Event) + Send + Sync>>);
|
||||
|
||||
impl<T> Handler<T> {
|
||||
pub fn new<F>(handler_fn: F) -> Handler<T>
|
||||
where
|
||||
F: FnMut(&mut T, Event, &StateChanger) + 'static + Send + Sync,
|
||||
F: FnMut(&mut T, Event) + 'static + Send + Sync,
|
||||
{
|
||||
Handler(Arc::new(Mutex::new(handler_fn)))
|
||||
}
|
||||
@@ -123,9 +123,7 @@ impl<T> Handler<T> {
|
||||
|
||||
impl<T> Default for Handler<T> {
|
||||
fn default() -> Self {
|
||||
Handler(Arc::new(Mutex::new(
|
||||
|_: &mut T, _: Event, _: &StateChanger| {},
|
||||
)))
|
||||
Handler(Arc::new(Mutex::new(|_: &mut T, _: Event| {})))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,55 +133,6 @@ impl<T> std::fmt::Debug for Handler<T> {
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// State Enum and StateChanger Struct
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum State {
|
||||
Exit,
|
||||
Rebuild,
|
||||
Normal,
|
||||
Hover,
|
||||
Custom(usize),
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn from_u32(s: u32) -> State {
|
||||
match s {
|
||||
0 => State::Exit,
|
||||
1 => State::Rebuild,
|
||||
2 => State::Normal,
|
||||
3 => State::Hover,
|
||||
_ => State::Custom((s - 4) as usize),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_u32(&self) -> u32 {
|
||||
match self {
|
||||
State::Exit => 0,
|
||||
State::Rebuild => 1,
|
||||
State::Normal => 2,
|
||||
State::Hover => 3,
|
||||
State::Custom(s) => (4 + s) as u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StateChanger(*mut u32);
|
||||
|
||||
impl StateChanger {
|
||||
pub fn set_state(&self, s: State) {
|
||||
unsafe {
|
||||
(*self.0) = s.to_u32();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_state(&self) -> State {
|
||||
State::from_u32(unsafe { *self.0 })
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// Children Enum
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
+8
-9
@@ -83,7 +83,7 @@ impl Component for Div<'_> {
|
||||
frame.join("\n")
|
||||
}
|
||||
|
||||
fn event(&mut self, event: Event, state: &crate::StateChanger) {
|
||||
fn event(&mut self, event: Event) {
|
||||
if let Some(styling) = self.styling.clone() {
|
||||
self.set_styling(&styling);
|
||||
}
|
||||
@@ -117,7 +117,7 @@ impl Component for Div<'_> {
|
||||
|
||||
_ => {
|
||||
if let Some(c) = children.get_mut(*child) {
|
||||
c.event(event, state);
|
||||
c.event(event);
|
||||
}
|
||||
*child
|
||||
}
|
||||
@@ -126,7 +126,7 @@ impl Component for Div<'_> {
|
||||
}
|
||||
_ => {
|
||||
if let Some(child) = self.get_child() {
|
||||
child.event(event, state);
|
||||
child.event(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ pub struct Button {
|
||||
|
||||
impl Component for Button<'_> {
|
||||
fn render(&self, focused: bool) -> String {
|
||||
if self.state == "click" {
|
||||
if self.state == "clicked" {
|
||||
return self.style.write("click", &self.children.get_text());
|
||||
}
|
||||
if focused {
|
||||
@@ -153,15 +153,14 @@ impl Component for Button<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn event(&mut self, event: Event, state: &StateChanger) {
|
||||
fn event(&mut self, event: Event) {
|
||||
match event {
|
||||
Event::Key(key) => {
|
||||
if key.code == KeyCode::Enter {
|
||||
let prev_state = state.get_state();
|
||||
run_handler!(self.on_click(event, state));
|
||||
state.set_state(State::Custom(0));
|
||||
self.state = "clicked";
|
||||
run_handler!(self.on_click(event));
|
||||
sleep(100);
|
||||
state.set_state(prev_state);
|
||||
self.state = "";
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
Reference in New Issue
Block a user