Added after render and render on extension with child handlers #26
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use crate::extensions::{Extension, ExtensionContext};
|
||||
use crate::extensions::{Extension, Context};
|
||||
|
||||
pub struct InputExtension;
|
||||
|
||||
impl Extension for InputExtension {
|
||||
fn init(&mut self, ctx: &ExtensionContext) {
|
||||
fn init(&mut self, ctx: &Context) {
|
||||
let ctx = ctx.clone();
|
||||
crossterm::terminal::enable_raw_mode().unwrap();
|
||||
std::thread::spawn(move || loop {
|
||||
|
||||
+24
-5
@@ -21,10 +21,17 @@ use crate::{
|
||||
};
|
||||
|
||||
pub trait Extension {
|
||||
fn init(&mut self, _ctx: &ExtensionContext) {}
|
||||
fn event(&mut self, _ctx: &ExtensionContext, _event: &dyn Event) {}
|
||||
fn init(&mut self, _ctx: &Context) {}
|
||||
fn event(&mut self, _ctx: &Context, _event: &dyn Event) {}
|
||||
fn on_close(&mut self) {}
|
||||
fn render_widget(&mut self, _scope: &mut RenderScope, _widget: &Arc<Widget>) {}
|
||||
fn render_widget(&mut self, _ctx: &Context, _scope: &mut RenderScope, _widget: &Arc<Widget>) {}
|
||||
fn after_render_widget(
|
||||
&mut self,
|
||||
_ctx: &Context,
|
||||
_scope: &mut RenderScope,
|
||||
_widget: &Arc<Widget>,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Event: Send + Sync {
|
||||
@@ -32,7 +39,7 @@ pub trait Event: Send + Sync {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExtensionContext {
|
||||
pub struct Context {
|
||||
screen: Arc<Screen>,
|
||||
}
|
||||
|
||||
@@ -71,7 +78,7 @@ impl<'a> dyn Event + 'a {
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtensionContext {
|
||||
impl Context {
|
||||
pub fn new(screen: Arc<Screen>) -> Self {
|
||||
Self { screen }
|
||||
}
|
||||
@@ -108,4 +115,16 @@ impl ExtensionContext {
|
||||
}
|
||||
components
|
||||
}
|
||||
|
||||
pub fn render(&self, w: &Arc<Widget>, scope: &mut RenderScope) {
|
||||
for ext in self.screen.extensions.lock().unwrap().iter() {
|
||||
ext.lock().unwrap().render_widget(self, scope, w);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn after_render(&self, w: &Arc<Widget>, scope: &mut RenderScope) {
|
||||
for ext in self.screen.extensions.lock().unwrap().iter() {
|
||||
ext.lock().unwrap().after_render_widget(self, scope, w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::event;
|
||||
use crate::extensions::{Extension, ExtensionContext};
|
||||
use crate::extensions::{Context, Extension};
|
||||
|
||||
pub struct TickExtension(pub u16);
|
||||
|
||||
event!(TickEvent(pub u32));
|
||||
|
||||
impl Extension for TickExtension {
|
||||
fn init(&mut self, ctx: &ExtensionContext) {
|
||||
fn init(&mut self, ctx: &Context) {
|
||||
let ctx = ctx.clone();
|
||||
let rate_dur = 1000 / self.0 as u64;
|
||||
std::thread::spawn({
|
||||
|
||||
@@ -42,7 +42,7 @@ impl VelocityExtension {
|
||||
}
|
||||
|
||||
impl Extension for VelocityExtension {
|
||||
fn init(&mut self, ctx: &super::ExtensionContext) {
|
||||
fn init(&mut self, ctx: &super::Context) {
|
||||
let ctx = ctx.clone();
|
||||
std::thread::spawn({
|
||||
move || {
|
||||
|
||||
Reference in New Issue
Block a user