1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-17 14:51:04 +02:00

refactor: move label truncate function to ext trait

 Conflicts:
	src/gtk_helpers.rs
	src/modules/music/mod.rs
This commit is contained in:
Jake Stanger 2024-06-28 23:27:52 +01:00
parent cc6f21ed68
commit 8b05ed526d
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
8 changed files with 44 additions and 35 deletions

View file

@ -1,6 +1,8 @@
use crate::config::TruncateMode;
use glib::{markup_escape_text, IsA};
use gtk::pango::EllipsizeMode;
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.
@ -83,9 +85,11 @@ pub trait IronbarLabelExt {
/// 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);
fn truncate(&self, mode: TruncateMode);
}
impl IronbarLabelExt for gtk::Label {
impl IronbarLabelExt for Label {
fn set_label_escaped(&self, label: &str) {
if !label.contains("<span") {
self.set_label(&markup_escape_text(label));
@ -93,4 +97,16 @@ impl IronbarLabelExt for gtk::Label {
self.set_label(label);
}
}
fn truncate(&self, mode: TruncateMode) {
self.set_ellipsize(<TruncateMode as Into<EllipsizeMode>>::into(mode));
if let Some(length) = mode.length() {
self.set_width_chars(length);
}
if let Some(length) = mode.max_length() {
self.set_max_width_chars(length);
}
}
}