1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Add support for text truncation to egui::Style (#4556)

* Closes #4473

This PR introduce `Style::wrap_mode`, which adds support for text
truncation in addition to text wrapping. This PR also update some width
calculation of the ComboBox.

#### Core

- Add `egui::TextWrapMode` (pure enum with `Extend`, `Wrap`, `Truncate`)
- Add `Style::wrap_mode: Option<tTextWrapMode>`
- **DEPRECATED**: `Style::wrap`, use `Style::wrap_mode` instead.
- Add `Ui::wrap_mode()` to return the wrap mode to use in the current
ui. If specified in `Style`, return it. Otherwise, return
`TextWrapMode::Wrap` for vertical layout and wrapping horizontal layout,
and `TextWrapMode::Extend` otherwise.
- **DEPRECATED**: `Ui::wrap_text()`, use `Ui::wrap_mode` instead.

#### Widget

- Update the width calculation of the `ComboBox` button (_not_ its popup
menu).
- Now, `ComboBox::width()` (defaulting to `Spacing::combo_width`) is
always considered a minimum width and will extend the `Ui`, regardless
of the selected text width and wrap mode.
- Introduce `ComboBox::wrap_mode`, which overrides `Ui::wrap_mode` for
the selected text layout.
- Note: since `ComboBox` uses `ui.horizontal` internally, the default
wrap mode is always `TextWrapMode::Extend`, regardless of the caller's
`Ui`'s layout.
- The `ComboBox` button no longer extend to `ui.available_width()` with
wrapping is enabled.
- **BREAKING**: `ComboBox::wrap()` no longer has a `bool` argument and
is now a short-hand for `ComboBox::wrap_mode(TextWrapMode::Wrap)`.
- Added `ComboBox::truncate()` as short-hand for
`ComboBox::wrap_mode(TextWrapMode::Truncate)`.
- Update `Label`
  - Add `Label::wrap_mode()` to specify the text wrap mode.
- **BREAKING**: `Label::wrap()` no longer has a `bool` argument and is
now a short-hand for `Label::wrap_mode(TextWrapMode::Wrap)`.
- **BREAKING**: `Label::truncate()` no longer has a `bool` argument and
is now a short-hand for `Label::wrap_mode(TextWrapMode::Truncate)`.
- Update `Button`
  - Add `Button::wrap_mode()` to specify the text wrap mode.
- **BREAKING**: `Button::wrap()` no longer has a `bool` argument and is
now a short-hand for `Button::wrap_mode(TextWrapMode::Wrap)`.
- Added `Button::truncate()` as short-hand for
`Button::wrap_mode(TextWrapMode::Truncate)`.

#### Low-level

- **BREAKING**: `WidgetText::into_galley()` now takes an
`Option<TextWrapMode>` instead of a `Option<bool>` argument.
- **BREAKING**: `WidgetText::into_galley_impl(()` now takes a
`TextWrapping` argument instead of `wrap: bool` and `availalbe_width:
f32` arguments.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Antoine Beyeler
2024-05-28 13:10:41 +02:00
committed by GitHub
parent 4b59c6d414
commit bcd91f27a1
29 changed files with 281 additions and 162 deletions

View File

@@ -319,6 +319,24 @@ impl TextFormat {
// ----------------------------------------------------------------------------
/// How to wrap and elide text.
///
/// This enum is used in high-level APIs where providing a [`TextWrapping`] is too verbose.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum TextWrapMode {
/// The text should expand the `Ui` size when reaching its boundary.
Extend,
/// The text should wrap to the next line when reaching the `Ui` boundary.
Wrap,
/// The text should be elided using "…" when reaching the `Ui` boundary.
///
/// Note that using [`TextWrapping`] and [`LayoutJob`] offers more control over the elision.
Truncate,
}
/// Controls the text wrapping and elision of a [`LayoutJob`].
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -335,7 +353,7 @@ pub struct TextWrapping {
/// Maximum amount of rows the text galley should have.
///
/// If this limit is reached, text will be truncated and
/// If this limit is reached, text will be truncated
/// and [`Self::overflow_character`] appended to the final row.
/// You can detect this by checking [`Galley::elided`].
///
@@ -394,6 +412,15 @@ impl Default for TextWrapping {
}
impl TextWrapping {
/// Create a [`TextWrapping`] from a [`TextWrapMode`] and an available width.
pub fn from_wrap_mode_and_width(mode: TextWrapMode, max_width: f32) -> Self {
match mode {
TextWrapMode::Extend => Self::no_max_width(),
TextWrapMode::Wrap => Self::wrap_at_width(max_width),
TextWrapMode::Truncate => Self::truncate_at_width(max_width),
}
}
/// A row can be as long as it need to be.
pub fn no_max_width() -> Self {
Self {
@@ -402,6 +429,14 @@ impl TextWrapping {
}
}
/// A row can be at most `max_width` wide but can wrap in any number of lines.
pub fn wrap_at_width(max_width: f32) -> Self {
Self {
max_width,
..Default::default()
}
}
/// Elide text that doesn't fit within the given width, replaced with `…`.
pub fn truncate_at_width(max_width: f32) -> Self {
Self {