use std::sync::Arc;
use super::{
icons, names,
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::{fading, glyph, pressable},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{IconSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
layout::text_style::FontWeight,
node::{CursorIcon, Element, EventHandler, IntoMouseEventHandler, border::Border},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LinkPart {
Root,
Label,
Icon,
}
names!(LinkPart {
Root => "root",
Label => "label",
Icon => "icon",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LinkState {
Disabled,
}
names!(LinkState {
Disabled => "disabled",
});
pub type LinkRenderer = Arc<dyn Fn(LinkView) -> Element + Send + Sync>;
#[derive(Clone)]
pub struct Link {
label: Arc<str>,
trailing_icon: Option<Name>,
external: bool,
href: Option<Arc<str>>,
disabled: bool,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
on_click: Vec<EventHandler<crate::app::events::MouseEvent>>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Link {
pub fn new(label: &str) -> Self {
Self {
label: Arc::from(label),
trailing_icon: None,
external: false,
href: None,
disabled: false,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
on_click: Vec::new(),
id: None,
key: None,
}
}
pub fn trailing_icon(mut self, name: impl Into<Name>) -> Self {
self.trailing_icon = Some(name.into());
self
}
pub fn external(mut self, external: bool) -> Self {
self.external = external;
self
}
pub fn href(mut self, url: &str) -> Self {
self.href = Some(Arc::from(url.trim()));
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn on_click(mut self, handler: impl IntoMouseEventHandler) -> Self {
self.on_click.push(handler.into_event_handler());
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub fn build(self, theme: &Theme) -> Element {
let case = Case {
variant: None,
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(LinkState::Disabled, self.disabled);
let view = LinkView {
label: self.label,
trailing_icon: self
.trailing_icon
.or_else(|| self.external.then(|| Name::from(icons::ARROW_UP_RIGHT))),
href: self.href,
disabled: self.disabled,
recipe: theme
.resolve::<Link>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
forced: self.forced,
on_click: self.on_click,
id: self.id,
key: self.key,
};
match theme.renderer::<Link>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<Link> for Element {
fn from(link: Link) -> Self {
link.build(¤t_theme())
}
}
pub struct LinkView {
pub label: Arc<str>,
pub trailing_icon: Option<Name>,
pub href: Option<Arc<str>>,
pub disabled: bool,
pub recipe: ResolvedRecipe,
forced: Option<Interaction>,
on_click: Vec<EventHandler<crate::app::events::MouseEvent>>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl LinkView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part).forced(self.forced)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn root(&self) -> crate::components::Button {
let mut root = pressable(&self.part(LinkPart::Root), self.disabled).interaction_group(true);
for handler in &self.on_click {
root = root.on_click(handler.clone());
}
if let Some(url) = self.href.clone().filter(|_| !self.disabled) {
root = root.on_click(move |event: crate::app::events::MouseEvent| {
if !event.default_prevented()
&& let Err(error) = crate::app::open_url(&url)
{
tracing::warn!("the link did not open {url}: {error}");
}
});
}
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root
}
pub fn render(self) -> Element {
let label = self.part(LinkPart::Label);
let mut root = self.root().child(label.text(&self.label).nowrap().part(&label));
if let Some(name) = self.trailing_icon {
root = root.child(glyph(&self.part(LinkPart::Icon), name, false));
}
root.into()
}
}
impl crate::ui::Styled for Link {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for Link {
const NAME: &'static str = "link";
type Renderer = LinkRenderer;
fn parts() -> crate::ui::Declared {
LinkPart::declared()
}
fn states() -> crate::ui::Declared {
LinkState::declared()
}
fn recipe() -> Recipe {
use LinkPart::{Icon, Label, Root};
use PaletteColor::Foreground;
Recipe::new()
.base(Root, |s| {
fading(
s.gap(4.0)
.radius(RadiusSize::Sm)
.foreground(Foreground)
.cursor(CursorIcon::Pointer),
)
})
.base(Label, |s| {
s.typography(TypographyStyle::Body).weight(FontWeight::Medium)
})
.base(Icon, |s| s.square(IconSize::Sm))
.within(Label, |s| s.underline(Border::inside(1.0, Foreground)))
.focus_visible(Root, |s| s.focus_ring())
.state(LinkState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
}
}