Added after render and render on extension with child handlers #26

This commit is contained in:
2025-08-04 05:58:11 -05:00
parent 62e0c1eac1
commit ca4ee54e9d
12 changed files with 108 additions and 55 deletions
+2 -2
View File
@@ -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
View File
@@ -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);
}
}
}
+2 -2
View File
@@ -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({
+1 -1
View File
@@ -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 || {