1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 10:41:03 +02:00

feat(truncate): ability to set fixed length

BREAKING CHANGE: This changes the behaviour of `truncate.length`. A new property, `truncate.max_length`, has been introduced that uses the old behaviour.
This commit is contained in:
Jake Stanger 2023-02-25 14:22:49 +00:00
parent 1ad1961396
commit ca4fe422f2
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
3 changed files with 44 additions and 30 deletions

View file

@ -8,13 +8,14 @@ Displays the title and/or icon of the currently focused window.
> Type: `focused` > Type: `focused`
| Name | Type | Default | Description | | Name | Type | Default | Description |
|-------------------|---------------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------| |-----------------------|---------------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| `show_icon` | `boolean` | `true` | Whether to show the app's icon | | `show_icon` | `boolean` | `true` | Whether to show the app's icon |
| `show_title` | `boolean` | `true` | Whether to show the app's title | | `show_title` | `boolean` | `true` | Whether to show the app's title |
| `icon_size` | `integer` | `32` | Size of icon in pixels | | `icon_size` | `integer` | `32` | Size of icon in pixels |
| `truncate` | `start` or `middle` or `end` or `Map` | `null` | 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` | `start` or `middle` or `end` or `Map` | `null` | 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` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. | | `truncate.mode` | `start` or `middle` or `end` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. |
| `truncate.length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. | | `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>

View file

@ -12,12 +12,13 @@ in MPRIS mode, the widget will listen to all players and automatically detect/di
> Type: `music` > Type: `music`
| | Type | Default | Description | | | Type | Default | Description |
|-------------------|---------------------------------------|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------| |-----------------------|---------------------------------------|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| `player_type` | `mpris` or `mpd` | `mpris` | Whether to connect to MPRIS players or an MPD server. | | `player_type` | `mpris` or `mpd` | `mpris` | Whether to connect to MPRIS players or an MPD server. |
| `format` | `string` | `{title} / {artist}` | Format string for the widget. More info below. | | `format` | `string` | `{title} / {artist}` | Format string for the widget. More info below. |
| `truncate` | `start` or `middle` or `end` or `Map` | `null` | 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` | `start` or `middle` or `end` or `Map` | `null` | 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` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. | | `truncate.mode` | `start` or `middle` or `end` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. |
| `truncate.length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. | | `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. |
| `icons.play` | `string/image` | `` | Icon to show when playing. | | `icons.play` | `string/image` | `` | Icon to show when playing. |
| `icons.pause` | `string/image` | `` | Icon to show when paused. | | `icons.pause` | `string/image` | `` | Icon to show when paused. |
| `icons.prev` | `string/image` | `玲` | Icon to show on previous button. | | `icons.prev` | `string/image` | `玲` | Icon to show on previous button. |

View file

@ -24,31 +24,43 @@ impl From<EllipsizeMode> for GtkEllipsizeMode {
#[serde(untagged)] #[serde(untagged)]
pub enum TruncateMode { pub enum TruncateMode {
Auto(EllipsizeMode), Auto(EllipsizeMode),
MaxLength { Length {
mode: EllipsizeMode, mode: EllipsizeMode,
length: Option<i32>, length: Option<i32>,
max_length: Option<i32>,
}, },
} }
impl TruncateMode { impl TruncateMode {
const fn mode(&self) -> EllipsizeMode { const fn mode(&self) -> EllipsizeMode {
match self { match self {
Self::MaxLength { mode, .. } | Self::Auto(mode) => *mode, Self::Length { mode, .. } | Self::Auto(mode) => *mode,
} }
} }
const fn length(&self) -> Option<i32> { const fn length(&self) -> Option<i32> {
match self { match self {
Self::Auto(_) => None, Self::Auto(_) => None,
Self::MaxLength { length, .. } => *length, Self::Length { length, .. } => *length,
}
}
const fn max_length(&self) -> Option<i32> {
match self {
Self::Auto(_) => None,
Self::Length { max_length, .. } => *max_length,
} }
} }
pub fn truncate_label(&self, label: &gtk::Label) { pub fn truncate_label(&self, label: &gtk::Label) {
label.set_ellipsize(self.mode().into()); label.set_ellipsize(self.mode().into());
if let Some(max_length) = self.length() { if let Some(length) = self.length() {
label.set_max_width_chars(max_length); label.set_width_chars(length);
}
if let Some(length) = self.max_length() {
label.set_max_width_chars(length);
} }
} }
} }