lib: Add default impl for callback builders

This commit is contained in:
Erik Kundt
2024-04-23 17:43:51 +02:00
parent c5deca6247
commit 5c8ecd5f0a
4 changed files with 16 additions and 89 deletions
+13 -34
View File
@@ -43,14 +43,23 @@ pub trait View<S, A> {
Self: Sized;
/// Should set the optional custom event handler.
fn on_event(self, callback: EventCallback<A>) -> Self
fn on_event(mut self, callback: EventCallback<A>) -> Self
where
Self: Sized;
Self: Sized,
{
self.base_mut().on_event = Some(callback);
self
}
/// Should set the optional update handler.
fn on_update(self, callback: UpdateCallback<S>) -> Self
fn on_update(mut self, callback: UpdateCallback<S>) -> Self
where
Self: Sized;
Self: Sized,
{
self.base_mut().on_update = Some(callback);
self
}
/// Returns a boxed `View`
fn to_boxed(self) -> Box<Self>
@@ -193,16 +202,6 @@ where
&mut self.base
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn update(&mut self, state: &S) {
self.props =
WindowProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());
@@ -327,16 +326,6 @@ impl<S, A> View<S, A> for Shortcuts<S, A> {
&mut self.base
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn handle_key_event(&mut self, _key: Key) {}
fn update(&mut self, state: &S) {
@@ -571,16 +560,6 @@ where
&mut self.base
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn update(&mut self, state: &S) {
self.props = TableProps::<'_, R>::from_callback(self.base.on_update, state)
.unwrap_or(self.props.clone());
+1 -33
View File
@@ -11,9 +11,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row};
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
use crate::ui::theme::style;
use super::{
BaseView, BoxedWidget, Column, EventCallback, Properties, UpdateCallback, View, Widget,
};
use super::{BaseView, BoxedWidget, Column, Properties, View, Widget};
#[derive(Clone, Debug)]
pub struct HeaderProps<'a> {
@@ -95,16 +93,6 @@ impl<'a: 'static, S, A> View<S, A> for Header<'a, S, A> {
&mut self.base
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn update(&mut self, state: &S) {
self.props = self
.base
@@ -262,16 +250,6 @@ impl<'a: 'static, S, A> View<S, A> for Footer<'a, S, A> {
&mut self.base
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn update(&mut self, state: &S) {
self.props = self
.base
@@ -434,16 +412,6 @@ where
&mut self.base
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn update(&mut self, state: &S) {
self.props =
ContainerProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());
+1 -11
View File
@@ -9,7 +9,7 @@ use ratatui::prelude::{Backend, Rect};
use ratatui::style::Stylize;
use ratatui::text::{Line, Span};
use super::{BaseView, EventCallback, Properties, UpdateCallback, View, Widget};
use super::{BaseView, Properties, View, Widget};
#[derive(Clone)]
pub struct TextFieldProps {
@@ -146,16 +146,6 @@ impl<S, A> View<S, A> for TextField<S, A> {
&mut self.base
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn update(&mut self, state: &S) {
if let Some(on_update) = self.base.on_update {
if let Some(props) = (on_update)(state).downcast_ref::<TextFieldProps>() {
+1 -11
View File
@@ -8,7 +8,7 @@ use ratatui::backend::Backend;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::text::Text;
use super::{BaseView, EventCallback, Properties, UpdateCallback, View, Widget};
use super::{BaseView, Properties, View, Widget};
#[derive(Clone)]
pub struct ParagraphProps<'a> {
@@ -165,16 +165,6 @@ impl<'a: 'static, S, A> View<S, A> for Paragraph<'a, S, A> {
&mut self.base
}
fn on_event(mut self, callback: EventCallback<A>) -> Self {
self.base.on_event = Some(callback);
self
}
fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
self.base.on_update = Some(callback);
self
}
fn update(&mut self, state: &S) {
self.props =
ParagraphProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());