mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Add extra_text_line_spacing to control vertical spacing between text lines (#8040)
Add `extra_text_line_spacing` to control vertical spacing between text lines **Description** This PR adds a new `Spacing::extra_text_line_spacing` field to control additional vertical spacing between lines of text. The spacing is applied to text layout by adjusting `TextFormat::line_height` based on the font row height plus the configured extra spacing. This improves text readability and allows consistent line spacing customization for widgets such as `TextEdit` and `Label`. --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -419,6 +419,9 @@ pub struct Spacing {
|
|||||||
/// Default width of a [`crate::TextEdit`].
|
/// Default width of a [`crate::TextEdit`].
|
||||||
pub text_edit_width: f32,
|
pub text_edit_width: f32,
|
||||||
|
|
||||||
|
/// Additional vertical spacing between lines of text.
|
||||||
|
pub extra_text_line_spacing: f32,
|
||||||
|
|
||||||
/// Checkboxes, radio button and collapsing headers have an icon at the start.
|
/// Checkboxes, radio button and collapsing headers have an icon at the start.
|
||||||
/// This is the width/height of the outer part of this icon (e.g. the BOX of the checkbox).
|
/// This is the width/height of the outer part of this icon (e.g. the BOX of the checkbox).
|
||||||
pub icon_width: f32,
|
pub icon_width: f32,
|
||||||
@@ -1458,6 +1461,7 @@ impl Default for Spacing {
|
|||||||
slider_rail_height: 8.0,
|
slider_rail_height: 8.0,
|
||||||
combo_width: 100.0,
|
combo_width: 100.0,
|
||||||
text_edit_width: 280.0,
|
text_edit_width: 280.0,
|
||||||
|
extra_text_line_spacing: 0.0,
|
||||||
icon_width: 14.0,
|
icon_width: 14.0,
|
||||||
icon_width_inner: 8.0,
|
icon_width_inner: 8.0,
|
||||||
icon_spacing: 4.0,
|
icon_spacing: 4.0,
|
||||||
@@ -1948,6 +1952,7 @@ impl Spacing {
|
|||||||
slider_rail_height,
|
slider_rail_height,
|
||||||
combo_width,
|
combo_width,
|
||||||
text_edit_width,
|
text_edit_width,
|
||||||
|
extra_text_line_spacing,
|
||||||
icon_width,
|
icon_width,
|
||||||
icon_width_inner,
|
icon_width_inner,
|
||||||
icon_spacing,
|
icon_spacing,
|
||||||
@@ -2014,6 +2019,10 @@ impl Spacing {
|
|||||||
ui.add(DragValue::new(text_edit_width).range(0.0..=1000.0));
|
ui.add(DragValue::new(text_edit_width).range(0.0..=1000.0));
|
||||||
ui.end_row();
|
ui.end_row();
|
||||||
|
|
||||||
|
ui.label("Extra text line spacing");
|
||||||
|
ui.add(DragValue::new(extra_text_line_spacing).range(0.0..=20.0));
|
||||||
|
ui.end_row();
|
||||||
|
|
||||||
ui.label("Tooltip wrap width");
|
ui.label("Tooltip wrap width");
|
||||||
ui.add(DragValue::new(tooltip_width).range(0.0..=1000.0));
|
ui.add(DragValue::new(tooltip_width).range(0.0..=1000.0));
|
||||||
ui.end_row();
|
ui.end_row();
|
||||||
|
|||||||
@@ -769,14 +769,19 @@ impl WidgetText {
|
|||||||
.visuals
|
.visuals
|
||||||
.override_text_color
|
.override_text_color
|
||||||
.unwrap_or(crate::Color32::PLACEHOLDER);
|
.unwrap_or(crate::Color32::PLACEHOLDER);
|
||||||
|
|
||||||
|
// We want the style overrides to take precedence over the fallback font
|
||||||
|
let font_id = FontSelection::default().resolve_with_fallback(style, fallback_font);
|
||||||
|
let line_height = ctx
|
||||||
|
.fonts_mut(|f| f.row_height(&font_id) + style.spacing.extra_text_line_spacing);
|
||||||
|
|
||||||
let mut layout_job = LayoutJob::simple_format(
|
let mut layout_job = LayoutJob::simple_format(
|
||||||
text,
|
text,
|
||||||
TextFormat {
|
TextFormat {
|
||||||
// We want the style overrides to take precedence over the fallback font
|
font_id,
|
||||||
font_id: FontSelection::default()
|
|
||||||
.resolve_with_fallback(style, fallback_font),
|
|
||||||
color,
|
color,
|
||||||
valign: default_valign,
|
valign: default_valign,
|
||||||
|
line_height: Some(line_height),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -471,6 +471,8 @@ impl TextEdit<'_> {
|
|||||||
|
|
||||||
let font_id = font_selection.resolve(ui.style());
|
let font_id = font_selection.resolve(ui.style());
|
||||||
let row_height = ui.fonts_mut(|f| f.row_height(&font_id));
|
let row_height = ui.fonts_mut(|f| f.row_height(&font_id));
|
||||||
|
let line_height = row_height + ui.spacing().extra_text_line_spacing;
|
||||||
|
|
||||||
const MIN_WIDTH: f32 = 24.0; // Never make a [`TextEdit`] more narrow than this.
|
const MIN_WIDTH: f32 = 24.0; // Never make a [`TextEdit`] more narrow than this.
|
||||||
let available_width = ui.available_width().at_least(MIN_WIDTH);
|
let available_width = ui.available_width().at_least(MIN_WIDTH);
|
||||||
let desired_width = desired_width
|
let desired_width = desired_width
|
||||||
@@ -489,12 +491,17 @@ impl TextEdit<'_> {
|
|||||||
layout_job.halign = align.x();
|
layout_job.halign = align.x();
|
||||||
// We want to keep the trailing whitespace, since hiding it feels really weird when typing
|
// We want to keep the trailing whitespace, since hiding it feels really weird when typing
|
||||||
layout_job.keep_trailing_whitespace = true;
|
layout_job.keep_trailing_whitespace = true;
|
||||||
|
|
||||||
|
for section in &mut layout_job.sections {
|
||||||
|
section.format.line_height = Some(line_height);
|
||||||
|
}
|
||||||
|
|
||||||
ui.fonts_mut(|f| f.layout_job(layout_job))
|
ui.fonts_mut(|f| f.layout_job(layout_job))
|
||||||
};
|
};
|
||||||
|
|
||||||
let layouter = layouter.unwrap_or(&mut default_layouter);
|
let layouter = layouter.unwrap_or(&mut default_layouter);
|
||||||
|
|
||||||
let min_inner_height = (desired_height_rows.at_least(1) as f32) * row_height;
|
let min_inner_height = (desired_height_rows.at_least(1) as f32) * line_height;
|
||||||
|
|
||||||
let id = id.unwrap_or_else(|| {
|
let id = id.unwrap_or_else(|| {
|
||||||
if let Some(id_salt) = id_salt {
|
if let Some(id_salt) = id_salt {
|
||||||
|
|||||||
Reference in New Issue
Block a user