Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[epaint] Add more text wrapping options #1291

Merged
merged 13 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,14 @@ impl WidgetText {
Self::RichText(text) => {
let valign = ui.layout().vertical_align();
let mut text_job = text.into_text_job(ui.style(), fallback_font.into(), valign);
text_job.job.wrap_width = wrap_width;
text_job.job.wrap.max_width = wrap_width;
WidgetTextGalley {
galley: ui.fonts().layout_job(text_job.job),
galley_has_color: text_job.job_has_color,
}
}
Self::LayoutJob(mut job) => {
job.wrap_width = wrap_width;
job.wrap.max_width = wrap_width;
WidgetTextGalley {
galley: ui.fonts().layout_job(job),
galley_has_color: true,
Expand Down
6 changes: 3 additions & 3 deletions egui/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Label {
let first_row_indentation = available_width - ui.available_size_before_wrap().x;
egui_assert!(first_row_indentation.is_finite());

text_job.job.wrap_width = available_width;
text_job.job.wrap.max_width = available_width;
text_job.job.first_row_min_height = cursor.height();
text_job.job.halign = Align::Min;
text_job.job.justify = false;
Expand All @@ -129,9 +129,9 @@ impl Label {
(pos, text_galley, response)
} else {
if should_wrap {
text_job.job.wrap_width = available_width;
text_job.job.wrap.max_width = available_width;
} else {
text_job.job.wrap_width = f32::INFINITY;
text_job.job.wrap.max_width = f32::INFINITY;
};

if ui.is_grid() {
Expand Down
2 changes: 1 addition & 1 deletion egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'t> TextEdit<'t> {
/// # fn my_memoized_highlighter(s: &str) -> egui::text::LayoutJob { Default::default() }
/// let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
/// let mut layout_job: egui::text::LayoutJob = my_memoized_highlighter(string);
/// layout_job.wrap_width = wrap_width;
/// layout_job.wrap.max_width = wrap_width;
/// ui.fonts().layout_job(layout_job)
/// };
/// ui.add(egui::TextEdit::multiline(&mut my_code).layouter(&mut layouter));
Expand Down
2 changes: 1 addition & 1 deletion egui_demo_lib/src/apps/demo/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl super::View for CodeEditor {
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
let mut layout_job =
crate::syntax_highlighting::highlight(ui.ctx(), &theme, string, language);
layout_job.wrap_width = wrap_width;
layout_job.wrap.max_width = wrap_width;
ui.fonts().layout_job(layout_job)
};

Expand Down
49 changes: 46 additions & 3 deletions egui_demo_lib/src/apps/demo/misc_demo_window.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use super::*;
use egui::{color::*, *};
use crate::LOREM_IPSUM;
use egui::{color::*, epaint::text::TextWrapping, *};

/// Showcase some ui code
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct MiscDemoWindow {
num_columns: usize,

break_anywhere: bool,
max_rows: usize,
overflow_character: Option<char>,

widgets: Widgets,
colors: ColorWidgets,
tree: Tree,
Expand All @@ -18,6 +23,10 @@ impl Default for MiscDemoWindow {
MiscDemoWindow {
num_columns: 2,

max_rows: 2,
break_anywhere: false,
overflow_character: Some('…'),

widgets: Default::default(),
colors: Default::default(),
tree: Tree::demo(),
Expand Down Expand Up @@ -53,7 +62,12 @@ impl View for MiscDemoWindow {
CollapsingHeader::new("Text layout")
.default_open(false)
.show(ui, |ui| {
text_layout_ui(ui);
text_layout_ui(
ui,
&mut self.max_rows,
&mut self.break_anywhere,
&mut self.overflow_character,
);
});

CollapsingHeader::new("Colors")
Expand Down Expand Up @@ -401,7 +415,12 @@ impl SubTree {

// ----------------------------------------------------------------------------

fn text_layout_ui(ui: &mut egui::Ui) {
fn text_layout_ui(
ui: &mut egui::Ui,
max_rows: &mut usize,
break_anywhere: &mut bool,
overflow_character: &mut Option<char>,
) {
use egui::text::LayoutJob;

let mut job = LayoutJob::default();
Expand Down Expand Up @@ -556,6 +575,30 @@ fn text_layout_ui(ui: &mut egui::Ui) {

ui.label(job);

ui.separator();

ui.horizontal(|ui| {
ui.add(DragValue::new(max_rows));
ui.label("Max rows");
});
ui.checkbox(break_anywhere, "Break anywhere");
ui.horizontal(|ui| {
ui.selectable_value(overflow_character, None, "None");
ui.selectable_value(overflow_character, Some('…'), "…");
ui.selectable_value(overflow_character, Some('—'), "—");
ui.selectable_value(overflow_character, Some('-'), " - ");
ui.label("Overflow character");
});

let mut job = LayoutJob::single_section(LOREM_IPSUM.to_string(), TextFormat::default());
job.wrap = TextWrapping {
max_rows: *max_rows,
break_anywhere: *break_anywhere,
overflow_character: *overflow_character,
..Default::default()
};
ui.label(job);

ui.vertical_centered(|ui| {
ui.add(crate::__egui_github_link_file_line!());
});
Expand Down
4 changes: 2 additions & 2 deletions egui_demo_lib/src/apps/http_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl ColoredText {
// Selectable text:
let mut layouter = |ui: &egui::Ui, _string: &str, wrap_width: f32| {
let mut layout_job = self.0.clone();
layout_job.wrap_width = wrap_width;
layout_job.wrap.max_width = wrap_width;
ui.fonts().layout_job(layout_job)
};

Expand All @@ -257,7 +257,7 @@ impl ColoredText {
);
} else {
let mut job = self.0.clone();
job.wrap_width = ui.available_width();
job.wrap.max_width = ui.available_width();
let galley = ui.fonts().layout_job(job);
let (response, painter) = ui.allocate_painter(galley.size(), egui::Sense::hover());
painter.add(egui::Shape::galley(response.rect.min, galley));
Expand Down
2 changes: 1 addition & 1 deletion egui_demo_lib/src/easy_mark/easy_mark_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl EasyMarkEditor {
let response = if self.highlight_editor {
let mut layouter = |ui: &egui::Ui, easymark: &str, wrap_width: f32| {
let mut layout_job = highlighter.highlight(ui.style(), easymark);
layout_job.wrap_width = wrap_width;
layout_job.wrap.max_width = wrap_width;
ui.fonts().layout_job(layout_job)
};

Expand Down
2 changes: 1 addition & 1 deletion egui_demo_lib/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn code_view_ui(ui: &mut egui::Ui, mut code: &str) {

let mut layouter = |ui: &egui::Ui, string: &str, _wrap_width: f32| {
let layout_job = highlight(ui.ctx(), &theme, string, language);
// layout_job.wrap_width = wrap_width; // no wrapping
// layout_job.wrap.max_width = wrap_width; // no wrapping
ui.fonts().layout_job(layout_job)
};

Expand Down
7 changes: 7 additions & 0 deletions epaint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to the epaint crate will be documented in this file.

## Unreleased
* Add `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)).
* Added more text wrapping options ([#1291](https://github.com/emilk/egui/pull/1291)):
* Added `TextWrapping` struct containing all wrapping options.
* Added `LayoutJob::wrap` field containing these options.
* Moved `LayoutJob::wrap_width` to `TextWrapping::max_width`.
* Added `TextWrapping::max_rows` to limit amount of rows the text should have.
* Added `TextWrapping::break_anywhere` to control should the text break at appropriate places or not.
* Added `TextWrapping::overflow_character` to specify what character should be used to represent clipped text.


## 0.17.0 - 2022-02-22
Expand Down
Loading