Skip to content

Commit 266b8d5

Browse files
committed
v1.0.2
- Seconds countdown below 61s - shows 1m at 61s, then 60, 59... 0 counting down each second, then now when reset time passes - Fast polling at reset - when either timer shows now, polls the API every 5s to pick up fresh rate limit data - Auto-restores normal polling - stops the 5s fast poll as soon as fresh data comes back with a new reset time
1 parent d8d15dc commit 266b8d5

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/native_interop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub const WINEVENT_OUTOFCONTEXT: u32 = 0x0000;
1616
// Timer IDs
1717
pub const TIMER_POLL: usize = 1;
1818
pub const TIMER_COUNTDOWN: usize = 2;
19+
pub const TIMER_RESET_POLL: usize = 3;
1920

2021
// Custom messages
2122
pub const WM_APP: u32 = 0x8000;

src/poller.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ fn format_countdown(resets_at: Option<SystemTime>) -> String {
235235
format!("{total_days}d")
236236
} else if total_mins > 61 {
237237
format!("{total_hours}h")
238-
} else {
238+
} else if total_secs > 60 {
239239
format!("{total_mins}m")
240+
} else {
241+
format!("{total_secs}")
240242
}
241243
}
242244

@@ -250,6 +252,11 @@ pub fn time_until_display_change(resets_at: Option<SystemTime>) -> Option<Durati
250252
let total_hours = total_secs / 3600;
251253
let total_days = total_secs / 86400;
252254

255+
if total_secs <= 60 {
256+
// Update every second during final countdown
257+
return Some(Duration::from_secs(1));
258+
}
259+
253260
let next_boundary = if total_days >= 1 {
254261
Duration::from_secs(total_days * 86400)
255262
} else if total_mins > 61 {
@@ -269,3 +276,10 @@ pub fn time_until_display_change(resets_at: Option<SystemTime>) -> Option<Durati
269276
Some(Duration::from_secs(1))
270277
}
271278
}
279+
280+
/// Returns true if either section has reached "now" (reset time has passed).
281+
pub fn is_past_reset(data: &UsageData) -> bool {
282+
let now = SystemTime::now();
283+
let past = |s: &UsageSection| matches!(s.resets_at, Some(t) if now.duration_since(t).is_ok());
284+
past(&data.session) || past(&data.weekly)
285+
}

src/window.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use windows::Win32::UI::WindowsAndMessaging::*;
1111
use windows::core::PCWSTR;
1212

1313
use crate::models::UsageData;
14-
use crate::native_interop::{self, Color, TIMER_COUNTDOWN, TIMER_POLL, WM_APP_USAGE_UPDATED};
14+
use crate::native_interop::{self, Color, TIMER_COUNTDOWN, TIMER_POLL, TIMER_RESET_POLL, WM_APP_USAGE_UPDATED};
1515
use crate::poller;
1616
use crate::theme;
1717

@@ -559,6 +559,13 @@ fn do_poll(send_hwnd: SendHwnd) {
559559
s.weekly_percent = data.weekly.percentage;
560560
s.session_text = session_text;
561561
s.weekly_text = weekly_text;
562+
// Stop fast-poll if reset data is now fresh
563+
if !poller::is_past_reset(&data) {
564+
unsafe {
565+
let _ = KillTimer(hwnd, TIMER_RESET_POLL);
566+
}
567+
}
568+
562569
s.data = Some(data);
563570
s.last_poll_ok = true;
564571

@@ -615,6 +622,14 @@ fn schedule_countdown_timer() {
615622
};
616623

617624
let hwnd = s.hwnd.to_hwnd();
625+
626+
// If a reset time has passed, poll every 5s to pick up fresh data
627+
if poller::is_past_reset(data) {
628+
unsafe {
629+
SetTimer(hwnd, TIMER_RESET_POLL, 5_000, None);
630+
}
631+
}
632+
618633
let session_delay = poller::time_until_display_change(data.session.resets_at);
619634
let weekly_delay = poller::time_until_display_change(data.weekly.resets_at);
620635

@@ -804,7 +819,12 @@ unsafe extern "system" fn wnd_proc(
804819
render_layered();
805820
schedule_countdown_timer();
806821
}
807-
3 => {}
822+
TIMER_RESET_POLL => {
823+
let sh = SendHwnd::from_hwnd(hwnd);
824+
std::thread::spawn(move || {
825+
do_poll(sh);
826+
});
827+
}
808828
_ => {}
809829
}
810830
LRESULT(0)

0 commit comments

Comments
 (0)