use gpui::{AnyElement, App, Global, SharedString, Window};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Preview {
pub title: Option<SharedString>,
pub description: Option<SharedString>,
pub image: Option<SharedString>,
pub icon: Option<SharedString>,
pub label: Option<SharedString>,
pub glyph: Option<ui::icons::Icon>,
}
pub type LinkCard = fn(url: &str, form: crate::Form, &mut Window, &mut App) -> Option<AnyElement>;
struct InstalledCard(LinkCard);
impl Global for InstalledCard {}
pub(crate) fn set_link_card(cx: &mut App, card: LinkCard) {
cx.set_global(InstalledCard(card));
}
pub(crate) fn card(
url: &str,
form: crate::Form,
window: &mut Window,
cx: &mut App,
) -> Option<AnyElement> {
let card = cx.try_global::<InstalledCard>()?.0;
card(url, form, window, cx)
}
pub type LinkHover = fn(url: &str, &mut Window, &mut App) -> Option<AnyElement>;
struct InstalledHover(LinkHover);
impl Global for InstalledHover {}
pub(crate) fn set_link_hover(cx: &mut App, hover: LinkHover) {
cx.set_global(InstalledHover(hover));
}
pub(crate) fn hover(url: &str, window: &mut Window, cx: &mut App) -> Option<AnyElement> {
let hover = cx.try_global::<InstalledHover>()?.0;
hover(url, window, cx)
}
pub type LinkPreview = fn(url: &str, cx: &App) -> Option<Preview>;
struct Installed(LinkPreview);
impl Global for Installed {}
pub(crate) fn set_link_preview(cx: &mut App, preview: LinkPreview) {
cx.set_global(Installed(preview));
}
pub(crate) fn of(cx: &App, url: &str) -> Option<Preview> {
let preview = cx.try_global::<Installed>()?.0;
preview(url, cx)
}
pub(crate) fn host(url: &str) -> &str {
let after = url.split_once("://").map_or(url, |(_, rest)| rest);
let host = after.split(['/', '?', '#']).next().unwrap_or(after);
host.strip_prefix("www.").unwrap_or(host)
}