From b20dbc9ad724420f495bfe4af04cfa9d74d5e34f Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 06:52:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(settings):=20=E3=80=8C=E6=89=93=E5=BC=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9B=AE=E5=BD=95=E3=80=8D=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=89=93=E5=BC=80=E8=B5=84=E6=96=99=E7=9B=AE=E5=BD=95=EF=BC=8C?= =?UTF-8?q?=E6=96=87=E6=A1=88=E4=B8=8D=E5=86=8D=E6=8F=90=20localStorage/In?= =?UTF-8?q?dexedDB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/lib.rs | 18 +++++++++++++++++- src/ui-settings-sections.js | 7 ++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f8a4e1c..e26faa8 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -136,13 +136,29 @@ fn grant_dir(app: AppHandle, dir: String) -> Result { app.fs_scope() .allow_directory(&p, true) .map_err(|e| e.to_string())?; + // 记到 app config:这样「打开资料目录」按钮不需要前端传路径,也不给前端任意开目录的能力 + if let Ok(cfg) = app.path().app_config_dir() { + let _ = std::fs::create_dir_all(&cfg); + let _ = std::fs::write(cfg.join("data-dir.txt"), p.to_string_lossy().as_bytes()); + } Ok(p.to_string_lossy().to_string()) } // 打开应用数据目录(设置里的「打开数据目录」按钮用;macOS 访达 / Windows 资源管理器 / Linux xdg-open) #[tauri::command] fn open_app_data_dir(app: AppHandle) -> Result { - let dir = app.path().app_data_dir().map_err(|e| e.to_string())?; + // 资料目录(用户选的那个)优先;没设置过才退回应用私有目录 + let recorded = app + .path() + .app_config_dir() + .ok() + .and_then(|c| std::fs::read_to_string(c.join("data-dir.txt")).ok()) + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty() && std::path::Path::new(s).exists()); + let dir = match recorded { + Some(p) => std::path::PathBuf::from(p), + None => app.path().app_data_dir().map_err(|e| e.to_string())?, + }; std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; let path = dir.to_string_lossy().to_string(); #[cfg(target_os = "macos")] diff --git a/src/ui-settings-sections.js b/src/ui-settings-sections.js index 2407ace..548a79e 100644 --- a/src/ui-settings-sections.js +++ b/src/ui-settings-sections.js @@ -139,16 +139,17 @@ function DataDirRow () { const D = (typeof window !== 'undefined') ? window.IWBDesktop : null const enabled = !!(D && D.isTauri && D.isTauri()) - const [path, setPath] = React.useState('') + // 初始就把「资料目录」显示出来(不等点按钮),避免把应用私有目录误当成数据所在 + const [path, setPath] = React.useState(() => { try { return (window.IWBStorageFile && window.IWBStorageFile.dir) ? window.IWBStorageFile.dir() : '' } catch (e) { return '' } }) const [err, setErr] = React.useState('') if (!enabled) return el('div', { className: 'iwb-hint' }, '数据保存:本浏览器(localStorage/IndexedDB)') const open = async () => { try { setPath(await D.openDataDir()); setErr('') } catch (e) { setErr(String((e && e.message) || e)) } } return el('div', null, - el('div', { className: 'iwb-hint' }, '数据保存:本机应用私有目录(localStorage / IndexedDB 都在里面)。' + (path ? ' 路径:' + path : '')), + el('div', { className: 'iwb-hint' }, '数据保存:资料目录(data.json + canvases/ + images/,都是普通文件)。' + (path ? ' 路径:' + path : '')), el('div', { className: 'iwb-row' }, - el('button', { className: 'iwb-btn iwb-btn-sm', onClick: open }, '打开数据目录'), + el('button', { className: 'iwb-btn iwb-btn-sm', onClick: open }, '打开资料目录'), err ? el('span', { className: 'iwb-status-err' }, err) : null, ), )