use std::rc::Rc;
use teksilo_canvas::Point;
use teksilo_core::widget::{EventContext, Widget};
use teksilo_i18n::LocalizedString;
use super::delegate::ContextMenuFactory;
use crate::IconWidget;
use crate::tooltip::RichTooltipSource;
pub type IconFactory = Rc<dyn Fn() -> IconWidget>;
pub type CompositeTooltipFactory = Rc<dyn Fn() -> Box<dyn Widget>>;
#[derive(Clone)]
pub struct TabInfo {
pub(crate) title: Option<LocalizedString>,
pub(crate) icon: Option<IconFactory>,
pub(crate) tooltip: Option<LocalizedString>,
pub(crate) rich_tooltip: Option<RichTooltipSource>,
pub(crate) composite_tooltip: Option<CompositeTooltipFactory>,
pub(crate) closable: bool,
pub(crate) pinned: bool,
pub(crate) initial_enabled: teksilo_core::signal::Prop<bool>,
pub(crate) focusable_panel: bool,
pub(crate) context_menu: Option<ContextMenuFactory>,
}
impl Default for TabInfo {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for TabInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TabInfo")
.field("title", &self.title)
.field("has_icon", &self.icon.is_some())
.field("tooltip", &self.tooltip)
.field("has_rich_tooltip", &self.rich_tooltip.is_some())
.field("has_composite_tooltip", &self.composite_tooltip.is_some())
.field("closable", &self.closable)
.field("pinned", &self.pinned)
.field("initial_enabled", &self.initial_enabled.get())
.field("focusable_panel", &self.focusable_panel)
.field("has_context_menu", &self.context_menu.is_some())
.finish()
}
}
impl TabInfo {
pub fn new() -> Self {
Self {
title: None,
icon: None,
tooltip: None,
rich_tooltip: None,
composite_tooltip: None,
closable: false,
pinned: false,
initial_enabled: teksilo_core::signal::Prop::Static(true),
focusable_panel: false,
context_menu: None,
}
}
pub fn context_menu(
mut self,
f: impl Fn(Point, &mut EventContext) -> Option<Box<dyn Widget>> + 'static,
) -> Self {
self.context_menu = Some(Rc::new(f));
self
}
pub fn title(mut self, t: impl Into<LocalizedString>) -> Self {
self.title = Some(t.into());
self
}
pub fn no_title(mut self) -> Self {
self.title = None;
self
}
pub fn icon(mut self, factory: impl Fn() -> IconWidget + 'static) -> Self {
self.icon = Some(Rc::new(factory));
self
}
pub fn tooltip(mut self, t: impl Into<LocalizedString>) -> Self {
self.tooltip = Some(t.into());
self.rich_tooltip = None;
self.composite_tooltip = None;
self
}
pub fn rich_tooltip(mut self, key: impl Into<String>) -> Self {
self.rich_tooltip = Some(RichTooltipSource::Key(key.into()));
self.tooltip = None;
self.composite_tooltip = None;
self
}
pub fn rich_tooltip_content(mut self, content: crate::tooltip::TooltipContent) -> Self {
self.rich_tooltip = Some(RichTooltipSource::Content(content));
self.tooltip = None;
self.composite_tooltip = None;
self
}
pub fn composite_tooltip<W>(mut self, factory: impl Fn() -> W + 'static) -> Self
where
W: Widget + 'static,
{
self.composite_tooltip = Some(Rc::new(move || -> Box<dyn Widget> { Box::new(factory()) }));
self.tooltip = None;
self.rich_tooltip = None;
self
}
pub fn closable(mut self, b: bool) -> Self {
self.closable = b;
self
}
pub fn pinned(mut self, b: bool) -> Self {
self.pinned = b;
self
}
pub fn enabled(mut self, enabled: impl Into<teksilo_core::signal::Prop<bool>>) -> Self {
self.initial_enabled = enabled.into();
self
}
pub fn focusable_panel(mut self, b: bool) -> Self {
self.focusable_panel = b;
self
}
}