-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
347 lines (314 loc) · 10.5 KB
/
renderer.js
File metadata and controls
347 lines (314 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
(() => {
'use strict';
const $ = (sel) => document.querySelector(sel);
const els = {
status: $('#status'),
btnScan: $('#btnScan'),
btnCancel: $('#btnCancel'),
btnClean: $('#btnClean'),
btnSelectRecommended: $('#btnSelectRecommended'),
checkAll: $('#checkAll'),
progressRing: $('#progressRing'),
progressLabel: $('#progressLabel'),
progressSub: $('#progressSub'),
totalSize: $('#totalSize'),
safeCount: $('#safeCount'),
optionalCount: $('#optionalCount'),
results: $('#results'),
resultsBody: $('#resultsBody'),
cleanSummary: $('#cleanSummary'),
freedSize: $('#freedSize'),
};
let state = {
items: [],
selected: new Set(),
scanning: false,
cleaning: false,
offScanProgress: null,
offScanComplete: null,
offCleanProgress: null,
offCleanComplete: null,
};
function formatBytes(n) {
n = Number(n || 0);
const u = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
let i = 0;
while (n >= 1024 && i < u.length - 1) {
n /= 1024;
i++;
}
const v = n >= 100 ? n.toFixed(0) : n >= 10 ? n.toFixed(1) : n.toFixed(2);
return `${v} ${u[i]}`;
}
function setStatus(mode, text) {
// mode: idle | scan | clean | cancel
els.status.classList.remove('pill-idle', 'pill-scan', 'pill-clean');
if (mode === 'scan') {
els.status.textContent = text || '扫描中';
els.status.classList.add('pill', 'pill-scan');
} else if (mode === 'clean') {
els.status.textContent = text || '清理中';
els.status.classList.add('pill', 'pill-clean');
} else if (mode === 'cancel') {
els.status.textContent = text || '已取消';
els.status.classList.add('pill', 'pill-idle');
} else {
els.status.textContent = text || '空闲';
els.status.classList.add('pill', 'pill-idle');
}
}
function setProgress(percent, sub) {
const p = Math.max(0, Math.min(100, Math.round(percent || 0)));
els.progressRing.style.setProperty('--p', String(p));
els.progressLabel.textContent = `${p}%`;
if (sub) els.progressSub.textContent = sub;
}
function updateButtons() {
els.btnScan.disabled = state.scanning || state.cleaning;
els.btnCancel.disabled = !(state.scanning || state.cleaning);
els.btnClean.disabled = state.scanning || state.cleaning || state.selected.size === 0;
els.btnSelectRecommended.disabled = state.scanning || state.cleaning || state.items.length === 0;
els.checkAll.disabled = state.scanning || state.cleaning || state.items.length === 0;
}
function updateStats() {
const safeTotal = state.items.filter((x) => x.type === 'safe').length;
const optTotal = state.items.filter((x) => x.type !== 'safe').length;
els.safeCount.textContent = `${safeTotal} 项`;
els.optionalCount.textContent = `${optTotal} 项`;
let selectedSum = 0;
for (const id of state.selected) {
const it = state.items.find((x) => x.id === id);
if (it) selectedSum += it.size || 0;
}
els.totalSize.textContent = formatBytes(selectedSum);
}
function renderRows() {
const rowsHtml = state.items
.map((it) => {
const checked = state.selected.has(it.id) ? 'checked' : '';
const badge = it.type === 'safe'
? '<span class="badge">推荐</span>'
: '<span class="badge opt">可选</span>';
const size = formatBytes(it.size || 0);
return `
<div class="row" data-id="${it.id}">
<div class="col-check">
<input type="checkbox" class="row-check" ${checked} />
</div>
<div class="cell col-name">${escapeHtml(it.label)}</div>
<div class="cell col-type">${badge}</div>
<div class="cell cell-size col-size">${size}</div>
<div class="cell col-hint">${escapeHtml(it.hint || '')}</div>
</div>
`;
})
.join('');
els.resultsBody.innerHTML = rowsHtml;
// Show/Hide placeholder
const placeholder = els.results.querySelector('.placeholder');
if (placeholder) {
placeholder.style.display = state.items.length ? 'none' : 'flex';
}
// Bind row checkbox change
els.resultsBody.querySelectorAll('.row').forEach((row) => {
const id = row.getAttribute('data-id');
const cb = row.querySelector('.row-check');
cb.addEventListener('change', () => {
if (cb.checked) state.selected.add(id);
else state.selected.delete(id);
updateStats();
updateButtons();
syncHeaderCheckAll();
});
});
syncHeaderCheckAll();
}
function syncHeaderCheckAll() {
if (!state.items.length) {
els.checkAll.indeterminate = false;
els.checkAll.checked = false;
return;
}
let selectedCount = 0;
for (const it of state.items) {
if (state.selected.has(it.id)) selectedCount++;
}
if (selectedCount === 0) {
els.checkAll.indeterminate = false;
els.checkAll.checked = false;
} else if (selectedCount === state.items.length) {
els.checkAll.indeterminate = false;
els.checkAll.checked = true;
} else {
els.checkAll.indeterminate = true;
}
}
function escapeHtml(s) {
return String(s || '')
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
function resetForScan() {
state.items = [];
state.selected.clear();
els.cleanSummary.classList.add('hidden');
els.freedSize.textContent = '0 B';
setProgress(0, '等待扫描');
renderRows();
updateStats();
updateButtons();
setStatus('scan');
}
function attachScanListeners() {
detachScanListeners();
state.offScanProgress = window.smartCleaner.onScanProgress((data) => {
setProgress(data?.percent ?? 0, '正在扫描...');
});
state.offScanComplete = window.smartCleaner.onScanComplete((data) => {
state.scanning = false;
detachScanListeners();
if (data?.aborted) {
setStatus('cancel', '已取消');
setProgress(0, '已取消');
} else {
setStatus('idle', '空闲');
setProgress(100, '扫描完成');
state.items = Array.isArray(data?.items) ? data.items : [];
// 默认不勾选,等待用户点击“全选推荐”或手动选择
state.selected.clear();
renderRows();
updateStats();
}
updateButtons();
});
}
function detachScanListeners() {
if (typeof state.offScanProgress === 'function') {
state.offScanProgress();
state.offScanProgress = null;
}
if (typeof state.offScanComplete === 'function') {
state.offScanComplete();
state.offScanComplete = null;
}
}
function attachCleanListeners() {
detachCleanListeners();
state.offCleanProgress = window.smartCleaner.onCleanProgress((data) => {
setProgress(data?.percent ?? 0, '正在清理...');
});
state.offCleanComplete = window.smartCleaner.onCleanComplete((data) => {
state.cleaning = false;
detachCleanListeners();
if (data?.aborted) {
setStatus('cancel', '已取消');
setProgress(0, '已取消');
} else {
setStatus('idle', '空闲');
setProgress(100, '清理完成');
const freed = Number(data?.freed || 0);
els.freedSize.textContent = formatBytes(freed);
els.cleanSummary.classList.remove('hidden');
// 就地更新各项大小
const details = Array.isArray(data?.details) ? data.details : [];
const map = new Map(details.map((d) => [d.id, d]));
state.items = state.items.map((it) => {
const d = map.get(it.id);
if (d && typeof d.reclaimed === 'number') {
const next = Math.max(0, (it.size || 0) - d.reclaimed);
return { ...it, size: next };
}
return it;
});
// 清理后可保留勾选,便于二次清理,但更新显示
renderRows();
updateStats();
}
updateButtons();
});
}
function detachCleanListeners() {
if (typeof state.offCleanProgress === 'function') {
state.offCleanProgress();
state.offCleanProgress = null;
}
if (typeof state.offCleanComplete === 'function') {
state.offCleanComplete();
state.offCleanComplete = null;
}
}
// Events
els.btnScan.addEventListener('click', () => {
if (state.scanning || state.cleaning) return;
resetForScan();
state.scanning = true;
updateButtons();
attachScanListeners();
try {
window.smartCleaner.startScan();
} catch {
state.scanning = false;
setStatus('idle', '空闲');
updateButtons();
}
});
els.btnCancel.addEventListener('click', () => {
if (state.scanning) {
try { window.smartCleaner.cancelScan(); } catch {}
}
if (state.cleaning) {
try { window.smartCleaner.cancelClean(); } catch {}
}
});
els.btnSelectRecommended.addEventListener('click', () => {
if (state.scanning || state.cleaning) return;
state.selected.clear();
for (const it of state.items) {
if (it.type === 'safe' && (it.size || 0) > 0) {
state.selected.add(it.id);
}
}
renderRows();
updateStats();
updateButtons();
});
els.checkAll.addEventListener('change', () => {
if (state.scanning || state.cleaning) return;
if (!state.items.length) return;
state.selected.clear();
if (els.checkAll.checked) {
for (const it of state.items) {
if ((it.size || 0) > 0) state.selected.add(it.id);
}
}
renderRows();
updateStats();
updateButtons();
});
els.btnClean.addEventListener('click', () => {
if (state.scanning || state.cleaning) return;
if (state.selected.size === 0) return;
setStatus('clean');
setProgress(0, '准备清理...');
const ids = Array.from(state.selected);
state.cleaning = true;
updateButtons();
attachCleanListeners();
try {
window.smartCleaner.startClean(ids);
} catch {
state.cleaning = false;
setStatus('idle', '空闲');
updateButtons();
}
});
// Initial
setStatus('idle', '空闲');
setProgress(0, '等待扫描');
updateButtons();
updateStats();
renderRows();
})();