Skip to content

Commit

Permalink
TextEdit: hint_text
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 1, 2021
1 parent 6cffcce commit f607dd0
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl CollapsingHeader {
paint_icon(ui, openness, &icon_response);
}

text.paint(ui.painter(), text_pos, &visuals);
text.paint_with_visuals(ui.painter(), text_pos, &visuals);

Prepared {
id,
Expand Down
2 changes: 1 addition & 1 deletion egui/src/containers/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn combo_box_dyn<'c, R>(
paint_icon(ui.painter(), icon_rect.expand(visuals.expansion), visuals);

let text_rect = Align2::LEFT_CENTER.align_size_within_rect(galley.size(), rect);
galley.paint(ui.painter(), text_rect.min, visuals);
galley.paint_with_visuals(ui.painter(), text_rect.min, visuals);
});

if button_response.clicked() {
Expand Down
7 changes: 5 additions & 2 deletions egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,11 @@ impl TitleBar {
emath::align::center_size_in_rect(self.title_galley.size(), full_top_rect).left_top();
let text_pos = text_pos - self.title_galley.galley().rect.min.to_vec2();
let text_pos = text_pos - 1.5 * Vec2::Y; // HACK: center on x-height of text (looks better)
self.title_galley
.paint_with_color(ui.painter(), text_pos, ui.visuals().text_color());
self.title_galley.paint_with_fallback_color(
ui.painter(),
text_pos,
ui.visuals().text_color(),
);

if let Some(content_response) = &content_response {
// paint separator between title and content:
Expand Down
4 changes: 2 additions & 2 deletions egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ impl SubMenuButton {
);

let text_color = visuals.text_color();
text_galley.paint_with_color(ui.painter(), text_pos, text_color);
icon_galley.paint_with_color(ui.painter(), icon_pos, text_color);
text_galley.paint_with_fallback_color(ui.painter(), text_pos, text_color);
icon_galley.paint_with_fallback_color(ui.painter(), icon_pos, text_color);
}
response
}
Expand Down
44 changes: 41 additions & 3 deletions egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl RichText {
}
}

#[inline]
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}

#[inline]
pub fn text(&self) -> &str {
&self.text
Expand Down Expand Up @@ -285,6 +290,15 @@ impl Default for WidgetText {
}

impl WidgetText {
#[inline]
pub fn is_empty(&self) -> bool {
match self {
Self::RichText(text) => text.is_empty(),
Self::LayoutJob(job) => job.is_empty(),
Self::Galley(galley) => galley.is_empty(),
}
}

#[inline]
pub fn text(&self) -> &str {
match self {
Expand Down Expand Up @@ -583,15 +597,39 @@ impl WidgetTextGalley {
&self.galley
}

pub fn paint(self, painter: &crate::Painter, text_pos: Pos2, visuals: &WidgetVisuals) {
/// Use the colors in the original [`WidgetText`] if any,
/// else fall back to the one specified by the [`WidgetVisuals`].
pub fn paint_with_visuals(
self,
painter: &crate::Painter,
text_pos: Pos2,
visuals: &WidgetVisuals,
) {
self.paint_with_fallback_color(painter, text_pos, visuals.text_color());
}

/// Use the colors in the original [`WidgetText`] if any,
/// else fall back to the given color.
pub fn paint_with_fallback_color(
self,
painter: &crate::Painter,
text_pos: Pos2,
text_color: Color32,
) {
if self.galley_has_color {
painter.galley(text_pos, self.galley);
} else {
painter.galley_with_color(text_pos, self.galley, visuals.text_color());
painter.galley_with_color(text_pos, self.galley, text_color);
}
}

pub fn paint_with_color(self, painter: &crate::Painter, text_pos: Pos2, text_color: Color32) {
/// Paint with this specific color.
pub fn paint_with_color_override(
self,
painter: &crate::Painter,
text_pos: Pos2,
text_color: Color32,
) {
painter.galley_with_color(text_pos, self.galley, text_color);
}
}
6 changes: 3 additions & 3 deletions egui/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Widget for Button {
);
}

text.paint(ui.painter(), text_pos, visuals);
text.paint_with_visuals(ui.painter(), text_pos, visuals);
}

response
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<'a> Widget for Checkbox<'a> {
));
}

text.paint(ui.painter(), text_pos, visuals);
text.paint_with_visuals(ui.painter(), text_pos, visuals);
response
}
}
Expand Down Expand Up @@ -360,7 +360,7 @@ impl Widget for RadioButton {
});
}

text.paint(ui.painter(), text_pos, visuals);
text.paint_with_visuals(ui.painter(), text_pos, visuals);
response
}
}
Expand Down
6 changes: 5 additions & 1 deletion egui/src/widgets/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ impl Widget for ProgressBar {
let text_color = visuals
.override_text_color
.unwrap_or(visuals.selection.stroke.color);
galley.paint_with_color(&ui.painter().sub_region(outer_rect), text_pos, text_color);
galley.paint_with_fallback_color(
&ui.painter().sub_region(outer_rect),
text_pos,
text_color,
);
}

response
Expand Down
2 changes: 1 addition & 1 deletion egui/src/widgets/selected_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Widget for SelectableLabel {
.rect(rect, corner_radius, visuals.bg_fill, visuals.bg_stroke);
}

text.paint(ui.painter(), text_pos, &visuals);
text.paint_with_visuals(ui.painter(), text_pos, &visuals);
response
}
}
17 changes: 8 additions & 9 deletions egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use super::{CCursorRange, CursorRange, TextEditOutput, TextEditState};
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct TextEdit<'t> {
text: &'t mut dyn TextBuffer,
hint_text: String,
hint_text: WidgetText,
id: Option<Id>,
id_source: Option<Id>,
text_style: Option<TextStyle>,
Expand Down Expand Up @@ -127,9 +127,8 @@ impl<'t> TextEdit<'t> {
}

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

Expand Down Expand Up @@ -512,12 +511,12 @@ impl<'t> TextEdit<'t> {

if text.as_ref().is_empty() && !hint_text.is_empty() {
let hint_text_color = ui.visuals().weak_text_color();
let galley = ui.fonts().layout_job(if multiline {
LayoutJob::simple(hint_text, text_style, hint_text_color, desired_size.x)
let galley = if multiline {
hint_text.into_galley(ui, Some(true), desired_size.x, text_style)
} else {
LayoutJob::simple_singleline(hint_text, text_style, hint_text_color)
});
painter.galley(response.rect.min, galley);
hint_text.into_galley(ui, Some(false), f32::INFINITY, text_style)
};
galley.paint_with_fallback_color(&painter, response.rect.min, hint_text_color);
}

if ui.memory().has_focus(id) {
Expand Down

0 comments on commit f607dd0

Please sign in to comment.