use std::fmt;
use ratatui::{Frame, layout::Rect};
use super::app::App;
#[allow(dead_code)]
pub trait Widget: Send {
fn id(&self) -> &str;
fn name(&self) -> &str;
fn enabled(&self) -> bool;
fn toggle(&mut self);
fn render(&self, frame: &mut Frame, area: Rect, app: &App);
}
pub struct WidgetRegistry {
widgets: Vec<Box<dyn Widget + Send + 'static>>
}
#[allow(dead_code)]
impl WidgetRegistry {
#[must_use]
pub fn new() -> Self {
let mut registry = Self {
widgets: Vec::new()
};
registry.register(Box::new(account::AccountWidget::new(true)));
registry.register(Box::new(resource_list::ResourceListWidget::new(true)));
registry.register(Box::new(details::DetailsWidget::new(true)));
registry.register(Box::new(stats::StatsWidget::new(true)));
registry.register(Box::new(token_info::TokenInfoWidget::new(true)));
registry.register(Box::new(events::EventsWidget::new(true)));
registry.register(Box::new(help::HelpWidget::new()));
registry
}
pub fn register(&mut self, widget: Box<dyn Widget + Send>) {
self.widgets.push(widget);
}
#[allow(dead_code)]
pub fn get(&self, id: &str) -> Option<&(dyn Widget + Send)> {
self.widgets
.iter()
.find(|w| w.id() == id)
.map(std::convert::AsRef::as_ref)
}
pub fn get_mut<'a>(&'a mut self, id: &str) -> Option<&'a mut dyn Widget> {
for w in &mut self.widgets {
if w.id() == id {
return Some(w.as_mut());
}
}
None
}
pub fn toggle(&mut self, id: &str) {
if let Some(w) = self.widgets.iter_mut().find(|w| w.id() == id) {
w.toggle();
}
}
}
impl Default for WidgetRegistry {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for WidgetRegistry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WidgetRegistry")
.field("widget_count", &self.widgets.len())
.finish()
}
}
pub mod account;
pub mod button;
pub mod card_grid;
pub mod create_panel;
pub mod details;
pub mod events;
pub mod help;
pub mod resource_cards;
pub mod resource_list;
pub mod service_header;
pub mod settings_panel;
pub mod sidebar;
pub mod skeleton;
pub mod stats;
pub mod token_info;