From 2554fce70e83f7b1f5a4a485a095b26f3eb9bdec Mon Sep 17 00:00:00 2001 From: david Date: Wed, 17 Mar 2021 20:17:37 -0400 Subject: [PATCH] fix(tauri/webview): Use different user_data_path for Windows (#1365) (#1366) Co-authored-by: Lucas Nogueira --- tauri/Cargo.toml | 2 +- tauri/src/app/webview.rs | 3 +++ tauri/src/app/webview/wry.rs | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/tauri/Cargo.toml b/tauri/Cargo.toml index bc6b06ca3..09a2afcc0 100644 --- a/tauri/Cargo.toml +++ b/tauri/Cargo.toml @@ -30,7 +30,7 @@ thiserror = "1.0.24" once_cell = "1.7.2" tauri-api = { version = "0.7.5", path = "../tauri-api" } tauri-macros = { version = "0.1", path = "../tauri-macros" } -wry = { git = "https://github.com/tauri-apps/wry", rev = "39575983dbd128fbbcde933d742b33b691fd1def" } +wry = { git = "https://github.com/tauri-apps/wry", rev = "8dd58eec77d4c89491b1af427d06c4ee6cfa8e58" } rand = "0.8" [build-dependencies] diff --git a/tauri/src/app/webview.rs b/tauri/src/app/webview.rs index 1c5d85ed2..4e3433090 100644 --- a/tauri/src/app/webview.rs +++ b/tauri/src/app/webview.rs @@ -154,6 +154,9 @@ pub trait WebviewBuilderExt: Sized { /// Whether the icon was set or not. fn has_icon(&self) -> bool; + /// User data path for the webview. Actually only supported on Windows. + fn user_data_path(self, user_data_path: Option) -> Self; + /// Builds the webview instance. fn finish(self) -> crate::Result; } diff --git a/tauri/src/app/webview/wry.rs b/tauri/src/app/webview/wry.rs index bdff61a20..fad057fa7 100644 --- a/tauri/src/app/webview/wry.rs +++ b/tauri/src/app/webview/wry.rs @@ -3,12 +3,17 @@ use super::{ RpcRequest, WebviewBuilderExt, WebviewBuilderExtPrivate, WebviewRpcHandler, WindowConfig, }; +use crate::plugin::PluginStore; use once_cell::sync::Lazy; -use crate::plugin::PluginStore; +#[cfg(target_os = "windows")] +use std::fs::create_dir_all; +#[cfg(target_os = "windows")] +use tauri_api::path::{resolve_path, BaseDirectory}; use std::{ convert::{TryFrom, TryInto}, + path::PathBuf, sync::{Arc, Mutex}, }; @@ -65,6 +70,30 @@ impl From for wry::Attributes { if let Some(y) = window_config.0.y { webview = webview.y(y); } + + // If we are on windows use App Data Local as user_data + // to prevent any bundled application to failed. + + // Should fix: + // https://github.com/tauri-apps/tauri/issues/1365 + + #[cfg(target_os = "windows")] + { + //todo(lemarier): we should replace with AppName from the context + // will be available when updater will merge + + // https://docs.rs/dirs-next/2.0.0/dirs_next/fn.data_local_dir.html + + let local_app_data = resolve_path("Tauri", Some(BaseDirectory::LocalData)); + + if let Ok(user_data_dir) = local_app_data { + // Make sure the directory exist without panic + if let Ok(()) = create_dir_all(&user_data_dir) { + webview = webview.user_data_path(Some(user_data_dir)); + } + } + } + webview } } @@ -172,6 +201,11 @@ impl WebviewBuilderExt for wry::Attributes { self.icon.is_some() } + fn user_data_path(mut self, user_data_path: Option) -> Self { + self.user_data_path = user_data_path; + self + } + fn finish(self) -> crate::Result { Ok(self) }