use std::sync::Arc;
use crate::{
App, HighlightStyle, HighlightTheme, IntoElement, Pixels, Rems, RenderOnce, SharedString,
StyleRefinement, Window, px, rems,
};
#[derive(Clone)]
pub struct ComponentTextViewStyle {
pub paragraph_gap: Rems,
pub heading_base_font_size: Pixels,
pub heading_font_size: Option<Arc<dyn Fn(u8, Pixels) -> Pixels + Send + Sync + 'static>>,
pub highlight_theme: Arc<HighlightTheme>,
pub code_block: StyleRefinement,
pub table: StyleRefinement,
pub table_cell: StyleRefinement,
pub inline_code: HighlightStyle,
pub is_dark: bool,
}
impl Default for ComponentTextViewStyle {
fn default() -> Self {
Self {
paragraph_gap: rems(1.),
heading_base_font_size: px(14.),
heading_font_size: None,
highlight_theme: HighlightTheme::default_light(),
code_block: StyleRefinement::default(),
table: StyleRefinement::default(),
table_cell: StyleRefinement::default(),
inline_code: HighlightStyle::default(),
is_dark: false,
}
}
}
impl ComponentTextViewStyle {
pub fn paragraph_gap(mut self, gap: Rems) -> Self {
self.paragraph_gap = gap;
self
}
pub fn heading_font_size<F>(mut self, f: F) -> Self
where
F: Fn(u8, Pixels) -> Pixels + Send + Sync + 'static,
{
self.heading_font_size = Some(Arc::new(f));
self
}
pub fn code_block(mut self, style: StyleRefinement) -> Self {
self.code_block = style;
self
}
pub fn inline_code(mut self, style: HighlightStyle) -> Self {
self.inline_code = style;
self
}
pub fn table(mut self, style: StyleRefinement) -> Self {
self.table = style;
self
}
pub fn table_cell(mut self, style: StyleRefinement) -> Self {
self.table_cell = style;
self
}
}
#[derive(IntoElement, Clone)]
pub enum ComponentText {
String(SharedString),
}
impl From<SharedString> for ComponentText {
fn from(s: SharedString) -> Self {
Self::String(s)
}
}
impl From<&str> for ComponentText {
fn from(s: &str) -> Self {
Self::String(SharedString::from(s.to_string()))
}
}
impl From<String> for ComponentText {
fn from(s: String) -> Self {
Self::String(s.into())
}
}
impl ComponentText {
pub fn style(self, _style: ComponentTextViewStyle) -> Self {
match self {
Self::String(s) => Self::String(s),
}
}
pub fn get_text(&self, _cx: &App) -> SharedString {
match self {
Self::String(s) => s.clone(),
}
}
}
impl RenderOnce for ComponentText {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
match self {
Self::String(s) => s.into_any_element(),
}
}
}