feat: add Ellipsize to widgets

This commit is contained in:
Hojjat 2026-02-19 09:27:37 -07:00 committed by Ashley Wulber
parent f2ef716ad5
commit cc670e1966
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
18 changed files with 181 additions and 9 deletions

View file

@ -157,6 +157,18 @@ impl text::Paragraph for () {
fn span_bounds(&self, _index: usize) -> Vec<Rectangle> {
vec![]
}
fn min_width(&self) -> f32 {
self.min_bounds().width
}
fn min_height(&self) -> f32 {
self.min_bounds().height
}
fn ellipsize(&self) -> text::Ellipsize {
text::Ellipsize::default()
}
}
impl text::Editor for () {

View file

@ -44,6 +44,9 @@ pub struct Text<Content = String, Font = crate::Font> {
/// The [`Wrapping`] strategy of the [`Text`].
pub wrapping: Wrapping,
/// The [`Ellipsize`] strategy of the [`Text`].
pub ellipsize: Ellipsize,
}
impl<Content, Font> Text<Content, Font>
@ -63,6 +66,7 @@ where
align_y: self.align_y,
shaping: self.shaping,
wrapping: self.wrapping,
ellipsize: self.ellipsize,
}
}
}
@ -192,6 +196,31 @@ pub enum Wrapping {
WordOrGlyph,
}
/// The ellipsizing strategy of some text.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Ellipsize {
/// No ellipsizing.
///
/// This is the default.
#[default]
None,
/// Ellipsize at the start of the text.
Start(EllipsizeHeightLimit),
/// Ellipsize in the middle of the text.
Middle(EllipsizeHeightLimit),
/// Ellipsize at the end of the text.
End(EllipsizeHeightLimit),
}
/// The ellipsizing strategy of some text when it exceeds a certain height.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EllipsizeHeightLimit {
/// The number of lines after which ellipsizing should occur.
Lines(usize),
/// The height limit in pixels
Height(f32),
}
/// The height of a line of text in a paragraph.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineHeight {

View file

@ -5,6 +5,8 @@ use crate::text::{
};
use crate::{Pixels, Point, Rectangle, Size};
use super::Ellipsize;
/// A text paragraph.
pub trait Paragraph: Sized + Default {
/// The font of this [`Paragraph`].
@ -78,6 +80,10 @@ pub trait Paragraph: Sized + Default {
fn min_height(&self) -> f32 {
self.min_bounds().height
}
/// Returns the [`Ellipsize`] strategy of the [`Paragraph`]>
fn ellipsize(&self) -> Ellipsize;
}
/// A [`Paragraph`] of plain text.
@ -169,6 +175,7 @@ impl<P: Paragraph> Plain<P> {
align_y: self.raw.align_y(),
shaping: self.raw.shaping(),
wrapping: self.raw.wrapping(),
ellipsize: self.raw.ellipsize()
}
}
}

View file

@ -32,7 +32,7 @@ use crate::{
};
use std::borrow::Cow;
pub use text::{Alignment, LineHeight, Shaping, Wrapping};
pub use text::{Alignment, Ellipsize, LineHeight, Shaping, Wrapping};
/// A bunch of text.
///
@ -158,6 +158,12 @@ where
self
}
// Sets the [`Ellipsize`] strategy of the [`Text`].
pub fn ellipsize(mut self, ellipsize: Ellipsize) -> Self {
self.format.ellipsize = ellipsize;
self
}
/// Sets the style of the [`Text`].
#[must_use]
pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self
@ -328,6 +334,7 @@ pub struct Format<Font> {
pub align_y: alignment::Vertical,
pub shaping: Shaping,
pub wrapping: Wrapping,
pub ellipsize: Ellipsize,
}
impl<Font> Default for Format<Font> {
@ -342,6 +349,7 @@ impl<Font> Default for Format<Font> {
align_y: alignment::Vertical::Top,
shaping: Shaping::default(),
wrapping: Wrapping::default(),
ellipsize: Ellipsize::default(),
}
}
}
@ -373,6 +381,7 @@ where
align_y: format.align_y,
shaping: format.shaping,
wrapping: format.wrapping,
ellipsize: format.ellipsize,
});
paragraph.min_bounds()