mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 22:30:03 -04:00
[epaint] Add more text wrapping options (#1291)
This commit is contained in:
@@ -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)
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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))]
|
||||
@@ -7,6 +8,10 @@ use egui::{color::*, *};
|
||||
pub struct MiscDemoWindow {
|
||||
num_columns: usize,
|
||||
|
||||
break_anywhere: bool,
|
||||
max_rows: usize,
|
||||
overflow_character: Option<char>,
|
||||
|
||||
widgets: Widgets,
|
||||
colors: ColorWidgets,
|
||||
tree: Tree,
|
||||
@@ -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(),
|
||||
@@ -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")
|
||||
@@ -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();
|
||||
@@ -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!());
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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)
|
||||
};
|
||||
|
||||
|
||||
@@ -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)
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user