diff --git a/.changeset/fix-tauri-external-link.md b/.changeset/fix-tauri-external-link.md new file mode 100644 index 000000000..38c0f6343 --- /dev/null +++ b/.changeset/fix-tauri-external-link.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +tauri: make sure external links open in separate browser, instead of replacing the app diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e80dd3f46..b33a631fb 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -156,6 +156,30 @@ pub fn show_or_create_main_window(app: &AppHandle) -> taur .use_https_scheme(true) .background_color(tauri::window::Color(0x1A, 0x1C, 0x28, 0xFF)); + // Only android: intercept navigation to external URLs and open them in the system browser + // Other platforms: might need other URI schemes, and on_navigation might get called on iframe urls + // (as of now, the following is confirmed not to work with linux/webkit2gtk) + #[cfg(target_os = "android")] + let builder = { + let nav_handle = app.clone(); + builder.on_navigation(move |url| { + let internal = url.scheme() == "tauri" + || url.host_str() == Some("tauri.localhost") + || (cfg!(dev) && url.host_str() == Some("localhost")); + if !internal { + // open in new thread + // open_url blocks on the ui thread but we are on the ui thread... + let handle = nav_handle.clone(); + let url = url.to_string(); + std::thread::spawn(move || { + use tauri_plugin_opener::OpenerExt; + let _ = handle.opener().open_url(url, None::<&str>); + }); + } + internal + }) + }; + #[cfg(desktop)] let title = main_window_title(app); diff --git a/src/app/components/user-profile/UserChips.tsx b/src/app/components/user-profile/UserChips.tsx index c2709a9b3..b75e9f295 100644 --- a/src/app/components/user-profile/UserChips.tsx +++ b/src/app/components/user-profile/UserChips.tsx @@ -2,6 +2,7 @@ import type { KeyboardEventHandler, MouseEventHandler } from 'react'; import type { CSSProperties } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; +import { isTauri } from '@tauri-apps/api/core'; import FocusTrap from 'focus-trap-react'; import { isKeyHotkey } from 'is-hotkey'; import type { Room } from '$types/matrix-sdk'; @@ -173,8 +174,15 @@ export function ServerChip({ size="300" radii="300" onClick={() => { - window.open(`https://${server}`, '_blank'); + const url = `https://${server}`; close(); + if (isTauri()) { + import('@tauri-apps/plugin-opener') + .then(({ openUrl }) => openUrl(url)) + .catch(() => window.open(url, '_blank')); + return; + } + window.open(url, '_blank'); }} className={css.UserHeroMenuItem} style={heroMenuItemStyle(