Tokio app structure
This commit is contained in:
Generated
+18
@@ -2,6 +2,12 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pin-project-lite"
|
||||||
|
version = "0.2.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pulse-trader"
|
name = "pulse-trader"
|
||||||
version = "0.1.0-alpha.0"
|
version = "0.1.0-alpha.0"
|
||||||
@@ -12,3 +18,15 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "pulse-ui"
|
name = "pulse-ui"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio"
|
||||||
|
version = "1.52.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe"
|
||||||
|
dependencies = [
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
tokio = { version = "1.52.3", features = ["sync"] }
|
||||||
|
|||||||
+29
-14
@@ -1,31 +1,46 @@
|
|||||||
|
use std::any::Any;
|
||||||
|
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
pub mod layout;
|
pub mod layout;
|
||||||
pub mod render;
|
pub mod render;
|
||||||
pub mod unit;
|
pub mod unit;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Context {
|
||||||
|
tx: mpsc::Sender<Box<dyn Any>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Refresh;
|
||||||
|
|
||||||
pub trait App {
|
pub trait App {
|
||||||
// State
|
// State
|
||||||
fn init(&mut self);
|
#[allow(async_fn_in_trait)]
|
||||||
fn update(&mut self);
|
async fn init(&mut self, ctx: Context);
|
||||||
fn tick(&self);
|
#[allow(async_fn_in_trait)]
|
||||||
|
async fn update(&mut self, event: Box<dyn Any>);
|
||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
fn layout(&self) -> layout::Allocation;
|
fn layout(&self) -> layout::Allocation;
|
||||||
fn render(&mut self, layout: layout::Allocation);
|
fn render(&mut self, layout: layout::Allocation);
|
||||||
|
|
||||||
fn refresh(&mut self) {
|
|
||||||
self.render(self.layout());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Running
|
pub struct AppHandler<A: App> {
|
||||||
fn run(&mut self) {
|
pub app: A,
|
||||||
self.init();
|
}
|
||||||
self.refresh();
|
|
||||||
|
|
||||||
loop {
|
impl<A: App> AppHandler<A> {
|
||||||
self.tick();
|
pub async fn run(&mut self) {
|
||||||
self.update();
|
let (tx, mut rx) = mpsc::channel(100);
|
||||||
self.refresh();
|
|
||||||
|
self.app.init(Context { tx }).await;
|
||||||
|
|
||||||
|
self.app.render(self.app.layout());
|
||||||
|
|
||||||
|
while let Some(event) = rx.recv().await {
|
||||||
|
self.app.update(event).await;
|
||||||
|
self.app.render(self.app.layout());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user