Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/devices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ use thistermination::TerminationFull;

const PASSIVE_REFRESH_TIME_OUT: Duration = Duration::from_secs(2);

pub fn format_int_value(value: u8, suffix: &str) -> String {
if value == 0 && suffix == "min" {
"never".to_string()
} else {
format!("{}{}", value, suffix)
}
}

type DeviceFactory = fn(DeviceState) -> Box<dyn Device>;

struct DeviceEntry {
Expand Down Expand Up @@ -589,8 +597,8 @@ impl DeviceProperties {
let (prefix, data, suffix) = match prop {
PropertyDescriptorWrapper::Int(property_descriptor, _) => (
property_descriptor.prefix,
&property_descriptor.data.map(|v| v.to_string()),
property_descriptor.suffix,
&property_descriptor.data.map(|v| format_int_value(v, property_descriptor.suffix)),
"",
),
PropertyDescriptorWrapper::Bool(property_descriptor) => (
property_descriptor.prefix,
Expand All @@ -617,8 +625,8 @@ impl DeviceProperties {
let (prefix, data, suffix, property_type) = match prop {
PropertyDescriptorWrapper::Int(property_descriptor, _) => (
property_descriptor.prefix,
&property_descriptor.data.map(|v| v.to_string()),
property_descriptor.suffix,
&property_descriptor.data.map(|v| format_int_value(v, property_descriptor.suffix)),
"",
property_descriptor.property_type,
),
PropertyDescriptorWrapper::Bool(property_descriptor) => (
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ fn main() {
.required(false)
.help("Use verbose output ")
)
.arg(Arg::new("monochrome_icons")
.long("monochrome-icons")
.action(ArgAction::SetTrue)
.required(false)
.help("Use the symbolic (monochrome) variants of the system tray icons")
)
.get_matches();

let press_mute_key = *matches.get_one::<bool>("press_mute_key").unwrap_or(&true);
Expand All @@ -205,11 +211,12 @@ fn main() {
None
};
VERBOSE.set(matches.get_flag("verbose")).unwrap();
let monochrome_icons = matches.get_flag("monochrome_icons");

let refresh_interval = *matches.get_one::<u64>("refresh_interval").unwrap_or(&3);
let refresh_interval = Duration::from_secs(refresh_interval);
let (tx, rx) = mpsc::channel();
let tray_handler = TrayHandler::new(StatusTray::new(tx));
let tray_handler = TrayHandler::new(StatusTray::new(tx, monochrome_icons));
loop {
let mut device = loop {
match connect_compatible_device() {
Expand Down
40 changes: 31 additions & 9 deletions src/status_tray.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::mpsc::Sender;

use hyper_headset::devices::{DeviceEvent, DeviceProperties, DeviceState, PropertyType};
use hyper_headset::devices::{
format_int_value, DeviceEvent, DeviceProperties, DeviceState, PropertyType,
};
use ksni::{
menu::{StandardItem, SubMenu},
Handle, MenuItem, ToolTip, Tray, TrayService,
Expand Down Expand Up @@ -39,13 +41,31 @@ impl TrayHandler {
pub struct StatusTray {
device_properties: Option<DeviceProperties>,
update_sender: Sender<DeviceEvent>,
monochrome_icons: bool,
}

impl StatusTray {
pub fn new(update_sender: Sender<DeviceEvent>) -> Self {
pub fn new(update_sender: Sender<DeviceEvent>, monochrome_icons: bool) -> Self {
StatusTray {
device_properties: None,
update_sender,
monochrome_icons,
}
}

fn fallback_headset_icon(&self) -> &'static str {
if self.monochrome_icons {
"audio-headset-symbolic"
} else {
"audio-headset"
}
}

fn exit_icon(&self) -> &'static str {
if self.monochrome_icons {
"application-exit-symbolic"
} else {
"application-exit"
}
}
}
Expand All @@ -57,7 +77,7 @@ impl Tray for StatusTray {

fn icon_name(&self) -> String {
TrayBatteryIconState::from_device_properties(self.device_properties.as_ref())
.linux_icon_name()
.linux_icon_name(self.monochrome_icons)
.to_string()
}

Expand All @@ -66,7 +86,7 @@ impl Tray for StatusTray {
return ToolTip {
title: "Unknown".to_string(),
description: NO_COMPATIBLE_DEVICE.to_string(),
icon_name: "audio-headset".into(),
icon_name: self.fallback_headset_icon().into(),
icon_pixmap: Vec::new(),
};
};
Expand All @@ -88,16 +108,17 @@ impl Tray for StatusTray {
.unwrap_or("Unknown".to_string()),
description,
icon_name: TrayBatteryIconState::from_device_properties(Some(device_properties))
.linux_icon_name()
.linux_icon_name(self.monochrome_icons)
.to_string(),
icon_pixmap: Vec::new(),
}
}

fn menu(&self) -> Vec<MenuItem<Self>> {
let exit_icon = self.exit_icon();
let make_exit = || StandardItem {
label: "Quit".into(),
icon_name: "application-exit".into(),
icon_name: exit_icon.into(),
activate: Box::new(|_| std::process::exit(0)),
..Default::default()
};
Expand Down Expand Up @@ -162,7 +183,7 @@ impl Tray for StatusTray {
.map(|val| {
let update_sender = self.update_sender.clone();
StandardItem {
label: format!("{}{}", val, property.suffix),
label: format_int_value(*val, property.suffix),
enabled: property.property_type == PropertyType::ReadWrite
&& property.data.is_some(),
activate: Box::new(move |_| {
Expand All @@ -178,8 +199,9 @@ impl Tray for StatusTray {
menu_items.push(
SubMenu {
label: format!(
"{} {}{}",
property.prefix, current_value, property.suffix
"{} {}",
property.prefix,
format_int_value(current_value, property.suffix)
),
enabled: property.property_type == PropertyType::ReadWrite
&& property.data.is_some(),
Expand Down
10 changes: 5 additions & 5 deletions src/status_tray_not_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

#[cfg(target_os = "windows")]
use image::{Rgba, RgbaImage};
use hyper_headset::devices::{DeviceEvent, DeviceProperties, PropertyType};
use hyper_headset::devices::{format_int_value, DeviceEvent, DeviceProperties, PropertyType};
use tray_icon::{
menu::{CheckMenuItem, Menu, MenuEvent, MenuId, MenuItem, PredefinedMenuItem, Submenu},
TrayIcon, TrayIconBuilder,
Expand Down Expand Up @@ -404,7 +404,7 @@ impl TrayApp {
continue;
};
let menu_item = MenuItem::new(
format!("{} {}{}", property.prefix, current_value, property.suffix),
format!("{} {}", property.prefix, format_int_value(current_value, property.suffix)),
false,
None,
);
Expand All @@ -415,13 +415,13 @@ impl TrayApp {
continue;
};
let submenu = Submenu::new(
format!("{} {}{}", property.prefix, current_value, property.suffix),
format!("{} {}", property.prefix, format_int_value(current_value, property.suffix)),
property.property_type == PropertyType::ReadWrite,
);

for item_value in items {
let entry =
MenuItem::new(format!("{}{}", item_value, property.suffix), true, None);
let entry =
MenuItem::new(format_int_value(*item_value, property.suffix), true, None, );
submenu.append(&entry).unwrap();

let create_event = property.create_event;
Expand Down
36 changes: 27 additions & 9 deletions src/tray_battery_icon_state.rs
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using my current icon pack, BeautyLine, most symbolic battery indicators look the same. Furthermore, battery-medium-symbolic seems not to exist in my icon pack.
I think it would be best to not make this the default and instead add a flag, something like --monochrome-icons to enable monochrome icons.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. The use of symbolic icons is a convention on recent Gnome/Gtk distrib (see here) but not necessarily followed everywhere. I will work on this option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--monochrome-icons option added

Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ impl TrayBatteryIconState {
}

#[cfg(target_os = "linux")]
pub fn linux_icon_name(self) -> &'static str {
pub fn linux_icon_name(self, monochrome: bool) -> &'static str {
match self {
Self::NoDevice | Self::Disconnected | Self::ConnectedUnknown => "audio-headset",
Self::NoDevice | Self::Disconnected | Self::ConnectedUnknown => {
if monochrome {
"audio-headset-symbolic"
} else {
"audio-headset"
}
}
Self::Connected { percent, charging } => {
let level_name = if percent <= 10 {
"battery-caution"
Expand All @@ -64,15 +70,27 @@ impl TrayBatteryIconState {
"battery-full"
};
if charging {
match level_name {
"battery-caution" => "battery-caution-charging",
"battery-low" => "battery-low-charging",
"battery-medium" => "battery-medium-charging",
"battery-good" => "battery-good-charging",
_ => "battery-full-charging",
match (level_name, monochrome) {
("battery-caution", false) => "battery-caution-charging",
("battery-low", false) => "battery-low-charging",
("battery-medium", false) => "battery-medium-charging",
("battery-good", false) => "battery-good-charging",
(_, false) => "battery-full-charging",
("battery-caution", true) => "battery-caution-charging-symbolic",
("battery-low", true) => "battery-low-charging-symbolic",
("battery-medium", true) => "battery-medium-charging-symbolic",
("battery-good", true) => "battery-good-charging-symbolic",
(_, true) => "battery-full-charging-symbolic",
}
} else {
level_name
match (level_name, monochrome) {
("battery-caution", true) => "battery-caution-symbolic",
("battery-low", true) => "battery-low-symbolic",
("battery-medium", true) => "battery-medium-symbolic",
("battery-good", true) => "battery-good-symbolic",
(name, false) => name,
(_, true) => "battery-full-symbolic",
}
}
}
}
Expand Down