Skip to content

Commit f607dd0

Browse files
committed
TextEdit: hint_text
1 parent 6cffcce commit f607dd0

File tree

9 files changed

+67
-23
lines changed

9 files changed

+67
-23
lines changed

egui/src/containers/collapsing_header.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl CollapsingHeader {
333333
paint_icon(ui, openness, &icon_response);
334334
}
335335

336-
text.paint(ui.painter(), text_pos, &visuals);
336+
text.paint_with_visuals(ui.painter(), text_pos, &visuals);
337337

338338
Prepared {
339339
id,

egui/src/containers/combo_box.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ fn combo_box_dyn<'c, R>(
182182
paint_icon(ui.painter(), icon_rect.expand(visuals.expansion), visuals);
183183

184184
let text_rect = Align2::LEFT_CENTER.align_size_within_rect(galley.size(), rect);
185-
galley.paint(ui.painter(), text_rect.min, visuals);
185+
galley.paint_with_visuals(ui.painter(), text_rect.min, visuals);
186186
});
187187

188188
if button_response.clicked() {

egui/src/containers/window.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,11 @@ impl TitleBar {
831831
emath::align::center_size_in_rect(self.title_galley.size(), full_top_rect).left_top();
832832
let text_pos = text_pos - self.title_galley.galley().rect.min.to_vec2();
833833
let text_pos = text_pos - 1.5 * Vec2::Y; // HACK: center on x-height of text (looks better)
834-
self.title_galley
835-
.paint_with_color(ui.painter(), text_pos, ui.visuals().text_color());
834+
self.title_galley.paint_with_fallback_color(
835+
ui.painter(),
836+
text_pos,
837+
ui.visuals().text_color(),
838+
);
836839

837840
if let Some(content_response) = &content_response {
838841
// paint separator between title and content:

egui/src/menu.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ impl SubMenuButton {
442442
);
443443

444444
let text_color = visuals.text_color();
445-
text_galley.paint_with_color(ui.painter(), text_pos, text_color);
446-
icon_galley.paint_with_color(ui.painter(), icon_pos, text_color);
445+
text_galley.paint_with_fallback_color(ui.painter(), text_pos, text_color);
446+
icon_galley.paint_with_fallback_color(ui.painter(), icon_pos, text_color);
447447
}
448448
response
449449
}

egui/src/widget_text.rs

+41-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ impl RichText {
5454
}
5555
}
5656

57+
#[inline]
58+
pub fn is_empty(&self) -> bool {
59+
self.text.is_empty()
60+
}
61+
5762
#[inline]
5863
pub fn text(&self) -> &str {
5964
&self.text
@@ -285,6 +290,15 @@ impl Default for WidgetText {
285290
}
286291

287292
impl WidgetText {
293+
#[inline]
294+
pub fn is_empty(&self) -> bool {
295+
match self {
296+
Self::RichText(text) => text.is_empty(),
297+
Self::LayoutJob(job) => job.is_empty(),
298+
Self::Galley(galley) => galley.is_empty(),
299+
}
300+
}
301+
288302
#[inline]
289303
pub fn text(&self) -> &str {
290304
match self {
@@ -583,15 +597,39 @@ impl WidgetTextGalley {
583597
&self.galley
584598
}
585599

586-
pub fn paint(self, painter: &crate::Painter, text_pos: Pos2, visuals: &WidgetVisuals) {
600+
/// Use the colors in the original [`WidgetText`] if any,
601+
/// else fall back to the one specified by the [`WidgetVisuals`].
602+
pub fn paint_with_visuals(
603+
self,
604+
painter: &crate::Painter,
605+
text_pos: Pos2,
606+
visuals: &WidgetVisuals,
607+
) {
608+
self.paint_with_fallback_color(painter, text_pos, visuals.text_color());
609+
}
610+
611+
/// Use the colors in the original [`WidgetText`] if any,
612+
/// else fall back to the given color.
613+
pub fn paint_with_fallback_color(
614+
self,
615+
painter: &crate::Painter,
616+
text_pos: Pos2,
617+
text_color: Color32,
618+
) {
587619
if self.galley_has_color {
588620
painter.galley(text_pos, self.galley);
589621
} else {
590-
painter.galley_with_color(text_pos, self.galley, visuals.text_color());
622+
painter.galley_with_color(text_pos, self.galley, text_color);
591623
}
592624
}
593625

594-
pub fn paint_with_color(self, painter: &crate::Painter, text_pos: Pos2, text_color: Color32) {
626+
/// Paint with this specific color.
627+
pub fn paint_with_color_override(
628+
self,
629+
painter: &crate::Painter,
630+
text_pos: Pos2,
631+
text_color: Color32,
632+
) {
595633
painter.galley_with_color(text_pos, self.galley, text_color);
596634
}
597635
}

egui/src/widgets/button.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Widget for Button {
162162
);
163163
}
164164

165-
text.paint(ui.painter(), text_pos, visuals);
165+
text.paint_with_visuals(ui.painter(), text_pos, visuals);
166166
}
167167

168168
response
@@ -260,7 +260,7 @@ impl<'a> Widget for Checkbox<'a> {
260260
));
261261
}
262262

263-
text.paint(ui.painter(), text_pos, visuals);
263+
text.paint_with_visuals(ui.painter(), text_pos, visuals);
264264
response
265265
}
266266
}
@@ -360,7 +360,7 @@ impl Widget for RadioButton {
360360
});
361361
}
362362

363-
text.paint(ui.painter(), text_pos, visuals);
363+
text.paint_with_visuals(ui.painter(), text_pos, visuals);
364364
response
365365
}
366366
}

egui/src/widgets/progress_bar.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ impl Widget for ProgressBar {
132132
let text_color = visuals
133133
.override_text_color
134134
.unwrap_or(visuals.selection.stroke.color);
135-
galley.paint_with_color(&ui.painter().sub_region(outer_rect), text_pos, text_color);
135+
galley.paint_with_fallback_color(
136+
&ui.painter().sub_region(outer_rect),
137+
text_pos,
138+
text_color,
139+
);
136140
}
137141

138142
response

egui/src/widgets/selected_label.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Widget for SelectableLabel {
7373
.rect(rect, corner_radius, visuals.bg_fill, visuals.bg_stroke);
7474
}
7575

76-
text.paint(ui.painter(), text_pos, &visuals);
76+
text.paint_with_visuals(ui.painter(), text_pos, &visuals);
7777
response
7878
}
7979
}

egui/src/widgets/text_edit/builder.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use super::{CCursorRange, CursorRange, TextEditOutput, TextEditState};
4545
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
4646
pub struct TextEdit<'t> {
4747
text: &'t mut dyn TextBuffer,
48-
hint_text: String,
48+
hint_text: WidgetText,
4949
id: Option<Id>,
5050
id_source: Option<Id>,
5151
text_style: Option<TextStyle>,
@@ -127,9 +127,8 @@ impl<'t> TextEdit<'t> {
127127
}
128128

129129
/// Show a faint hint text when the text field is empty.
130-
#[allow(clippy::needless_pass_by_value)]
131-
pub fn hint_text(mut self, hint_text: impl ToString) -> Self {
132-
self.hint_text = hint_text.to_string();
130+
pub fn hint_text(mut self, hint_text: impl Into<WidgetText>) -> Self {
131+
self.hint_text = hint_text.into();
133132
self
134133
}
135134

@@ -512,12 +511,12 @@ impl<'t> TextEdit<'t> {
512511

513512
if text.as_ref().is_empty() && !hint_text.is_empty() {
514513
let hint_text_color = ui.visuals().weak_text_color();
515-
let galley = ui.fonts().layout_job(if multiline {
516-
LayoutJob::simple(hint_text, text_style, hint_text_color, desired_size.x)
514+
let galley = if multiline {
515+
hint_text.into_galley(ui, Some(true), desired_size.x, text_style)
517516
} else {
518-
LayoutJob::simple_singleline(hint_text, text_style, hint_text_color)
519-
});
520-
painter.galley(response.rect.min, galley);
517+
hint_text.into_galley(ui, Some(false), f32::INFINITY, text_style)
518+
};
519+
galley.paint_with_fallback_color(&painter, response.rect.min, hint_text_color);
521520
}
522521

523522
if ui.memory().has_focus(id) {

0 commit comments

Comments
 (0)