1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-06 12:51:03 +02:00

refactor: partially re-add text label support to volume module

This commit is contained in:
Reinout Meliesie 2024-08-05 14:27:10 +02:00
parent 75e61cfd93
commit 081678464a
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6

View file

@ -1,3 +1,12 @@
use std::collections::HashMap;
use glib::Propagation;
use gtk::pango::EllipsizeMode;
use gtk::prelude::*;
use gtk::{Button, CellRendererText, ComboBoxText, Image, Label, Orientation, Scale, ToggleButton};
use serde::Deserialize;
use tokio::sync::mpsc;
use crate::clients::volume::{self, Event}; use crate::clients::volume::{self, Event};
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::gtk_helpers::IronbarGtkExt; use crate::gtk_helpers::IronbarGtkExt;
@ -6,20 +15,17 @@ use crate::modules::{
Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, PopupButton, WidgetContext, Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, PopupButton, WidgetContext,
}; };
use crate::{glib_recv, lock, module_impl, send_async, spawn, try_send}; use crate::{glib_recv, lock, module_impl, send_async, spawn, try_send};
use glib::Propagation;
use gtk::pango::EllipsizeMode;
use gtk::prelude::*;
use gtk::{
Box as GtkBox, Button, CellRendererText, ComboBoxText, Image, Label, Orientation, Scale,
ToggleButton,
};
use serde::Deserialize;
use std::collections::HashMap;
use tokio::sync::mpsc;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct VolumeModule { pub struct VolumeModule {
/// The format string to use for the widget button label.
/// For available tokens, see [below](#formatting-tokens).
///
/// **Default**: `{icon} {percentage}%`
#[serde(default = "default_format")]
format: String,
/// Maximum value to allow volume sliders to reach. /// Maximum value to allow volume sliders to reach.
/// Pulse supports values > 100 but this may result in distortion. /// Pulse supports values > 100 but this may result in distortion.
/// ///
@ -27,6 +33,12 @@ pub struct VolumeModule {
#[serde(default = "default_max_volume")] #[serde(default = "default_max_volume")]
max_volume: f64, max_volume: f64,
/// Volume state icons.
///
/// See [icons](#icons).
#[serde(default)]
icons: Icons,
#[serde(default = "default_icon_size")] #[serde(default = "default_icon_size")]
icon_size: i32, icon_size: i32,
@ -35,10 +47,79 @@ pub struct VolumeModule {
pub common: Option<CommonConfig>, pub common: Option<CommonConfig>,
} }
fn default_format() -> String {
String::from("{icon} {percentage}%")
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Icons {
/// Icon to show for high volume levels.
///
/// **Default**: `󰕾`
#[serde(default = "default_icon_volume_high")]
volume_high: String,
/// Icon to show for medium volume levels.
///
/// **Default**: `󰖀`
#[serde(default = "default_icon_volume_medium")]
volume_medium: String,
/// Icon to show for low volume levels.
///
/// **Default**: `󰕿`
#[serde(default = "default_icon_volume_low")]
volume_low: String,
/// Icon to show for muted outputs.
///
/// **Default**: `󰝟`
#[serde(default = "default_icon_muted")]
muted: String,
}
impl Icons {
fn volume_icon(&self, volume_percent: f64) -> &str {
match volume_percent as u32 {
0..=33 => &self.volume_low,
34..=66 => &self.volume_medium,
67.. => &self.volume_high,
}
}
}
impl Default for Icons {
fn default() -> Self {
Self {
volume_high: default_icon_volume_high(),
volume_medium: default_icon_volume_medium(),
volume_low: default_icon_volume_low(),
muted: default_icon_muted(),
}
}
}
const fn default_max_volume() -> f64 { const fn default_max_volume() -> f64 {
100.0 100.0
} }
fn default_icon_volume_high() -> String {
String::from("󰕾")
}
fn default_icon_volume_medium() -> String {
String::from("󰖀")
}
fn default_icon_volume_low() -> String {
String::from("󰕿")
}
fn default_icon_muted() -> String {
String::from("󰝟")
}
const fn default_icon_size() -> i32 { const fn default_icon_size() -> i32 {
24 24
} }
@ -182,16 +263,16 @@ impl Module<Button> for VolumeModule {
rx: tokio::sync::broadcast::Receiver<Self::SendMessage>, rx: tokio::sync::broadcast::Receiver<Self::SendMessage>,
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>, _context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
info: &ModuleInfo, info: &ModuleInfo,
) -> Option<GtkBox> ) -> Option<gtk::Box>
where where
Self: Sized, Self: Sized,
{ {
let container = GtkBox::new(Orientation::Horizontal, 10); let container = gtk::Box::new(Orientation::Horizontal, 10);
let sink_container = GtkBox::new(Orientation::Vertical, 5); let sink_container = gtk::Box::new(Orientation::Vertical, 5);
sink_container.add_class("device-box"); sink_container.add_class("device-box");
let input_container = GtkBox::new(Orientation::Vertical, 5); let input_container = gtk::Box::new(Orientation::Vertical, 5);
input_container.add_class("apps-box"); input_container.add_class("apps-box");
container.add(&sink_container); container.add(&sink_container);
@ -323,7 +404,7 @@ impl Module<Button> for VolumeModule {
Event::AddInput(info) => { Event::AddInput(info) => {
let index = info.index; let index = info.index;
let item_container = GtkBox::new(Orientation::Vertical, 0); let item_container = gtk::Box::new(Orientation::Vertical, 0);
item_container.add_class("app-box"); item_container.add_class("app-box");
let label = Label::new(Some(&info.name)); let label = Label::new(Some(&info.name));
@ -407,7 +488,7 @@ impl Module<Button> for VolumeModule {
} }
struct InputUi { struct InputUi {
container: GtkBox, container: gtk::Box,
label: Label, label: Label,
slider: Scale, slider: Scale,
btn_mute_icon: Image, btn_mute_icon: Image,