2023-01-28 23:01:44 +00:00
|
|
|
use gtk::pango::EllipsizeMode as GtkEllipsizeMode;
|
|
|
|
use gtk::prelude::*;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone, Copy)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-01-28 23:01:44 +00:00
|
|
|
pub enum EllipsizeMode {
|
2024-06-28 23:24:30 +01:00
|
|
|
None,
|
2023-01-28 23:01:44 +00:00
|
|
|
Start,
|
|
|
|
Middle,
|
|
|
|
End,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EllipsizeMode> for GtkEllipsizeMode {
|
|
|
|
fn from(value: EllipsizeMode) -> Self {
|
|
|
|
match value {
|
2024-06-28 23:24:30 +01:00
|
|
|
EllipsizeMode::None => Self::None,
|
2023-01-28 23:01:44 +00:00
|
|
|
EllipsizeMode::Start => Self::Start,
|
|
|
|
EllipsizeMode::Middle => Self::Middle,
|
|
|
|
EllipsizeMode::End => Self::End,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 15:16:01 +01:00
|
|
|
/// Some modules provide options for truncating text.
|
|
|
|
/// This is controlled using a common `TruncateMode` type,
|
|
|
|
/// which is defined below.
|
|
|
|
///
|
|
|
|
/// The option can be configured in one of two modes.
|
|
|
|
///
|
2023-01-28 23:01:44 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone, Copy)]
|
|
|
|
#[serde(untagged)]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-01-28 23:01:44 +00:00
|
|
|
pub enum TruncateMode {
|
2024-06-28 23:24:30 +01:00
|
|
|
/// Do not truncate content.
|
|
|
|
///
|
|
|
|
/// Setting this option may cause excessively long content to overflow other widgets,
|
|
|
|
/// shifting them off-screen.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```corn
|
|
|
|
/// { truncate = "off" }
|
|
|
|
Off,
|
|
|
|
|
2024-05-19 15:16:01 +01:00
|
|
|
/// Auto mode lets GTK decide when to ellipsize.
|
|
|
|
///
|
|
|
|
/// To use this mode, set the truncate option to a string
|
|
|
|
/// declaring the location to truncate text from and place the ellipsis.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```corn
|
|
|
|
/// { truncate = "start" }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// **Valid options**: `start`, `middle`, `end`
|
|
|
|
/// <br>
|
|
|
|
/// **Default**: `null`
|
2023-01-28 23:01:44 +00:00
|
|
|
Auto(EllipsizeMode),
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Length mode defines a fixed point at which to ellipsize.
|
|
|
|
///
|
|
|
|
/// Generally you will want to set only one of `length` or `max_length`,
|
|
|
|
/// but you can set both if required.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```corn
|
|
|
|
/// {
|
|
|
|
/// truncate.mode = "start"
|
|
|
|
/// truncate.length = 50
|
|
|
|
/// truncate.max_length = 70
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-02-25 14:22:49 +00:00
|
|
|
Length {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// The location to truncate text from and place the ellipsis.
|
|
|
|
/// **Valid options**: `start`, `middle`, `end`
|
|
|
|
/// <br>
|
|
|
|
/// **Default**: `null`
|
2023-01-28 23:01:44 +00:00
|
|
|
mode: EllipsizeMode,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// The fixed width (in characters) of the widget.
|
|
|
|
///
|
|
|
|
/// The widget will be expanded to this width
|
|
|
|
/// if it would have otherwise been smaller.
|
|
|
|
///
|
|
|
|
/// Leave unset to let GTK automatically handle.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-01-28 23:01:44 +00:00
|
|
|
length: Option<i32>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// The maximum number of characters to show
|
|
|
|
/// before truncating.
|
|
|
|
///
|
|
|
|
/// Leave unset to let GTK automatically handle.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-02-25 14:22:49 +00:00
|
|
|
max_length: Option<i32>,
|
2023-01-28 23:01:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TruncateMode {
|
|
|
|
const fn mode(&self) -> EllipsizeMode {
|
|
|
|
match self {
|
2023-02-25 14:22:49 +00:00
|
|
|
Self::Length { mode, .. } | Self::Auto(mode) => *mode,
|
2023-01-28 23:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn length(&self) -> Option<i32> {
|
|
|
|
match self {
|
2024-06-28 23:24:30 +01:00
|
|
|
Self::Auto(_) | Self::Off => None,
|
2023-02-25 14:22:49 +00:00
|
|
|
Self::Length { length, .. } => *length,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn max_length(&self) -> Option<i32> {
|
|
|
|
match self {
|
2024-06-28 23:24:30 +01:00
|
|
|
Self::Auto(_) | Self::Off => None,
|
2023-02-25 14:22:49 +00:00
|
|
|
Self::Length { max_length, .. } => *max_length,
|
2023-01-28 23:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn truncate_label(&self, label: >k::Label) {
|
|
|
|
label.set_ellipsize(self.mode().into());
|
|
|
|
|
2023-02-25 14:22:49 +00:00
|
|
|
if let Some(length) = self.length() {
|
|
|
|
label.set_width_chars(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(length) = self.max_length() {
|
|
|
|
label.set_max_width_chars(length);
|
2023-01-28 23:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|