use std::{any::TypeId, sync::Arc};
use parking_lot::Mutex;
use super::{
Icon, Picture, PictureFit, PicturePart, Skeleton, icons, names,
part_style::{FontFamily, Length},
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
shared::{fading, pressable},
theme::{Themed, current_theme},
};
use crate::{
app::{
component::Component,
ctx::{Ctx, FocusHandle},
events::MouseEvent,
theme::{BorderSize, IconSize, OpacityLevel, PaletteColor, RadiusSize, ShadowStyle, Theme, TypographyStyle},
},
components::{self, Column, Row, Stack, TextOverflow},
core::{DisplayWords, ElementRef, Locale, Signal},
layout::{
Alignment, StackAlignment,
layout_kind::Justify,
text_style::{FontWeight, TextUnderline},
},
node::{CursorIcon, Element, HitTestBehavior, border::Border, color::Color, dimension::Dimension},
resources::{LinkFetch, LinkFetcher, LinkMeta, LinkPreviewError, LoadWaker, Source, fetch_off_thread, link_fetcher},
};
const ICON_PIXELS: u32 = 64;
pub const LINK_PREVIEW_PICTURE_RATIO: f32 = 360.0 / 188.0;
const CARD_PICTURE_RATIO: f32 = LINK_PREVIEW_PICTURE_RATIO;
const SQUARE_ENOUGH: f32 = 1.5;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum LinkPreviewVariant {
#[default]
Card,
Compact,
Inline,
}
names!(LinkPreviewVariant {
Card => "card",
Compact => "compact",
Inline => "inline",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LinkPreviewPart {
Root,
Media,
Play,
PlayIcon,
Length,
Text,
Site,
Icon,
Domain,
Title,
Description,
Skeleton,
Close,
CloseIcon,
}
names!(LinkPreviewPart {
Root => "root",
Media => "media",
Play => "play",
PlayIcon => "play_icon",
Length => "length",
Text => "text",
Site => "site",
Icon => "icon",
Domain => "domain",
Title => "title",
Description => "description",
Skeleton => "skeleton",
Close => "close",
CloseIcon => "close_icon",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LinkPreviewState {
Loading,
Media,
Video,
Empty,
Removable,
SiteIcon,
Transparent,
Disabled,
}
names!(LinkPreviewState {
Loading => "loading",
Media => "media",
Video => "video",
Empty => "empty",
Removable => "removable",
SiteIcon => "site_icon",
Transparent => "transparent",
Disabled => "disabled",
});
#[derive(Clone)]
pub enum LinkImage {
Source(Source),
Picture(Box<Picture>),
}
impl From<&str> for LinkImage {
fn from(address: &str) -> Self {
Self::Source(Source::resource(address))
}
}
impl From<String> for LinkImage {
fn from(address: String) -> Self {
Self::from(address.as_str())
}
}
impl From<Source> for LinkImage {
fn from(source: Source) -> Self {
Self::Source(source)
}
}
impl From<Picture> for LinkImage {
fn from(picture: Picture) -> Self {
Self::Picture(Box::new(picture))
}
}
impl LinkImage {
fn picture(&self) -> (Picture, u64) {
match self {
Self::Source(source) => (Picture::from_source(source.clone()), source.cache_key()),
Self::Picture(picture) => ((**picture).clone(), picture_key(picture)),
}
}
}
fn picture_key(picture: &Picture) -> u64 {
picture.source_key().unwrap_or(0)
}
#[derive(Clone)]
enum Given<T> {
Fetched,
None,
Is(T),
}
impl<T> Given<T> {
fn decided(&self) -> bool {
!matches!(self, Self::Fetched)
}
}
type OpenHandler = Arc<dyn Fn(&str) + Send + Sync>;
type RemoveHandler = Arc<dyn Fn() + Send + Sync>;
pub type LinkPreviewRenderer = Arc<dyn Fn(LinkPreviewView) -> Element + Send + Sync>;
pub struct LinkPreview {
url: Arc<str>,
title: Option<Arc<str>>,
description: Option<Arc<str>>,
site: Option<Arc<str>>,
site_shown: bool,
image: Given<LinkImage>,
icon: Given<LinkImage>,
video: Option<Option<f64>>,
variant: Name,
width: Option<Length>,
loading: bool,
disabled: bool,
label: Option<Arc<str>>,
fetcher: Option<(Arc<dyn LinkFetcher>, FetcherId)>,
words: Option<DisplayWords>,
no_preview_text: Option<Arc<str>>,
remove_label: Option<Arc<str>>,
on_open: Vec<OpenHandler>,
on_remove: Vec<RemoveHandler>,
return_focus: Option<ElementRef>,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Clone for LinkPreview {
fn clone(&self) -> Self {
Self {
url: self.url.clone(),
title: self.title.clone(),
description: self.description.clone(),
site: self.site.clone(),
site_shown: self.site_shown,
image: self.image.clone(),
icon: self.icon.clone(),
video: self.video,
variant: self.variant,
width: self.width,
loading: self.loading,
disabled: self.disabled,
label: self.label.clone(),
fetcher: self.fetcher.clone(),
words: self.words.clone(),
no_preview_text: self.no_preview_text.clone(),
remove_label: self.remove_label.clone(),
on_open: self.on_open.clone(),
on_remove: self.on_remove.clone(),
return_focus: self.return_focus.clone(),
states: self.states.clone(),
rules: self.rules.clone(),
forced: self.forced,
id: self.id.clone(),
key: self.key.clone(),
}
}
}
impl LinkPreview {
pub fn new(url: &str) -> Self {
Self {
url: Arc::from(url.trim()),
title: None,
description: None,
site: None,
site_shown: true,
image: Given::Fetched,
icon: Given::Fetched,
video: None,
variant: LinkPreviewVariant::default().into(),
width: None,
loading: false,
disabled: false,
label: None,
fetcher: None,
words: None,
no_preview_text: None,
remove_label: None,
on_open: Vec::new(),
on_remove: Vec::new(),
return_focus: None,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
pub fn title(mut self, title: &str) -> Self {
self.title = Some(Arc::from(title));
self
}
pub fn description(mut self, description: &str) -> Self {
self.description = Some(Arc::from(description));
self
}
pub fn site(mut self, site: &str) -> Self {
self.site = Some(Arc::from(site));
self
}
pub fn image(mut self, image: impl Into<LinkImage>) -> Self {
self.image = Given::Is(image.into());
self
}
pub fn no_image(mut self) -> Self {
self.image = Given::None;
self
}
pub fn no_site(mut self) -> Self {
self.site_shown = false;
self
}
pub fn icon(mut self, icon: impl Into<LinkImage>) -> Self {
self.icon = Given::Is(icon.into());
self
}
pub fn no_icon(mut self) -> Self {
self.icon = Given::None;
self
}
pub fn video(mut self, length: impl Into<Option<f64>>) -> Self {
self.video = Some(length.into());
self
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = Some(width.into());
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.loading = loading;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
pub fn fetcher(mut self, fetcher: impl LinkFetcher + 'static) -> Self {
let id = FetcherId::of(&fetcher);
self.fetcher = Some((Arc::new(fetcher), id));
self
}
pub fn shared_fetcher(mut self, fetcher: Arc<dyn LinkFetcher>) -> Self {
let id = FetcherId::shared(&fetcher);
self.fetcher = Some((fetcher, id));
self
}
pub fn locale(mut self, locale: &Locale) -> Self {
self.words = Some(locale.display.clone());
self
}
pub fn no_preview_text(mut self, text: &str) -> Self {
self.no_preview_text = Some(Arc::from(text));
self
}
pub fn remove_label(mut self, label: &str) -> Self {
self.remove_label = Some(Arc::from(label));
self
}
pub fn on_open(mut self, handler: impl Fn(&str) + Send + Sync + 'static) -> Self {
self.on_open.push(Arc::new(handler));
self
}
pub fn on_remove(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
self.on_remove.push(Arc::new(handler));
self
}
pub fn return_focus(mut self, element: &ElementRef) -> Self {
self.return_focus = Some(element.clone());
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 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 url(&self) -> &str {
&self.url
}
pub fn needs_nothing_fetched(&self) -> bool {
let inline = self.variant == Name::from(LinkPreviewVariant::Inline);
self.title.is_some()
&& (self.site.is_some() || !self.site_shown)
&& self.icon.decided()
&& (inline || (self.description.is_some() && self.image.decided()))
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
let key = self.key.clone();
let props = LinkPreviewProps(self);
match key {
Some(key) => ctx.mount_keyed::<LinkPreviewHost>(&key, props),
None => ctx.mount::<LinkPreviewHost>(props),
}
}
pub fn build(self, theme: &Theme) -> Element {
let fetched = if self.needs_nothing_fetched() {
None
} else {
Some(LinkFetch::ready(LinkMeta::new(&self.url)))
};
self.view(theme, fetched, None).render_themed()
}
fn view(self, theme: &Theme, fetched: Option<LinkFetch>, live: Option<&Live>) -> LinkPreviewView {
let words = self
.words
.clone()
.unwrap_or_else(|| super::current_locale().display.clone());
let variant = LinkPreviewVariant::ALL
.into_iter()
.find(|variant| Name::from(*variant) == self.variant)
.unwrap_or_default();
let (meta, failure, pending) = match fetched {
None => (None, None, false),
Some(LinkFetch::Loading) => (None, None, true),
Some(LinkFetch::Ready(meta)) => (Some(meta), None, false),
Some(LinkFetch::Failed(error)) => (None, Some(error), false),
};
let meta = meta.as_deref();
let title = self.title.clone().or_else(|| meta.and_then(|meta| meta.title.clone()));
let description = self
.description
.clone()
.or_else(|| meta.and_then(|meta| meta.description.clone()));
let site = self
.site
.clone()
.or_else(|| meta.and_then(|meta| meta.site.clone()))
.unwrap_or_else(|| Arc::from(domain_of(&self.url)));
let failed = |key: u64, which: Option<&Signal<Option<u64>>>| which.is_some_and(|failed| failed.get() == Some(key));
let pick = |given: &Given<LinkImage>, fetched: Option<&Arc<str>>| -> Option<(Picture, u64)> {
match given {
Given::None => None,
Given::Is(image) => Some(image.picture()),
Given::Fetched => fetched.map(|address| LinkImage::from(&**address).picture()),
}
};
let image = pick(&self.image, meta.and_then(|meta| meta.image.as_ref()))
.filter(|(_, key)| !failed(*key, live.map(|live| &live.failed_image)));
let failed_icons = live.map(|live| live.failed_icon.get()).unwrap_or_default();
let icon = match &self.icon {
Given::None => None,
Given::Is(icon) => Some(icon.picture()),
Given::Fetched => self
.page_icons(meta, live.is_some())
.iter()
.map(|address| {
let (picture, key) = LinkImage::from(&**address).picture();
(picture.downscale_to(ICON_PIXELS, ICON_PIXELS), key)
})
.find(|(_, key)| !failed_icons.contains(key)),
}
.filter(|(_, key)| !failed_icons.contains(key));
let video = self
.video
.or_else(|| meta.filter(|meta| meta.video).map(|meta| meta.video_length));
let loading = self.loading || (pending && self.title.is_none());
let empty = !loading && title.is_none() && description.is_none() && image.is_none();
let fallback = match &failure {
Some(error) => words.link_error(error),
None => self
.no_preview_text
.clone()
.unwrap_or_else(|| words.link_no_preview.clone()),
};
let (title, description) = if empty {
(Arc::from(address_of(&self.url)), Some(fallback))
} else {
(title.unwrap_or_else(|| Arc::from(address_of(&self.url))), description)
};
let shown_image = if loading || empty || variant == LinkPreviewVariant::Inline {
None
} else {
image
};
let looked = shown_image.as_ref().and_then(|(picture, _)| looked_at(picture, live));
let transparent = looked.is_some_and(|(transparent, _)| transparent);
let far_from_square = looked.is_some_and(|(_, ratio)| !(1.0 / SQUARE_ENOUGH..=SQUARE_ENOUGH).contains(&ratio));
let whole = transparent || (variant == LinkPreviewVariant::Compact && far_from_square);
let shown_image = shown_image.map(|(picture, key)| match live {
Some(live) if looked.is_none() => {
let tick = live.tick.clone();
(
picture.on_load(move || tick.set(tick.get_untracked().wrapping_add(1))),
key,
)
}
_ => (picture, key),
});
let removable = !self.on_remove.is_empty();
let case = Case {
variant: Some(self.variant),
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(LinkPreviewState::Loading, loading)
.state(
LinkPreviewState::Media,
shown_image.is_some() || (loading && variant != LinkPreviewVariant::Inline),
)
.state(LinkPreviewState::Video, video.is_some() && shown_image.is_some())
.state(LinkPreviewState::Empty, empty)
.state(LinkPreviewState::Removable, removable)
.state(LinkPreviewState::SiteIcon, icon.is_some() && !empty && !loading)
.state(LinkPreviewState::Transparent, transparent)
.state(LinkPreviewState::Disabled, self.disabled);
let mut rules = self.rules.clone();
if let Some(width) = self.width {
rules = Recipe::new()
.base(LinkPreviewPart::Root, |s| s.width(width))
.extend(&rules);
}
LinkPreviewView {
url: self.url.clone(),
variant,
title,
description,
site,
site_shown: self.site_shown,
image: shown_image.map(|(picture, _)| picture),
transparent,
whole,
icon: if empty {
None
} else {
icon.clone().map(|(picture, _)| picture)
},
video: if empty { None } else { video },
loading,
empty,
error: failure,
disabled: self.disabled,
label: self.label.clone(),
remove_label: self.remove_label.clone().unwrap_or_else(|| words.link_remove.clone()),
words,
recipe: theme
.resolve::<LinkPreview>(&case, Some(&rules))
.owned_by(self.id.as_ref()),
forced: self.forced,
hovered: live.map(|live| live.hovered.clone()),
failed_image: live.map(|live| live.failed_image.clone()),
failed_icon: live.map(|live| live.failed_icon.clone()),
focus: live.map(|live| live.focus.clone()),
return_focus: self.return_focus.clone(),
image_key: self.image_key(meta),
icon_key: icon.as_ref().map_or(0, |(_, key)| *key),
on_open: self.on_open,
on_remove: self.on_remove,
id: self.id,
key: self.key,
}
}
fn image_key(&self, meta: Option<&LinkMeta>) -> u64 {
match &self.image {
Given::Is(image) => image.picture().1,
Given::Fetched => meta
.and_then(|meta| meta.image.as_ref())
.map_or(0, |address| Source::resource(address).cache_key()),
Given::None => 0,
}
}
fn page_icons(&self, meta: Option<&LinkMeta>, in_an_app: bool) -> Vec<Arc<str>> {
let Some(meta) = meta else {
return Vec::new();
};
let mut icons = if meta.icons.is_empty() {
meta.icon.iter().cloned().collect()
} else {
meta.icons.clone()
};
if in_an_app
&& let Some(favicon) = favicon_of(&meta.url)
&& !icons.contains(&favicon)
{
icons.push(favicon);
}
icons
}
}
impl LinkPreviewVariant {
const ALL: [Self; 3] = [Self::Card, Self::Compact, Self::Inline];
}
#[cfg(feature = "raster")]
fn looked_at(picture: &Picture, live: Option<&Live>) -> Option<(bool, f32)> {
let pixels = picture.pixels()?;
let ratio = pixels.width() as f32 / pixels.height().max(1) as f32;
let Some(live) = live else {
return Some((pixels.has_transparency(), ratio));
};
let mut seen = live.seen.lock();
let transparent = match *seen {
Some((id, said)) if id == pixels.id() => said,
_ => {
let said = pixels.has_transparency();
*seen = Some((pixels.id(), said));
said
}
};
Some((transparent, ratio))
}
#[cfg(not(feature = "raster"))]
fn looked_at(_: &Picture, _: Option<&Live>) -> Option<(bool, f32)> {
None
}
fn favicon_of(url: &str) -> Option<Arc<str>> {
let (scheme, after) = url.split_once("://")?;
if !scheme.eq_ignore_ascii_case("http") && !scheme.eq_ignore_ascii_case("https") {
return None;
}
let host = after.split(['/', '?', '#']).next().filter(|host| !host.is_empty())?;
Some(Arc::from(format!("{scheme}://{host}/favicon.ico")))
}
fn domain_of(url: &str) -> String {
let after = url.split_once("://").map_or(url, |(_, after)| after);
let host = after.split(['/', '?', '#']).next().unwrap_or(after);
let host = host.rsplit('@').next().unwrap_or(host);
let host = host.split(':').next().unwrap_or(host);
host.strip_prefix("www.").unwrap_or(host).to_owned()
}
fn address_of(url: &str) -> String {
let after = url.split_once("://").map_or(url, |(_, after)| after);
let after = after.strip_prefix("www.").unwrap_or(after);
after.strip_suffix('/').unwrap_or(after).to_owned()
}
impl From<LinkPreview> for Element {
fn from(preview: LinkPreview) -> Self {
let key = preview.key.clone();
let props = LinkPreviewProps(preview);
match key {
Some(key) => Element::component_keyed::<LinkPreviewHost>(key, props),
None => Element::component::<LinkPreviewHost>(props),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum FetcherId {
Type(TypeId),
Shared(usize),
}
impl FetcherId {
fn of<F: 'static>(_: &F) -> Self {
Self::Type(TypeId::of::<F>())
}
fn shared(fetcher: &Arc<dyn LinkFetcher>) -> Self {
Self::Shared(Arc::as_ptr(fetcher).cast::<()>() as usize)
}
}
#[derive(Default)]
struct Asked {
of: Option<(Arc<str>, FetcherId)>,
answer: Option<LinkFetch>,
asking: bool,
again: bool,
round: u64,
}
#[derive(Clone)]
struct LinkPreviewProps(LinkPreview);
impl PartialEq for LinkPreviewProps {
fn eq(&self, _: &Self) -> bool {
false
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for LinkPreviewProps {}
#[derive(Clone)]
struct Live {
tick: Signal<u64>,
asked: Arc<Mutex<Asked>>,
hovered: Signal<bool>,
failed_image: Signal<Option<u64>>,
failed_icon: Signal<Vec<u64>>,
seen: Arc<Mutex<Option<(u64, bool)>>>,
focus: FocusHandle,
}
struct LinkPreviewHost {
live: Live,
}
impl Component for LinkPreviewHost {
type Props = LinkPreviewProps;
fn create(ctx: &mut Ctx) -> Self {
Self {
live: Live {
tick: ctx.signal(0u64),
asked: Arc::new(Mutex::new(Asked::default())),
hovered: ctx.signal(false),
failed_image: ctx.signal(None),
failed_icon: ctx.signal(Vec::new()),
seen: Arc::default(),
focus: ctx.focus_handle(),
},
}
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let preview = ctx.props::<LinkPreviewProps>().0.clone();
self.live.tick.get();
let fetched = if preview.needs_nothing_fetched() {
None
} else {
let (fetcher, id) = preview.fetcher.clone().unwrap_or_else(|| {
let fetcher = link_fetcher();
let id = FetcherId::shared(&fetcher);
(fetcher, id)
});
Some(self.fetched(ctx, &preview.url, fetcher, id))
};
let theme = current_theme();
preview.view(&theme, fetched, Some(&self.live)).render_themed()
}
}
impl LinkPreviewHost {
fn fetched(&self, ctx: &Ctx, url: &Arc<str>, fetcher: Arc<dyn LinkFetcher>, id: FetcherId) -> LinkFetch {
let mut asked = self.live.asked.lock();
let anew = asked.of.as_ref().is_none_or(|(of, of_id)| of != url || *of_id != id);
if anew {
*asked = Asked {
of: Some((url.clone(), id)),
round: asked.round.wrapping_add(1),
..Asked::default()
};
}
if anew || (asked.again && !asked.asking) {
if let Some(held) = fetcher.held(url).filter(|held| *held != LinkFetch::Loading) {
asked.answer = Some(held.clone());
asked.again = false;
return held;
}
asked.asking = true;
asked.again = false;
let round = asked.round;
let bump = {
let tick = self.live.tick.clone();
move || tick.set(tick.get_untracked().wrapping_add(1))
};
let wake: LoadWaker = {
let (state, bump) = (self.live.asked.clone(), bump.clone());
Arc::new(move || {
{
let mut asked = state.lock();
if asked.round != round {
return;
}
asked.again = true;
}
bump();
})
};
let state = self.live.asked.clone();
let loader = ctx.app_handle().map(|app| app.loader().clone());
fetch_off_thread(fetcher, url.clone(), loader, wake, move |answer| {
let told = {
let mut asked = state.lock();
if asked.round != round {
return;
}
let changed = asked.answer.as_ref() != Some(&answer);
asked.answer = Some(answer);
asked.asking = false;
changed || asked.again
};
if told {
bump();
}
});
}
asked.answer.clone().unwrap_or(LinkFetch::Loading)
}
}
pub struct LinkPreviewView {
pub url: Arc<str>,
pub variant: LinkPreviewVariant,
pub title: Arc<str>,
pub description: Option<Arc<str>>,
pub site: Arc<str>,
pub site_shown: bool,
pub image: Option<Picture>,
pub transparent: bool,
pub whole: bool,
pub icon: Option<Picture>,
pub video: Option<Option<f64>>,
pub loading: bool,
pub empty: bool,
pub error: Option<LinkPreviewError>,
pub disabled: bool,
pub label: Option<Arc<str>>,
pub remove_label: Arc<str>,
pub words: DisplayWords,
pub recipe: ResolvedRecipe,
forced: Option<Interaction>,
hovered: Option<Signal<bool>>,
failed_image: Option<Signal<Option<u64>>>,
failed_icon: Option<Signal<Vec<u64>>>,
focus: Option<FocusHandle>,
return_focus: Option<ElementRef>,
image_key: u64,
icon_key: u64,
on_open: Vec<OpenHandler>,
on_remove: Vec<RemoveHandler>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl LinkPreviewView {
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 underlined(&self) -> bool {
self.forced == Some(Interaction::Hovered) || self.hovered.as_ref().is_some_and(|hovered| hovered.get())
}
pub fn name(&self) -> Arc<str> {
self.label.clone().unwrap_or_else(|| self.title.clone())
}
pub fn copy_text(&self) -> String {
if self.empty {
self.url.to_string()
} else {
let title = self.title.replace('[', "\\[").replace(']', "\\]");
format!("[{title}]({})", self.url)
}
}
pub fn tooling_description(&self) -> String {
let mut said: Vec<String> = vec![self.site.to_string()];
if let Some(description) = &self.description {
said.push(description.to_string());
}
if let Some(length) = self.video {
said.push(self.words.link_video_text(length));
}
said.join(" · ")
}
pub fn icon(&self) -> Element {
let part = self.part(LinkPreviewPart::Icon);
let side = part.rest().width.unwrap_or(Length::Icon(IconSize::Sm));
match &self.icon {
Some(picture) => {
let mut picture = picture
.clone()
.width(side)
.height(side)
.decorative(true)
.bare(true)
.rules(&self.recipe.lend(LinkPreviewPart::Icon))
.style(PicturePart::Root, |s| s.radius(RadiusSize::Sm));
if let Some(failed) = &self.failed_icon {
let (failed, key) = (failed.clone(), self.icon_key);
picture = picture.on_error(move |_| {
failed.update(|failed| {
if !failed.contains(&key) {
failed.push(key);
}
})
});
}
Row::new().part(&part.unnamed()).child(picture).into()
}
None => Row::new()
.part(&part)
.justify(Justify::Center)
.align_items(Alignment::Center)
.child(Icon::new(icons::LINK).size(side).build(self.theme()))
.into(),
}
}
pub fn site(&self) -> Row {
let part = self.part(LinkPreviewPart::Domain);
let row = Row::new()
.part(&self.part(LinkPreviewPart::Site))
.align_items(Alignment::Center)
.min_width(0.0)
.child(self.icon());
if !self.site_shown {
return row;
}
row.child(
Row::new().flex_shrink(1.0).min_width(0.0).child(
part
.text(&self.site)
.nowrap()
.text_overflow(TextOverflow::Elipsis)
.part(&part),
),
)
}
pub fn title(&self) -> Element {
let part = self.part(LinkPreviewPart::Title);
let mut text = part.text(&self.title);
if self.variant == LinkPreviewVariant::Inline {
text = text.nowrap().text_overflow(TextOverflow::Elipsis);
} else {
text = text
.width(Dimension::Pct(100.0))
.max_lines(self.lines(LinkPreviewPart::Title));
}
if self.underlined() {
text = text.underline(TextUnderline::new());
}
let holder = if self.variant == LinkPreviewVariant::Inline {
Row::new().flex_shrink(1.0).min_width(0.0)
} else {
Row::new().width(Dimension::Pct(100.0))
};
holder.child(text.part(&part)).into()
}
fn lines(&self, part: LinkPreviewPart) -> u32 {
match (self.variant, part) {
(LinkPreviewVariant::Card, _) => 2,
_ => 1,
}
}
pub fn description(&self) -> Option<Element> {
let description = self.description.as_ref()?;
let part = self.part(LinkPreviewPart::Description);
Some(
Row::new()
.width(Dimension::Pct(100.0))
.child(
part
.text(description)
.width(Dimension::Pct(100.0))
.max_lines(self.lines(LinkPreviewPart::Description))
.part(&part),
)
.into(),
)
}
pub fn skeleton(&self) -> Column {
let part = self.part(LinkPreviewPart::Skeleton);
let line = |width: Length, height: f32| -> Element {
let room = match width {
Length::Px(width) => Dimension::Px(width),
_ => Dimension::Pct(100.0),
};
Row::new()
.width(room)
.max_width(Dimension::Pct(100.0))
.child(Skeleton::line().width(Length::Fill).height(height).build(self.theme()))
.into()
};
let fill = Length::Fill;
let lines: Vec<Element> = match self.variant {
LinkPreviewVariant::Card => vec![
line(Length::Px(88.0), 10.0),
line(fill, 14.0),
line(fill, 12.0),
line(Length::Px(168.0), 12.0),
],
_ => vec![
line(Length::Px(88.0), 10.0),
line(Length::Px(232.0), 14.0),
line(Length::Px(168.0), 12.0),
],
};
Column::new()
.part(&part)
.width(Dimension::Pct(100.0))
.with_children(lines)
}
pub fn text(&self) -> Column {
let column = Column::new()
.part(&self.part(LinkPreviewPart::Text))
.flex(1.0)
.min_width(0.0);
if self.loading {
return column.child(self.skeleton());
}
let mut column = column.child(self.site()).child(self.title());
if let Some(description) = self.description() {
column = column.child(description);
}
column
}
fn video_marks(&self) -> Vec<Element> {
let Some(length) = self.video else {
return Vec::new();
};
let layer = || {
Stack::new()
.size(Dimension::Pct(100.0), Dimension::Pct(100.0))
.pointer_events_none()
};
let play_part = self.part(LinkPreviewPart::PlayIcon);
let side = play_part.rest().width.unwrap_or(Length::Icon(IconSize::Md));
let play = Row::new()
.part(&self.part(LinkPreviewPart::Play))
.justify(Justify::Center)
.align_items(Alignment::Center)
.child(
Row::new()
.part(&play_part)
.child(Icon::new(icons::PLAY).size(side).build(self.theme())),
);
let mut marks = vec![layer().stack_align(StackAlignment::Center).child(play).into()];
if let Some(length) = length {
let part = self.part(LinkPreviewPart::Length);
marks.push(
layer()
.stack_align(StackAlignment::BottomEnd)
.child(
Row::new()
.part(&part)
.child(part.text(&super::media_time_text(length, false)).nowrap().part(&part)),
)
.into(),
);
}
marks
}
pub fn media(&self) -> Option<Row> {
let part = self.part(LinkPreviewPart::Media);
let picture = match (&self.image, self.loading) {
(_, true) => Picture::empty().loading(true).decorative(true),
(Some(picture), false) => {
let mut picture = picture.clone().decorative(true);
if let Some(failed) = &self.failed_image {
let (failed, key) = (failed.clone(), self.image_key);
picture = picture.on_error(move |_| failed.set(Some(key)));
}
picture
}
(None, false) => return None,
};
let transparent = self.transparent && !self.loading;
let picture = match self.variant {
_ if transparent => picture
.fit(PictureFit::Contain)
.bare(true)
.width(Length::Fill)
.height(Length::Fill),
_ if self.whole && !self.loading => picture
.fit(PictureFit::Contain)
.width(Length::Fill)
.height(Length::Fill),
LinkPreviewVariant::Card => picture
.width(Length::Fill)
.aspect_ratio(CARD_PICTURE_RATIO)
.style(PicturePart::Root, |s| {
s.corners(RadiusSize::Xl, RadiusSize::Xl, 0.0, 0.0)
})
.style(PicturePart::Edge, |s| {
s.corners(RadiusSize::Xl, RadiusSize::Xl, 0.0, 0.0)
}),
_ => picture.width(Length::Fill).height(Length::Fill),
};
let picture = self
.video_marks()
.into_iter()
.fold(picture, Picture::child)
.rules(&self.recipe.lend(LinkPreviewPart::Media));
let mut media = Row::new().part(&part.unnamed()).overflow_visible();
if transparent && self.variant == LinkPreviewVariant::Card {
media = media.aspect_ratio(CARD_PICTURE_RATIO);
}
Some(media.child(picture))
}
pub fn close(&self) -> Option<components::Button> {
if self.on_remove.is_empty() {
return None;
}
let part = self.part(LinkPreviewPart::Close);
let icon = self.part(LinkPreviewPart::CloseIcon);
let side = icon.rest().width.unwrap_or(Length::Icon(IconSize::Sm));
let handlers = self.on_remove.clone();
let (focus, return_focus) = (self.focus.clone(), self.return_focus.clone());
let mut close = pressable(&part, self.disabled)
.align_items(Alignment::Center)
.describe("a11y_role", "button")
.describe("a11y_name", self.remove_label.clone())
.child(Row::new().part(&icon).child(Icon::new(icons::CLOSE).size(side).build(self.theme())))
.on_click(move |event: MouseEvent| {
event.stop_propagation();
for handler in &handlers {
handler();
}
match (&focus, &return_focus) {
(Some(focus), Some(element)) => focus.focus(element),
(Some(focus), None) => focus.focus_onward(),
_ => {}
}
});
if let Some(id) = &self.id {
close = close.id(format!("{id}-remove"));
}
Some(close)
}
pub fn root(&self) -> components::Button {
let mut root = pressable(&self.part(LinkPreviewPart::Root), self.disabled)
.justify(Justify::Start)
.stops_inside(true)
.describe("a11y_role", "link")
.describe("a11y_name", self.name())
.describe("a11y_description", self.tooling_description())
.copy_text(&self.copy_text());
if !self.disabled {
let (url, handlers) = (self.url.clone(), self.on_open.clone());
root = root.on_click(move |_: MouseEvent| {
if handlers.is_empty() {
if let Err(error) = crate::app::open_url(&url) {
tracing::warn!("the link preview did not open {url}: {error}");
}
} else {
handlers.iter().for_each(|handler| handler(&url));
}
});
if let Some(hovered) = &self.hovered {
let (enter, leave) = (hovered.clone(), hovered.clone());
root = root
.on_mouse_enter(move || {
if !enter.get_untracked() {
enter.set(true);
}
})
.on_mouse_leave(move || {
if leave.get_untracked() {
leave.set(false);
}
});
}
}
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 {
match self.variant {
LinkPreviewVariant::Card => {
let mut column = Column::new().width(Dimension::Pct(100.0)).min_width(0.0);
if let Some(media) = self.media() {
column = column.child(media);
}
column = column.child(self.text());
let mut stack = Stack::new()
.width(Dimension::Pct(100.0))
.stack_align(StackAlignment::TopStart)
.overflow_visible()
.child(column);
if let Some(close) = self.close() {
stack = stack.child(
Stack::new()
.size(Dimension::Pct(100.0), Dimension::Pct(100.0))
.stack_align(StackAlignment::TopEnd)
.hit_test(HitTestBehavior::ContentOnly)
.padding_top(8.0)
.padding_right(8.0)
.child(close),
);
}
self.root().align_items(Alignment::Stretch).child(stack).into()
}
LinkPreviewVariant::Compact => {
let mut root = self.root().align_items(Alignment::Center).child(self.text());
if let Some(media) = self.media() {
root = root.child(media);
}
if let Some(close) = self.close() {
root = root.child(close);
}
root.into()
}
LinkPreviewVariant::Inline => {
let mut root = self.root().align_items(Alignment::Center);
if self.loading {
root = root
.child(Row::new().part(&self.part(LinkPreviewPart::Icon)))
.child(Skeleton::line().width(96.0).height(10.0).build(self.theme()));
} else {
root = root.child(self.icon()).child(self.title());
if !self.empty && self.site_shown {
let part = self.part(LinkPreviewPart::Domain);
root = root.child(
Row::new()
.flex_shrink(0.0)
.child(part.text(&self.site).nowrap().part(&part)),
);
}
}
if let Some(close) = self.close() {
root = root.child(close);
}
root.into()
}
}
}
fn render_themed(self) -> Element {
match self.theme().renderer::<LinkPreview>() {
Some(renderer) => renderer(self),
None => self.render(),
}
}
}
impl crate::ui::Styled for LinkPreview {
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 LinkPreview {
const NAME: &'static str = "link_preview";
type Renderer = LinkPreviewRenderer;
fn parts() -> crate::ui::Declared {
LinkPreviewPart::declared()
}
fn states() -> crate::ui::Declared {
LinkPreviewState::declared()
}
fn recipe() -> Recipe {
use LinkPreviewPart::*;
use LinkPreviewState::{Disabled, Loading, Media as WithMedia, SiteIcon, Transparent};
use LinkPreviewVariant::{Card, Compact, Inline};
use PaletteColor::{
Border as Edge, Card as Surface, CardForeground, Foreground, Muted, MutedForeground, StateHover,
};
let edge = || Border::inside(BorderSize::Sm, Edge);
let white = Color::new(255, 255, 255, 255);
Recipe::new()
.base(Root, |s| {
fading(
s.background(Surface)
.foreground(CardForeground)
.border(edge())
.cursor(CursorIcon::Pointer)
.max_width(super::part_style::Length::Fill),
)
})
.variant(Card, Root, |s| s.width(360.0).radius(RadiusSize::Xl).shadow(ShadowStyle::Sm))
.variant(Compact, Root, |s| {
s.width(432.0)
.radius(RadiusSize::Xl)
.gap(12.0)
.padding_x(12.0)
.padding_y(12.0)
})
.variant(Inline, Root, |s| {
s.height(24.0)
.radius(RadiusSize::Md)
.gap(6.0)
.padding_start(5.0)
.padding_end(8.0)
.foreground(Foreground)
})
.hovered(Root, |s| s.veil(StateHover))
.when(Selector::new().variant(Card).hovered(), Root, |s| s.shadow(ShadowStyle::Md))
.focus_visible(Root, |s| s.focus_ring())
.state(Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
.variant(Card, Media, |s| s.width(super::part_style::Length::Fill))
.when(Selector::new().variant(Card).state(WithMedia), Text, |s| s.top_line(edge()))
.variant(Compact, Media, |s| s.square(72.0).radius(RadiusSize::Md))
.state(Transparent, Media, |s| s.background(white).padding_x(16.0).padding_y(16.0))
.when(Selector::new().variant(Card).state(Transparent), Media, |s| {
s.corners(RadiusSize::Xl, RadiusSize::Xl, 0.0, 0.0)
})
.when(Selector::new().variant(Compact).state(Transparent), Media, |s| {
s.padding_x(8.0).padding_y(8.0)
})
.base(Play, |s| {
s.square(48.0)
.radius(RadiusSize::Full)
.background(Color::new(0, 0, 0, 0x99))
.foreground(white)
})
.base(PlayIcon, |s| s.square(20.0))
.base(Length, |s| {
s.padding_x(6.0)
.padding_y(2.0)
.radius(RadiusSize::Sm)
.background(Color::new(0, 0, 0, 0xB3))
.foreground(white)
.font_family(FontFamily::Mono)
.font_size(11.0)
})
.variant(Card, Text, |s| {
s.gap(6.0)
.padding_top(12.0)
.padding_x(16.0)
.padding_bottom(14.0)
})
.variant(Compact, Text, |s| s.gap(4.0).padding_start(4.0))
.base(Site, |s| s.gap(6.0))
.base(Icon, |s| s.square(14.0).foreground(MutedForeground))
.variant(Card, Icon, |s| s.square(16.0))
.state(SiteIcon, Icon, |s| s.background(white).radius(RadiusSize::Sm))
.when(Selector::new().variant(Inline).state(Loading), Icon, |s| {
s.background(Muted).radius(RadiusSize::Sm)
})
.base(Domain, |s| {
s.typography(TypographyStyle::Body)
.font_size(12.0)
.weight(FontWeight::Normal)
.foreground(MutedForeground)
})
.base(Title, |s| {
s.typography(TypographyStyle::Body)
.font_size(14.0)
.weight(FontWeight::Medium)
.line_height(1.4)
.foreground(CardForeground)
})
.variant(Inline, Title, |s| s.font_size(13.0).foreground(Foreground))
.base(Description, |s| {
s.typography(TypographyStyle::Body)
.font_size(13.0)
.weight(FontWeight::Normal)
.line_height(1.4)
.foreground(MutedForeground)
})
.variant(Card, Description, |s| s.line_height(1.5))
.base(Skeleton, |s| s.gap(10.0).padding_y(4.0))
.variant(Card, Skeleton, |s| s.padding_top(2.0).padding_bottom(4.0))
.base(Close, |s| {
fading(
s.square(28.0)
.radius(RadiusSize::Md)
.foreground(Foreground)
.cursor(CursorIcon::Pointer),
)
})
.hovered(Close, |s| s.veil(StateHover))
.focus_visible(Close, |s| s.focus_ring())
.when(Selector::new().variant(Card).state(WithMedia), Close, |s| {
s.radius(RadiusSize::Full)
.background(Color::new(0, 0, 0, 0x99))
.foreground(white)
})
.variant(Inline, Close, |s| s.square(20.0))
.base(CloseIcon, |s| s.square(16.0))
.variant(Inline, CloseIcon, |s| s.square(14.0))
}
}