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

View File

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