From feb3a793d953687486d1858dd5a7e3e639a12687 Mon Sep 17 00:00:00 2001 From: futile Date: Sat, 19 Apr 2025 04:54:02 +0100 Subject: [PATCH] redesign how anti afk activates --- src/features/anti_afk.rs | 20 +++++++++++--------- src/gui/app.rs | 5 ++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/features/anti_afk.rs b/src/features/anti_afk.rs index 6551360..1ca6a92 100644 --- a/src/features/anti_afk.rs +++ b/src/features/anti_afk.rs @@ -5,7 +5,7 @@ use windows::Win32::UI::Input::KeyboardAndMouse::{ keybd_event, }; -pub const INTERVAL: Duration = Duration::from_secs(60); +const INTERVAL: Duration = Duration::from_secs(60); const PRESS_KEYS: [VIRTUAL_KEY; 2] = [VK_NUMPAD4, VK_NUMPAD6]; #[derive(Debug)] @@ -24,19 +24,21 @@ impl Default for AntiAfk { } impl AntiAfk { + pub fn can_activate(&self) -> bool { + use util::win::{is_any_key_pressed, is_cursor_visible, is_window_focused}; + is_window_focused(WINDOW_TITLE) && !is_any_key_pressed(&PRESS_KEYS) && !is_cursor_visible() + } + + pub fn should_activate(&self) -> bool { + self.enabled && self.interval.elapsed() >= INTERVAL + } + pub fn activate(&mut self) { - if can_activate() { - send(&PRESS_KEYS); - } + send(&PRESS_KEYS); self.interval = Instant::now(); } } -pub fn can_activate() -> bool { - use util::win::{is_any_key_pressed, is_cursor_visible, is_window_focused}; - is_window_focused(WINDOW_TITLE) && !is_any_key_pressed(&PRESS_KEYS) && !is_cursor_visible() -} - fn send(vk_codes: &[VIRTUAL_KEY]) { vk_codes.iter().for_each(|vk_code| unsafe { keybd_event( diff --git a/src/gui/app.rs b/src/gui/app.rs index 1c9d387..60e54b7 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -137,8 +137,7 @@ impl App { }); } }); - if self.anti_afk.enabled && self.anti_afk.interval.elapsed() >= features::anti_afk::INTERVAL - { + if self.anti_afk.can_activate() && self.anti_afk.should_activate() { self.anti_afk.activate(); } } @@ -269,7 +268,7 @@ impl App { )); ui.label(format!( "can activate: {}", - features::anti_afk::can_activate() + self.anti_afk.can_activate() )); }); ui.collapsing("sysinfo", |ui| {