diff --git a/docs/modules/Custom.md b/docs/modules/Custom.md index f7d2510..d93d7c7 100644 --- a/docs/modules/Custom.md +++ b/docs/modules/Custom.md @@ -54,11 +54,16 @@ A text label. Pango markup is supported. > Type `label` -| Name | Type | Default | Description | -|---------------|------------------------------------------------------------|----------------|----------------------------------------------------------------------| -| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Widget text label. Pango markup and embedded scripts are supported. | -| `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Orientation of the label text. | -| `justify` | `'left'`, `'right'`, `'center'`, or `'fill'` | `'left'` | Justification (alignment) of the label text. | +| Name | Type | Default | Description | +|-----------------------|------------------------------------------------------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------| +| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Widget text label. Pango markup and embedded scripts are supported. | +| `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Orientation of the label text. | +| `justify` | `'left'`, `'right'`, `'center'`, or `'fill'` | `'left'` | Justification (alignment) of the label text. | +| `truncate` | `'start'` or `'middle'` or `'end'` or `off` or `Map` | `off` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. Use the long-hand `Map` version if specifying a length. | +| `truncate.mode` | `'start'` or `'middle'` or `'end'` or `off` | `off` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. | +| `truncate.length` | `integer` | `null` | The fixed width (in chars) of the widget. Leave blank to let GTK automatically handle. | +| `truncate.max_length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. | + #### Button diff --git a/docs/modules/Label.md b/docs/modules/Label.md index c203554..206614a 100644 --- a/docs/modules/Label.md +++ b/docs/modules/Label.md @@ -7,9 +7,14 @@ For more advanced use-cases, use [custom](custom). > Type: `label` -| Name | Type | Default | Description | -|---------|-------------------------------------------------|---------|------------------------| -| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Text to show on label. | +| Name | Type | Default | Description | +|-----------------------|------------------------------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------| +| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Text to show on label. | +| `truncate` | `'start'` or `'middle'` or `'end'` or `off` or `Map` | `off` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. Use the long-hand `Map` version if specifying a length. | +| `truncate.mode` | `'start'` or `'middle'` or `'end'` or `off` | `off` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. | +| `truncate.length` | `integer` | `null` | The fixed width (in chars) of the widget. Leave blank to let GTK automatically handle. | +| `truncate.max_length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. | +
JSON diff --git a/src/modules/custom/label.rs b/src/modules/custom/label.rs index 8903789..26a1ad5 100644 --- a/src/modules/custom/label.rs +++ b/src/modules/custom/label.rs @@ -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, } 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| { diff --git a/src/modules/label.rs b/src/modules/label.rs index ccb783f..6aa440e 100644 --- a/src/modules/label.rs +++ b/src/modules/label.rs @@ -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, + /// See [common options](module-level-options#common-options). #[serde(flatten)] pub common: Option, @@ -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