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

feat(music): ability to truncate button text

Adds new `truncate.mode` and `truncate.length` options, and `truncate` shorthand for mode.

Resolves #56.
This commit is contained in:
Jake Stanger 2023-01-28 21:55:27 +00:00
parent 012762e102
commit 8691824db1
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
3 changed files with 75 additions and 11 deletions

View file

@ -11,15 +11,17 @@ 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` | `{icon} {title} / {artist}` | Format string for the widget. More info below. | | `format` | `string` | `{icon} {title} / {artist}` | Format string for the widget. More info below. |
| `icons.play` | `string` | `` | Icon to show when playing. | | `truncate` or `truncate.mode` | `start` or `middle` or `end` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. Use the long-hand version if specifying a length. |
| `icons.pause` | `string` | `` | Icon to show when paused. | | `truncate.length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. |
| `icons.volume` | `string` | `墳` | Icon to show under popup volume slider. | | `icons.play` | `string` | `` | Icon to show when playing. |
| `host` | `string` | `localhost:6600` | [MPD Only] TCP or Unix socket for the MPD server. | | `icons.pause` | `string` | `` | Icon to show when paused. |
| `music_dir` | `string` | `$HOME/Music` | [MPD Only] Path to MPD server's music directory on disc. Required for album art. | | `icons.volume` | `string` | `墳` | Icon to show under popup volume slider. |
| `host` | `string` | `localhost:6600` | [MPD Only] TCP or Unix socket for the MPD server. |
| `music_dir` | `string` | `$HOME/Music` | [MPD Only] Path to MPD server's music directory on disc. Required for album art. |
<details> <details>
<summary>JSON</summary> <summary>JSON</summary>
@ -31,6 +33,7 @@ in MPRIS mode, the widget will listen to all players and automatically detect/di
"type": "music", "type": "music",
"player_type": "mpd", "player_type": "mpd",
"format": "{icon} {title} / {artist}", "format": "{icon} {title} / {artist}",
"truncate": "end",
"icons": { "icons": {
"play": "", "play": "",
"pause": "" "pause": ""
@ -52,6 +55,7 @@ type = "music"
player_type = "mpd" player_type = "mpd"
format = "{icon} {title} / {artist}" format = "{icon} {title} / {artist}"
music_dir = "/home/jake/Music" music_dir = "/home/jake/Music"
truncate = "end"
[[start.icons]] [[start.icons]]
play = "" play = ""
@ -68,6 +72,7 @@ start:
- type: "music" - type: "music"
player_type: "mpd" player_type: "mpd"
format: "{icon} {title} / {artist}" format: "{icon} {title} / {artist}"
truncate: "end"
icons: icons:
play: "" play: ""
pause: "" pause: ""
@ -86,6 +91,7 @@ start:
type = "music" type = "music"
player_type = "mpd" player_type = "mpd"
format = "{icon} {title} / {artist}" format = "{icon} {title} / {artist}"
truncate = "end"
icons.play = "" icons.play = ""
icons.pause = "" icons.pause = ""
music_dir = "/home/jake/Music" music_dir = "/home/jake/Music"

View file

@ -7,6 +7,7 @@ use color_eyre::Result;
use dirs::{audio_dir, home_dir}; use dirs::{audio_dir, home_dir};
use glib::Continue; use glib::Continue;
use gtk::gdk_pixbuf::Pixbuf; use gtk::gdk_pixbuf::Pixbuf;
use gtk::pango::EllipsizeMode as GtkEllipsizeMode;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Button, Image, Label, Orientation, Scale}; use gtk::{Button, Image, Label, Orientation, Scale};
use regex::Regex; use regex::Regex;
@ -53,7 +54,6 @@ impl Default for Icons {
#[derive(Debug, Deserialize, Clone, Copy)] #[derive(Debug, Deserialize, Clone, Copy)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum PlayerType { pub enum PlayerType {
// Auto,
Mpd, Mpd,
Mpris, Mpris,
} }
@ -64,6 +64,50 @@ impl Default for PlayerType {
} }
} }
#[derive(Debug, Deserialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum EllipsizeMode {
Start,
Middle,
End,
}
impl From<EllipsizeMode> for GtkEllipsizeMode {
fn from(value: EllipsizeMode) -> Self {
match value {
EllipsizeMode::Start => Self::Start,
EllipsizeMode::Middle => Self::Middle,
EllipsizeMode::End => Self::End,
}
}
}
#[derive(Debug, Deserialize, Clone, Copy)]
#[serde(untagged)]
enum TruncateMode {
Auto(EllipsizeMode),
MaxLength {
mode: EllipsizeMode,
length: Option<i32>,
},
}
impl TruncateMode {
fn mode(&self) -> EllipsizeMode {
match self {
TruncateMode::Auto(mode) => *mode,
TruncateMode::MaxLength { mode, .. } => *mode,
}
}
fn length(&self) -> Option<i32> {
match self {
TruncateMode::Auto(_) => None,
TruncateMode::MaxLength { length, .. } => *length,
}
}
}
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct MusicModule { pub struct MusicModule {
/// Type of player to connect to /// Type of player to connect to
@ -78,6 +122,8 @@ pub struct MusicModule {
#[serde(default)] #[serde(default)]
icons: Icons, icons: Icons,
truncate: Option<TruncateMode>,
// -- MPD -- // -- MPD --
/// TCP or Unix socket address. /// TCP or Unix socket address.
#[serde(default = "default_socket")] #[serde(default = "default_socket")]
@ -245,8 +291,20 @@ impl Module<Button> for MusicModule {
info: &ModuleInfo, info: &ModuleInfo,
) -> Result<ModuleWidget<Button>> { ) -> Result<ModuleWidget<Button>> {
let button = Button::new(); let button = Button::new();
let label = Label::new(None); let label = Label::new(None);
label.set_angle(info.bar_position.get_angle()); label.set_angle(info.bar_position.get_angle());
if let Some(truncate) = self.truncate {
println!("{truncate:?}");
label.set_ellipsize(truncate.mode().into());
if let Some(max_length) = truncate.length() {
label.set_max_width_chars(max_length);
}
}
button.add(&label); button.add(&label);
let orientation = info.bar_position.get_orientation(); let orientation = info.bar_position.get_orientation();

View file

@ -1,6 +1,7 @@
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::script::{OutputStream, Script, ScriptMode}; use crate::script::{OutputStream, Script, ScriptMode};
use crate::try_send;
use color_eyre::{Help, Report, Result}; use color_eyre::{Help, Report, Result};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::Label; use gtk::Label;
@ -8,7 +9,6 @@ use serde::Deserialize;
use tokio::spawn; use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tracing::error; use tracing::error;
use crate::try_send;
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct ScriptModule { pub struct ScriptModule {