lib: Introduce BoxedAny traits

This commit is contained in:
Erik Kundt
2024-05-13 13:24:26 +02:00
parent 780dbaf94d
commit 7bcf2bc7a8
6 changed files with 30 additions and 26 deletions
+11 -17
View File
@@ -144,16 +144,9 @@ pub trait Properties {
Box::new(self)
}
fn from_boxed_any(any: Box<dyn Any>) -> Option<Self>
where
Self: Sized + Clone + 'static,
{
any.downcast_ref::<Self>().cloned()
}
fn from_callback<S>(callback: Option<UpdateCallback<S>>, state: &S) -> Option<Self>
where
Self: Sized + Clone + 'static,
Self: Sized + Clone + 'static + BoxedAny,
{
callback
.map(|callback| (callback)(state))
@@ -161,18 +154,19 @@ pub trait Properties {
}
}
pub trait WidgetState {
fn to_boxed_any(self) -> Box<dyn Any>
where
Self: Sized + Clone + 'static,
{
Box::new(self)
}
/// Provide default implementations for conversions to and from `Box<dyn Any>`.
pub trait BoxedAny {
fn from_boxed_any(any: Box<dyn Any>) -> Option<Self>
where
Self: Sized + Clone + 'static,
{
any.downcast_ref::<Self>().cloned()
}
}
fn to_boxed_any(self) -> Box<dyn Any>
where
Self: Sized + Clone + 'static,
{
Box::new(self)
}
}
+6 -2
View File
@@ -10,7 +10,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row};
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
use crate::ui::theme::style;
use super::{BaseView, BoxedWidget, Properties, RenderProps, Widget, WidgetState};
use super::{BaseView, BoxedAny, BoxedWidget, Properties, RenderProps, Widget};
#[derive(Clone, Debug)]
pub struct Column<'a> {
@@ -65,6 +65,7 @@ impl<'a> Default for HeaderProps<'a> {
}
impl<'a: 'static> Properties for HeaderProps<'a> {}
impl<'a: 'static> BoxedAny for HeaderProps<'a> {}
pub struct Header<'a: 'static, S, A> {
/// Internal props
@@ -205,6 +206,7 @@ impl<'a> Default for FooterProps<'a> {
}
impl<'a: 'static> Properties for FooterProps<'a> {}
impl<'a: 'static> BoxedAny for FooterProps<'a> {}
pub struct Footer<'a, S, A> {
/// Internal props
@@ -325,6 +327,7 @@ impl ContainerProps {
}
impl Properties for ContainerProps {}
impl BoxedAny for ContainerProps {}
pub struct Container<S, A> {
/// Internal base
@@ -459,7 +462,7 @@ pub struct SectionGroupState {
focus: Option<usize>,
}
impl WidgetState for SectionGroupState {}
impl BoxedAny for SectionGroupState {}
#[derive(Clone, Default)]
pub struct SectionGroupProps {
@@ -475,6 +478,7 @@ impl SectionGroupProps {
}
impl Properties for SectionGroupProps {}
impl BoxedAny for SectionGroupProps {}
pub struct SectionGroup<S, A> {
/// Internal base
+2 -2
View File
@@ -6,7 +6,7 @@ use ratatui::layout::{Constraint, Layout};
use ratatui::style::Stylize;
use ratatui::text::{Line, Span};
use super::{BaseView, Properties, RenderProps, Widget, WidgetState};
use super::{BaseView, BoxedAny, Properties, RenderProps, Widget};
#[derive(Clone)]
pub struct TextFieldProps {
@@ -54,7 +54,7 @@ pub struct TextFieldState {
pub cursor_position: usize,
}
impl WidgetState for TextFieldState {}
impl BoxedAny for TextFieldState {}
pub struct TextField<S, A> {
/// Internal base
+5 -2
View File
@@ -13,7 +13,8 @@ use ratatui::widgets::TableState;
use crate::ui::theme::style;
use crate::ui::{layout, span};
use super::{container::Column, BaseView, Properties, RenderProps, ToRow, Widget, WidgetState};
use super::BoxedAny;
use super::{container::Column, BaseView, Properties, RenderProps, ToRow, Widget};
#[derive(Clone, Debug)]
pub struct TableProps<'a, R, const W: usize>
@@ -83,7 +84,9 @@ where
}
impl<'a: 'static, R, const W: usize> Properties for TableProps<'a, R, W> where R: ToRow<W> + 'static {}
impl WidgetState for TableState {}
impl<'a: 'static, R, const W: usize> BoxedAny for TableProps<'a, R, W> where R: ToRow<W> + 'static {}
impl BoxedAny for TableState {}
pub struct Table<'a, S, A, R, const W: usize>
where
+3 -2
View File
@@ -5,7 +5,7 @@ use termion::event::Key;
use ratatui::layout::{Constraint, Layout};
use ratatui::text::Text;
use super::{BaseView, Properties, RenderProps, Widget, WidgetState};
use super::{BaseView, BoxedAny, Properties, RenderProps, Widget};
#[derive(Clone)]
pub struct ParagraphProps<'a> {
@@ -41,6 +41,7 @@ impl<'a> Default for ParagraphProps<'a> {
}
impl<'a: 'static> Properties for ParagraphProps<'a> {}
impl<'a: 'static> BoxedAny for ParagraphProps<'a> {}
#[derive(Clone)]
pub struct ParagraphState {
@@ -50,7 +51,7 @@ pub struct ParagraphState {
pub progress: usize,
}
impl WidgetState for ParagraphState {}
impl BoxedAny for ParagraphState {}
pub struct Paragraph<'a, S, A> {
/// Internal base
+3 -1
View File
@@ -12,7 +12,7 @@ use ratatui::widgets::Row;
use crate::ui::theme::style;
use super::{BaseView, BoxedWidget, Properties, RenderProps, Widget};
use super::{BaseView, BoxedAny, BoxedWidget, Properties, RenderProps, Widget};
#[derive(Clone)]
pub struct WindowProps<Id> {
@@ -33,6 +33,7 @@ impl<Id> Default for WindowProps<Id> {
}
impl<Id> Properties for WindowProps<Id> {}
impl<Id> BoxedAny for WindowProps<Id> {}
pub struct Window<S, A, Id> {
/// Internal base
@@ -152,6 +153,7 @@ impl Default for ShortcutsProps {
}
impl Properties for ShortcutsProps {}
impl BoxedAny for ShortcutsProps {}
pub struct Shortcuts<S, A> {
/// Internal properties