mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-08-16 22:31:03 +02:00
feat(label): add truncate to module and custom widget
This commit is contained in:
parent
183ca402d4
commit
ba5ec8015d
4 changed files with 40 additions and 10 deletions
|
@ -54,11 +54,16 @@ A text label. Pango markup is supported.
|
||||||
|
|
||||||
> Type `label`
|
> Type `label`
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|---------------|------------------------------------------------------------|----------------|----------------------------------------------------------------------|
|
|-----------------------|------------------------------------------------------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Widget text label. Pango markup and embedded scripts are supported. |
|
| `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. |
|
| `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. |
|
| `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
|
#### Button
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,14 @@ For more advanced use-cases, use [custom](custom).
|
||||||
|
|
||||||
> Type: `label`
|
> Type: `label`
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|---------|-------------------------------------------------|---------|------------------------|
|
|-----------------------|------------------------------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Text to show on label. |
|
| `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>
|
<details>
|
||||||
<summary>JSON</summary>
|
<summary>JSON</summary>
|
||||||
|
|
|
@ -4,7 +4,7 @@ use serde::Deserialize;
|
||||||
|
|
||||||
use super::{CustomWidget, CustomWidgetContext};
|
use super::{CustomWidget, CustomWidgetContext};
|
||||||
use crate::build;
|
use crate::build;
|
||||||
use crate::config::{ModuleJustification, ModuleOrientation};
|
use crate::config::{ModuleJustification, ModuleOrientation, TruncateMode};
|
||||||
use crate::dynamic_value::dynamic_string;
|
use crate::dynamic_value::dynamic_string;
|
||||||
use crate::gtk_helpers::IronbarLabelExt;
|
use crate::gtk_helpers::IronbarLabelExt;
|
||||||
|
|
||||||
|
@ -44,6 +44,11 @@ pub struct LabelWidget {
|
||||||
/// **Default**: `left`
|
/// **Default**: `left`
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
justify: ModuleJustification,
|
justify: ModuleJustification,
|
||||||
|
|
||||||
|
/// See [truncate options](module-level-options#truncate-mode).
|
||||||
|
///
|
||||||
|
/// **Default**: `null`
|
||||||
|
truncate: Option<TruncateMode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CustomWidget for LabelWidget {
|
impl CustomWidget for LabelWidget {
|
||||||
|
@ -56,6 +61,10 @@ impl CustomWidget for LabelWidget {
|
||||||
label.set_justify(self.justify.into());
|
label.set_justify(self.justify.into());
|
||||||
label.set_use_markup(true);
|
label.set_use_markup(true);
|
||||||
|
|
||||||
|
if let Some(truncate) = self.truncate {
|
||||||
|
label.truncate(truncate);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let label = label.clone();
|
let label = label.clone();
|
||||||
dynamic_string(&self.label, move |string| {
|
dynamic_string(&self.label, move |string| {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::config::CommonConfig;
|
use crate::config::{CommonConfig, TruncateMode};
|
||||||
use crate::dynamic_value::dynamic_string;
|
use crate::dynamic_value::dynamic_string;
|
||||||
use crate::gtk_helpers::IronbarLabelExt;
|
use crate::gtk_helpers::IronbarLabelExt;
|
||||||
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
|
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
|
||||||
|
@ -17,6 +17,12 @@ pub struct LabelModule {
|
||||||
/// **Required**
|
/// **Required**
|
||||||
label: String,
|
label: String,
|
||||||
|
|
||||||
|
// -- Common --
|
||||||
|
/// See [truncate options](module-level-options#truncate-mode).
|
||||||
|
///
|
||||||
|
/// **Default**: `null`
|
||||||
|
truncate: Option<TruncateMode>,
|
||||||
|
|
||||||
/// See [common options](module-level-options#common-options).
|
/// See [common options](module-level-options#common-options).
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub common: Option<CommonConfig>,
|
pub common: Option<CommonConfig>,
|
||||||
|
@ -26,6 +32,7 @@ impl LabelModule {
|
||||||
pub(crate) fn new(label: String) -> Self {
|
pub(crate) fn new(label: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
label,
|
label,
|
||||||
|
truncate: None,
|
||||||
common: Some(CommonConfig::default()),
|
common: Some(CommonConfig::default()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,6 +65,10 @@ impl Module<Label> for LabelModule {
|
||||||
) -> Result<ModuleParts<Label>> {
|
) -> Result<ModuleParts<Label>> {
|
||||||
let label = Label::builder().use_markup(true).build();
|
let label = Label::builder().use_markup(true).build();
|
||||||
|
|
||||||
|
if let Some(truncate) = self.truncate {
|
||||||
|
label.truncate(truncate);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let label = label.clone();
|
let label = label.clone();
|
||||||
glib_recv!(context.subscribe(), string => label.set_label_escaped(&string));
|
glib_recv!(context.subscribe(), string => label.set_label_escaped(&string));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue