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

119 lines
3.4 KiB
Rust
Raw Normal View History

use crate::config::TruncateMode;
2024-10-15 22:10:37 +01:00
use glib::{markup_escape_text, IsA};
use gtk::pango::EllipsizeMode;
use gtk::prelude::*;
use gtk::{Label, Orientation, Widget};
/// Represents a widget's size
/// and location relative to the bar's start edge.
#[derive(Debug, Copy, Clone)]
pub struct WidgetGeometry {
/// Position of the start edge of the widget
/// from the start edge of the bar.
pub position: i32,
/// The length of the widget.
pub size: i32,
/// The length of the bar.
pub bar_size: i32,
}
pub trait IronbarGtkExt {
/// Adds a new CSS class to the widget.
fn add_class(&self, class: &str);
/// Removes a CSS class from the widget
fn remove_class(&self, class: &str);
/// Gets the geometry for the widget
fn geometry(&self, orientation: Orientation) -> WidgetGeometry;
/// Gets a data tag on a widget, if it exists.
fn get_tag<V: 'static>(&self, key: &str) -> Option<&V>;
/// Sets a data tag on a widget.
fn set_tag<V: 'static>(&self, key: &str, value: V);
}
impl<W: IsA<Widget>> IronbarGtkExt for W {
fn add_class(&self, class: &str) {
self.style_context().add_class(class);
}
fn remove_class(&self, class: &str) {
self.style_context().remove_class(class);
}
fn geometry(&self, orientation: Orientation) -> WidgetGeometry {
let allocation = self.allocation();
let widget_size = if orientation == Orientation::Horizontal {
allocation.width()
} else {
allocation.height()
};
let top_level = self.toplevel().expect("Failed to get top-level widget");
let top_level_allocation = top_level.allocation();
let bar_size = if orientation == Orientation::Horizontal {
top_level_allocation.width()
} else {
top_level_allocation.height()
};
let (widget_x, widget_y) = self
.translate_coordinates(&top_level, 0, 0)
.unwrap_or((0, 0));
let widget_pos = if orientation == Orientation::Horizontal {
widget_x
} else {
widget_y
};
WidgetGeometry {
position: widget_pos,
size: widget_size,
bar_size,
}
}
fn get_tag<V: 'static>(&self, key: &str) -> Option<&V> {
unsafe { self.data(key).map(|val| val.as_ref()) }
}
fn set_tag<V: 'static>(&self, key: &str, value: V) {
unsafe { self.set_data(key, value) }
}
}
2024-10-15 22:10:37 +01:00
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);
fn truncate(&self, mode: TruncateMode);
2024-10-15 22:10:37 +01:00
}
impl IronbarLabelExt for Label {
2024-10-15 22:10:37 +01:00
fn set_label_escaped(&self, label: &str) {
if label.contains("<span") {
2024-10-15 22:10:37 +01:00
self.set_label(label);
} else {
self.set_label(&markup_escape_text(label));
2024-10-15 22:10:37 +01:00
}
}
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);
}
}
2024-10-15 22:10:37 +01:00
}