use std::sync::Arc;
use crate::{
app::theme::{MarkdownFocusRing, MarkdownInlineStyle, MarkdownLinkState, ThemeMarkdown},
node::{border::BorderRadius, inline_link::InlineLinkLook},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MarkdownLink {
pub(super) destination: Arc<str>,
pub(super) title: Arc<str>,
pub(super) text: Arc<str>,
pub(super) index: usize,
}
impl MarkdownLink {
pub fn destination(&self) -> &str {
&self.destination
}
pub fn title(&self) -> &str {
&self.title
}
pub fn text(&self) -> &str {
&self.text
}
pub fn index(&self) -> usize {
self.index
}
}
#[derive(Clone, Default, PartialEq)]
pub struct MarkdownRun {
pub(super) text: Arc<str>,
pub(super) style: MarkdownInlineStyle,
}
impl std::fmt::Debug for MarkdownRun {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MarkdownRun")
.field("text", &self.text)
.finish_non_exhaustive()
}
}
impl MarkdownRun {
pub fn new(text: impl Into<Arc<str>>, style: MarkdownInlineStyle) -> Self {
Self {
text: text.into(),
style,
}
}
pub fn plain(text: impl Into<Arc<str>>) -> Self {
Self::new(text, MarkdownInlineStyle::default())
}
}
#[derive(Clone, Default, PartialEq)]
pub struct MarkdownLinkPresentation {
pub(super) as_text: bool,
pub(super) style: Option<MarkdownInlineStyle>,
pub(super) hovered: Option<MarkdownLinkState>,
pub(super) focused: Option<MarkdownLinkState>,
pub(super) focus_ring: Option<Option<MarkdownFocusRing>>,
pub(super) radius: Option<BorderRadius>,
pub(super) trailing: Vec<MarkdownRun>,
pub(super) tooltip: Option<Arc<str>>,
pub(super) pointer_only: bool,
}
impl std::fmt::Debug for MarkdownLinkPresentation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MarkdownLinkPresentation")
.field("as_text", &self.as_text)
.field("has_style", &self.style.is_some())
.field("hovered", &self.hovered)
.field("focused", &self.focused)
.field("focus_ring", &self.focus_ring)
.field("radius", &self.radius)
.field("trailing", &self.trailing)
.field("tooltip", &self.tooltip)
.field("pointer_only", &self.pointer_only)
.finish()
}
}
impl MarkdownLinkPresentation {
pub fn link() -> Self {
Self::default()
}
pub fn text() -> Self {
Self {
as_text: true,
..Self::default()
}
}
pub fn style(mut self, style: MarkdownInlineStyle) -> Self {
self.style = Some(style);
self
}
pub fn hovered(mut self, state: MarkdownLinkState) -> Self {
self.hovered = Some(state);
self
}
pub fn focused(mut self, state: MarkdownLinkState) -> Self {
self.focused = Some(state);
self
}
pub fn focus_ring(mut self, ring: Option<MarkdownFocusRing>) -> Self {
self.focus_ring = Some(ring);
self
}
pub fn radius(mut self, radius: BorderRadius) -> Self {
self.radius = Some(radius);
self
}
pub fn trailing(mut self, runs: impl IntoIterator<Item = MarkdownRun>) -> Self {
self.trailing = runs.into_iter().collect();
self
}
pub fn tooltip(mut self, text: impl Into<Arc<str>>) -> Self {
self.tooltip = Some(text.into());
self
}
pub fn pointer_only(mut self) -> Self {
self.pointer_only = true;
self
}
pub(super) fn look(&self, theme: &ThemeMarkdown) -> Arc<InlineLinkLook> {
Arc::new(InlineLinkLook {
hovered: self.hovered.clone().unwrap_or_else(|| theme.link_hovered.clone()),
focused: self.focused.clone().unwrap_or_else(|| theme.link_focused.clone()),
focus_ring: self.focus_ring.clone().unwrap_or_else(|| theme.link_focus_ring.clone()),
radius: self.radius.unwrap_or(theme.link_radius),
})
}
}