From bde4438663417f652446d2300f0a37f01f599bfa Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 17 Jan 2025 22:13:42 +0100 Subject: [PATCH] few changes --- src/console.rs | 13 ++++++++ src/lib.rs | 3 ++ src/main.rs | 18 ++--------- src/state.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 src/state.rs diff --git a/src/console.rs b/src/console.rs index c97b909..624966e 100644 --- a/src/console.rs +++ b/src/console.rs @@ -15,6 +15,7 @@ pub fn init() -> crate::Result { crossterm::terminal::enable_raw_mode()?; crate::utils::clear()?; crate::utils::hide_cursor()?; + crossterm::execute!(std::io::stdout(), crossterm::event::EnableMouseCapture)?; Ok(Console(Frame::new(crossterm::terminal::size()?))) } @@ -29,6 +30,18 @@ impl Console { crate::utils::clear()?; f(&self.0) } + + pub fn run(&mut self, ui: Element) -> crate::Result<()> { + loop { + self.draw(ui.clone())?; + let event = read()?; + if let Event::Resize(w, h) = event { + self.0.area.width = w; + self.0.area.height = h; + } + + } + } } pub fn read() -> crate::Result { diff --git a/src/lib.rs b/src/lib.rs index 69e0e59..d9b4f71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,9 @@ pub type Element = std::sync::Arc crate::Result<()>>; pub trait Widget { fn render(&self) -> String; + fn event(&mut self, event: console::Event) { + _ = event; + } } #[derive(Debug, Clone, Copy)] diff --git a/src/main.rs b/src/main.rs index 8cea794..3713ea6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,10 @@ use osui::prelude::*; fn main() -> osui::Result<()> { - let con = console::init()?; + let mut con = console::init()?; - let (ui, count) = app(); - - loop { - let count = count.copy_state(); - let mut count1 = count.copy_state(); - con.draw(std::sync::Arc::clone(&ui))?; - - if let Event::Key(KeyEvent { code, .. }) = console::read()? { - if code == KeyCode::Char('q') { - break; - } - } - count1 += 1; - } + let (ui, _count) = app(); + con.run(ui).unwrap(); console::end() } diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..af6c13f --- /dev/null +++ b/src/state.rs @@ -0,0 +1,85 @@ +pub struct State(pub(crate) *mut T); + +impl std::fmt::Display for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", unsafe { &*self.0 }) + } +} + +impl std::fmt::Debug for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "State({:?})", unsafe { &*self.0 }) + } +} + +//// State ops //// + +impl + Clone> std::ops::Add for State { + type Output = T; + + fn add(self, rhs: T) -> Self::Output { + unsafe { + let lhs = &mut *self.0; + lhs.clone() + rhs + } + } +} + +impl + Clone> std::ops::Sub for State { + type Output = T; + + fn sub(self, rhs: T) -> Self::Output { + unsafe { + let lhs = &mut *self.0; + lhs.clone() - rhs + } + } +} + +impl + Clone> std::ops::Div for State { + type Output = T; + + fn div(self, rhs: T) -> Self::Output { + unsafe { + let lhs = &mut *self.0; + lhs.clone() / rhs + } + } +} + +impl std::ops::AddAssign for State { + fn add_assign(&mut self, rhs: T) { + unsafe { *self.0 += rhs } + } +} + +impl std::ops::SubAssign for State { + fn sub_assign(&mut self, rhs: T) { + unsafe { *self.0 -= rhs } + } +} + +impl std::ops::Deref for State { + type Target = T; + fn deref(&self) -> &Self::Target { + unsafe { &*self.0 } + } +} + +impl std::ops::DerefMut for State { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { &mut *self.0 } + } +} + +impl State { + pub fn copy_state(&self) -> Self { + Self(self.0) + } +} + +impl> PartialEq for State { + fn eq(&self, other: &T) -> bool { + unsafe { &*self.0 == other } + } +}