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

Improve text truncation: always include elision character (#3366)

* Add Row::text

* Rename elide_at_width -> truncate_at_width

* Move text layout tests to own module

* Add test to check that elision character is always included

* Include elision character in more circumstances

* Append overflow character if we can't replace

* Always append … when eliding

* Add a secondary text to the text layout demo
This commit is contained in:
Emil Ernerfeldt
2023-09-21 10:41:49 +02:00
committed by GitHub
parent c07394b576
commit 33a0f50f6a
3 changed files with 300 additions and 129 deletions

View File

@@ -7,16 +7,18 @@ pub struct TextLayoutDemo {
overflow_character: Option<char>,
extra_letter_spacing_pixels: i32,
line_height_pixels: u32,
lorem_ipsum: bool,
}
impl Default for TextLayoutDemo {
fn default() -> Self {
Self {
max_rows: 3,
max_rows: 6,
break_anywhere: true,
overflow_character: Some('…'),
extra_letter_spacing_pixels: 0,
line_height_pixels: 0,
lorem_ipsum: true,
}
}
}
@@ -45,6 +47,7 @@ impl super::View for TextLayoutDemo {
overflow_character,
extra_letter_spacing_pixels,
line_height_pixels,
lorem_ipsum,
} = self;
use egui::text::LayoutJob;
@@ -104,32 +107,55 @@ impl super::View for TextLayoutDemo {
}
});
ui.end_row();
ui.label("Text:");
ui.horizontal(|ui| {
ui.selectable_value(lorem_ipsum, true, "Lorem Ipsum");
ui.selectable_value(lorem_ipsum, false, "La Pasionaria");
});
});
ui.add_space(12.0);
egui::ScrollArea::vertical().show(ui, |ui| {
let extra_letter_spacing = points_per_pixel * *extra_letter_spacing_pixels as f32;
let line_height =
(*line_height_pixels != 0).then_some(points_per_pixel * *line_height_pixels as f32);
let text = if *lorem_ipsum {
crate::LOREM_IPSUM_LONG
} else {
TO_BE_OR_NOT_TO_BE
};
let mut job = LayoutJob::single_section(
crate::LOREM_IPSUM_LONG.to_owned(),
egui::TextFormat {
extra_letter_spacing,
line_height,
egui::ScrollArea::vertical()
.auto_shrink([false; 2])
.show(ui, |ui| {
let extra_letter_spacing = points_per_pixel * *extra_letter_spacing_pixels as f32;
let line_height = (*line_height_pixels != 0)
.then_some(points_per_pixel * *line_height_pixels as f32);
let mut job = LayoutJob::single_section(
text.to_owned(),
egui::TextFormat {
extra_letter_spacing,
line_height,
..Default::default()
},
);
job.wrap = egui::text::TextWrapping {
max_rows: *max_rows,
break_anywhere: *break_anywhere,
overflow_character: *overflow_character,
..Default::default()
},
);
job.wrap = egui::text::TextWrapping {
max_rows: *max_rows,
break_anywhere: *break_anywhere,
overflow_character: *overflow_character,
..Default::default()
};
};
// NOTE: `Label` overrides some of the wrapping settings, e.g. wrap width
ui.label(job);
});
// NOTE: `Label` overrides some of the wrapping settings, e.g. wrap width
ui.label(job);
});
}
}
/// Excerpt from Dolores Ibárruri's farwel speech to the International Brigades:
const TO_BE_OR_NOT_TO_BE: &str = "Mothers! Women!\n
When the years pass by and the wounds of war are stanched; when the memory of the sad and bloody days dissipates in a present of liberty, of peace and of wellbeing; when the rancor have died out and pride in a free country is felt equally by all Spaniards, speak to your children. Tell them of these men of the International Brigades.\n\
\n\
Recount for them how, coming over seas and mountains, crossing frontiers bristling with bayonets, sought by raving dogs thirsting to tear their flesh, these men reached our country as crusaders for freedom, to fight and die for Spains liberty and independence threatened by German and Italian fascism. \
They gave up everything — their loves, their countries, home and fortune, fathers, mothers, wives, brothers, sisters and children — and they came and said to us: “We are here. Your cause, Spains cause, is ours. It is the cause of all advanced and progressive mankind.”\n\
\n\
- Dolores Ibárruri, 1938";