Skip to content

Commit cd2409f

Browse files
committed
fix: open in default browser with external crate
1 parent 91a0dc9 commit cd2409f

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src-tauri/Cargo.lock

Lines changed: 45 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ once_cell = "1.19.0"
1919
percent-encoding = "2.3"
2020
regex = "1.10.3"
2121
clipboard-files = "0.1.1"
22+
webbrowser = "0.8.12"
2223
serde = { version = "1.0", features = ["derive"] }
2324
tauri = { version = "1.5.4", features = [ "cli", "api-all", "updater", "devtools", "linux-protocol-headers"] }
2425
winapi = { version = "0.3", features = ["fileapi"] }

src-tauri/src/main.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ windows_subsystem = "windows"
44
)]
55
use std::env;
66

7+
use webbrowser;
8+
79
use tauri::{Manager};
810
use std::path::PathBuf;
911

@@ -179,6 +181,16 @@ fn zoom_window(window: tauri::Window, scale_factor: f64) {
179181
});
180182
}
181183

184+
#[tauri::command]
185+
fn open_url_in_browser(url: String) -> Result<(), String> {
186+
// Attempt to open the URL in the default web browser
187+
if webbrowser::open(&url).is_ok() {
188+
Ok(())
189+
} else {
190+
Err("Failed to open URL in the browser".into())
191+
}
192+
}
193+
182194
fn process_window_event(event: &GlobalWindowEvent) {
183195
if let tauri::WindowEvent::CloseRequested { .. } = event.event() {
184196
// this does nothing and is here if in future you need to persist something on window close.
@@ -291,7 +303,7 @@ fn main() {
291303
.invoke_handler(tauri::generate_handler![
292304
get_mac_deep_link_requests,
293305
toggle_devtools, console_log, console_error, _get_commandline_args, get_current_working_dir,
294-
_get_window_labels,
306+
_get_window_labels, open_url_in_browser,
295307
_get_windows_drives, _rename_path, show_in_folder, zoom_window, _get_clipboard_files])
296308
.setup(|app| {
297309
init::init_app(app);

0 commit comments

Comments
 (0)