Added status display. Added mute state.

This commit is contained in:
lennard
2023-06-11 18:16:16 +02:00
parent 76427f1381
commit 57c2687ed3
2 changed files with 46 additions and 30 deletions

View File

@@ -6,8 +6,8 @@ pub struct TrayHandler {
} }
impl TrayHandler { impl TrayHandler {
pub fn new() -> Self { pub fn new(tray: BatteryTray) -> Self {
let tray_service = TrayService::new(BatteryTray::new()); let tray_service = TrayService::new(tray);
let handle = tray_service.handle(); let handle = tray_service.handle();
tray_service.spawn(); tray_service.spawn();
TrayHandler { TrayHandler {
@@ -18,12 +18,21 @@ impl TrayHandler {
pub fn update(&self, device: &Device) { pub fn update(&self, device: &Device) {
self.handle.update(|tray: &mut BatteryTray| { tray.update(device); }) self.handle.update(|tray: &mut BatteryTray| { tray.update(device); })
} }
pub fn set_status(&mut self, message: &str) {
self.handle.update(|tray: &mut BatteryTray| { tray.set_status(message); })
}
pub fn clear_status(&mut self) {
self.handle.update(|tray: &mut BatteryTray| { tray.clear_status(); })
}
} }
#[derive(Debug)] #[derive(Debug)]
pub struct BatteryTray { pub struct BatteryTray {
battery_level: u8, battery_level: u8,
charging: bool, charging: bool,
muted: bool,
status_message: Option<String>, status_message: Option<String>,
} }
@@ -32,6 +41,7 @@ impl BatteryTray {
BatteryTray { BatteryTray {
battery_level: 0, battery_level: 0,
charging: false, charging: false,
muted: false,
status_message: Some("No device found".to_string()), status_message: Some("No device found".to_string()),
} }
} }
@@ -39,13 +49,14 @@ impl BatteryTray {
pub fn update(&mut self, device: &Device) { pub fn update(&mut self, device: &Device) {
self.battery_level = device.battery_level; self.battery_level = device.battery_level;
self.charging = device.charging; self.charging = device.charging;
self.muted = device.muted;
} }
pub fn set_status_message(&mut self, message: &str) { pub fn set_status(&mut self, message: &str) {
self.status_message = Some(message.to_string()); self.status_message = Some(message.to_string());
} }
pub fn clear_status_message(&mut self) { pub fn clear_status(&mut self) {
self.status_message = None; self.status_message = None;
} }
} }
@@ -66,20 +77,24 @@ impl Tray for BatteryTray {
] ]
} }
fn tool_tip(&self) -> ToolTip { fn tool_tip(&self) -> ToolTip {
let status = let description = match &self.status_message {
match &self.status_message { Some(m) => m.clone(),
Some(m) => m, None => {
None => "", let mut description = format!("Battery level: {}%", self.battery_level);
}; if self.charging {
let state = description += "\nCharging";
if self.charging { } else {
format!("Battery level: {}%\nCharging", self.battery_level) description += "\nNot charging";
} else { }
format!("Battery level: {}%\nNot charging", self.battery_level) if self.muted {
}; description += "\nMuted";
}
description
},
};
ToolTip { ToolTip {
title: "HyperX Cloud II".to_string(), title: "HyperX Cloud II".to_string(),
description: format!("{}\n{}", status, state), description: description,
icon_name: "".into(), icon_name: "".into(),
icon_pixmap: Vec::new(), icon_pixmap: Vec::new(),
} }

View File

@@ -2,7 +2,7 @@ use std::time::Duration;
use hyper_x_cloud_ii_wireless::{Device, DeviceError}; use hyper_x_cloud_ii_wireless::{Device, DeviceError};
mod battery_tray; mod battery_tray;
use battery_tray::TrayHandler; use battery_tray::{TrayHandler, BatteryTray};
fn pair_device() -> Device { fn pair_device() -> Device {
loop { loop {
@@ -16,16 +16,12 @@ fn pair_device() -> Device {
} }
} }
//TODO: status messages fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &mut TrayHandler) {
//TODO: use mute state
//TODO: make trayHandler dynamic
fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &TrayHandler) {
match error { match error {
DeviceError::HidError(hidapi::HidError::HidApiError { message }) => { DeviceError::HidError(hidapi::HidError::HidApiError { message }) => {
if message == "No such device" { if message == "No such device" {
eprintln!("No device found."); eprintln!("No device found.");
// handle.update(|tray: &mut BatteryTray| { tray.set_status_message("No device found"); }); tray_handler.set_status("No device found.");
*device = pair_device(); *device = pair_device();
} else { } else {
eprintln!("{message}"); eprintln!("{message}");
@@ -33,11 +29,11 @@ fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &TrayHand
} }
DeviceError::NoDeviceFound() => { DeviceError::NoDeviceFound() => {
eprintln!("{}", DeviceError::NoDeviceFound()); eprintln!("{}", DeviceError::NoDeviceFound());
// handle.update(|tray: &mut BatteryTray| { tray.set_status_message("No device found"); }); tray_handler.set_status( &DeviceError::NoDeviceFound().to_string());
} }
DeviceError::HeadSetOff() => { DeviceError::HeadSetOff() => {
eprintln!("{}", DeviceError::HeadSetOff()); eprintln!("{}", DeviceError::HeadSetOff());
// handle.update(|tray: &mut BatteryTray| { tray.set_status_message(&DeviceError::HeadSetOff().to_string()); }); tray_handler.set_status(&DeviceError::HeadSetOff().to_string());
} }
error => { error => {
eprintln!("{error}"); eprintln!("{error}");
@@ -46,7 +42,7 @@ fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &TrayHand
} }
fn main() { fn main() {
let tray_handler = TrayHandler::new(); let mut tray_handler = TrayHandler::new(BatteryTray::new());
let mut device = pair_device(); let mut device = pair_device();
tray_handler.update(&device); tray_handler.update(&device);
@@ -55,17 +51,22 @@ fn main() {
std::thread::sleep(std::time::Duration::from_secs(1)); std::thread::sleep(std::time::Duration::from_secs(1));
match device.update_battery_level() { match device.update_battery_level() {
Ok(_) => { Ok(_) => {
tray_handler.clear_status();
tray_handler.update(&device); tray_handler.update(&device);
}, },
Err(error) => { Err(error) => {
handle_error(error, &mut device, &tray_handler); handle_error(error, &mut device, &mut tray_handler);
continue; continue;
}, },
}; };
match device.wait_for_updates(Duration::from_secs(30)) { match device.wait_for_updates(Duration::from_secs(60)) {
Ok(_) => tray_handler.update(&device), Ok(_) => {
tray_handler.clear_status();
tray_handler.update(&device)
},
Err(DeviceError::NoResponse()) => (),
Err(error) => { Err(error) => {
handle_error(error, &mut device, &tray_handler); handle_error(error, &mut device, &mut tray_handler);
continue; continue;
} }
} }