diff --git a/docs/modules/Focused.md b/docs/modules/Focused.md index be65632..d581c77 100644 --- a/docs/modules/Focused.md +++ b/docs/modules/Focused.md @@ -7,12 +7,14 @@ Displays the title and/or icon of the currently focused window. > Type: `focused` -| Name | Type | Default | Description | -|--------------|-----------|---------|---------------------------------| -| `show_icon` | `boolean` | `true` | Whether to show the app's icon | -| `show_title` | `boolean` | `true` | Whether to show the app's title | -| `icon_size` | `integer` | `32` | Size of icon in pixels | -| `icon_theme` | `string` | `null` | GTK icon theme to use | +| Name | Type | Default | Description | +|-------------------------------|------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------| +| `show_icon` | `boolean` | `true` | Whether to show the app's icon | +| `show_title` | `boolean` | `true` | Whether to show the app's title | +| `icon_size` | `integer` | `32` | Size of icon in pixels | +| `icon_theme` | `string` | `null` | GTK icon theme to use | +| `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. | +| `truncate.length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. |
JSON diff --git a/src/config/mod.rs b/src/config/mod.rs index c054a3d..51f1d21 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,4 +1,5 @@ mod r#impl; +mod truncate; use crate::modules::clock::ClockModule; use crate::modules::custom::CustomModule; @@ -13,6 +14,8 @@ use crate::script::ScriptInput; use serde::Deserialize; use std::collections::HashMap; +pub use self::truncate::{EllipsizeMode, TruncateMode}; + #[derive(Debug, Deserialize, Clone)] pub struct CommonConfig { pub show_if: Option, diff --git a/src/config/truncate.rs b/src/config/truncate.rs new file mode 100644 index 0000000..ebf8a8c --- /dev/null +++ b/src/config/truncate.rs @@ -0,0 +1,54 @@ +use gtk::pango::EllipsizeMode as GtkEllipsizeMode; +use gtk::prelude::*; +use serde::Deserialize; + +#[derive(Debug, Deserialize, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum EllipsizeMode { + Start, + Middle, + End, +} + +impl From 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, + }, +} + +impl TruncateMode { + const fn mode(&self) -> EllipsizeMode { + match self { + Self::MaxLength { mode, .. } | Self::Auto(mode) => *mode, + } + } + + const fn length(&self) -> Option { + match self { + Self::Auto(_) => None, + Self::MaxLength { length, .. } => *length, + } + } + + pub fn truncate_label(&self, label: >k::Label) { + label.set_ellipsize(self.mode().into()); + + if let Some(max_length) = self.length() { + label.set_max_width_chars(max_length); + } + } +} diff --git a/src/modules/focused.rs b/src/modules/focused.rs index 84c9eca..fbbe679 100644 --- a/src/modules/focused.rs +++ b/src/modules/focused.rs @@ -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, + truncate: Option, + #[serde(flatten)] pub common: Option, } @@ -102,6 +104,10 @@ impl Module 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); diff --git a/src/modules/music/config.rs b/src/modules/music/config.rs index 85d3f2d..e52b824 100644 --- a/src/modules/music/config.rs +++ b/src/modules/music/config.rs @@ -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 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, - }, -} - -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 { - 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, - // -- 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, + #[serde(flatten)] pub common: Option, } diff --git a/src/modules/music/mod.rs b/src/modules/music/mod.rs index cb443b5..c84c35f 100644 --- a/src/modules/music/mod.rs +++ b/src/modules/music/mod.rs @@ -165,13 +165,7 @@ impl Module