From ff7a3832b68a8ecccf531511d1a17d92821b5430 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Mon, 27 May 2024 17:28:03 +0200 Subject: [PATCH] TextEdit hint text styling (#4517) Simply adds a `hint_text_font` (any suggestions for a better name? hint_text_style seemed misleading as it doesn't only accept `TextStyle`) method to the TextEdit builder that allows to set the styling (specifically, a `FontSelection`) for the hint text. My personal use-case for this was having body-sized hint text in a heading-sized TextEdit, but I'm sure there's more out there. The PR shouldn't break compatibility, as it's stored as an `Option` that defaults to the `font_id` (which was the only behaviour prior to this) when empty. It looked too trivial to add something to the actual demo, but I have it locally, so let me know if I should commit that. Ran the check script locally, had no complaints. --- crates/egui/src/widgets/text_edit/builder.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index b61b0f813f6..4458fd031f7 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -60,6 +60,7 @@ use super::{TextEditOutput, TextEditState}; pub struct TextEdit<'t> { text: &'t mut dyn TextBuffer, hint_text: WidgetText, + hint_text_font: Option, id: Option, id_source: Option, font_selection: FontSelection, @@ -111,6 +112,7 @@ impl<'t> TextEdit<'t> { Self { text, hint_text: Default::default(), + hint_text_font: None, id: None, id_source: None, font_selection: Default::default(), @@ -189,6 +191,13 @@ impl<'t> TextEdit<'t> { self } + /// Set a specific style for the hint text. + #[inline] + pub fn hint_text_font(mut self, hint_text_font: impl Into) -> Self { + self.hint_text_font = Some(hint_text_font.into()); + self + } + /// If true, hide the letters from view and prevent copying from the field. #[inline] pub fn password(mut self, password: bool) -> Self { @@ -438,6 +447,7 @@ impl<'t> TextEdit<'t> { let TextEdit { text, hint_text, + hint_text_font, id, id_source, font_selection, @@ -653,10 +663,11 @@ impl<'t> TextEdit<'t> { if text.as_str().is_empty() && !hint_text.is_empty() { let hint_text_color = ui.visuals().weak_text_color(); + let hint_text_font_id = hint_text_font.unwrap_or(font_id.into()); let galley = if multiline { - hint_text.into_galley(ui, Some(true), desired_inner_size.x, font_id) + hint_text.into_galley(ui, Some(true), desired_inner_size.x, hint_text_font_id) } else { - hint_text.into_galley(ui, Some(false), f32::INFINITY, font_id) + hint_text.into_galley(ui, Some(false), f32::INFINITY, hint_text_font_id) }; painter.galley(rect.min, galley, hint_text_color); }