Skip to content

Commit a8ab41b

Browse files
Merge pull request #285 from alley-rs/dev
2 parents 9ef53a8 + 2f3e429 commit a8ab41b

File tree

10 files changed

+154
-23
lines changed

10 files changed

+154
-23
lines changed

src-tauri/capabilities/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"permissions": [
77
"core:default",
88
"core:window:allow-start-dragging",
9+
"core:webview:allow-clear-all-browsing-data",
910
"shell:allow-open",
1011
"clipboard-manager:allow-write-text",
1112
"dialog:allow-open",
1213
"process:default",
1314
"dialog:allow-ask"
1415
]
1516
}
16-

src-tauri/src/cache.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::{
2+
path::Path,
3+
sync::{
4+
atomic::{AtomicU64, Ordering},
5+
Arc,
6+
},
7+
thread,
8+
};
9+
10+
use crate::LsarResult;
11+
12+
fn format_size(size: u64) -> String {
13+
const KB: u64 = 1024;
14+
const MB: u64 = KB * 1024;
15+
const GB: u64 = MB * 1024;
16+
17+
if size < KB {
18+
format!("{} B", size)
19+
} else if size < MB {
20+
format!("{:.2} KB", size as f64 / KB as f64)
21+
} else if size < GB {
22+
format!("{:.2} MB", size as f64 / MB as f64)
23+
} else {
24+
format!("{:.2} GB", size as f64 / GB as f64)
25+
}
26+
}
27+
28+
#[tauri::command]
29+
pub fn calc_cache_size() -> LsarResult<String> {
30+
let path = dirs::cache_dir()
31+
.ok_or(std::io::Error::new(
32+
std::io::ErrorKind::NotFound,
33+
"cache dir not found",
34+
))?
35+
.join("lsar");
36+
37+
let total = Arc::new(AtomicU64::new(0));
38+
walk_parallel(&path, Arc::clone(&total))?;
39+
Ok(format_size(total.load(Ordering::Relaxed)))
40+
}
41+
42+
fn walk_parallel(path: &Path, total: Arc<AtomicU64>) -> std::io::Result<()> {
43+
if path.is_dir() {
44+
let entries: Vec<_> = std::fs::read_dir(path)?.filter_map(|e| e.ok()).collect();
45+
46+
// 子目录数量足够多时才开新线程,避免线程爆炸
47+
let (dirs, files): (Vec<_>, Vec<_>) = entries.into_iter().partition(|e| e.path().is_dir());
48+
49+
// 文件直接累加(无锁原子操作)
50+
for f in &files {
51+
if let Ok(meta) = f.path().metadata() {
52+
total.fetch_add(meta.len(), Ordering::Relaxed);
53+
}
54+
}
55+
56+
// 子目录:每个起一个线程(可换成线程池限制并发)
57+
let handles: Vec<_> = dirs
58+
.into_iter()
59+
.map(|d| {
60+
let t = Arc::clone(&total);
61+
let p = d.path();
62+
thread::spawn(move || {
63+
let _ = walk_parallel(&p, t);
64+
})
65+
})
66+
.collect();
67+
68+
for h in handles {
69+
let _ = h.join();
70+
}
71+
} else {
72+
if let Ok(meta) = path.metadata() {
73+
total.fetch_add(meta.len(), Ordering::Relaxed);
74+
}
75+
}
76+
Ok(())
77+
}

src-tauri/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
33

4+
mod cache;
45
mod config;
56
mod data;
67
mod error;
@@ -26,6 +27,7 @@ use std::env;
2627

2728
use tauri::{AppHandle, Manager};
2829

30+
use crate::cache::calc_cache_size;
2931
use crate::config::{read_config_file, write_config_file};
3032
use crate::data::db::{delete_a_history_by_id, get_all_history, insert_a_history};
3133
use crate::error::LsarResult;
@@ -118,6 +120,7 @@ fn main() {
118120
parse_bilibili,
119121
parse_yy,
120122
get_player_paths,
123+
calc_cache_size,
121124
#[cfg(windows)]
122125
set_titlebar_color_mode
123126
])

src/commands/cache.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { invoke } from "@tauri-apps/api/core";
2+
3+
export const calcCacheSize = (): Promise<string> => {
4+
return invoke<string>("calc_cache_size");
5+
};

src/components/settings/components/BiliCookie/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const BiliCookie = (props: BiliCookieProps) => {
1414
tips="不看 B 站直播无需配置此项。获取方法请自行搜索,互联网上教程很多。"
1515
>
1616
<LazyTextArea
17-
rows={props.cookie ? 15 : 3}
17+
rows={props.cookie ? 10 : 3}
1818
value={props.cookie}
1919
onChange={props.onChange}
2020
/>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { style } from "@vanilla-extract/css";
2+
3+
export const content = style({
4+
display: "flex",
5+
justifyContent: "space-between",
6+
alignItems: "center",
7+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createResource, createSignal } from "solid-js";
2+
3+
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
4+
5+
import { calcCacheSize } from "~/commands/cache";
6+
7+
import { LazyButton, LazyCaption1 } from "~/lazy";
8+
import SettingItem from "../SettingItem";
9+
10+
import * as styles from "./index.css";
11+
12+
const appWindow = getCurrentWebviewWindow();
13+
14+
const Cache = () => {
15+
const [cacheSize, { refetch }] = createResource(calcCacheSize);
16+
const [clearing, setClearing] = createSignal(false);
17+
18+
const clearCache = async () => {
19+
setClearing(true);
20+
try {
21+
await appWindow.clearAllBrowsingData();
22+
23+
const id = setTimeout(() => {
24+
refetch();
25+
clearTimeout(id);
26+
setClearing(false);
27+
}, 1000);
28+
} catch {
29+
setClearing(false);
30+
}
31+
};
32+
33+
return (
34+
<SettingItem
35+
label="缓存"
36+
tips="每次启动应用后都会自动产生缓存,如果缓存过大可以手动清除。"
37+
>
38+
<div class={styles.content}>
39+
<LazyCaption1>{cacheSize()}</LazyCaption1>
40+
41+
<LazyButton
42+
size="small"
43+
appearance="primary"
44+
disabled={!cacheSize()}
45+
isLoading={clearing()}
46+
onClick={clearCache}
47+
>
48+
清除
49+
</LazyButton>
50+
</div>
51+
</SettingItem>
52+
);
53+
};
54+
55+
export default Cache;

src/components/settings/components/SettingItem/index.css.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/components/settings/components/SettingItem/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import type { FieldProps } from "fluent-solid";
44

55
import { LazyField } from "~/lazy";
66

7-
import * as styles from "./index.css";
8-
97
interface SettingItemProps {
108
label: string;
119
children: JSX.Element;
@@ -22,7 +20,8 @@ const SettingItem = (props: SettingItemProps) => {
2220
return (
2321
<LazyField
2422
orientation={merged.orientation}
25-
label={{ children: props.label, class: styles.label }}
23+
// label={{ children: props.label, class: styles.label }}
24+
label={props.label}
2625
validationMessage={merged.tips}
2726
>
2827
{props.children}

src/components/settings/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Drawer } from "../drawer/Drawer";
99
import DarkMode from "./components/DarkMode";
1010
import PlayerPath from "./components/PlayerPath";
1111
import BiliCookie from "./components/BiliCookie";
12+
import Cache from "./components/Cache";
1213

1314
import { useToast } from "fluent-solid";
1415
import { useConfigContext } from "~/contexts/ConfigContext";
@@ -127,6 +128,8 @@ const Settings = () => {
127128
}
128129
/>
129130

131+
<Cache />
132+
130133
<div class={styles.buttons}>
131134
<LazyButton onClick={onCancel} disabled={!lsarConfig()?.player.path}>
132135
取消

0 commit comments

Comments
 (0)