Fix input events and drop spawning tokio

This commit is contained in:
2026-07-14 22:29:45 +02:00
parent 08edc981ee
commit b8162ebe3c
3 changed files with 16 additions and 8 deletions
+2
View File
@@ -38,9 +38,11 @@ pub async fn run<A: App>(create_app: impl FnOnce(&state::Context) -> A) {
crossterm::terminal::enable_raw_mode().unwrap(); crossterm::terminal::enable_raw_mode().unwrap();
tokio::spawn(async move { tokio::spawn(async move {
loop {
tx.send(Box::new(crossterm::event::read().unwrap())) tx.send(Box::new(crossterm::event::read().unwrap()))
.await .await
.unwrap(); .unwrap();
}
}); });
let mut app = create_app(&ctx); let mut app = create_app(&ctx);
+3 -1
View File
@@ -59,7 +59,9 @@ impl<'a, T> std::ops::DerefMut for StateGuard<'a, T> {
impl<'a, T> Drop for StateGuard<'a, T> { impl<'a, T> Drop for StateGuard<'a, T> {
fn drop(&mut self) { fn drop(&mut self) {
if self.is_mutated { if self.is_mutated {
self.tx.blocking_send(Box::new(Refresh)).unwrap() let tx = self.tx.clone();
tokio::spawn(async move { tx.send(Box::new(Refresh)).await.unwrap() });
} }
} }
} }
+8 -4
View File
@@ -1,9 +1,9 @@
use std::any::Any; use std::any::Any;
use pulse_ui::{ use pulse_ui::{
App, App, get_screen,
layout::{LayoutItem, layout}, layout::{LayoutItem, layout},
state::State, state::{Refresh, State},
unit::Size, unit::Size,
widget::outline::Outline, widget::outline::Outline,
}; };
@@ -16,7 +16,11 @@ impl App for PulseTradeApp {
async fn init(&mut self, ctx: &pulse_ui::state::Context) {} async fn init(&mut self, ctx: &pulse_ui::state::Context) {}
async fn update(&mut self, ctx: &pulse_ui::state::Context, event: Box<dyn Any + Send + Sync>) { async fn update(&mut self, ctx: &pulse_ui::state::Context, event: Box<dyn Any + Send + Sync>) {
ctx.close().await if let Some(Refresh) = event.downcast_ref() {
return;
}
*self.count.lock().await += 1;
} }
async fn layout(&self) -> pulse_ui::layout::LayoutItem { async fn layout(&self) -> pulse_ui::layout::LayoutItem {
@@ -33,7 +37,7 @@ impl App for PulseTradeApp {
} }
async fn render(&mut self, layout: pulse_ui::layout::Allocation) { async fn render(&mut self, layout: pulse_ui::layout::Allocation) {
layout.draw(1, "Yo"); layout.draw(1, format!("{:?}", *self.count.lock().await).as_str());
layout.draw_frame(1, Outline); layout.draw_frame(1, Outline);
layout.draw_frame(2, Outline); layout.draw_frame(2, Outline);
} }