Skip to content

Commit 6d8d96c

Browse files
committed
完善设置
1 parent b641c17 commit 6d8d96c

File tree

9 files changed

+291
-8
lines changed

9 files changed

+291
-8
lines changed

src-tauri/Cargo.lock

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tauri-plugin-opener = "2"
2323
tauri-plugin-fs = "2"
2424
tauri-plugin-dialog = "2"
2525
tauri-plugin-single-instance = "2"
26+
tauri-plugin-shell = "2"
2627
serde = { version = "1", features = ["derive"] }
2728
serde_json = "1"
2829
dirs = "5"

src-tauri/capabilities/default.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@
3232
{ "path": "$APPCACHE/**" },
3333
{ "path": "**" }
3434
]
35-
}
35+
},
36+
{
37+
"identifier": "shell:allow-spawn",
38+
"allow": [
39+
{
40+
"name": "explorer",
41+
"cmd": "explorer",
42+
"args": true
43+
}
44+
]
45+
},
46+
"shell:allow-execute"
3647
]
3748
}

src-tauri/src/lib.rs

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,98 @@ fn clear_cache(app: tauri::AppHandle) -> Result<String, String> {
421421
Ok(format!("已清除 {} 个文件,共 {:.2} MB", cleared_files, cleared_size as f64 / 1024.0 / 1024.0))
422422
}
423423

424+
/// 检查并执行自动清除缓存
425+
#[tauri::command]
426+
fn check_auto_clear_cache(app: tauri::AppHandle) -> Result<bool, String> {
427+
let config_dir = app.path().app_config_dir()
428+
.map_err(|e| format!("Failed to get config dir: {}", e))?;
429+
430+
let config_file = config_dir.join("config.json");
431+
432+
if !config_file.exists() {
433+
return Ok(false);
434+
}
435+
436+
let config_content = std::fs::read_to_string(&config_file)
437+
.map_err(|e| format!("Failed to read config: {}", e))?;
438+
439+
let config: serde_json::Value = serde_json::from_str(&config_content)
440+
.map_err(|e| format!("Failed to parse config: {}", e))?;
441+
442+
let auto_clear_days = config.get("autoClearCacheDays")
443+
.and_then(|v| v.as_u64())
444+
.unwrap_or(0);
445+
446+
if auto_clear_days == 0 {
447+
log::info!("自动清除缓存已关闭");
448+
return Ok(false);
449+
}
450+
451+
let last_clear_date = config.get("lastCacheClearDate")
452+
.and_then(|v| v.as_str())
453+
.unwrap_or("");
454+
455+
let today = chrono::Local::now().format("%Y-%m-%d").to_string();
456+
457+
if last_clear_date == today {
458+
log::info!("今日已执行过自动清除缓存");
459+
return Ok(false);
460+
}
461+
462+
if last_clear_date.is_empty() {
463+
let mut updated_config = config.clone();
464+
updated_config["lastCacheClearDate"] = serde_json::json!(today);
465+
let updated_content = serde_json::to_string_pretty(&updated_config)
466+
.map_err(|e| format!("Failed to serialize config: {}", e))?;
467+
std::fs::write(&config_file, updated_content)
468+
.map_err(|e| format!("Failed to write config: {}", e))?;
469+
log::info!("首次设置自动清除缓存日期");
470+
return Ok(false);
471+
}
472+
473+
let last_date = chrono::NaiveDate::parse_from_str(last_clear_date, "%Y-%m-%d")
474+
.map_err(|e| format!("Failed to parse last clear date: {}", e))?;
475+
let today_date = chrono::Local::now().date_naive();
476+
477+
let days_since_last_clear = (today_date - last_date).num_days();
478+
479+
if days_since_last_clear >= auto_clear_days as i64 {
480+
log::info!("执行自动清除缓存,距上次清除 {} 天", days_since_last_clear);
481+
482+
let cache_dir = app.path().app_cache_dir()
483+
.map_err(|e| format!("Failed to get cache dir: {}", e))?;
484+
485+
if cache_dir.exists() {
486+
fn remove_dir_contents(path: &std::path::Path) {
487+
if let Ok(entries) = std::fs::read_dir(path) {
488+
for entry in entries.flatten() {
489+
let entry_path = entry.path();
490+
if entry_path.is_dir() {
491+
remove_dir_contents(&entry_path);
492+
let _ = std::fs::remove_dir(&entry_path);
493+
} else {
494+
let _ = std::fs::remove_file(&entry_path);
495+
}
496+
}
497+
}
498+
}
499+
remove_dir_contents(&cache_dir);
500+
}
501+
502+
let mut updated_config = config.clone();
503+
updated_config["lastCacheClearDate"] = serde_json::json!(today);
504+
let updated_content = serde_json::to_string_pretty(&updated_config)
505+
.map_err(|e| format!("Failed to serialize config: {}", e))?;
506+
std::fs::write(&config_file, updated_content)
507+
.map_err(|e| format!("Failed to write config: {}", e))?;
508+
509+
log::info!("自动清除缓存完成");
510+
return Ok(true);
511+
}
512+
513+
Ok(false)
514+
}
515+
424516
/// 获取应用配置目录
425517
#[tauri::command]
426518
fn get_config_dir(app: tauri::AppHandle) -> Result<String, String> {
@@ -1053,7 +1145,9 @@ fn get_default_config() -> serde_json::Value {
10531145
{"r": 255, "g": 255, "b": 255}
10541146
],
10551147
"fileAssociations": false,
1056-
"wordAssociations": false
1148+
"wordAssociations": false,
1149+
"autoClearCacheDays": 15,
1150+
"lastCacheClearDate": ""
10571151
})
10581152
}
10591153

@@ -1756,6 +1850,7 @@ pub fn run() {
17561850
.plugin(tauri_plugin_opener::init())
17571851
.plugin(tauri_plugin_fs::init())
17581852
.plugin(tauri_plugin_dialog::init())
1853+
.plugin(tauri_plugin_shell::init())
17591854
.plugin(tauri_plugin_single_instance::init(|app, args, _cwd| {
17601855
println!("单实例回调: args={:?}", args);
17611856
if args.len() > 1 {
@@ -1858,6 +1953,7 @@ pub fn run() {
18581953
get_cache_dir,
18591954
get_cache_size,
18601955
clear_cache,
1956+
check_auto_clear_cache,
18611957
get_config_dir,
18621958
get_cds_dir,
18631959
enhance_image,

src/assets/icon/folder.svg

Lines changed: 3 additions & 0 deletions
Loading

src/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ window.addEventListener('DOMContentLoaded', async () => {
380380

381381
await initCacheDir();
382382

383+
// 检查并执行自动清除缓存
384+
try {
385+
const { invoke } = window.__TAURI__.core;
386+
const cleared = await invoke('check_auto_clear_cache');
387+
if (cleared) {
388+
console.log('自动清除缓存已执行');
389+
}
390+
} catch (e) {
391+
console.log('检查自动清除缓存失败:', e);
392+
}
393+
383394
await loadCameraSetting();
384395

385396
// 检测摄像头是否存在

src/settings.css

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ body {
193193
font-size: 16px;
194194
width: 20px;
195195
text-align: center;
196+
display: flex;
197+
align-items: center;
198+
justify-content: center;
199+
}
200+
201+
.btn-icon img {
202+
width: 16px;
203+
height: 16px;
196204
}
197205

198206
.btn-text {
@@ -201,19 +209,19 @@ body {
201209
}
202210

203211
.btn-clear-cache {
204-
padding: 8px 16px;
205-
background: rgba(255, 255, 255, 0.1);
206-
border: 1px solid rgba(255, 255, 255, 0.2);
212+
padding: 8px 12px;
213+
background: rgba(255, 255, 255, 0.08);
214+
border: 1px solid rgba(255, 255, 255, 0.15);
207215
border-radius: 6px;
208216
color: #fff;
209-
font-size: 13px;
217+
font-size: 12px;
210218
cursor: pointer;
211219
transition: all 0.2s ease;
212220
}
213221

214222
.btn-clear-cache:hover {
215-
background: rgba(255, 100, 100, 0.3);
216-
border-color: rgba(255, 100, 100, 0.5);
223+
background: rgba(255, 100, 100, 0.2);
224+
border-color: rgba(255, 100, 100, 0.4);
217225
}
218226

219227
.btn-clear-cache:active {

src/settings.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<span class="btn-icon"></span>
3030
<span class="btn-text">应用设置</span>
3131
</button>
32+
<button class="sidebar-btn" id="btnStorage">
33+
<span class="btn-icon"><img src="assets/icon/folder.svg" width="16" height="16" style="filter: invert(1);"></span>
34+
<span class="btn-text">存储</span>
35+
</button>
3236
<button class="sidebar-btn" id="btnCanvas">
3337
<span class="btn-icon"></span>
3438
<span class="btn-text">Canvas调节</span>
@@ -296,10 +300,32 @@ <h2 class="page-title">信号源调节</h2>
296300
<span class="toggle-slider"></span>
297301
</label>
298302
</div>
303+
</div>
304+
305+
<!-- 存储页面 -->
306+
<div class="page" id="pageStorage">
307+
<h2 class="page-title">存储</h2>
299308
<div class="setting-item">
300309
<span class="setting-label">缓存 <span id="cacheSize" style="color: #888; font-size: 12px;"></span></span>
301310
<button class="btn-clear-cache" id="btnClearCache">清除缓存</button>
302311
</div>
312+
<div class="setting-item">
313+
<span class="setting-label">自动清除缓存</span>
314+
<div class="custom-select" id="autoClearCacheSelect">
315+
<div class="select-selected" id="autoClearCacheSelected">每半月</div>
316+
<div class="select-options" id="autoClearCacheOptions">
317+
<div class="select-option" data-value="0">关闭</div>
318+
<div class="select-option" data-value="1">每天</div>
319+
<div class="select-option" data-value="7">每周</div>
320+
<div class="select-option" data-value="15">每半月</div>
321+
<div class="select-option" data-value="30">每月</div>
322+
</div>
323+
</div>
324+
</div>
325+
<div class="setting-item">
326+
<span class="setting-label">日志</span>
327+
<button class="btn-clear-cache" id="btnOpenLogDir">打开日志目录</button>
328+
</div>
303329
</div>
304330

305331
<div class="page" id="pageAbout">

0 commit comments

Comments
 (0)