1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-16 14:21:03 +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

@ -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

View file

@ -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. |
<details>
<summary>JSON</summary>

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));