mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-01 18:51:04 +02:00
feat(focused): ability to truncate label text
This commit is contained in:
parent
97502559b3
commit
07dbf78010
6 changed files with 77 additions and 61 deletions
|
@ -1,5 +1,5 @@
|
|||
use crate::clients::wayland::{self, ToplevelChange};
|
||||
use crate::config::CommonConfig;
|
||||
use crate::config::{CommonConfig, TruncateMode};
|
||||
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
||||
use crate::{await_sync, icon, read_lock, send_async};
|
||||
use color_eyre::Result;
|
||||
|
@ -25,6 +25,8 @@ pub struct FocusedModule {
|
|||
/// GTK icon theme to use.
|
||||
icon_theme: Option<String>,
|
||||
|
||||
truncate: Option<TruncateMode>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub common: Option<CommonConfig>,
|
||||
}
|
||||
|
@ -102,6 +104,10 @@ impl Module<gtk::Box> for FocusedModule {
|
|||
let icon = Image::builder().name("icon").build();
|
||||
let label = Label::builder().name("label").build();
|
||||
|
||||
if let Some(truncate) = self.truncate {
|
||||
truncate.truncate_label(&label);
|
||||
}
|
||||
|
||||
container.add(&icon);
|
||||
container.add(&label);
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::config::CommonConfig;
|
||||
use crate::config::{CommonConfig, TruncateMode};
|
||||
use dirs::{audio_dir, home_dir};
|
||||
use gtk::pango::EllipsizeMode as GtkEllipsizeMode;
|
||||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
@ -40,49 +39,6 @@ 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)]
|
||||
pub enum TruncateMode {
|
||||
Auto(EllipsizeMode),
|
||||
MaxLength {
|
||||
mode: EllipsizeMode,
|
||||
length: Option<i32>,
|
||||
},
|
||||
}
|
||||
|
||||
impl TruncateMode {
|
||||
pub(crate) const fn mode(&self) -> EllipsizeMode {
|
||||
match self {
|
||||
Self::MaxLength { mode, .. } | Self::Auto(mode) => *mode,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const fn length(&self) -> Option<i32> {
|
||||
match self {
|
||||
Self::Auto(_) => None,
|
||||
Self::MaxLength { length, .. } => *length,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct MusicModule {
|
||||
/// Type of player to connect to
|
||||
|
@ -97,8 +53,6 @@ pub struct MusicModule {
|
|||
#[serde(default)]
|
||||
pub(crate) icons: Icons,
|
||||
|
||||
pub(crate) truncate: Option<TruncateMode>,
|
||||
|
||||
// -- MPD --
|
||||
/// TCP or Unix socket address.
|
||||
#[serde(default = "default_socket")]
|
||||
|
@ -107,6 +61,9 @@ pub struct MusicModule {
|
|||
#[serde(default = "default_music_dir")]
|
||||
pub(crate) music_dir: PathBuf,
|
||||
|
||||
// -- Common --
|
||||
pub(crate) truncate: Option<TruncateMode>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub common: Option<CommonConfig>,
|
||||
}
|
||||
|
|
|
@ -165,13 +165,7 @@ impl Module<Button> for MusicModule {
|
|||
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);
|
||||
}
|
||||
truncate.truncate_label(&label);
|
||||
}
|
||||
|
||||
button.add(&label);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue