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.
This commit is contained in:
zaaarf 2024-05-27 17:28:03 +02:00 committed by GitHub
parent 1ae2d2803a
commit ff7a3832b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 2 deletions

View File

@ -60,6 +60,7 @@ use super::{TextEditOutput, TextEditState};
pub struct TextEdit<'t> { pub struct TextEdit<'t> {
text: &'t mut dyn TextBuffer, text: &'t mut dyn TextBuffer,
hint_text: WidgetText, hint_text: WidgetText,
hint_text_font: Option<FontSelection>,
id: Option<Id>, id: Option<Id>,
id_source: Option<Id>, id_source: Option<Id>,
font_selection: FontSelection, font_selection: FontSelection,
@ -111,6 +112,7 @@ impl<'t> TextEdit<'t> {
Self { Self {
text, text,
hint_text: Default::default(), hint_text: Default::default(),
hint_text_font: None,
id: None, id: None,
id_source: None, id_source: None,
font_selection: Default::default(), font_selection: Default::default(),
@ -189,6 +191,13 @@ impl<'t> TextEdit<'t> {
self self
} }
/// Set a specific style for the hint text.
#[inline]
pub fn hint_text_font(mut self, hint_text_font: impl Into<FontSelection>) -> Self {
self.hint_text_font = Some(hint_text_font.into());
self
}
/// If true, hide the letters from view and prevent copying from the field. /// If true, hide the letters from view and prevent copying from the field.
#[inline] #[inline]
pub fn password(mut self, password: bool) -> Self { pub fn password(mut self, password: bool) -> Self {
@ -438,6 +447,7 @@ impl<'t> TextEdit<'t> {
let TextEdit { let TextEdit {
text, text,
hint_text, hint_text,
hint_text_font,
id, id,
id_source, id_source,
font_selection, font_selection,
@ -653,10 +663,11 @@ impl<'t> TextEdit<'t> {
if text.as_str().is_empty() && !hint_text.is_empty() { if text.as_str().is_empty() && !hint_text.is_empty() {
let hint_text_color = ui.visuals().weak_text_color(); 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 { 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 { } 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); painter.galley(rect.min, galley, hint_text_color);
} }