Skip to content

Commit 05e8a57

Browse files
authored
Merge pull request #652 from multiplex55/codex/create-recycle-bin-widget-module
Add Recycle Bin dashboard widget and Windows query helper
2 parents fd8b537 + c8ed860 commit 05e8a57

4 files changed

Lines changed: 261 additions & 9 deletions

File tree

src/dashboard/data_cache.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::plugins::fav::{load_favs, FavEntry, FAV_FILE};
55
use crate::plugins::note::{load_notes, Note};
66
use crate::plugins::snippets::{load_snippets, SnippetEntry, SNIPPETS_FILE};
77
use crate::plugins::todo::{load_todos, TodoEntry, TODO_FILE};
8+
use crate::{launcher, launcher::RecycleBinInfo};
89
use std::sync::{Arc, Mutex};
910
use std::time::{Duration, Instant};
1011
use sysinfo::{Disks, Networks, System};
@@ -20,6 +21,21 @@ pub struct SystemStatusSnapshot {
2021
pub brightness_percent: Option<u8>,
2122
}
2223

24+
#[derive(Clone, Debug, Default)]
25+
pub struct RecycleBinSnapshot {
26+
pub size_bytes: u64,
27+
pub items: u64,
28+
}
29+
30+
impl From<RecycleBinInfo> for RecycleBinSnapshot {
31+
fn from(info: RecycleBinInfo) -> Self {
32+
Self {
33+
size_bytes: info.size_bytes,
34+
items: info.items,
35+
}
36+
}
37+
}
38+
2339
#[derive(Clone)]
2440
pub struct DashboardDataSnapshot {
2541
pub clipboard_history: Arc<Vec<String>>,
@@ -30,6 +46,7 @@ pub struct DashboardDataSnapshot {
3046
pub favorites: Arc<Vec<FavEntry>>,
3147
pub process_error: Option<String>,
3248
pub system_status: Option<SystemStatusSnapshot>,
49+
pub recycle_bin: Option<RecycleBinSnapshot>,
3350
}
3451

3552
impl Default for DashboardDataSnapshot {
@@ -43,6 +60,7 @@ impl Default for DashboardDataSnapshot {
4360
favorites: Arc::new(Vec::new()),
4461
process_error: None,
4562
system_status: None,
63+
recycle_bin: None,
4664
}
4765
}
4866
}
@@ -58,6 +76,7 @@ impl DashboardDataSnapshot {
5876
favorites: Arc::clone(&self.favorites),
5977
process_error: self.process_error.clone(),
6078
system_status: self.system_status.clone(),
79+
recycle_bin: self.recycle_bin.clone(),
6180
}
6281
}
6382

@@ -71,6 +90,7 @@ impl DashboardDataSnapshot {
7190
favorites: Arc::clone(&self.favorites),
7291
process_error: self.process_error.clone(),
7392
system_status: self.system_status.clone(),
93+
recycle_bin: self.recycle_bin.clone(),
7494
}
7595
}
7696

@@ -84,6 +104,7 @@ impl DashboardDataSnapshot {
84104
favorites: Arc::clone(&self.favorites),
85105
process_error: self.process_error.clone(),
86106
system_status: self.system_status.clone(),
107+
recycle_bin: self.recycle_bin.clone(),
87108
}
88109
}
89110

@@ -97,6 +118,7 @@ impl DashboardDataSnapshot {
97118
favorites: Arc::clone(&self.favorites),
98119
process_error: self.process_error.clone(),
99120
system_status: self.system_status.clone(),
121+
recycle_bin: self.recycle_bin.clone(),
100122
}
101123
}
102124

@@ -110,6 +132,7 @@ impl DashboardDataSnapshot {
110132
favorites: Arc::new(favorites),
111133
process_error: self.process_error.clone(),
112134
system_status: self.system_status.clone(),
135+
recycle_bin: self.recycle_bin.clone(),
113136
}
114137
}
115138

@@ -123,6 +146,7 @@ impl DashboardDataSnapshot {
123146
favorites: Arc::clone(&self.favorites),
124147
process_error,
125148
system_status: self.system_status.clone(),
149+
recycle_bin: self.recycle_bin.clone(),
126150
}
127151
}
128152

@@ -136,6 +160,21 @@ impl DashboardDataSnapshot {
136160
favorites: Arc::clone(&self.favorites),
137161
process_error: self.process_error.clone(),
138162
system_status,
163+
recycle_bin: self.recycle_bin.clone(),
164+
}
165+
}
166+
167+
fn with_recycle_bin(&self, recycle_bin: Option<RecycleBinSnapshot>) -> Self {
168+
Self {
169+
clipboard_history: Arc::clone(&self.clipboard_history),
170+
snippets: Arc::clone(&self.snippets),
171+
notes: Arc::clone(&self.notes),
172+
todos: Arc::clone(&self.todos),
173+
processes: Arc::clone(&self.processes),
174+
favorites: Arc::clone(&self.favorites),
175+
process_error: self.process_error.clone(),
176+
system_status: self.system_status.clone(),
177+
recycle_bin,
139178
}
140179
}
141180
}
@@ -144,6 +183,7 @@ struct DashboardDataState {
144183
snapshot: Arc<DashboardDataSnapshot>,
145184
last_process_refresh: Instant,
146185
last_system_refresh: Instant,
186+
last_recycle_refresh: Instant,
147187
last_network_totals: (u64, u64),
148188
last_network_time: Instant,
149189
}
@@ -159,6 +199,7 @@ impl DashboardDataCache {
159199
snapshot: Arc::new(DashboardDataSnapshot::default()),
160200
last_process_refresh: Instant::now() - Duration::from_secs(60),
161201
last_system_refresh: Instant::now() - Duration::from_secs(60),
202+
last_recycle_refresh: Instant::now() - Duration::from_secs(60),
162203
last_network_totals: (0, 0),
163204
last_network_time: Instant::now() - Duration::from_secs(60),
164205
}),
@@ -180,6 +221,7 @@ impl DashboardDataCache {
180221
self.refresh_favorites();
181222
self.refresh_processes(plugins);
182223
self.refresh_system_status();
224+
self.refresh_recycle_bin();
183225
}
184226

185227
pub fn refresh_clipboard(&self) {
@@ -314,6 +356,25 @@ impl DashboardDataCache {
314356
}
315357
}
316358

359+
pub fn maybe_refresh_recycle_bin(&self, interval: Duration) {
360+
let should_refresh = self
361+
.state
362+
.lock()
363+
.map(|state| state.last_recycle_refresh.elapsed() >= interval)
364+
.unwrap_or(false);
365+
if should_refresh {
366+
self.refresh_recycle_bin();
367+
}
368+
}
369+
370+
pub fn refresh_recycle_bin(&self) {
371+
let snapshot = launcher::query_recycle_bin().map(|data| RecycleBinSnapshot::from(data));
372+
if let Ok(mut state) = self.state.lock() {
373+
state.snapshot = Arc::new(state.snapshot.with_recycle_bin(snapshot));
374+
state.last_recycle_refresh = Instant::now();
375+
}
376+
}
377+
317378
fn load_processes(plugins: &PluginManager) -> (Vec<Action>, Option<String>) {
318379
let plugin = plugins.iter().find(|p| p.name() == "processes");
319380
if let Some(plugin) = plugin {
@@ -360,12 +421,12 @@ fn get_system_volume() -> Option<u8> {
360421

361422
#[cfg(target_os = "windows")]
362423
fn get_main_display_brightness() -> Option<u8> {
363-
use windows::Win32::Foundation::{BOOL, LPARAM, RECT};
364-
use windows::Win32::Graphics::Gdi::{EnumDisplayMonitors, HDC, HMONITOR};
365424
use windows::Win32::Devices::Display::{
366425
DestroyPhysicalMonitors, GetMonitorBrightness, GetNumberOfPhysicalMonitorsFromHMONITOR,
367426
GetPhysicalMonitorsFromHMONITOR, PHYSICAL_MONITOR,
368427
};
428+
use windows::Win32::Foundation::{BOOL, LPARAM, RECT};
429+
use windows::Win32::Graphics::Gdi::{EnumDisplayMonitors, HDC, HMONITOR};
369430

370431
unsafe extern "system" fn enum_monitors(
371432
hmonitor: HMONITOR,
@@ -382,8 +443,7 @@ fn get_main_display_brightness() -> Option<u8> {
382443
let mut min = 0u32;
383444
let mut cur = 0u32;
384445
let mut max = 0u32;
385-
if GetMonitorBrightness(m.hPhysicalMonitor, &mut min, &mut cur, &mut max) != 0
386-
{
446+
if GetMonitorBrightness(m.hPhysicalMonitor, &mut min, &mut cur, &mut max) != 0 {
387447
if max > min {
388448
*percent_ptr = ((cur - min) * 100 / (max - min)) as u32;
389449
} else {

src/dashboard/widgets/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ mod pinned_query_results;
2020
mod plugin_home;
2121
mod process_list;
2222
mod query_list;
23+
mod quick_tools;
2324
mod recent_commands;
2425
mod recent_notes;
26+
mod recycle_bin;
2527
mod snippets_favorites;
2628
mod system_actions;
2729
mod system_status;
28-
mod todo_focus;
2930
mod todo;
31+
mod todo_focus;
3032
mod weather_site;
3133
mod window_list;
3234
mod windows_overview;
33-
mod quick_tools;
3435

3536
pub use active_timers::ActiveTimersWidget;
3637
pub use browser_tabs::BrowserTabsWidget;
@@ -45,17 +46,18 @@ pub use pinned_query_results::PinnedQueryResultsWidget;
4546
pub use plugin_home::PluginHomeWidget;
4647
pub use process_list::ProcessesWidget;
4748
pub use query_list::QueryListWidget;
49+
pub use quick_tools::QuickToolsWidget;
4850
pub use recent_commands::RecentCommandsWidget;
4951
pub use recent_notes::RecentNotesWidget;
52+
pub use recycle_bin::RecycleBinWidget;
5053
pub use snippets_favorites::SnippetsFavoritesWidget;
5154
pub use system_actions::SystemWidget;
5255
pub use system_status::SystemStatusWidget;
53-
pub use todo_focus::TodoFocusWidget;
5456
pub use todo::TodoWidget;
57+
pub use todo_focus::TodoFocusWidget;
5558
pub use weather_site::WeatherSiteWidget;
5659
pub use window_list::WindowsWidget;
5760
pub use windows_overview::WindowsOverviewWidget;
58-
pub use quick_tools::QuickToolsWidget;
5961

6062
/// Result of a widget activation.
6163
#[derive(Debug, Clone)]
@@ -299,7 +301,13 @@ impl WidgetRegistry {
299301
);
300302
reg.register(
301303
"quick_tools",
302-
WidgetFactory::new(QuickToolsWidget::new).with_settings_ui(QuickToolsWidget::settings_ui),
304+
WidgetFactory::new(QuickToolsWidget::new)
305+
.with_settings_ui(QuickToolsWidget::settings_ui),
306+
);
307+
reg.register(
308+
"recycle_bin",
309+
WidgetFactory::new(RecycleBinWidget::new)
310+
.with_settings_ui(RecycleBinWidget::settings_ui),
303311
);
304312
reg
305313
}

0 commit comments

Comments
 (0)