use crate::{
BlockStyle, Border, ListPresentation, ScrollbarPresentation, TablePresentation, TextStyle,
TreePresentation,
};
use super::{ComponentRole, ListRole, ScrollbarRole, SemanticTokens, TableRole, TreeRole};
const COMPONENT_ROLE_COUNT: usize = 15;
#[derive(Debug, Clone, PartialEq)]
pub struct ComponentTheme {
text_styles: [TextStyle; COMPONENT_ROLE_COUNT],
panel: BlockStyle,
panel_focused: BlockStyle,
list: ListPresentation,
tree: TreePresentation,
table: TablePresentation,
scrollbar: ScrollbarPresentation,
}
impl ComponentTheme {
pub fn from_tokens(tokens: &SemanticTokens) -> Self {
let panel = BlockStyle::new()
.foreground(tokens.text)
.background(tokens.surface)
.border(Border::ROUNDED)
.border_foreground(tokens.border)
.padding((0, 1));
let text_styles = [
TextStyle::new().foreground(tokens.text),
TextStyle::new().foreground(tokens.text_muted).dim(),
TextStyle::new().foreground(tokens.accent).bold(),
TextStyle::new().foreground(tokens.success),
TextStyle::new().foreground(tokens.error).bold(),
TextStyle::new().foreground(tokens.accent).bold(),
TextStyle::new().foreground(tokens.text),
TextStyle::new().foreground(tokens.text_muted).italic(),
TextStyle::new().foreground(tokens.accent).bold(),
TextStyle::new().foreground(tokens.text),
TextStyle::new()
.foreground(tokens.accent_text)
.background(tokens.accent)
.bold(),
TextStyle::new()
.foreground(tokens.text)
.background(tokens.surface),
TextStyle::new()
.foreground(tokens.accent_text)
.background(tokens.accent)
.bold(),
TextStyle::new().foreground(tokens.text_muted).dim(),
TextStyle::new().foreground(tokens.error),
];
Self {
text_styles,
panel: panel.clone(),
panel_focused: panel.border_foreground(tokens.accent),
list: ListPresentation::new(
TextStyle::new().foreground(tokens.text),
TextStyle::new().foreground(tokens.text_muted),
),
tree: TreePresentation::new(
TextStyle::new().foreground(tokens.text).bold(),
TextStyle::new().foreground(tokens.text),
TextStyle::new().foreground(tokens.text_muted),
),
table: TablePresentation::new(
BlockStyle::new().foreground(tokens.text).bold(),
BlockStyle::new().foreground(tokens.text),
TextStyle::new().foreground(tokens.border),
),
scrollbar: ScrollbarPresentation::new(
TextStyle::new().foreground(tokens.accent),
TextStyle::new().foreground(tokens.border),
),
}
}
pub fn get_text_style(&self, role: ComponentRole) -> &TextStyle {
&self.text_styles[role.index()]
}
pub const fn get_panel(&self) -> &BlockStyle {
&self.panel
}
pub const fn get_panel_focused(&self) -> &BlockStyle {
&self.panel_focused
}
pub fn get_list_style(&self, role: ListRole) -> &TextStyle {
self.list.get_style(role)
}
pub fn get_tree_style(&self, role: TreeRole) -> &TextStyle {
self.tree.get_style(role)
}
pub fn get_table_style(&self, role: TableRole) -> &BlockStyle {
self.table.get_style(role)
}
pub fn get_scrollbar_style(&self, role: ScrollbarRole) -> &TextStyle {
self.scrollbar.get_style(role)
}
pub fn list(&self) -> &ListPresentation {
&self.list
}
pub fn tree(&self) -> &TreePresentation {
&self.tree
}
pub fn table(&self) -> &TablePresentation {
&self.table
}
pub fn scrollbar(&self) -> &ScrollbarPresentation {
&self.scrollbar
}
#[must_use]
pub fn text_style(mut self, role: ComponentRole, style: TextStyle) -> Self {
self.text_styles[role.index()] = style;
self
}
#[must_use]
pub fn panel(mut self, panel: BlockStyle) -> Self {
self.panel = panel;
self
}
#[must_use]
pub fn panel_focused(mut self, panel: BlockStyle) -> Self {
self.panel_focused = panel;
self
}
#[must_use]
pub fn list_style(mut self, role: ListRole, style: TextStyle) -> Self {
self.list = self.list.style(role, style);
self
}
#[must_use]
pub fn tree_style(mut self, role: TreeRole, style: TextStyle) -> Self {
self.tree = self.tree.style(role, style);
self
}
#[must_use]
pub fn table_style(mut self, role: TableRole, style: BlockStyle) -> Self {
self.table = self.table.style(role, style);
self
}
#[must_use]
pub fn scrollbar_style(mut self, role: ScrollbarRole, style: TextStyle) -> Self {
self.scrollbar = self.scrollbar.style(role, style);
self
}
}