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

feat(label): add truncate to module and custom widget

This commit is contained in:
Jake Stanger 2025-02-21 21:11:41 +00:00
parent 183ca402d4
commit ba5ec8015d
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
4 changed files with 40 additions and 10 deletions

View file

@ -4,7 +4,7 @@ use serde::Deserialize;
use super::{CustomWidget, CustomWidgetContext};
use crate::build;
use crate::config::{ModuleJustification, ModuleOrientation};
use crate::config::{ModuleJustification, ModuleOrientation, TruncateMode};
use crate::dynamic_value::dynamic_string;
use crate::gtk_helpers::IronbarLabelExt;
@ -44,6 +44,11 @@ pub struct LabelWidget {
/// **Default**: `left`
#[serde(default)]
justify: ModuleJustification,
/// See [truncate options](module-level-options#truncate-mode).
///
/// **Default**: `null`
truncate: Option<TruncateMode>,
}
impl CustomWidget for LabelWidget {
@ -56,6 +61,10 @@ impl CustomWidget for LabelWidget {
label.set_justify(self.justify.into());
label.set_use_markup(true);
if let Some(truncate) = self.truncate {
label.truncate(truncate);
}
{
let label = label.clone();
dynamic_string(&self.label, move |string| {

View file

@ -1,4 +1,4 @@
use crate::config::CommonConfig;
use crate::config::{CommonConfig, TruncateMode};
use crate::dynamic_value::dynamic_string;
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
@ -17,6 +17,12 @@ pub struct LabelModule {
/// **Required**
label: String,
// -- Common --
/// See [truncate options](module-level-options#truncate-mode).
///
/// **Default**: `null`
truncate: Option<TruncateMode>,
/// See [common options](module-level-options#common-options).
#[serde(flatten)]
pub common: Option<CommonConfig>,
@ -26,6 +32,7 @@ impl LabelModule {
pub(crate) fn new(label: String) -> Self {
Self {
label,
truncate: None,
common: Some(CommonConfig::default()),
}
}
@ -58,6 +65,10 @@ impl Module<Label> for LabelModule {
) -> Result<ModuleParts<Label>> {
let label = Label::builder().use_markup(true).build();
if let Some(truncate) = self.truncate {
label.truncate(truncate);
}
{
let label = label.clone();
glib_recv!(context.subscribe(), string => label.set_label_escaped(&string));