From a32ba7470b5acd556c816ad249441532a4df7516 Mon Sep 17 00:00:00 2001 From: jake <77554505+brxken128@users.noreply.github.com> Date: Tue, 17 Oct 2023 08:17:56 +0100 Subject: [PATCH] [ENG-1289] Reload webview support for Windows and Linux (#1590) * full webview reload for linux/windows * cleanup platform-specific code * code cleanup & bindings * add reload to the debug popover * try to fix windows builds * even better fix --- Cargo.lock | 3 +- apps/desktop/src-tauri/Cargo.toml | 23 ++++++++--- apps/desktop/src-tauri/src/main.rs | 39 +++++++++++++++++-- apps/desktop/src-tauri/src/menu.rs | 18 ++------- apps/desktop/src/commands.ts | 4 ++ .../Layout/Sidebar/DebugPopover.tsx | 15 +++++++ interface/util/Platform.tsx | 1 + 7 files changed, 78 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5878f22b3..7f34e6afc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6787,7 +6787,6 @@ name = "sd-desktop" version = "0.1.1" dependencies = [ "axum", - "dotenv", "futures", "http", "opener", @@ -6808,6 +6807,8 @@ dependencies = [ "tokio", "tracing 0.2.0", "uuid", + "webkit2gtk", + "webview2-com", "window-shadows", ] diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 36f14556f..dd0410d70 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -9,13 +9,23 @@ repository = { workspace = true } edition = { workspace = true } [dependencies] -tauri = { version = "1.5.2", features = ["dialog-all", "linux-protocol-headers", "macos-private-api", "os-all", "path-all", "protocol-all", "shell-all", "updater", "window-all"] } +tauri = { version = "1.5.2", features = [ + "dialog-all", + "linux-protocol-headers", + "macos-private-api", + "os-all", + "path-all", + "protocol-all", + "shell-all", + "updater", + "window-all", +] } rspc = { workspace = true, features = ["tauri"] } sd-core = { path = "../../../core", features = [ - "ffmpeg", - "location-watcher", - "heif", + "ffmpeg", + "location-watcher", + "heif", ] } tokio = { workspace = true, features = ["sync"] } window-shadows = "0.2.1" @@ -33,19 +43,20 @@ rand = "0.8.5" prisma-client-rust = { workspace = true } sd-prisma = { path = "../../../crates/prisma" } -dotenv = "0.15.0" [target.'cfg(target_os = "linux")'.dependencies] sd-desktop-linux = { path = "../crates/linux" } +webkit2gtk = { version = "0.18.2", features = ["v2_2"] } [target.'cfg(target_os = "macos")'.dependencies] sd-desktop-macos = { path = "../crates/macos" } [target.'cfg(target_os = "windows")'.dependencies] sd-desktop-windows = { path = "../crates/windows" } +webview2-com = "0.19.1" [build-dependencies] -tauri-build = { version = "1.5.0", features = [] } +tauri-build = "1.5.0" [features] default = ["custom-protocol"] diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs index 9b2214fe3..4f1b0f7df 100644 --- a/apps/desktop/src-tauri/src/main.rs +++ b/apps/desktop/src-tauri/src/main.rs @@ -7,7 +7,7 @@ use std::{fs, path::PathBuf, sync::Arc, time::Duration}; use sd_core::{Node, NodeError}; -use tauri::{api::path, ipc::RemoteDomainAccessScope, AppHandle, Manager}; +use tauri::{api::path, ipc::RemoteDomainAccessScope, window::PlatformWebview, AppHandle, Manager}; use tauri_plugins::{sd_error_plugin, sd_server_plugin}; use tokio::time::sleep; use tracing::error; @@ -28,6 +28,39 @@ async fn app_ready(app_handle: AppHandle) { window.show().unwrap(); } +#[tauri::command(async)] +#[specta::specta] +async fn reload_webview(app_handle: AppHandle) { + app_handle + .get_window("main") + .expect("Error getting window handle") + .with_webview(reload_webview_inner) + .expect("Error while reloading webview"); +} + +fn reload_webview_inner(webview: PlatformWebview) { + #[cfg(target_os = "macos")] + { + unsafe { + sd_desktop_macos::reload_webview(&(webview.inner() as _)); + } + } + #[cfg(target_os = "linux")] + { + use webkit2gtk::traits::WebViewExt; + + webview.inner().reload(); + } + #[cfg(target_os = "windows")] + unsafe { + webview + .controller() + .CoreWebView2() + .expect("Unable to get handle on inner webview") + .Reload(); + } +} + #[tauri::command(async)] #[specta::specta] async fn reset_spacedrive(app_handle: AppHandle) { @@ -76,9 +109,6 @@ const CLIENT_ID: &str = "2abb241e-40b8-4517-a3e3-5594375c8fbb"; #[tokio::main] async fn main() -> tauri::Result<()> { - #[cfg(debug_assertions)] - dotenv::dotenv().ok(); - #[cfg(target_os = "linux")] sd_desktop_linux::normalize_environment(); @@ -182,6 +212,7 @@ async fn main() -> tauri::Result<()> { app_ready, reset_spacedrive, open_logs_dir, + reload_webview, file::open_file_paths, file::open_ephemeral_files, file::get_file_path_open_with_apps, diff --git a/apps/desktop/src-tauri/src/menu.rs b/apps/desktop/src-tauri/src/menu.rs index 54a1f2def..d3257b334 100644 --- a/apps/desktop/src-tauri/src/menu.rs +++ b/apps/desktop/src-tauri/src/menu.rs @@ -103,20 +103,10 @@ pub(crate) fn handle_menu_event(event: WindowMenuEvent) { .emit("keybind", "open_search".to_string()) .unwrap(), "reload_app" => { - #[cfg(target_os = "macos")] - { - event - .window() - .with_webview(|webview| { - unsafe { sd_desktop_macos::reload_webview(&(webview.inner() as _)) }; - }) - .unwrap(); - } - - #[cfg(not(target_os = "macos"))] - { - unimplemented!(); - } + event + .window() + .with_webview(crate::reload_webview_inner) + .expect("Error while reloading webview"); } #[cfg(debug_assertions)] "toggle_devtools" => { diff --git a/apps/desktop/src/commands.ts b/apps/desktop/src/commands.ts index 70485e77c..e9d66ac3e 100644 --- a/apps/desktop/src/commands.ts +++ b/apps/desktop/src/commands.ts @@ -22,6 +22,10 @@ export function openLogsDir() { return invoke()("open_logs_dir") } +export function reloadWebview() { + return invoke()("reload_webview") +} + export function openFilePaths(library: string, ids: number[]) { return invoke()("open_file_paths", { library,ids }) } diff --git a/interface/app/$libraryId/Layout/Sidebar/DebugPopover.tsx b/interface/app/$libraryId/Layout/Sidebar/DebugPopover.tsx index 00acb1f30..1300e2707 100644 --- a/interface/app/$libraryId/Layout/Sidebar/DebugPopover.tsx +++ b/interface/app/$libraryId/Layout/Sidebar/DebugPopover.tsx @@ -111,6 +111,21 @@ export default () => { )} + {platform.reloadWebview && ( + +
+ +
+
+ )} ; getFilePathOpenWithApps?(library: string, ids: number[]): Promise; + reloadWebview?(): Promise; getEphemeralFilesOpenWithApps?(paths: string[]): Promise; openFilePathWith?(library: string, fileIdsAndAppUrls: [number, string][]): Promise; openEphemeralFileWith?(pathsAndUrls: [string, string][]): Promise;