2023-04-07 20:22:31 +01:00
|
|
|
use gtk::Label;
|
2025-02-21 16:35:54 +00:00
|
|
|
use gtk::prelude::*;
|
2023-04-07 20:22:31 +01:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
2024-10-15 22:10:37 +01:00
|
|
|
use super::{CustomWidget, CustomWidgetContext};
|
2023-07-16 18:57:00 +01:00
|
|
|
use crate::build;
|
2025-02-21 21:11:41 +00:00
|
|
|
use crate::config::{ModuleJustification, ModuleOrientation, TruncateMode};
|
2023-07-16 18:57:00 +01:00
|
|
|
use crate::dynamic_value::dynamic_string;
|
2024-10-15 22:10:37 +01:00
|
|
|
use crate::gtk_helpers::IronbarLabelExt;
|
2023-07-16 18:57:00 +01:00
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-04-07 20:22:31 +01:00
|
|
|
pub struct LabelWidget {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// Widget name.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-07 20:22:31 +01:00
|
|
|
name: Option<String>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Widget class name.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-07 20:22:31 +01:00
|
|
|
class: Option<String>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Widget text label. Pango markup and embedded scripts are supported.
|
|
|
|
///
|
|
|
|
/// This is a [Dynamic String](dynamic-values#dynamic-string).
|
|
|
|
///
|
|
|
|
/// **Required**
|
2023-04-10 13:51:07 +01:00
|
|
|
label: String,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Orientation of the label.
|
|
|
|
/// Setting to vertical will rotate text 90 degrees.
|
|
|
|
///
|
|
|
|
/// **Valid options**: `horizontal`, `vertical`, `h`, `v`
|
|
|
|
/// <br />
|
|
|
|
/// **Default**: `horizontal`
|
2024-04-04 16:12:45 -04:00
|
|
|
#[serde(default)]
|
|
|
|
orientation: ModuleOrientation,
|
2025-01-01 12:07:42 -08:00
|
|
|
|
|
|
|
/// The justification (alignment) of the label text.
|
|
|
|
///
|
|
|
|
/// **Valid options**: `left`, `right`, `center`, `fill`
|
|
|
|
/// <br>
|
|
|
|
/// **Default**: `left`
|
|
|
|
#[serde(default)]
|
2025-01-01 12:27:26 -08:00
|
|
|
justify: ModuleJustification,
|
2025-02-21 21:11:41 +00:00
|
|
|
|
|
|
|
/// See [truncate options](module-level-options#truncate-mode).
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
|
|
|
truncate: Option<TruncateMode>,
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CustomWidget for LabelWidget {
|
|
|
|
type Widget = Label;
|
|
|
|
|
|
|
|
fn into_widget(self, _context: CustomWidgetContext) -> Self::Widget {
|
2023-04-10 13:51:07 +01:00
|
|
|
let label = build!(self, Self::Widget);
|
2023-04-07 20:22:31 +01:00
|
|
|
|
2024-04-05 13:28:47 -04:00
|
|
|
label.set_angle(self.orientation.to_angle());
|
2025-01-01 12:07:42 -08:00
|
|
|
label.set_justify(self.justify.into());
|
2023-04-10 13:51:07 +01:00
|
|
|
label.set_use_markup(true);
|
2023-04-07 20:22:31 +01:00
|
|
|
|
2025-02-21 21:11:41 +00:00
|
|
|
if let Some(truncate) = self.truncate {
|
|
|
|
label.truncate(truncate);
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
{
|
|
|
|
let label = label.clone();
|
2023-06-22 23:07:40 +01:00
|
|
|
dynamic_string(&self.label, move |string| {
|
2024-10-15 22:10:37 +01:00
|
|
|
label.set_label_escaped(&string);
|
2023-04-07 20:22:31 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
label
|
|
|
|
}
|
|
|
|
}
|