[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
This commit is contained in:
jake 2023-10-17 08:17:56 +01:00 committed by GitHub
parent 1b856848e8
commit a32ba7470b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 25 deletions

3
Cargo.lock generated
View file

@ -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",
]

View file

@ -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"]

View file

@ -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,

View file

@ -103,20 +103,10 @@ pub(crate) fn handle_menu_event(event: WindowMenuEvent<Wry>) {
.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" => {

View file

@ -22,6 +22,10 @@ export function openLogsDir() {
return invoke()<null>("open_logs_dir")
}
export function reloadWebview() {
return invoke()<null>("reload_webview")
}
export function openFilePaths(library: string, ids: number[]) {
return invoke()<OpenFilePathResult[]>("open_file_paths", { library,ids })
}

View file

@ -111,6 +111,21 @@ export default () => {
</div>
</Setting>
)}
{platform.reloadWebview && (
<Setting mini title="Reload webview" description="Reload the window's webview">
<div className="mt-2">
<Button
size="sm"
variant="gray"
onClick={() => {
platform.reloadWebview && platform.reloadWebview();
}}
>
Reload
</Button>
</div>
</Setting>
)}
<Setting
mini
title="React Query Devtools"

View file

@ -37,6 +37,7 @@ export type Platform = {
)[]
): Promise<unknown>;
getFilePathOpenWithApps?(library: string, ids: number[]): Promise<unknown>;
reloadWebview?(): Promise<unknown>;
getEphemeralFilesOpenWithApps?(paths: string[]): Promise<unknown>;
openFilePathWith?(library: string, fileIdsAndAppUrls: [number, string][]): Promise<unknown>;
openEphemeralFileWith?(pathsAndUrls: [string, string][]): Promise<unknown>;