mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
Replace text with icons in volume module
This commit is contained in:
parent
17e830afbf
commit
da94923f73
1 changed files with 71 additions and 80 deletions
|
@ -1,6 +1,7 @@
|
|||
use crate::clients::volume::{self, Event};
|
||||
use crate::config::CommonConfig;
|
||||
use crate::gtk_helpers::IronbarGtkExt;
|
||||
use crate::image::ImageProvider;
|
||||
use crate::modules::{
|
||||
Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, PopupButton, WidgetContext,
|
||||
};
|
||||
|
@ -8,81 +9,32 @@ use crate::{glib_recv, lock, module_impl, send_async, spawn, try_send};
|
|||
use glib::Propagation;
|
||||
use gtk::pango::EllipsizeMode;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Button, CellRendererText, ComboBoxText, Label, Orientation, Scale, ToggleButton};
|
||||
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)]
|
||||
pub struct VolumeModule {
|
||||
#[serde(default = "default_format")]
|
||||
format: String,
|
||||
|
||||
#[serde(default = "default_max_volume")]
|
||||
max_volume: f64,
|
||||
|
||||
#[serde(default)]
|
||||
icons: Icons,
|
||||
#[serde(default = "default_icon_size")]
|
||||
icon_size: i32,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub common: Option<CommonConfig>,
|
||||
}
|
||||
|
||||
fn default_format() -> String {
|
||||
String::from("{icon} {percentage}%")
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Icons {
|
||||
#[serde(default = "default_icon_volume_high")]
|
||||
volume_high: String,
|
||||
#[serde(default = "default_icon_volume_medium")]
|
||||
volume_medium: String,
|
||||
#[serde(default = "default_icon_volume_low")]
|
||||
volume_low: String,
|
||||
#[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 {
|
||||
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 {
|
||||
24
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -185,22 +137,23 @@ impl Module<Button> for VolumeModule {
|
|||
|
||||
{
|
||||
let rx = context.subscribe();
|
||||
let icons = self.icons.clone();
|
||||
let button = button.clone();
|
||||
let icon_theme = info.icon_theme.clone();
|
||||
|
||||
let format = self.format.clone();
|
||||
let image_icon = Image::new();
|
||||
image_icon.add_class("icon");
|
||||
button.set_image(Some(&image_icon));
|
||||
|
||||
glib_recv!(rx, event => {
|
||||
match event {
|
||||
Event::AddSink(sink) | Event::UpdateSink(sink) if sink.active => {
|
||||
let label = format
|
||||
.replace("{icon}", if sink.muted { &icons.muted } else { icons.volume_icon(sink.volume) })
|
||||
.replace("{percentage}", &sink.volume.to_string())
|
||||
.replace("{name}", &sink.description);
|
||||
|
||||
button.set_label(&label);
|
||||
ImageProvider::parse(
|
||||
&determine_volume_icon(sink.muted, sink.volume),
|
||||
&icon_theme,
|
||||
false,
|
||||
self.icon_size,
|
||||
).map(|provider| provider.load_into_image(image_icon.clone()));
|
||||
},
|
||||
_ => {}
|
||||
_ => {},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -222,17 +175,17 @@ impl Module<Button> for VolumeModule {
|
|||
tx: mpsc::Sender<Self::ReceiveMessage>,
|
||||
rx: tokio::sync::broadcast::Receiver<Self::SendMessage>,
|
||||
_context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
||||
_info: &ModuleInfo,
|
||||
) -> Option<gtk::Box>
|
||||
info: &ModuleInfo,
|
||||
) -> Option<GtkBox>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let container = gtk::Box::new(Orientation::Horizontal, 10);
|
||||
let container = GtkBox::new(Orientation::Horizontal, 10);
|
||||
|
||||
let sink_container = gtk::Box::new(Orientation::Vertical, 5);
|
||||
let sink_container = GtkBox::new(Orientation::Vertical, 5);
|
||||
sink_container.add_class("device-box");
|
||||
|
||||
let input_container = gtk::Box::new(Orientation::Vertical, 5);
|
||||
let input_container = GtkBox::new(Orientation::Vertical, 5);
|
||||
input_container.add_class("apps-box");
|
||||
|
||||
container.add(&sink_container);
|
||||
|
@ -292,6 +245,8 @@ impl Module<Button> for VolumeModule {
|
|||
|
||||
let btn_mute = ToggleButton::new();
|
||||
btn_mute.add_class("btn-mute");
|
||||
let btn_mute_icon = Image::new();
|
||||
btn_mute.set_image(Some(&btn_mute_icon));
|
||||
sink_container.add(&btn_mute);
|
||||
|
||||
{
|
||||
|
@ -311,6 +266,7 @@ impl Module<Button> for VolumeModule {
|
|||
let mut inputs = HashMap::new();
|
||||
|
||||
{
|
||||
let icon_theme = info.icon_theme.clone();
|
||||
let input_container = input_container.clone();
|
||||
|
||||
let mut sinks = vec![];
|
||||
|
@ -325,7 +281,12 @@ impl Module<Button> for VolumeModule {
|
|||
slider.set_value(info.volume);
|
||||
|
||||
btn_mute.set_active(info.muted);
|
||||
btn_mute.set_label(if info.muted { &self.icons.muted } else { self.icons.volume_icon(info.volume) });
|
||||
ImageProvider::parse(
|
||||
&determine_volume_icon(info.muted, info.volume),
|
||||
&icon_theme,
|
||||
false,
|
||||
self.icon_size,
|
||||
).map(|provider| provider.load_into_image(btn_mute_icon.clone()));
|
||||
}
|
||||
|
||||
sinks.push(info);
|
||||
|
@ -337,7 +298,12 @@ impl Module<Button> for VolumeModule {
|
|||
slider.set_value(info.volume);
|
||||
|
||||
btn_mute.set_active(info.muted);
|
||||
btn_mute.set_label(if info.muted { &self.icons.muted } else { self.icons.volume_icon(info.volume) });
|
||||
ImageProvider::parse(
|
||||
&determine_volume_icon(info.muted, info.volume),
|
||||
&icon_theme,
|
||||
false,
|
||||
self.icon_size,
|
||||
).map(|provider| provider.load_into_image(btn_mute_icon.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -351,7 +317,7 @@ impl Module<Button> for VolumeModule {
|
|||
Event::AddInput(info) => {
|
||||
let index = info.index;
|
||||
|
||||
let item_container = gtk::Box::new(Orientation::Vertical, 0);
|
||||
let item_container = GtkBox::new(Orientation::Vertical, 0);
|
||||
item_container.add_class("app-box");
|
||||
|
||||
let label = Label::new(Some(&info.name));
|
||||
|
@ -375,9 +341,16 @@ impl Module<Button> for VolumeModule {
|
|||
|
||||
let btn_mute = ToggleButton::new();
|
||||
btn_mute.add_class("btn-mute");
|
||||
let btn_mute_icon = Image::new();
|
||||
btn_mute.set_image(Some(&btn_mute_icon));
|
||||
|
||||
btn_mute.set_active(info.muted);
|
||||
btn_mute.set_label(if info.muted { &self.icons.muted } else { self.icons.volume_icon(info.volume) });
|
||||
ImageProvider::parse(
|
||||
&determine_volume_icon(info.muted, info.volume),
|
||||
&icon_theme,
|
||||
false,
|
||||
self.icon_size,
|
||||
).map(|provider| provider.load_into_image(btn_mute_icon.clone()));
|
||||
|
||||
{
|
||||
let tx = tx.clone();
|
||||
|
@ -398,7 +371,7 @@ impl Module<Button> for VolumeModule {
|
|||
container: item_container,
|
||||
label,
|
||||
slider,
|
||||
btn_mute
|
||||
btn_mute_icon,
|
||||
});
|
||||
}
|
||||
Event::UpdateInput(info) => {
|
||||
|
@ -406,7 +379,12 @@ impl Module<Button> for VolumeModule {
|
|||
ui.label.set_label(&info.name);
|
||||
ui.slider.set_value(info.volume);
|
||||
ui.slider.set_sensitive(info.can_set_volume);
|
||||
ui.btn_mute.set_label(if info.muted { &self.icons.muted } else { self.icons.volume_icon(info.volume) });
|
||||
ImageProvider::parse(
|
||||
&determine_volume_icon(info.muted, info.volume),
|
||||
&icon_theme,
|
||||
false,
|
||||
self.icon_size,
|
||||
).map(|provider| provider.load_into_image(ui.btn_mute_icon.clone()));
|
||||
}
|
||||
}
|
||||
Event::RemoveInput(index) => {
|
||||
|
@ -423,8 +401,21 @@ impl Module<Button> for VolumeModule {
|
|||
}
|
||||
|
||||
struct InputUi {
|
||||
container: gtk::Box,
|
||||
container: GtkBox,
|
||||
label: Label,
|
||||
slider: Scale,
|
||||
btn_mute: ToggleButton,
|
||||
btn_mute_icon: Image,
|
||||
}
|
||||
|
||||
fn determine_volume_icon(muted: bool, volume: f64) -> String {
|
||||
let icon_variant = if muted {
|
||||
"muted"
|
||||
} else if volume <= 33.3333 {
|
||||
"low"
|
||||
} else if volume <= 66.6667 {
|
||||
"medium"
|
||||
} else {
|
||||
"high"
|
||||
};
|
||||
format!("audio-volume-{icon_variant}-symbolic")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue