Added a close function #18
This commit is contained in:
+8
-4
@@ -1,6 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use osui::prelude::*;
|
||||
|
||||
pub fn app() -> Rsx {
|
||||
pub fn app(screen: Arc<Screen>) -> Rsx {
|
||||
let count = use_state(0);
|
||||
|
||||
std::thread::spawn({
|
||||
@@ -12,9 +14,11 @@ pub fn app() -> Rsx {
|
||||
});
|
||||
|
||||
rsx! {
|
||||
@Handler::new(|_, _: &crossterm::event::Event| {
|
||||
panic!("idk lol");
|
||||
});
|
||||
@Handler::new({
|
||||
let screen = screen.clone();
|
||||
move |_, _: &crossterm::event::Event| {
|
||||
screen.close();
|
||||
}});
|
||||
FlexRow {
|
||||
FlexCol {
|
||||
@Transform::new().padding(2, 2);
|
||||
|
||||
@@ -15,6 +15,10 @@ impl Extension for InputExtension {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn on_close(&mut self, _screen: std::sync::Arc<crate::Screen>) {
|
||||
crossterm::terminal::disable_raw_mode().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::extensions::Event for crossterm::event::Event {
|
||||
|
||||
@@ -22,6 +22,7 @@ use crate::{
|
||||
|
||||
pub trait Extension {
|
||||
fn init(&mut self, _screen: Arc<Screen>) {}
|
||||
fn on_close(&mut self, _screen: Arc<Screen>) {}
|
||||
fn render_widget(&mut self, _scope: &mut RenderScope, _widget: &Arc<Widget>) {}
|
||||
}
|
||||
|
||||
|
||||
+19
-1
@@ -75,6 +75,8 @@ pub struct Screen {
|
||||
pub widgets: Mutex<Vec<Arc<Widget>>>,
|
||||
/// Registered extensions for the screen.
|
||||
extensions: Mutex<Vec<Arc<Mutex<Box<dyn Extension + Send + Sync>>>>>,
|
||||
/// If it's still running
|
||||
running: Mutex<bool>,
|
||||
}
|
||||
|
||||
event!(RenderWrapperEvent(*mut RenderScope));
|
||||
@@ -99,6 +101,7 @@ impl Screen {
|
||||
Arc::new(Self {
|
||||
widgets: Mutex::new(Vec::new()),
|
||||
extensions: Mutex::new(Vec::new()),
|
||||
running: Mutex::new(true),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -159,10 +162,12 @@ impl Screen {
|
||||
|
||||
utils::hide_cursor()?;
|
||||
|
||||
loop {
|
||||
while *self.running.lock().unwrap() {
|
||||
self.render()?;
|
||||
std::thread::sleep(std::time::Duration::from_millis(28));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Renders all widgets and applies extensions.
|
||||
@@ -213,4 +218,17 @@ impl Screen {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Closes the loop and calls `on_close` in the extensions
|
||||
pub fn close(self: &Arc<Self>) {
|
||||
*self.running.lock().unwrap() = false;
|
||||
|
||||
utils::show_cursor().unwrap();
|
||||
|
||||
utils::clear().unwrap();
|
||||
|
||||
for ext in self.extensions.lock().unwrap().iter() {
|
||||
ext.lock().unwrap().on_close(self.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ fn main() -> std::io::Result<()> {
|
||||
let screen = Screen::new();
|
||||
screen.extension(InputExtension);
|
||||
|
||||
demos::app().draw(&screen);
|
||||
demos::app(screen.clone()).draw(&screen);
|
||||
|
||||
screen.run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user