Compare commits

...

4 Commits

Author SHA1 Message Date
Timofey Gelazoniya 2c8e4da552
docs: update support statement for Linux compatibility 2024-09-06 03:02:08 +03:00
Timofey Gelazoniya 42c3278f82
chore: update todo list, add imports
- use std::time::{Duration, Instant} for convenience
2024-09-06 02:50:05 +03:00
Timofey Gelazoniya 46af703f1d
chore: bump version (v0.2.2) 2024-09-06 02:36:33 +03:00
Timofey Gelazoniya 9866802f91
refactor: reduce CPU usage 2024-09-06 02:32:16 +03:00
4 changed files with 46 additions and 37 deletions

2
Cargo.lock generated
View File

@ -1813,7 +1813,7 @@ dependencies = [
[[package]]
name = "razer-battery-report"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"hidapi",
"image",

View File

@ -1,6 +1,6 @@
[package]
name = "razer-battery-report"
version = "0.2.1"
version = "0.2.2"
authors = ["xzeldon <contact@zeldon.ru>"]
edition = "2021"
description = "Razer Battery Level Tray Indicator"

View File

@ -12,7 +12,7 @@ Show your wireless Razer devices battery levels in your system tray.
> This is a work in progress and currently support only **Razer DeathAdder V3 Pro**.
> This works pretty well on **Windows**, should work on **Linux** if you _add udev rule to get access to usb devices_ (see [here](https://github.com/libusb/hidapi/blob/master/udev/69-hid.rules)). But I haven't tested yet.
> Currently, this works only on **Windows**, should work on **Linux** if you _add udev rule to get access to usb devices_ (see [here](https://github.com/libusb/hidapi/blob/master/udev/69-hid.rules)) and remove/`cfg(windows)` some platform-specific code. But I haven't tested yet.
## Usage
@ -43,6 +43,7 @@ To build, you must have [Rust](https://www.rust-lang.org/) and
- [ ] Force update devices button in tray menu
- [ ] Colored tray icons for different battery levels
- [x] Show log window button in tray menu
- [ ] Further reduce CPU usage by using Event Loop Proxy events (more info [here](https://github.com/tauri-apps/tray-icon/issues/83#issuecomment-1697773065))
- [ ] Prebuilt Binary
- [ ] Command Line Arguments for update frequency
- [ ] Support for other Razer Devices (I only have DeathAdder V3 Pro, so I won't be able to test it with other devices)

View File

@ -3,6 +3,7 @@ use std::{
rc::Rc,
sync::{mpsc, Arc, Mutex},
thread,
time::{Duration, Instant},
};
use crate::manager::DeviceManager;
@ -14,11 +15,11 @@ use tray_icon::{
};
use winapi::um::{
wincon::GetConsoleWindow,
winuser::{self, ShowWindow, SW_SHOW},
winuser::{self, ShowWindow},
};
const BATTERY_UPDATE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(60);
const DEVICE_FETCH_INTERVAL: std::time::Duration = std::time::Duration::from_secs(5);
const BATTERY_UPDATE_INTERVAL: Duration = Duration::from_secs(60);
const DEVICE_FETCH_INTERVAL: Duration = Duration::from_secs(5);
#[derive(Debug)]
pub struct MemoryDevice {
@ -193,47 +194,54 @@ impl TrayApp {
let menu_channel = MenuEvent::receiver();
event_loop.run(move |event, _, control_flow| {
*control_flow = tao::event_loop::ControlFlow::WaitUntil(
std::time::Instant::now() + std::time::Duration::from_millis(100),
);
let mut last_update = Instant::now();
let update_interval = Duration::from_millis(100);
if let Ok(device_ids) = rx.try_recv() {
event_loop.run(move |event, _, control_flow| {
*control_flow = tao::event_loop::ControlFlow::Wait;
match event {
tao::event::Event::NewEvents(tao::event::StartCause::Init) => {
TrayInner::build_tray(&tray_icon, &tray_menu, icon.clone());
}
tao::event::Event::MainEventsCleared => {
// Check if it's time to process updates
if last_update.elapsed() >= update_interval {
while let Ok(device_ids) = rx.try_recv() {
Self::update(&devices, &device_manager, &device_ids, &tray_icon);
}
if let tao::event::Event::NewEvents(tao::event::StartCause::Init) = event {
// We create the icon once the event loop is actually running
// to prevent issues like https://github.com/tauri-apps/tray-icon/issues/90
TrayInner::build_tray(&tray_icon, &tray_menu, icon.clone());
}
if let Ok(event) = menu_channel.try_recv() {
while let Ok(event) = menu_channel.try_recv() {
let menu_items = menu_items.lock().unwrap();
let show_console_item = &menu_items[0];
let quit_item = &menu_items[1];
if event.id == show_console_item.id() {
let mut visible = console_state.lock().unwrap();
*visible = !*visible;
if *visible {
unsafe { ShowWindow(GetConsoleWindow(), winuser::SW_SHOW) };
show_console_item.set_text("Hide Log Window");
trace!("showing log window");
} else {
unsafe { ShowWindow(GetConsoleWindow(), winuser::SW_HIDE) };
show_console_item.set_text("Show Log Window");
trace!("hiding log window");
*visible = false;
} else {
unsafe { ShowWindow(GetConsoleWindow(), SW_SHOW) };
show_console_item.set_text("Hide Log Window");
trace!("showing log window");
*visible = true
}
}
if event.id == quit_item.id() {
*control_flow = tao::event_loop::ControlFlow::Exit;
return;
}
}
last_update = Instant::now();
}
}
_ => (),
}
});
}