Skip to main content

markdown/
link.rs

1//! Who opens a link.
2//!
3//! Installed once at boot like the highlighter, and read at click. Without it a
4//! link goes to [`App::open_url`].
5
6use gpui::{App, Global, Window};
7
8/// Opens a link a reader clicked: an inline link or a bookmark card.
9pub type LinkHandler = fn(url: &str, &mut Window, &mut App);
10
11struct Installed(LinkHandler);
12
13impl Global for Installed {}
14
15/// `cx.set_link_handler(my_links)` — call once at boot.
16pub(crate) fn set_link_handler(cx: &mut App, handler: LinkHandler) {
17    cx.set_global(Installed(handler));
18}
19
20pub(crate) fn open(url: &str, window: &mut Window, cx: &mut App) {
21    match cx.try_global::<Installed>().map(|installed| installed.0) {
22        Some(handler) => handler(url, window, cx),
23        None => cx.open_url(url),
24    }
25}