feat(links): show "Opening <host>..." while a clicked link opens
- message links, flow text links and link previews go through one open_link hook - the app raises a short notice, then hands the URL to the system Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -745,7 +745,7 @@ impl Element for FlowText {
745
745
if let Some(url) = url
746
746
&& !open_profile_link(&url, event.position, cx)
747
747
{
748
cx.open_url(&url);
748
super::hooks::open_link(&url, cx);
749
749
}
750
750
});
751
751
}
@@ -745,7 +745,7 @@ impl Element for FlowText {745
if let Some(url) = url745
if let Some(url) = url746
&& !open_profile_link(&url, event.position, cx)746
&& !open_profile_link(&url, event.position, cx)747
{747
{748
cx.open_url(&url);748
super::hooks::open_link(&url, cx);749
}749
}750
});750
});751
}751
}@@ -5,6 +5,7 @@ pub struct RenderHooks {
5
5
pub open_profile_link: fn(&str, Point<Pixels>, &mut App) -> bool,
6
6
pub jump_to_quoted: fn(String, &mut App),
7
7
pub inline_image: fn(&str, &App) -> Option<std::path::PathBuf>,
8
pub open_link: fn(&str, &mut App),
8
9
}
9
10
10
11
impl Global for RenderHooks {}
@@ -16,6 +17,13 @@ pub fn open_profile_link(url: &str, position: Point<Pixels>, cx: &mut App) -> bo
16
17
}
17
18
}
18
19
20
pub fn open_link(url: &str, cx: &mut App) {
21
match cx.try_global::<RenderHooks>().copied() {
22
Some(hooks) => (hooks.open_link)(url, cx),
23
None => cx.open_url(url),
24
}
25
}
26
19
27
pub fn jump_to_quoted(message_id: String, cx: &mut App) {
20
28
if let Some(hooks) = cx.try_global::<RenderHooks>().copied() {
21
29
(hooks.jump_to_quoted)(message_id, cx);
@@ -5,6 +5,7 @@ pub struct RenderHooks {5
pub open_profile_link: fn(&str, Point<Pixels>, &mut App) -> bool,5
pub open_profile_link: fn(&str, Point<Pixels>, &mut App) -> bool,6
pub jump_to_quoted: fn(String, &mut App),6
pub jump_to_quoted: fn(String, &mut App),7
pub inline_image: fn(&str, &App) -> Option<std::path::PathBuf>,7
pub inline_image: fn(&str, &App) -> Option<std::path::PathBuf>,8
pub open_link: fn(&str, &mut App),8
}9
}9
10
10
impl Global for RenderHooks {}11
impl Global for RenderHooks {}@@ -16,6 +17,13 @@ pub fn open_profile_link(url: &str, position: Point<Pixels>, cx: &mut App) -> bo16
}17
}17
}18
}18
19
20
pub fn open_link(url: &str, cx: &mut App) {21
match cx.try_global::<RenderHooks>().copied() {22
Some(hooks) => (hooks.open_link)(url, cx),23
None => cx.open_url(url),24
}25
}26
19
pub fn jump_to_quoted(message_id: String, cx: &mut App) {27
pub fn jump_to_quoted(message_id: String, cx: &mut App) {20
if let Some(hooks) = cx.try_global::<RenderHooks>().copied() {28
if let Some(hooks) = cx.try_global::<RenderHooks>().copied() {21
(hooks.jump_to_quoted)(message_id, cx);29
(hooks.jump_to_quoted)(message_id, cx);@@ -9,7 +9,7 @@ use gpui_kit::base::{
9
9
use gpui_kit::*;
10
10
11
11
use super::blocks::{CODE_PADDING, EMOJI_PLACEHOLDER};
12
use super::hooks::open_profile_link;
12
use super::hooks::{open_link, open_profile_link};
13
13
14
14
pub(super) const TIME_ROOM: char = '\u{2007}';
15
15
pub(super) const HIT_SLOP: f32 = 12.;
@@ -402,7 +402,7 @@ impl Element for SelectableRichText {
402
402
if let Some(url) = link_at(&layout, &links, event.position)
403
403
&& !open_profile_link(&url, event.position, cx)
404
404
{
405
cx.open_url(&url);
405
open_link(&url, cx);
406
406
}
407
407
});
408
408
}
@@ -9,7 +9,7 @@ use gpui_kit::base::{9
use gpui_kit::*;9
use gpui_kit::*;10
10
11
use super::blocks::{CODE_PADDING, EMOJI_PLACEHOLDER};11
use super::blocks::{CODE_PADDING, EMOJI_PLACEHOLDER};12
use super::hooks::open_profile_link;12
use super::hooks::{open_link, open_profile_link};13
13
14
pub(super) const TIME_ROOM: char = '\u{2007}';14
pub(super) const TIME_ROOM: char = '\u{2007}';15
pub(super) const HIT_SLOP: f32 = 12.;15
pub(super) const HIT_SLOP: f32 = 12.;@@ -402,7 +402,7 @@ impl Element for SelectableRichText {402
if let Some(url) = link_at(&layout, &links, event.position)402
if let Some(url) = link_at(&layout, &links, event.position)403
&& !open_profile_link(&url, event.position, cx)403
&& !open_profile_link(&url, event.position, cx)404
{404
{405
cx.open_url(&url);405
open_link(&url, cx);406
}406
}407
});407
});408
}408
}@@ -158,7 +158,7 @@ pub fn link_preview_card(
158
158
),
159
159
)
160
160
.children(close_button)
161
.on_click(move |_, _, cx| cx.open_url(&url))
161
.on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&url, cx))
162
162
.into_any_element()
163
163
}
164
164
@@ -158,7 +158,7 @@ pub fn link_preview_card(158
),158
),159
)159
)160
.children(close_button)160
.children(close_button)161
.on_click(move |_, _, cx| cx.open_url(&url))161
.on_click(move |_, _, cx| app_ui_kit::render::hooks::open_link(&url, cx))162
.into_any_element()162
.into_any_element()163
}163
}164
164
@@ -103,9 +103,32 @@ pub fn install_globals(state: &Entity<AppState>, user_input: Arc<UserInput>, cx:
103
103
open_profile_link: super::profile_card::open_profile_link,
104
104
jump_to_quoted,
105
105
inline_image,
106
open_link,
106
107
});
107
108
}
108
109
110
fn open_link(url: &str, cx: &mut App) {
111
if let Some(handle) = cx.try_global::<crate::app_state::AppHandle>() {
112
let text = opening_link_text(url);
113
handle.0.clone().update(cx, |state, cx| state.raise_notice(text, None, cx));
114
}
115
cx.open_url(url);
116
}
117
118
fn opening_link_text(url: &str) -> String {
119
let host = url
120
.split_once("://")
121
.map_or(url, |(_, rest)| rest)
122
.split(['/', '?', '#'])
123
.next()
124
.unwrap_or_default();
125
if host.is_empty() {
126
"Opening link...".to_owned()
127
} else {
128
format!("Opening {host}...")
129
}
130
}
131
109
132
fn inline_image(url: &str, cx: &App) -> Option<std::path::PathBuf> {
110
133
let handle = cx.try_global::<crate::app_state::AppHandle>()?;
111
134
let entry = handle.0.read(cx).directory.image(url)?;
@@ -1293,3 +1316,15 @@ impl Render for AppShell {
1293
1316
.child(crate::frame_log::probe("last"))
1294
1317
}
1295
1318
}
1319
1320
#[cfg(test)]
1321
mod opening_link_tests {
1322
use super::opening_link_text;
1323
1324
#[test]
1325
fn the_notice_names_the_host() {
1326
assert_eq!(opening_link_text("https://example.com/a?b#c"), "Opening example.com...");
1327
assert_eq!(opening_link_text("mailto:"), "Opening mailto:...");
1328
assert_eq!(opening_link_text(""), "Opening link...");
1329
}
1330
}
@@ -103,9 +103,32 @@ pub fn install_globals(state: &Entity<AppState>, user_input: Arc<UserInput>, cx:103
open_profile_link: super::profile_card::open_profile_link,103
open_profile_link: super::profile_card::open_profile_link,104
jump_to_quoted,104
jump_to_quoted,105
inline_image,105
inline_image,106
open_link,106
});107
});107
}108
}108
109
110
fn open_link(url: &str, cx: &mut App) {111
if let Some(handle) = cx.try_global::<crate::app_state::AppHandle>() {112
let text = opening_link_text(url);113
handle.0.clone().update(cx, |state, cx| state.raise_notice(text, None, cx));114
}115
cx.open_url(url);116
}117
118
fn opening_link_text(url: &str) -> String {119
let host = url120
.split_once("://")121
.map_or(url, |(_, rest)| rest)122
.split(['/', '?', '#'])123
.next()124
.unwrap_or_default();125
if host.is_empty() {126
"Opening link...".to_owned()127
} else {128
format!("Opening {host}...")129
}130
}131
109
fn inline_image(url: &str, cx: &App) -> Option<std::path::PathBuf> {132
fn inline_image(url: &str, cx: &App) -> Option<std::path::PathBuf> {110
let handle = cx.try_global::<crate::app_state::AppHandle>()?;133
let handle = cx.try_global::<crate::app_state::AppHandle>()?;111
let entry = handle.0.read(cx).directory.image(url)?;134
let entry = handle.0.read(cx).directory.image(url)?;@@ -1293,3 +1316,15 @@ impl Render for AppShell {1293
.child(crate::frame_log::probe("last"))1316
.child(crate::frame_log::probe("last"))1294
}1317
}1295
}1318
}1319
1320
#[cfg(test)]1321
mod opening_link_tests {1322
use super::opening_link_text;1323
1324
#[test]1325
fn the_notice_names_the_host() {1326
assert_eq!(opening_link_text("https://example.com/a?b#c"), "Opening example.com...");1327
assert_eq!(opening_link_text("mailto:"), "Opening mailto:...");1328
assert_eq!(opening_link_text(""), "Opening link...");1329
}1330
}