fix(tauri/webview): Use different user_data_path for Windows (#1365) (#1366)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
david
2021-03-17 20:17:37 -04:00
committed by GitHub
parent 914e46342c
commit 2554fce70e
3 changed files with 39 additions and 2 deletions

View File

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

View File

@@ -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<PathBuf>) -> Self;
/// Builds the webview instance.
fn finish(self) -> crate::Result<Self::Webview>;
}

View File

@@ -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<WindowConfig> 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<PathBuf>) -> Self {
self.user_data_path = user_data_path;
self
}
fn finish(self) -> crate::Result<Self::Webview> {
Ok(self)
}