use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::node::{ActionId, Common, IntoNode, Key, Node};
use crate::style::Style;
use crate::text::{Line, Span};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TabTitle {
pub key: Key,
pub title: Line,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub badge: Option<Line>,
}
impl TabTitle {
#[must_use]
pub fn new(key: impl Into<Key>, title: impl Into<Line>) -> Self {
Self {
key: key.into(),
title: title.into(),
badge: None,
}
}
#[must_use]
pub fn badge(mut self, badge: impl Into<Line>) -> Self {
self.badge = Some(badge.into());
self
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Tabs {
#[serde(flatten)]
pub common: Common,
pub titles: Vec<TabTitle>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub selected: Option<Key>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub divider: Option<Span>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub padding: Option<[Line; 2]>,
#[serde(skip_serializing_if = "Option::is_none")]
pub highlight_style: Option<Style>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub panes: Option<BTreeMap<Key, Node>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub on_select: Option<ActionId>,
}
impl Tabs {
#[must_use]
pub fn new(titles: impl IntoIterator<Item = TabTitle>) -> Self {
Self {
titles: titles.into_iter().collect(),
..Self::default()
}
}
#[must_use]
pub fn selected(mut self, key: impl Into<Key>) -> Self {
self.selected = Some(key.into());
self
}
#[must_use]
pub fn divider(mut self, divider: impl Into<Span>) -> Self {
self.divider = Some(divider.into());
self
}
#[must_use]
pub fn padding(mut self, padding: [Line; 2]) -> Self {
self.padding = Some(padding);
self
}
#[must_use]
pub fn highlight_style(mut self, style: Style) -> Self {
self.highlight_style = Some(style);
self
}
#[must_use]
pub fn panes(mut self, panes: impl IntoIterator<Item = (Key, impl IntoNode)>) -> Self {
self.panes = Some(panes.into_iter().map(|(k, n)| (k, n.into_node())).collect());
self
}
#[must_use]
pub fn on_select(mut self, action: impl Into<ActionId>) -> Self {
self.on_select = Some(action.into());
self
}
}
impl IntoNode for Tabs {}
impl From<Tabs> for Node {
fn from(t: Tabs) -> Self {
Node::Tabs(t)
}
}