Added basic tray icon.
This commit is contained in:
@@ -8,3 +8,4 @@ edition = "2021"
|
||||
[dependencies]
|
||||
hidapi = "2.3.3"
|
||||
thiserror = "1.0.40"
|
||||
ksni = "0.2.0"
|
||||
45
src/battery_tray.rs
Normal file
45
src/battery_tray.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use ksni::{Tray, MenuItem, menu::{StandardItem}};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BatteryTray {
|
||||
battery_level: u8,
|
||||
charging: bool,
|
||||
}
|
||||
|
||||
impl BatteryTray {
|
||||
pub fn new(battery_level: u8, charging: bool) -> Self {
|
||||
BatteryTray {
|
||||
battery_level,
|
||||
charging,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self, battery_level: u8, charging: bool) {
|
||||
self.battery_level = battery_level;
|
||||
self.charging = charging;
|
||||
}
|
||||
}
|
||||
|
||||
impl Tray for BatteryTray {
|
||||
fn icon_name(&self) -> String {
|
||||
"headset".into()
|
||||
}
|
||||
fn title(&self) -> String {
|
||||
if self.charging {
|
||||
format!("Battery level: {}%\nCharging", self.battery_level).to_string()
|
||||
} else {
|
||||
format!("Battery level: {}%\nNot charging", self.battery_level).to_string()
|
||||
}
|
||||
}
|
||||
fn menu(&self) -> Vec<MenuItem<Self>> {
|
||||
vec![
|
||||
StandardItem {
|
||||
label: "Exit".into(),
|
||||
icon_name: "application-exit".into(),
|
||||
activate: Box::new(|_| std::process::exit(0)),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
]
|
||||
}
|
||||
}
|
||||
22
src/main.rs
22
src/main.rs
@@ -1,4 +1,7 @@
|
||||
use hyper_x_cloud_ii_wireless::Device;
|
||||
use ksni::TrayService;
|
||||
use battery_tray::BatteryTray;
|
||||
mod battery_tray;
|
||||
|
||||
fn main() {
|
||||
let device = match Device::new() {
|
||||
@@ -21,4 +24,23 @@ fn main() {
|
||||
} else {
|
||||
println!("Not charging");
|
||||
}
|
||||
|
||||
let service = TrayService::new(BatteryTray::new(battery_level, charging));
|
||||
let handle = service.handle();
|
||||
service.spawn();
|
||||
|
||||
// Run loop
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
let (battery_level, charging) = match device.get_battery_level() {
|
||||
Ok(t) => t,
|
||||
Err(error) => {
|
||||
eprintln!("{error}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
handle.update(|tray: &mut BatteryTray| {
|
||||
tray.refresh(battery_level, charging);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user