1
0
Fork 0
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:
Jake Stanger 2023-01-28 23:01:44 +00:00
parent 97502559b3
commit 07dbf78010
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
6 changed files with 77 additions and 61 deletions

View file

@ -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<ScriptInput>,

54
src/config/truncate.rs Normal file
View file

@ -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<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 {
const fn mode(&self) -> EllipsizeMode {
match self {
Self::MaxLength { mode, .. } | Self::Auto(mode) => *mode,
}
}
const fn length(&self) -> Option<i32> {
match self {
Self::Auto(_) => None,
Self::MaxLength { length, .. } => *length,
}
}
pub fn truncate_label(&self, label: &gtk::Label) {
label.set_ellipsize(self.mode().into());
if let Some(max_length) = self.length() {
label.set_max_width_chars(max_length);
}
}
}