wayland: Implement xdg-activation when opening urls (#11368)

Since Wayland doesn't have a way for windows to activate themselves,
currently, when you click on a link in Zed, the browser window opens in
the background.

This PR implements the `xdg-activation` protocol to get an activation
token, which the browser can use to raise its window.


https://github.com/zed-industries/zed/assets/71973804/8b3456c0-89f8-4201-b1cb-633a149796b7

Release Notes:

- N/A
This commit is contained in:
apricotbucket28 2024-05-03 18:02:39 -03:00 committed by GitHub
parent dccf6dae01
commit 86696d88cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 79 additions and 5 deletions

View file

@ -61,6 +61,7 @@ pub trait LinuxClient {
options: WindowParams,
) -> Box<dyn PlatformWindow>;
fn set_cursor_style(&self, style: CursorStyle);
fn open_uri(&self, uri: &str);
fn write_to_primary(&self, item: ClipboardItem);
fn write_to_clipboard(&self, item: ClipboardItem);
fn read_from_primary(&self) -> Option<ClipboardItem>;
@ -216,7 +217,7 @@ impl<P: LinuxClient + 'static> Platform for P {
}
fn open_url(&self, url: &str) {
open::that(url);
self.open_uri(url);
}
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) {
@ -484,6 +485,20 @@ impl<P: LinuxClient + 'static> Platform for P {
}
}
pub(super) fn open_uri_internal(uri: &str, activation_token: Option<&str>) {
let mut last_err = None;
for mut command in open::commands(uri) {
if let Some(token) = activation_token {
command.env("XDG_ACTIVATION_TOKEN", token);
}
match command.status() {
Ok(_) => return,
Err(err) => last_err = Some(err),
}
}
log::error!("failed to open uri: {uri:?}, last error: {last_err:?}");
}
pub(super) fn is_within_click_distance(a: Point<Pixels>, b: Point<Pixels>) -> bool {
let diff = a - b;
diff.x.abs() <= DOUBLE_CLICK_DISTANCE && diff.y.abs() <= DOUBLE_CLICK_DISTANCE