1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +02:00

fix: correctly escape pango markup

This commit is contained in:
Jake Stanger 2024-06-02 15:23:30 +01:00
parent c28de8d902
commit 9d8a3eb370
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
10 changed files with 35 additions and 18 deletions

View file

@ -22,7 +22,7 @@ enum DynamicStringSegment {
///
/// ```rs
/// dynamic_string(&text, move |string| {
/// label.set_markup(&string);
/// label.set_markup_escaped(&string);
/// });
/// ```
pub fn dynamic_string<F>(input: &str, mut f: F)

View file

@ -1,6 +1,6 @@
use glib::IsA;
use glib::{markup_escape_text, IsA};
use gtk::prelude::*;
use gtk::{Orientation, Widget};
use gtk::{Label, Orientation, Widget};
/// Represents a widget's size
/// and location relative to the bar's start edge.
@ -75,3 +75,16 @@ impl<W: IsA<Widget>> IronbarGtkExt for W {
unsafe { self.set_data(key, value) }
}
}
pub trait IronbarLabelExt {
/// Sets a label's pango markup, escaping it in the process.
/// This should be used for any label values
/// which could contain special characters, for example `&`.
fn set_markup_escaped(&self, text: &str);
}
impl IronbarLabelExt for Label {
fn set_markup_escaped(&self, text: &str) {
self.set_markup(markup_escape_text(text).as_str())
}
}

View file

@ -9,7 +9,7 @@ use tokio::sync::{broadcast, mpsc};
use tokio::time::sleep;
use crate::config::{CommonConfig, ModuleOrientation};
use crate::gtk_helpers::IronbarGtkExt;
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
use crate::modules::{
Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, PopupButton, WidgetContext,
};
@ -143,7 +143,7 @@ impl Module<Button> for ClockModule {
let rx = context.subscribe();
glib_recv!(rx, date => {
let date_string = format!("{}", date.format_localized(&format, locale));
label.set_label(&date_string);
label.set_markup_escaped(&date_string);
});
let popup = self

View file

@ -4,6 +4,7 @@ use serde::Deserialize;
use crate::config::ModuleOrientation;
use crate::dynamic_value::dynamic_string;
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::PopupButton;
use crate::{build, try_send};
@ -75,7 +76,7 @@ impl CustomWidget for ButtonWidget {
button.add(&label);
dynamic_string(&text, move |string| {
label.set_markup(&string);
label.set_markup_escaped(&string);
});
}

View file

@ -5,6 +5,7 @@ use serde::Deserialize;
use crate::build;
use crate::config::ModuleOrientation;
use crate::dynamic_value::dynamic_string;
use crate::gtk_helpers::IronbarLabelExt;
use super::{CustomWidget, CustomWidgetContext};
@ -50,7 +51,7 @@ impl CustomWidget for LabelWidget {
{
let label = label.clone();
dynamic_string(&self.label, move |string| {
label.set_markup(&string);
label.set_markup_escaped(&string);
});
}

View file

@ -1,5 +1,6 @@
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;
@ -61,7 +62,7 @@ impl Module<Label> for LabelModule {
{
let label = label.clone();
glib_recv!(context.subscribe(), string => label.set_markup(&string));
glib_recv!(context.subscribe(), string => label.set_markup_escaped(&string));
}
Ok(ModuleParts {

View file

@ -5,7 +5,7 @@ use std::sync::Arc;
use std::time::Duration;
use color_eyre::Result;
use glib::{Propagation, PropertySet};
use glib::{markup_escape_text, 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::{
@ -222,7 +222,7 @@ impl Module<Button> for MusicModule {
};
if let Some(event) = event.take() {
label.set_label(&event.display_string);
label.set_markup_escaped(&event.display_string);
button.show();
@ -549,7 +549,7 @@ impl IconLabel {
let mut builder = Label::builder().use_markup(true);
if let Some(label) = label {
builder = builder.label(label);
builder = builder.label(markup_escape_text(label));
}
let label = builder.build();

View file

@ -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};
@ -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_markup_escaped(s.as_str()));
}
Ok(ModuleParts {

View file

@ -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;
@ -266,7 +266,7 @@ impl Module<gtk::Box> for SysInfoModule {
.to_string()
});
label.set_markup(format_compiled.as_ref());
label.set_markup_escaped(format_compiled.as_ref());
}
});
}

View file

@ -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::{
@ -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_markup_escaped(format.as_ref());
});
let rx = context.subscribe();
@ -263,7 +263,7 @@ impl Module<gtk::Button> for UpowerModule {
_ => String::new(),
};
label.set_markup(&format);
label.set_markup_escaped(&format);
});
container.show_all();