diff --git a/src/dynamic_value/dynamic_string.rs b/src/dynamic_value/dynamic_string.rs index 6c0cfb5..7c0dbdb 100644 --- a/src/dynamic_value/dynamic_string.rs +++ b/src/dynamic_value/dynamic_string.rs @@ -22,7 +22,7 @@ enum DynamicStringSegment { /// /// ```rs /// dynamic_string(&text, move |string| { -/// label.set_markup(&string); +/// label.set_label_escaped(&string); /// }); /// ``` pub fn dynamic_string(input: &str, mut f: F) diff --git a/src/gtk_helpers.rs b/src/gtk_helpers.rs index cc88822..628ac3a 100644 --- a/src/gtk_helpers.rs +++ b/src/gtk_helpers.rs @@ -1,4 +1,4 @@ -use glib::IsA; +use glib::{markup_escape_text, IsA}; use gtk::prelude::*; use gtk::{Orientation, Widget}; @@ -75,3 +75,22 @@ impl> IronbarGtkExt for W { unsafe { self.set_data(key, value) } } } + +pub trait IronbarLabelExt { + /// Sets the label value to the provided string. + /// + /// If the label does not contain markup `span` tags, + /// the text is escaped to avoid issues with special characters (ie `&`). + /// Otherwise, the text is used verbatim, and it is up to the user to escape. + fn set_label_escaped(&self, label: &str); +} + +impl IronbarLabelExt for gtk::Label { + fn set_label_escaped(&self, label: &str) { + if !label.contains(" for LabelModule { context: WidgetContext, _info: &ModuleInfo, ) -> Result> { - let label = Label::new(None); - label.set_use_markup(true); + let label = Label::builder().use_markup(true).build(); { let label = label.clone(); - glib_recv!(context.subscribe(), string => label.set_markup(&string)); + glib_recv!(context.subscribe(), string => label.set_label_escaped(&string)); } Ok(ModuleParts { diff --git a/src/modules/music/mod.rs b/src/modules/music/mod.rs index 76e45cd..96cb91a 100644 --- a/src/modules/music/mod.rs +++ b/src/modules/music/mod.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use std::time::Duration; use color_eyre::Result; -use glib::{markup_escape_text, Propagation, PropertySet}; +use glib::{Propagation, PropertySet}; use gtk::prelude::*; use gtk::{Button, IconTheme, Label, Orientation, Scale}; use regex::Regex; @@ -16,7 +16,7 @@ use crate::clients::music::{ self, MusicClient, PlayerState, PlayerUpdate, ProgressTick, Status, Track, }; use crate::clients::Clients; -use crate::gtk_helpers::IronbarGtkExt; +use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt}; use crate::image::{new_icon_button, new_icon_label, ImageProvider}; use crate::modules::PopupButton; use crate::modules::{ @@ -189,10 +189,11 @@ impl Module