From 64cf41d14069177c38e388abea131f8d4935c983 Mon Sep 17 00:00:00 2001 From: rajhansgithub Date: Tue, 1 Dec 2026 12:37:30 +0530 Subject: [PATCH 1/2] 2.33.0: Fresh-read lamps on every tray popup card --- Cargo.lock | 2 +- Cargo.toml | 2 +- res/resmon.manifest | 2 +- setup.iss | 2 +- src/app.rs | 23 ++++ src/popup.rs | 287 ++++++++++++++++++++++++++++++++++++++++-- src/sensors/mod.rs | 7 +- src/sensors/volume.rs | 27 +++- src/sensors/worker.rs | 7 +- 9 files changed, 342 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d32eaa..744195f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,7 +92,7 @@ dependencies = [ [[package]] name = "resmon" -version = "2.32.0" +version = "2.33.0" dependencies = [ "embed-resource", "windows", diff --git a/Cargo.toml b/Cargo.toml index 0fb3733..b89784b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "resmon" -version = "2.32.0" +version = "2.33.0" edition = "2021" authors = ["Emulsion Labs"] description = "Ultra-light taskbar resource monitor for Windows 10/11" diff --git a/res/resmon.manifest b/res/resmon.manifest index c31de29..14b22d6 100644 --- a/res/resmon.manifest +++ b/res/resmon.manifest @@ -1,6 +1,6 @@ - + diff --git a/setup.iss b/setup.iss index 76856c6..6be0007 100644 --- a/setup.iss +++ b/setup.iss @@ -1,6 +1,6 @@ ; Inno Setup script. Build the exe first: cargo build --release #define MyAppName "Resource Monitor" -#define MyAppVersion "2.32.0" +#define MyAppVersion "2.33.0" #define MyAppPublisher "Emulsion Labs" #define MyAppURL "https://github.com/rajhansgithub/Windows_Resource_Monitoring_Tool" #define MyAppExeName "ResourceMonitor.exe" diff --git a/src/app.rs b/src/app.rs index 033d219..0ee92e5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -409,6 +409,8 @@ pub struct App { pub popup_top: u8, /// Mouse position over the details panel (client coordinates). pub popup_hover: Option<(i32, i32)>, + /// When the values on screen were read, for the panel's freshness lamps. + pub fresh: popup::FreshState, /// Panel uses the native acrylic backdrop (Windows 11). pub popup_acrylic: bool, /// Theme the panel's backdrop was styled for (restyled when it changes). @@ -549,6 +551,7 @@ pub fn create(hinst: HINSTANCE, after_update: bool) -> Option<()> { popup_range: 60, popup_top: 0, popup_hover: None, + fresh: popup::FreshState::default(), popup_acrylic: false, popup_dark: true, popup_tracking: false, @@ -806,6 +809,7 @@ impl App { dev: crate::settings_ui::monitor_dev(), bt: self.m.s.mod_bluetooth, addons: self.m.s.addons_on(), + force_space: false, } } @@ -848,6 +852,19 @@ impl App { self.sensors.request(opts, false); } + /// A freshness lamp in the details panel was clicked: read now on the sensor thread + /// (never here), the drive space too when that is the lamp. The next tick moves a full + /// interval out, so the clicked read is not followed by a near-duplicate. + pub fn read_now(&mut self, item: popup::Fresh) { + if !self.fresh.ask(item) { + return; + } + let mut opts = self.opts(); + opts.force_space = item == popup::Fresh::Space; + self.sensors.request(opts, false); + self.update_timer(); + } + /// Take a sample now instead of at the next tick (a page that shows live data came on /// screen). Posted, so it runs after the current message. pub fn kick_sample(&self) { @@ -861,6 +878,7 @@ impl App { return false; } let _p = crate::perf::span("sample_ui"); + self.fresh.sampled(r.started, r.taken, r.snap.disk_space_age); crate::perf::milestone("first sample"); crate::perf::sample_arrived(self.m.s.interval_ms, r.opts.detail); if let Some(d) = r.detail { @@ -1804,6 +1822,11 @@ unsafe extern "system" fn main_proc(hwnd: HWND, msg: u32, w: WPARAM, l: LPARAM) // The only periodic work: read the metrics and redraw them. if w == T_SAMPLE { a.request_sample(); + // A late sample repaints nothing by itself: let the panel's lamps show the age + // climbing (same tick, no timer of its own). + if a.popup_open() && a.fresh.late(a.m.s.interval_ms) { + InvalidateRect(a.popup, std::ptr::null(), 0); + } } 0 } diff --git a/src/popup.rs b/src/popup.rs index ad46a48..b422465 100644 --- a/src/popup.rs +++ b/src/popup.rs @@ -8,6 +8,7 @@ use crate::taskbar::Edge; use crate::usage; use crate::util::*; use std::collections::VecDeque; +use std::time::{Duration, Instant}; use windows_sys::Win32::Foundation::*; use windows_sys::Win32::Graphics::Dwm::*; use windows_sys::Win32::Graphics::Gdi::*; @@ -259,6 +260,207 @@ pub enum PopupHit { Chart, /// Tiles as ring gauges (false) or lit part icons (true). TileStyle(bool), + /// A card's freshness lamp: read that item now. + Fresh(Fresh), +} + +// ================================================================ freshness lamps + +/// What a freshness lamp vouches for. Everything but the drive space comes from the one +/// sample per tick; the drive space is read every 30 s. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Fresh { + Cpu, + Mem, + Gpu, + Net, + Disk, + Space, + Usage, + Top, +} + +/// Seconds the drive space is cached for (`sensors::volume`). +const SPACE_SECS: f64 = 30.0; +/// A read-now ask with no answer after this long may be asked again. +const ASK_TIMEOUT: Duration = Duration::from_secs(5); +/// How long the lamp shows the time a read-now took. +const DONE_SHOW: Duration = Duration::from_millis(1600); + +/// When the values on screen were read, and the read-now ask in flight. +#[derive(Default)] +pub struct FreshState { + /// The last sample's values were read. + at: Option, + /// The drive space was read. + space_at: Option, + /// Lamp clicked, when. + wait: Option<(Fresh, Instant)>, + /// The ask answered: item, when, ms from click to values read. + done: Option<(Fresh, Instant, u32)>, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Level { + /// On time. + Ok, + /// More than 3 reads missed (amber). + Late, + /// More than 10 missed (red). + Stale, +} + +/// How late a value `age` seconds old is when it should be read every `cadence` seconds. +pub fn level(age: f64, cadence: f64) -> Level { + let c = cadence.max(0.1); + if age > c * 10.0 { + Level::Stale + } else if age > c * 3.0 + 0.5 { + Level::Late + } else { + Level::Ok + } +} + +/// "12 s", "3 min", "2 h": the lamp's late label. +fn short_age(secs: f64) -> String { + let s = secs.max(0.0).round() as u64; + match s { + 0..=59 => format!("{s} s"), + 60..=3599 => format!("{} min", s / 60), + _ => format!("{} h", s / 3600), + } +} + +impl FreshState { + /// A sample arrived: its values were read at `taken`, the drive space `space_age` s before. + pub fn sampled(&mut self, started: Instant, taken: Instant, space_age: f64) { + self.at = Some(taken); + self.space_at = taken.checked_sub(Duration::from_secs_f64(space_age.clamp(0.0, 86_400.0))).or(Some(taken)); + // Only a sample that started after the click answers it. + if let Some((item, t0)) = self.wait { + if started >= t0 { + self.wait = None; + let ms = taken.saturating_duration_since(t0).as_millis().min(u32::MAX as u128) as u32; + self.done = Some((item, Instant::now(), ms)); + } + } + } + + /// A lamp was clicked: false while another read-now is still out (clicks don't pile up). + pub fn ask(&mut self, item: Fresh) -> bool { + let now = Instant::now(); + if self.wait.is_some_and(|(_, t)| now.saturating_duration_since(t) < ASK_TIMEOUT) { + return false; + } + self.wait = Some((item, now)); + self.done = None; + true + } + + /// The last sample is overdue (the lamps need a repaint to show it). + pub fn late(&self, interval_ms: u32) -> bool { + self.at.is_some_and(|t| t.elapsed().as_secs_f64() > interval_ms as f64 / 1000.0 * 1.5) + } + + fn busy(&self, item: Fresh) -> bool { + self.wait.is_some_and(|(i, t)| i == item && t.elapsed() < ASK_TIMEOUT) + } + + /// Read-now time to show next to the lamp, ms. + fn just_read(&self, item: Fresh) -> Option { + self.done.filter(|(i, t, _)| *i == item && t.elapsed() < DONE_SHOW).map(|d| d.2) + } + + fn age(&self, item: Fresh, now: Instant) -> Option { + let at = if item == Fresh::Space { self.space_at } else { self.at }; + at.map(|t| now.saturating_duration_since(t).as_secs_f64()) + } + + fn cadence(item: Fresh, interval_ms: u32) -> f64 { + if item == Fresh::Space { + SPACE_SECS + } else { + interval_ms.max(100) as f64 / 1000.0 + } + } +} + +/// One freshness lamp centred on (cx, cy): a bezel with a lit core. Accent while on time, +/// amber / red with the age beside it when late, a pressed ring while a read-now runs, and the +/// read time beside it right after one. +fn lamp(c: &mut Canvas, e: &Env, out: &mut Out, item: Fresh, cx: i32, cy: i32) { + let a = e.a; + let tk = crate::look::tokens(a.dark); + let pal = &e.pal; + let now = Instant::now(); + let hit = Rect { l: cx - e.sc(10.0), t: cy - e.sc(10.0), r: cx + e.sc(10.0), b: cy + e.sc(10.0) }; + out.hits.push((hit, PopupHit::Fresh(item))); + let hot = e.hot(hit); + let busy = a.fresh.busy(item); + let age = a.fresh.age(item, now); + let cad = FreshState::cadence(item, a.s.interval_ms); + let lvl = age.map_or(Level::Ok, |s| level(s, cad)); + let (fx, fy) = (cx as f32, cy as f32); + let rb = e.sc(6.0); + let bez = Rect { l: cx - rb, t: cy - rb, r: cx + rb, b: cy + rb }; + c.fill_round(bez, rb as f32, pal.chip); + let tau = std::f32::consts::TAU; + if busy { + // Pressed: the ring lights part way, the core goes white-hot. + c.stroke_round(bez, rb as f32, pal.border); + c.stroke_arc(fx, fy, rb as f32 - 0.5, 1.6 * e.scale, -std::f32::consts::FRAC_PI_2, 0.9, tk.accent); + let rc = e.sc(2.5); + c.fill_round(Rect { l: cx - rc, t: cy - rc, r: cx + rc, b: cy + rc }, rc as f32, pal.fg); + } else { + c.stroke_round(bez, rb as f32, if hot { pal.dim } else { pal.border }); + let core = match lvl { + Level::Ok => tk.accent, + Level::Late => tk.warn, + Level::Stale => tk.crit, + }; + if age.is_some() { + if lvl == Level::Ok { + c.stroke_arc(fx, fy, 3.6 * e.scale, 1.4 * e.scale, 0.0, tau, core.with_a(70)); + } + let rc = e.sc(3.0); + c.fill_round(Rect { l: cx - rc, t: cy - rc, r: cx + rc, b: cy + rc }, rc as f32, core); + } + } + // Beside the lamp: the read-now time, or how late the value is. + let side = if busy { + None + } else if let Some(ms) = a.fresh.just_read(item) { + Some((format!("{ms} ms"), tk.accent)) + } else { + match (lvl, age) { + (Level::Late, Some(s)) => Some((short_age(s), tk.warn)), + (Level::Stale, Some(s)) => Some((short_age(s), tk.crit)), + _ => None, + } + }; + if let Some((t, col)) = side { + let (_, th) = c.measure(e.f.caps, &t); + c.text_right(e.f.caps, &t, cx - rb - e.sc(4.0), cy - th / 2, col); + } + if hot { + let what = match item { + Fresh::Space => " (every 30 s)", + _ => "", + }; + let text = if busy { + "Reading\u{2026}".to_string() + } else if let Some(ms) = a.fresh.just_read(item) { + format!("Read just now, took {ms} ms") + } else { + match age { + Some(s) if s < 1.0 => format!("Read under a second ago{what} \u{00B7} click to read now"), + Some(s) => format!("Read {}{} \u{00B7} click to read now", ago_text(s as f32), what), + None => "Not read yet \u{00B7} click to read now".to_string(), + } + }; + out.tip = Some(Tip { x: cx, r: hit, text }); + } } const RANGES: [(&str, usize); 3] = [("1m", 60), ("5m", 300), ("1h", 3600)]; @@ -740,6 +942,8 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let right = c.w - pad; let rad = if tk.look.is_new() { crate::looks::popup_style(tk.look).radius.map_or(10.0, |r| r as f32) * scale } else { 10.0 * scale }; c.clear(pal.bg); + // Freshness lamps, drawn last so they sit over charts (item, centre). + let mut lamps: Vec<(Fresh, i32, i32)> = Vec::with_capacity(8); if tk.look.is_new() { let ctx = crate::looks::DecorCtx { zone: crate::looks::Zone::Popup, rect: Rect { l: 0, t: 0, r: c.w, b: c.h }, tk: &tk, scale, t: 0.0, reduced: true }; crate::looks::decor(c, tk.look, &ctx); @@ -918,6 +1122,13 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let r = Rect { l: x, t: y, r: x + gw, b: y + gh }; card(c, r, rad, pal, e.hot(r), scale); out.hits.push((r, g.hit)); + let item = match g.m { + Metric::Cpu => Fresh::Cpu, + Metric::Ram => Fresh::Mem, + Metric::Gpu => Fresh::Gpu, + _ => Fresh::Disk, + }; + lamps.push((item, r.r - sc(11.0), r.t + sc(11.0))); let acc = accent(g.m, a.dark); let col = a.heat(acc, g.pct); let (cx, cy) = ((x + gw / 2) as f32, (y + sc(44.0)) as f32); @@ -962,6 +1173,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let r = Rect { l: pad, t: y, r: right, b: y + nh }; card(c, r, rad, pal, e.hot(r), scale); out.hits.push((r, PopupHit::NetSettings)); + lamps.push((Fresh::Net, right - sc(11.0), y + sc(11.0))); let down_c = accent(Metric::Down, a.dark); let up_c = accent(Metric::Up, a.dark); if icons { @@ -990,7 +1202,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { value_row(c, f, vals_x, vy, vals_w, "\u{25BC}", &fmt::speed(s.net_down, a.s.unit), true, ink(down_c), pal.dim, scale); value_row(c, f, vals_x, vy + sc(22.0), vals_w, "\u{25B2}", &fmt::speed(s.net_up, a.s.unit), false, ink(up_c), pal.dim, scale); { - let cr = Rect { l: chart_l, t: y + sc(14.0), r: right - sc(12.0), b: y + nh - sc(14.0) }; + let cr = Rect { l: chart_l, t: y + sc(21.0), r: right - sc(12.0), b: y + nh - sc(11.0) }; let tr = [ Trace { m: Metric::Down, col: down_c, tag: "\u{25BC}" }, Trace { m: Metric::Up, col: up_c, tag: "\u{25B2}" }, @@ -1005,6 +1217,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let r = Rect { l: pad, t: y, r: right, b: y + dh }; card(c, r, rad, pal, e.hot(r), scale); out.hits.push((r, PopupHit::ResMon)); + lamps.push((Fresh::Disk, right - sc(11.0), y + sc(11.0))); let rd_c = accent(Metric::DiskRead, a.dark); let mut wr_c = accent(Metric::DiskWrite, a.dark); if wr_c == rd_c { @@ -1032,7 +1245,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { value_row(c, f, vals_x, vy, vals_w, "R", &fmt::rate(s.disk_read), true, ink(rd_c), pal.dim, scale); value_row(c, f, vals_x, vy + sc(22.0), vals_w, "W", &fmt::rate(s.disk_write), false, ink(wr_c), pal.dim, scale); { - let cr = Rect { l: chart_l, t: y + sc(14.0), r: right - sc(12.0), b: y + nh - sc(14.0) }; + let cr = Rect { l: chart_l, t: y + sc(21.0), r: right - sc(12.0), b: y + nh - sc(11.0) }; let tr = [Trace { m: Metric::DiskRead, col: rd_c, tag: "R" }, Trace { m: Metric::DiskWrite, col: wr_c, tag: "W" }]; chart(c, &e, out, cr, Kind::Disk, &tr, true); } @@ -1042,11 +1255,12 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let free = format!("{} free", fmt::bytes(s.disk_total.saturating_sub(s.disk_used))); let ry = y + nh - sc(6.0); let (lw, lh) = c.measure(f.bold, &drive); + lamps.push((Fresh::Space, right - sc(11.0), ry + lh / 2)); c.text(f.bold, &drive, ix, ry, pal.fg); let fw = c.measure(f.small, &free).0; - c.text_right(f.small, &free, right - sc(12.0), ry + sc(1.0), pal.dim); + c.text_right(f.small, &free, right - sc(27.0), ry + sc(1.0), pal.dim); // 24 cells, filled for the used share, heat-coloured. - let (bl, br) = (ix + lw + sc(8.0), right - sc(12.0) - fw - sc(8.0)); + let (bl, br) = (ix + lw + sc(8.0), right - sc(27.0) - fw - sc(8.0)); let cell_gap = sc(2.0); let cells = 24; let cw = (br - bl - cell_gap * (cells - 1)) as f32 / cells as f32; @@ -1069,6 +1283,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let r = Rect { l: pad, t: y, r: right, b: y + uh }; card(c, r, rad, pal, e.hot(r), scale); out.hits.push((r, PopupHit::UsageReport)); + lamps.push((Fresh::Usage, right - sc(11.0), y + sc(11.0))); let now = usage::today(); let (yy, mm, _) = usage::ymd(now); let today = a.usage.get(now); @@ -1087,7 +1302,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let (mf, mt) = c.fit_text(&[f.bold, f.small], &mt, bars_l - (ix + mlw + sc(6.0))); c.text(mf, &mt, ix + mlw + sc(6.0), y + sc(58.0), pal.fg); { - let br = Rect { l: pad + sc(124.0), t: y + sc(12.0), r: right - sc(12.0), b: y + sc(USAGE) - sc(26.0) }; + let br = Rect { l: pad + sc(124.0), t: y + sc(12.0), r: right - sc(27.0), b: y + sc(USAGE) - sc(26.0) }; let mut days = Vec::with_capacity(USAGE_DAYS); let mut d = now; for _ in 0..USAGE_DAYS { @@ -1143,7 +1358,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { } // Legend, right-aligned under the bars. let ly = br.b + sc(7.0); - let mut lx = right - sc(12.0); + let mut lx = br.r; let (dw, dh) = c.measure(f.small, "14 days"); lx -= dw; c.text(f.small, "14 days", lx, ly, pal.dim); @@ -1171,6 +1386,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let ph = sc(PROCS); card(c, Rect { l: pad, t: y, r: right, b: y + ph }, rad, pal, false, scale); c.text(f.caps, "TOP", ix, y + sc(14.0), pal.dim); + lamps.push((Fresh::Top, right - sc(11.0), y + sc(8.0) + chip_h / 2)); let tabs: [(&str, Rgba); 3] = [ ("CPU", accent(Metric::Cpu, a.dark)), ("Memory", accent(Metric::Ram, a.dark)), @@ -1181,7 +1397,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let dot = sc(6.0); let widths: Vec = tabs.iter().map(|(t, _)| c.measure(f.small, t).0 + dot + sc(5.0) + sc(18.0)).collect(); let total = widths.iter().sum::() + sc(4.0); - let tr = Rect { l: right - sc(10.0) - total, t: y + sc(8.0), r: right - sc(10.0), b: y + sc(8.0) + chip_h }; + let tr = Rect { l: right - sc(27.0) - total, t: y + sc(8.0), r: right - sc(27.0), b: y + sc(8.0) + chip_h }; if look_chip(c, &tk, scale, tr, false, false).is_none() { c.fill_round(tr, chip_h as f32 / 2.0, pal.chip); } @@ -1252,6 +1468,9 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { } } + for (item, cx, cy) in lamps { + lamp(c, &e, out, item, cx, cy); + } if let Some(t) = out.tip.take() { draw_tip(c, &e, &t); } @@ -1323,7 +1542,7 @@ fn on_set_cursor(hwnd: HWND) -> bool { } let hand = matches!( hit_at(a, pt.x, pt.y, true), - Some(PopupHit::Range(_) | PopupHit::TopTab(_) | PopupHit::TaskMgr | PopupHit::ResMon | PopupHit::NetSettings | PopupHit::UsageReport | PopupHit::Monitor) + Some(PopupHit::Range(_) | PopupHit::TopTab(_) | PopupHit::TaskMgr | PopupHit::ResMon | PopupHit::NetSettings | PopupHit::UsageReport | PopupHit::Monitor | PopupHit::Fresh(_)) ); unsafe { SetCursor(LoadCursorW(std::ptr::null_mut(), if hand { IDC_HAND } else { IDC_ARROW })) }; true @@ -1340,6 +1559,11 @@ fn on_click(hwnd: HWND, l: LPARAM) { a.popup_top = i; unsafe { InvalidateRect(hwnd, std::ptr::null(), 0) }; } + Some(PopupHit::Fresh(item)) => { + // Reads on the sensor thread; the sample's arrival repaints with the answer. + a.read_now(item); + unsafe { InvalidateRect(hwnd, std::ptr::null(), 0) }; + } Some(PopupHit::TileStyle(on)) => { if a.m.s.tile_icons != on { a.m.s.tile_icons = on; @@ -1585,6 +1809,46 @@ mod tests { use super::*; use crate::look::Look; + #[test] + fn lamp_levels() { + assert_eq!(level(0.9, 1.0), Level::Ok); + assert_eq!(level(3.4, 1.0), Level::Ok); + assert_eq!(level(3.6, 1.0), Level::Late); + assert_eq!(level(10.5, 1.0), Level::Stale); + // Drive space is read every 30 s: a 31 s old value is on time. + assert_eq!(level(31.0, SPACE_SECS), Level::Ok); + assert_eq!(level(95.0, SPACE_SECS), Level::Late); + assert_eq!(short_age(12.4), "12 s"); + assert_eq!(short_age(185.0), "3 min"); + } + + #[test] + fn read_now_is_answered_only_by_a_later_sample() { + let mut f = FreshState::default(); + let before = Instant::now() - Duration::from_millis(10); + assert!(f.ask(Fresh::Net)); + assert!(!f.ask(Fresh::Cpu), "a second click while reading is ignored"); + // A sample that started before the click does not answer it. + f.sampled(before, Instant::now(), 0.0); + assert!(f.busy(Fresh::Net)); + let s = Instant::now() + Duration::from_millis(1); + f.sampled(s, s + Duration::from_millis(40), 0.0); + assert!(!f.busy(Fresh::Net)); + assert!(f.just_read(Fresh::Net).is_some_and(|ms| ms >= 40)); + assert!(f.just_read(Fresh::Cpu).is_none()); + assert!(f.ask(Fresh::Cpu)); + } + + #[test] + fn drive_space_age_counts_from_its_own_read() { + let mut f = FreshState::default(); + let t = Instant::now(); + f.sampled(t, t, 12.0); + assert!((f.age(Fresh::Space, t).unwrap() - 12.0).abs() < 0.01); + assert!(f.age(Fresh::Cpu, t).unwrap() < 0.01); + assert!(!f.late(1000)); + } + fn tw(f: HFONT, s: &str) -> i32 { crate::render::measure_text(f, s).0 } @@ -1617,6 +1881,13 @@ mod tests { assert!(tw(f.bold, "88.8 GB") <= bars_l - (ix + tw(f.caps, "MONTH") + sc(6.0)), "{}", ctx("month")); // Top list: values right of the bars, names in their column. assert!(tw(f.small, "888 MB/s") <= sc(64.0) + sc(8.0), "{}", ctx("top value")); + // Top tabs stay clear of "TOP" with the freshness lamp right of them. + let dot = sc(6.0); + let tabs: i32 = ["CPU", "Memory", "Disk"].iter().map(|t| tw(f.small, t) + dot + sc(5.0) + sc(18.0)).sum::() + sc(4.0); + assert!(ix + tw(f.caps, "TOP") + sc(8.0) <= right - sc(27.0) - tabs, "{}", ctx("top tabs")); + // Drive row: name, cells and "free" all left of the lamp. + let row = tw(f.bold, "C:") + sc(8.0) + sc(48.0) + sc(8.0) + tw(f.small, "888.8 GB free"); + assert!(ix + row <= right - sc(27.0), "{}", ctx("drive row")); } } } diff --git a/src/sensors/mod.rs b/src/sensors/mod.rs index c83837a..c62aa63 100644 --- a/src/sensors/mod.rs +++ b/src/sensors/mod.rs @@ -85,6 +85,8 @@ pub struct Snapshot { pub disk_used_pct: Option, pub disk_used: u64, pub disk_total: u64, + /// Seconds since the drive space above was read (it is read every 30 s). + pub disk_space_age: f64, /// Bytes/s, all physical disks. pub disk_read: f64, pub disk_write: f64, @@ -130,6 +132,8 @@ pub struct SampleOpts { pub bt: bool, /// Add-ons that are on: read while the Monitor page is on screen. pub addons: deep::addons::On, + /// Read the drive space now instead of from its 30 s cache (the popup's read-now lamp). + pub force_space: bool, } /// Gap between the priming collection and the real one when counters were just attached: @@ -284,7 +288,8 @@ impl Sensors { (s.gpu_temp, s.gpu_fan_rpm) = if opts.gpu { self.gpu_temp.sample() } else { (None, None) }; s.gpu_power_w = None; st.enter(Stage::DriveSpace); - (s.disk_used, s.disk_total, s.disk_used_pct) = self.sys_drive.sample(elapsed); + (s.disk_used, s.disk_total, s.disk_used_pct) = self.sys_drive.sample(elapsed, opts.force_space); + s.disk_space_age = self.sys_drive.age(); st.enter(Stage::Disk); if opts.disk { diff --git a/src/sensors/volume.rs b/src/sensors/volume.rs index c7c18ef..4301d88 100644 --- a/src/sensors/volume.rs +++ b/src/sensors/volume.rs @@ -13,9 +13,10 @@ pub struct SystemDrive { } impl SystemDrive { - /// Re-reads when due. Returns (used, total, used %) from the cached values. - pub fn sample(&mut self, elapsed: f64) -> (u64, u64, Option) { - let due = self.age.map_or(true, |a| a + elapsed >= REFRESH_SECS); + /// Re-reads when due, or now with `force` (the tray popup's read-now lamp). Returns + /// (used, total, used %) from the cached values. + pub fn sample(&mut self, elapsed: f64, force: bool) -> (u64, u64, Option) { + let due = force || self.age.map_or(true, |a| a + elapsed >= REFRESH_SECS); if due { self.age = Some(0.0); if let Some((total, free)) = query() { @@ -29,6 +30,11 @@ impl SystemDrive { .then(|| (self.used as f64 / self.total as f64 * 100.0).clamp(0.0, 100.0) as f32); (self.used, self.total, pct) } + + /// Seconds since the values were read (0 right after a read). + pub fn age(&self) -> f64 { + self.age.unwrap_or(0.0) + } } fn query() -> Option<(u64, u64)> { @@ -47,3 +53,18 @@ fn query() -> Option<(u64, u64)> { let ok = unsafe { GetDiskFreeSpaceExW(root.as_ptr(), &mut avail, &mut total, &mut free) }; (ok != 0 && total > 0).then_some((total, free)) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn force_reads_before_the_30_s_are_up() { + let mut d = SystemDrive::default(); + d.sample(0.0, false); + d.sample(4.0, false); + assert_eq!(d.age(), 4.0); + d.sample(1.0, true); + assert_eq!(d.age(), 0.0); + } +} diff --git a/src/sensors/worker.rs b/src/sensors/worker.rs index 9f9c9b8..882f883 100644 --- a/src/sensors/worker.rs +++ b/src/sensors/worker.rs @@ -20,6 +20,10 @@ pub struct Sample { pub detail: Option, /// Only primes rates (the popup is about to open): not a tick. pub baseline: bool, + /// When this thread started reading it (a read-now ask counts only a sample started after it). + pub started: std::time::Instant, + /// When the values were read (the popup's freshness lamps count from here). + pub taken: std::time::Instant, } #[derive(Default)] @@ -69,6 +73,7 @@ impl Worker { let (snap, stages, prime) = sensors.sample_timed(&opts); let snap = snap.clone(); let detail = opts.detail.then(|| sensors.detail().clone()); + let taken = std::time::Instant::now(); drop(p); let work = t0.elapsed().as_millis().saturating_sub(prime as u128); crate::perf::slow_sample(work, opts.detail, &stages, first); @@ -79,7 +84,7 @@ impl Worker { st.since = None; // A baseline never replaces a real tick still waiting. if !(baseline && st.done.as_ref().is_some_and(|d| !d.baseline)) { - st.done = Some(Sample { opts, snap, detail, baseline }); + st.done = Some(Sample { opts, snap, detail, baseline, started: t0, taken }); } } unsafe { PostMessageW(target as HWND, WM_APP_SAMPLED, 0, 0) }; From 58fa0fee353eab999f4d32751ec2faa3ea096e1f Mon Sep 17 00:00:00 2001 From: rajhansgithub Date: Tue, 1 Dec 2026 12:46:55 +0530 Subject: [PATCH 2/2] 2.33.0: Pick what the tray hover window shows from the Monitor page --- Cargo.lock | 2 +- Cargo.toml | 2 +- res/resmon.manifest | 2 +- setup.iss | 2 +- src/monitor.rs | 89 +++++++++++++++++++++++++++++++++++++++++++-- src/popup.rs | 67 +++++++++++++++++++++++++++++----- src/settings.rs | 57 +++++++++++++++++++++++++++++ src/settings_ui.rs | 12 ++++++ 8 files changed, 216 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d32eaa..744195f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,7 +92,7 @@ dependencies = [ [[package]] name = "resmon" -version = "2.32.0" +version = "2.33.0" dependencies = [ "embed-resource", "windows", diff --git a/Cargo.toml b/Cargo.toml index 0fb3733..b89784b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "resmon" -version = "2.32.0" +version = "2.33.0" edition = "2021" authors = ["Emulsion Labs"] description = "Ultra-light taskbar resource monitor for Windows 10/11" diff --git a/res/resmon.manifest b/res/resmon.manifest index c31de29..14b22d6 100644 --- a/res/resmon.manifest +++ b/res/resmon.manifest @@ -1,6 +1,6 @@ - + diff --git a/setup.iss b/setup.iss index 76856c6..6be0007 100644 --- a/setup.iss +++ b/setup.iss @@ -1,6 +1,6 @@ ; Inno Setup script. Build the exe first: cargo build --release #define MyAppName "Resource Monitor" -#define MyAppVersion "2.32.0" +#define MyAppVersion "2.33.0" #define MyAppPublisher "Emulsion Labs" #define MyAppURL "https://github.com/rajhansgithub/Windows_Resource_Monitoring_Tool" #define MyAppExeName "ResourceMonitor.exe" diff --git a/src/monitor.rs b/src/monitor.rs index 713ade5..c72fb3e 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -108,6 +108,8 @@ pub enum Hit { TileStyle(bool), /// Icon tile's style menu: 0 memory, 1 GPU, 2 disk, 3 network. IconPick(u8), + /// "Show in hover" button: puts item `settings::HOVER_*` in the tray hover window or takes it out. + HoverPin(u8), /// Add-ons page: open / close this add-on's tile (`addons::Addon::idx`). AddonOpen(u8), /// Add-ons page: this add-on's on / off switch. @@ -387,6 +389,8 @@ pub struct Monitor { pub speed: bool, /// Tiles drawn as lit part icons (from the settings each tick). pub icons: bool, + /// What the tray hover window shows (`Settings::hover_items`, from the settings each tick). + pub hover: u8, /// Icon picks from the settings: memory, GPU, disk, Ethernet style, Wi-Fi style. pub icon_picks: [u8; 6], /// Moving parts of the icons, stepped by the host each frame. @@ -457,6 +461,7 @@ impl Monitor { sp: speedpage::SpeedState::default(), speed: false, icons: false, + hover: crate::settings::HOVER_ALL, icon_picks: [0; 6], ianim: liticon::Anim::default(), } @@ -539,6 +544,7 @@ impl Monitor { static FORCE: std::sync::OnceLock = std::sync::OnceLock::new(); self.icons = a.m.s.tile_icons || *FORCE.get_or_init(|| std::env::var("RESMON_TILES").is_ok_and(|v| v == "icons")); self.icon_picks = [a.m.s.icon_mem, a.m.s.icon_gpu, a.m.s.icon_disk, a.m.s.icon_eth, a.m.s.icon_wifi, a.m.s.icon_net]; + self.hover = a.m.s.hover_items; if !self.bt_on && self.view == Some(Dev::Bluetooth) { self.close_dev(); } @@ -1017,6 +1023,7 @@ impl Monitor { let rr = if i as i32 == n - 1 { r } else { x + tw }; let rc = Rect { l: x, t, r: rr, b: t + th }; self.vits.push((*v, rc)); + self.push_pin(rc, rc.r - self.sc(6), *v); if let Some(d) = self.vit_dev(*v) { self.hits.push((rc, Hit::Open(d))); } @@ -1212,10 +1219,13 @@ impl Monitor { Vit::Net => Some(3), _ => None, }; + let b = self.sc(24); if let Some(k) = pick { - let b = self.sc(24); self.hits.push((Rect { l: rc.r - b - self.sc(6), t: rc.t + self.sc(6), r: rc.r - self.sc(6), b: rc.t + self.sc(6) + b }, Hit::IconPick(k))); } + // "Show in hover" sits left of the style button (or in its place). + let pin_r = if pick.is_some() { rc.r - self.sc(6) - b - self.sc(2) } else { rc.r - self.sc(6) }; + self.push_pin(rc, pin_r, *v); if let Some(d) = self.vit_dev(*v) { self.hits.push((rc, Hit::Open(d))); } @@ -1226,6 +1236,26 @@ impl Monitor { t } + /// The hover-window item a tile stands for. + fn vit_hover(v: Vit) -> u8 { + use crate::settings::*; + match v { + Vit::Cpu => HOVER_CPU, + Vit::Mem => HOVER_MEM, + Vit::Gpu | Vit::Vram => HOVER_GPU, + Vit::Disk => HOVER_DISK, + Vit::Net => HOVER_NET, + Vit::Bat => HOVER_BAT, + } + } + + /// A tile's "show in hover" button, 24 px, top right of `tile` ending at `right`. + fn push_pin(&mut self, tile: Rect, right: i32, v: Vit) { + let b = self.sc(24); + let t = tile.t + self.sc(6); + self.hits.push((Rect { l: right - b, t, r: right, b: t + b }, Hit::HoverPin(Self::vit_hover(v)))); + } + /// Parts of the GPU group in the icon view: load, then VRAM and temperature when known. fn gpu_parts(&self) -> Vec { let mut v = vec![0]; @@ -1358,7 +1388,15 @@ impl Monitor { Sec::Cpu => self.hits.push((r, Hit::Open(Dev::Cpu))), Sec::Mem => self.hits.push((r, Hit::Open(Dev::Mem))), Sec::Gpu => self.hits.push((r, Hit::Open(Dev::Gpu))), - Sec::Net => self.hits.push((r, Hit::Open(Dev::Net))), + Sec::Net => { + // "Show in hover" for data usage, right of the 14-day bars (see `nets`). + let on = self.det.adapters.iter().filter(|a| a.connected).count(); + let off = if on < self.det.adapters.len() { self.sc(26) } else { 0 }; + let t = r.t + head + on.max(1) as i32 * self.sc(84) + off + self.sc(24); + let b = self.sc(24); + self.hits.push((Rect { l: r.r - self.sc(12) - b, t, r: r.r - self.sc(12), b: t + b }, Hit::HoverPin(crate::settings::HOVER_USAGE))); + self.hits.push((r, Hit::Open(Dev::Net))); + } Sec::Bt => self.hits.push((r, Hit::Open(Dev::Bluetooth))), Sec::Addons => self.hits.push((r, Hit::Open(Dev::Addons))), Sec::Disk => { @@ -1409,6 +1447,7 @@ impl Monitor { let mut x = r.r - s(16) - tw * SORTS.len() as i32 - s(4); let t = r.t + s(14); let pl = x - s(12) - s(84); + self.hits.push((Rect { l: pl - s(8) - s(30) - s(8) - s(24), t: t - s(1), r: pl - s(8) - s(30) - s(8), b: t + s(23) }, Hit::HoverPin(crate::settings::HOVER_TOP))); self.hits.push((Rect { l: pl - s(8) - s(30), t, r: pl - s(8), b: t + s(22) }, Hit::Grouping)); self.hits.push((Rect { l: pl, t, r: x - s(12), b: t + s(22) }, Hit::Pause)); for i in 0..SORTS.len() as u8 { @@ -1469,6 +1508,7 @@ impl Monitor { let r = self.cur(Key::Vit(*v), *r); if vis(r) { d.vital(cv, *v, d.sh(r)); + d.pins(cv, d.sh(r)); if self.vit_dev(*v).is_some_and(|dv| d.hot_open(dv, r)) { d.open_ring(cv, d.sh(r)); } @@ -1494,6 +1534,7 @@ impl Monitor { Sec::Addons => d.addons_card(cv, r), Sec::Procs => d.procs(cv, r), } + d.pins(cv, r); let whole = match sec { Sec::Bt => Some(Dev::Bluetooth), Sec::Addons => Some(Dev::Addons), @@ -1613,6 +1654,34 @@ impl Draw<'_> { self.m.hits.iter().find(|x| x.1 == h).map(|x| self.sh(x.0)) } + /// The "show in hover" buttons inside `area`: a small window over a taskbar line. + /// Filled with the accent when the item is in the hover window, an outline when not. + fn pins(&self, cv: &mut Canvas, area: Rect) { + for (i, (hr, h)) in self.m.hits.iter().enumerate() { + let Hit::HoverPin(item) = *h else { continue }; + let b = self.sh(*hr); + if !area.contains(b.l, b.t) { + continue; + } + let on = self.m.hover & (1 << item) != 0; + let hot = self.hot == Some(i); + if hot { + cv.fill_round(b, b.h() as f32 / 2.0, self.col.fg.with_a(if self.dark() { 30 } else { 22 })); + } + let (cx, cy) = ((b.l + b.r) / 2, (b.t + b.b) / 2); + let win = Rect { l: cx - self.s(6), t: cy - self.s(6), r: cx + self.s(6), b: cy + self.s(2) }; + let bar = Rect { l: cx - self.s(6), t: cy + self.s(4), r: cx + self.s(6), b: cy + self.s(4) + self.s(2).max(1) }; + if on { + cv.fill_round(win, self.sf(2.0), self.col.accent); + cv.fill_round(bar, self.sf(1.0), self.col.accent); + } else { + let c = if hot { self.col.fg } else { self.col.dim.with_a(170) }; + cv.stroke_round(win, self.sf(2.0), c); + cv.fill_round(bar, self.sf(1.0), c.with_a(110)); + } + } + } + /// The pointer is on the `Open(d)` hit that covers exactly `r` (content coordinates). fn hot_open(&self, d: Dev, r: Rect) -> bool { self.hot.and_then(|i| self.m.hits.get(i)).is_some_and(|x| x.1 == Hit::Open(d) && x.0 == r) @@ -2817,7 +2886,7 @@ impl Draw<'_> { cv.numeral(self.f.head, &fmt::bytes(today_r + today_s), l, y + self.s(14), self.col.fg); self.cap(cv, "MONTH", l + self.s(120), y); cv.numeral(self.f.head, &fmt::bytes(m.month), l + self.s(120), y + self.s(14), self.col.fg); - let br = Rect { l: l + self.s(230), t: y, r: rr, b: y + self.s(58) }; + let br = Rect { l: l + self.s(230), t: y, r: rr - self.s(28), b: y + self.s(58) }; if br.w() > self.s(80) && !m.days.is_empty() { let n = m.days.len() as i32; let g = self.s(4); @@ -3297,6 +3366,20 @@ mod tests { let pick = m.hits.iter().position(|h| h.1 == Hit::IconPick(1)).unwrap(); let open = m.hits.iter().position(|h| h.1 == Hit::Open(Dev::Gpu)).unwrap(); assert!(pick < open); + // Every tile has a "show in hover" button, inside the tile, clear of the style + // button, and ahead of the tile's own click. + use crate::settings::*; + for (v, item) in [(Vit::Cpu, HOVER_CPU), (Vit::Mem, HOVER_MEM), (Vit::Gpu, HOVER_GPU), (Vit::Disk, HOVER_DISK), (Vit::Net, HOVER_NET), (Vit::Bat, HOVER_BAT)] { + let tile = m.vits.iter().find(|x| x.0 == v).unwrap().1; + let (i, pin) = m.hits.iter().enumerate().find(|(_, h)| h.1 == Hit::HoverPin(item)).map(|(i, h)| (i, h.0)).unwrap(); + assert!(pin.l >= tile.l && pin.r <= tile.r && pin.t >= tile.t && pin.b <= tile.b, "{v:?} pin inside its tile"); + if let Some(o) = m.vit_dev(v).and_then(|d| m.hits.iter().position(|h| h.1 == Hit::Open(d))) { + assert!(i < o, "{v:?} pin wins over the tile click"); + } + if let Some(p) = m.hits.iter().find(|h| matches!(h.1, Hit::IconPick(_)) && tile.contains(h.0.l, h.0.t)) { + assert!(pin.r <= p.0.l, "{v:?} pin left of the style button"); + } + } } } diff --git a/src/popup.rs b/src/popup.rs index ad46a48..3570eff 100644 --- a/src/popup.rs +++ b/src/popup.rs @@ -301,13 +301,49 @@ fn has_space(a: &App) -> bool { a.snap.disk_used_pct.is_some() && a.snap.disk_total > 0 } +/// The ring gauge picked for the hover window (`Settings::hover_items`). Disk active time takes +/// the GPU's place on a PC without a GPU. +fn gauge_shown(a: &App, m: Metric) -> bool { + use crate::settings::*; + match m { + Metric::Cpu => a.s.hover(HOVER_CPU), + Metric::Ram => a.s.hover(HOVER_MEM), + Metric::Gpu => a.s.hover(HOVER_GPU), + _ => a.s.hover(HOVER_DISK), + } +} + +/// Ring gauges the hover window shows, in order. +fn gauge_metrics(a: &App) -> Vec { + let s = &a.snap; + let mut v = vec![Metric::Cpu, Metric::Ram]; + if s.gpu.is_some() { + v.push(Metric::Gpu); + } else if s.disk_active.is_some() { + v.push(Metric::DiskActive); + } + v.retain(|m| gauge_shown(a, *m)); + v +} + fn size(a: &App) -> (i32, i32) { - let mut h = PAD + HEADER + GAP + RINGS + GAP + DIAL_CARD + GAP + DIAL_CARD + GAP + USAGE + GAP + PROCS + PAD; - if has_space(a) { - h += DISK_SPACE; + use crate::settings::*; + let mut h = PAD + HEADER + PAD; + let mut card = |v: f32| h += GAP + v; + if !gauge_metrics(a).is_empty() { + card(RINGS); + } + if a.s.hover(HOVER_NET) { + card(DIAL_CARD); + } + if a.s.hover(HOVER_DISK) { + card(DIAL_CARD + if has_space(a) { DISK_SPACE } else { 0.0 }); + } + if a.s.hover(HOVER_USAGE) { + card(USAGE + if a.s.data_cap_gb > 0.0 { CAP } else { 0.0 }); } - if a.s.data_cap_gb > 0.0 { - h += CAP; + if a.s.hover(HOVER_TOP) { + card(PROCS); } (sc(a, W), sc(a, h)) } @@ -848,7 +884,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { x -= w + sc(6.0); } } - if let Some(b) = s.battery { + if let (Some(b), true) = (s.battery, a.s.hover(crate::settings::HOVER_BAT)) { let mut t = format!("Battery {}%", b.percent); if b.charging { t.push_str(" \u{2191}"); @@ -906,13 +942,15 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { hit: PopupHit::ResMon, }); } + // Only what the Monitor page's "show in hover" buttons picked. + gauges.retain(|g| gauge_shown(a, g.m)); let icons = a.s.tile_icons; let pk = crate::liticon::resolve([a.s.icon_mem, a.s.icon_gpu, a.s.icon_disk, a.s.icon_eth, a.s.icon_wifi, a.s.icon_net], crate::liticon::detect(a.detail(), s)); let an = crate::liticon::Anim::default(); let live = crate::liticon::Live { warn: a.s.warn_pct, crit: a.s.crit_pct, ..Default::default() }; - let n = gauges.len() as i32; + let n = (gauges.len() as i32).max(1); let gw = (right - pad - gap * (n - 1)) / n; - let gh = sc(RINGS); + let gh = if gauges.is_empty() { -gap } else { sc(RINGS) }; for (i, g) in gauges.iter().enumerate() { let x = pad + i as i32 * (gw + gap); let r = Rect { l: x, t: y, r: x + gw, b: y + gh }; @@ -958,12 +996,14 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { let ix = pad + sc(14.0); // ---- network: dial (log scale), values, mirror chart + use crate::settings::{HOVER_DISK, HOVER_NET, HOVER_TOP, HOVER_USAGE}; + let down_c = accent(Metric::Down, a.dark); + let up_c = accent(Metric::Up, a.dark); + if a.s.hover(HOVER_NET) { let nh = sc(DIAL_CARD); let r = Rect { l: pad, t: y, r: right, b: y + nh }; card(c, r, rad, pal, e.hot(r), scale); out.hits.push((r, PopupHit::NetSettings)); - let down_c = accent(Metric::Down, a.dark); - let up_c = accent(Metric::Up, a.dark); if icons { let (cx, cy) = ((pad + sc(10.0)) as f32 + dial_d as f32 / 2.0, (y + nh / 2) as f32); let size = (nh - sc(12.0)) as f32; @@ -998,7 +1038,9 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { chart(c, &e, out, cr, Kind::Net, &tr, true); } y += nh + gap; + } + if a.s.hover(HOVER_DISK) { // ---- disk: active-time dial, R/W values, mirror chart, drive cells let space = has_space(a); let dh = sc(DIAL_CARD + if space { DISK_SPACE } else { 0.0 }); @@ -1062,7 +1104,9 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { } } y += dh + gap; + } + if a.s.hover(HOVER_USAGE) { // ---- data usage: today + month figures, 14-day stacked bars let cap = a.cycle_usage(); let uh = sc(USAGE) + if cap.is_some() { sc(CAP) } else { 0 }; @@ -1166,7 +1210,9 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { c.text(f.small, &label, right - sc(14.0) - lw, by - sc(4.0), pal.dim); } y += uh + gap; + } + if a.s.hover(HOVER_TOP) { // ---- top processes: one list, CPU / Memory / Disk tabs, bars let ph = sc(PROCS); card(c, Rect { l: pad, t: y, r: right, b: y + ph }, rad, pal, false, scale); @@ -1252,6 +1298,7 @@ fn draw(a: &App, c: &mut Canvas, f: &Fonts, scale: f32, out: &mut Out) { } } + } if let Some(t) = out.tip.take() { draw_tip(c, &e, &t); } diff --git a/src/settings.rs b/src/settings.rs index 7795305..52b25ec 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -215,6 +215,8 @@ pub struct Settings { pub bt_layout_set: bool, /// Monitor and popup tiles: false = ring gauges, true = lit part icons. pub tile_icons: bool, + /// What the tray hover window shows, a bit per `HOVER_ITEMS`. `hover_items = cpu, memory, …`. + pub hover_items: u8, /// Icon picks, indexes into `liticon::*_CHOICES` (0 = auto where offered). pub icon_mem: u8, pub icon_gpu: u8, @@ -288,6 +290,7 @@ impl Default for Settings { bt_layout: 1, bt_layout_set: false, tile_icons: false, + hover_items: HOVER_ALL, icon_mem: 0, icon_gpu: 0, icon_disk: 0, @@ -298,6 +301,40 @@ impl Default for Settings { } } +/// What the tray hover window can show, picked with the "show in hover" buttons on the +/// Monitor page. Bit `i` of `Settings::hover_items` is `HOVER_ITEMS[i]`. +pub const HOVER_ITEMS: [&str; 8] = ["cpu", "memory", "gpu", "disk", "network", "usage", "top", "battery"]; +pub const HOVER_CPU: u8 = 0; +pub const HOVER_MEM: u8 = 1; +pub const HOVER_GPU: u8 = 2; +pub const HOVER_DISK: u8 = 3; +pub const HOVER_NET: u8 = 4; +pub const HOVER_USAGE: u8 = 5; +pub const HOVER_TOP: u8 = 6; +pub const HOVER_BAT: u8 = 7; +/// Everything, as before the buttons existed. +pub const HOVER_ALL: u8 = 0xFF; + +impl Settings { + /// The hover window shows this item. + pub fn hover(&self, item: u8) -> bool { + self.hover_items & (1 << item) != 0 + } +} + +fn hover_parse(v: &str) -> u8 { + v.split(',').filter_map(|w| HOVER_ITEMS.iter().position(|k| k.eq_ignore_ascii_case(w.trim()))).fold(0, |m, i| m | 1 << i) +} + +fn hover_text(m: u8) -> String { + let on: Vec<&str> = HOVER_ITEMS.iter().enumerate().filter(|(i, _)| m & (1 << i) != 0).map(|(_, k)| *k).collect(); + if on.is_empty() { + "none".into() + } else { + on.join(", ") + } +} + /// Index of the choice whose key is `v` (case-insensitive). fn choice(t: &[(&str, &str)], v: &str) -> Option { t.iter().position(|(k, _)| k.eq_ignore_ascii_case(v)).map(|i| i as u8) @@ -501,6 +538,7 @@ impl Settings { } } "tile_style" => self.tile_icons = v.eq_ignore_ascii_case("icons"), + "hover_items" => self.hover_items = hover_parse(v), "icon_memory" => self.icon_mem = choice(&crate::liticon::MEM_CHOICES, v).unwrap_or(self.icon_mem), "icon_gpu" => self.icon_gpu = choice(&crate::liticon::GPU_CHOICES, v).unwrap_or(self.icon_gpu), "icon_disk" => self.icon_disk = choice(&crate::liticon::DISK_CHOICES, v).unwrap_or(self.icon_disk), @@ -738,6 +776,10 @@ impl Settings { k(&WIFI_CHOICES, self.icon_wifi), )); } + o.push_str(&format!( + "\n# Tray hover window: cpu, memory, gpu, disk, network, usage, top, battery (or none)\nhover_items = {}\n", + hover_text(self.hover_items) + )); if self.window.is_some() || !self.window_page.is_empty() { o.push_str("\n# Main window: x,y,width,height,maximized (screen pixels) and the last page\n"); if let Some(w) = &self.window { @@ -846,6 +888,21 @@ mod tests { s.apply("icon_memory = floppy\n"); assert_eq!(s.icon_mem, 3, "unknown word keeps the pick"); } + + #[test] + fn hover_items_round_trip() { + let mut s = Settings::default(); + assert!((0..8).all(|i| s.hover(i)), "everything until picked"); + s.apply("hover_items = CPU, network ,top\n"); + assert!(s.hover(HOVER_CPU) && s.hover(HOVER_NET) && s.hover(HOVER_TOP)); + assert!(!s.hover(HOVER_GPU) && !s.hover(HOVER_BAT)); + let mut back = Settings::default(); + back.apply(&s.to_text()); + assert_eq!(back.hover_items, s.hover_items); + s.apply("hover_items = none\n"); + back.apply(&s.to_text()); + assert_eq!(back.hover_items, 0); + } } /// Notify `hwnd` with `msg` whenever a file in the data folder is written. diff --git a/src/settings_ui.rs b/src/settings_ui.rs index 2eb5d76..46b8e68 100644 --- a/src/settings_ui.rs +++ b/src/settings_ui.rs @@ -4375,6 +4375,18 @@ fn activate(hwnd: HWND, id: i32) { } invalidate(hwnd); } + Some(MonHit::HoverPin(item)) => { + // Put the item in the tray hover window, or take it out. + u.mon.hover ^= 1 << item; + let m = u.mon.hover; + u.draft.hover_items = m; + invalidate(hwnd); + if let Some(a) = app() { + a.m.s.hover_items = m; + a.save_settings(); + crate::popup::refresh(); + } + } Some(MonHit::AddonOpen(i)) => { u.mon.addon_click(i); u.layout();