Skip to content

Commit

Permalink
fix: markup escape issues
Browse files Browse the repository at this point in the history
Fixes #629
  • Loading branch information
JakeStanger committed Oct 15, 2024
1 parent 56153f1 commit b2db7b0
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/dynamic_value/dynamic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(input: &str, mut f: F)
Expand Down
21 changes: 20 additions & 1 deletion src/gtk_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use glib::IsA;
use glib::{markup_escape_text, IsA};
use gtk::prelude::*;
use gtk::{Orientation, Widget};

Expand Down Expand Up @@ -75,3 +75,22 @@ impl<W: IsA<Widget>> 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("<span") {
self.set_label(&markup_escape_text(label));
} else {
self.set_label(label);
}
}
}
6 changes: 3 additions & 3 deletions src/modules/custom/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use gtk::prelude::*;
use gtk::{Button, Label, Orientation};
use serde::Deserialize;

use super::{CustomWidget, CustomWidgetContext, ExecEvent, WidgetConfig};
use crate::config::ModuleOrientation;
use crate::dynamic_value::dynamic_string;
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::PopupButton;
use crate::{build, try_send};

use super::{CustomWidget, CustomWidgetContext, ExecEvent, WidgetConfig};

#[derive(Debug, Deserialize, Clone)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ButtonWidget {
Expand Down Expand Up @@ -75,7 +75,7 @@ impl CustomWidget for ButtonWidget {
button.add(&label);

dynamic_string(&text, move |string| {
label.set_markup(&string);
label.set_label_escaped(&string);
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/modules/custom/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use gtk::prelude::*;
use gtk::Label;
use serde::Deserialize;

use super::{CustomWidget, CustomWidgetContext};
use crate::build;
use crate::config::ModuleOrientation;
use crate::dynamic_value::dynamic_string;

use super::{CustomWidget, CustomWidgetContext};
use crate::gtk_helpers::IronbarLabelExt;

#[derive(Debug, Deserialize, Clone)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -50,7 +50,7 @@ impl CustomWidget for LabelWidget {
{
let label = label.clone();
dynamic_string(&self.label, move |string| {
label.set_markup(&string);
label.set_label_escaped(&string);
});
}

Expand Down
7 changes: 3 additions & 4 deletions src/modules/label.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::config::CommonConfig;
use crate::dynamic_value::dynamic_string;
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{glib_recv, module_impl, try_send};
use color_eyre::Result;
use gtk::prelude::*;
use gtk::Label;
use serde::Deserialize;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -56,12 +56,11 @@ impl Module<Label> for LabelModule {
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
_info: &ModuleInfo,
) -> Result<ModuleParts<Label>> {
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 {
Expand Down
18 changes: 9 additions & 9 deletions src/modules/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand Down Expand Up @@ -189,10 +189,11 @@ impl Module<Button> for MusicModule {

let icon_play = new_icon_label(&self.icons.play, info.icon_theme, self.icon_size);
let icon_pause = new_icon_label(&self.icons.pause, info.icon_theme, self.icon_size);
let label = Label::new(None);

label.set_use_markup(true);
label.set_angle(info.bar_position.get_angle());
let label = Label::builder()
.use_markup(true)
.angle(info.bar_position.get_angle())
.build();

if let Some(truncate) = self.truncate {
truncate.truncate_label(&label);
Expand Down Expand Up @@ -222,7 +223,7 @@ impl Module<Button> for MusicModule {
};

if let Some(event) = event.take() {
label.set_label(&event.display_string);
label.set_label_escaped(&event.display_string);

button.show();

Expand Down Expand Up @@ -473,7 +474,7 @@ impl Module<Button> for MusicModule {
if let (Some(elapsed), Some(duration)) =
(progress_tick.elapsed, progress_tick.duration)
{
progress_label.set_label(&format!(
progress_label.set_label_escaped(&format!(
"{}/{}",
format_time(elapsed),
format_time(duration)
Expand All @@ -498,7 +499,7 @@ impl Module<Button> for MusicModule {
fn update_popup_metadata_label(text: Option<String>, label: &IconLabel) {
match text {
Some(value) => {
label.label.set_text(&value);
label.label.set_label_escaped(&value);
label.container.show_all();
}
None => {
Expand Down Expand Up @@ -531,7 +532,6 @@ fn get_token_value(song: &Track, token: &str) -> String {
"track" => song.track.map(|x| x.to_string()),
_ => Some(token.to_string()),
}
.map(|str| markup_escape_text(str.as_str()).to_string())
.unwrap_or_default()
}

Expand Down
3 changes: 2 additions & 1 deletion src/modules/script.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::CommonConfig;
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::script::{OutputStream, Script, ScriptMode};
use crate::{glib_recv, module_impl, spawn, try_send};
Expand Down Expand Up @@ -103,7 +104,7 @@ impl Module<Label> for ScriptModule {

{
let label = label.clone();
glib_recv!(context.subscribe(), s => label.set_markup(s.as_str()));
glib_recv!(context.subscribe(), s => label.set_label_escaped(&s));
}

Ok(ModuleParts {
Expand Down
6 changes: 4 additions & 2 deletions src/modules/sway/mode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::{CommonConfig, TruncateMode};
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{await_sync, glib_recv, module_impl, try_send};
use color_eyre::{Report, Result};
Expand Down Expand Up @@ -59,6 +60,7 @@ impl Module<Label> for SwayModeModule {
_info: &ModuleInfo,
) -> Result<ModuleParts<Label>> {
let label = Label::new(None);
label.set_use_markup(true);

{
let label = label.clone();
Expand All @@ -71,9 +73,9 @@ impl Module<Label> for SwayModeModule {
trace!("mode: {:?}", mode);
label.set_use_markup(mode.pango_markup);
if mode.change == "default" {
label.set_markup("");
label.set_label_escaped("");
} else {
label.set_markup(&mode.change);
label.set_label_escaped(&mode.change);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/modules/sysinfo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::{CommonConfig, ModuleOrientation};
use crate::gtk_helpers::IronbarGtkExt;
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{glib_recv, module_impl, send_async, spawn};
use color_eyre::Result;
Expand Down Expand Up @@ -266,7 +266,7 @@ impl Module<gtk::Box> for SysInfoModule {
.to_string()
});

label.set_markup(format_compiled.as_ref());
label.set_label_escaped(format_compiled.as_ref());
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/modules/upower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zbus;
use zbus::fdo::PropertiesProxy;

use crate::config::CommonConfig;
use crate::gtk_helpers::IronbarGtkExt;
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
use crate::image::ImageProvider;
use crate::modules::PopupButton;
use crate::modules::{
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Module<gtk::Button> for UpowerModule {
ImageProvider::parse(&icon_name, &icon_theme, false, self.icon_size)
.map(|provider| provider.load_into_image(icon.clone()));

label.set_markup(format.as_ref());
label.set_label_escaped(&format);
});

let rx = context.subscribe();
Expand All @@ -237,7 +237,7 @@ impl Module<gtk::Button> for UpowerModule {
.orientation(Orientation::Horizontal)
.build();

let label = Label::new(None);
let label = Label::builder().use_markup(true).build();
label.add_class("upower-details");
container.add(&label);

Expand All @@ -263,7 +263,7 @@ impl Module<gtk::Button> for UpowerModule {
_ => String::new(),
};

label.set_markup(&format);
label.set_label_escaped(&format);
});

container.show_all();
Expand Down

0 comments on commit b2db7b0

Please sign in to comment.