Add TextEdit::hint_text for showing a weak hint text when empty

This commit is contained in:
Emil Ernerfeldt 2021-02-03 21:05:50 +01:00
parent 4e7e128b2b
commit 0f37b009d6
4 changed files with 30 additions and 7 deletions

View File

@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Add support for secondary and middle mouse buttons.
* Add `Label` methods for code, strong, strikethrough, underline and italics.
* Add `ui.group(|ui| { … })` to visually group some widgets within a frame
* Text will now wrap at newlines, spaces, dashes, punctuation or in the middle of a words if necessary, in that order of priority.
* Add `TextEdit::hint_text` for showing a weak hint text when empty.
* `egui::popup::popup_below_widget`: show a popup area below another widget.
* Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range.
* Add: `ui.spacing()`, `ui.spacing_mut()`, `ui.visuals()`, `ui.visuals_mut()`.
@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed 🔧
* Text will now wrap at newlines, spaces, dashes, punctuation or in the middle of a words if necessary, in that order of priority.
* `mouse` has be renamed `pointer` everywhere (to make it clear it includes touches too).
* Most parts of `Response` are now methods, so `if ui.button("…").clicked {` is now `if ui.button("…").clicked() {`.
* `Response::active` is now gone. You can use `response.dragged()` or `response.clicked()` instead.

View File

@ -362,14 +362,14 @@ impl Widgets {
noninteractive: WidgetVisuals {
bg_stroke: Stroke::new(1.0, Color32::from_gray(65)), // window outline
bg_fill: Color32::from_gray(30), // window background
fg_stroke: Stroke::new(1.0, Color32::from_gray(160)), // text color
fg_stroke: Stroke::new(1.0, Color32::from_gray(160)), // normal text color
corner_radius: 4.0,
expansion: 0.0,
},
disabled: WidgetVisuals {
bg_fill: Color32::from_gray(40), // Should look grayed out
bg_stroke: Stroke::new(1.0, Color32::from_gray(70)),
fg_stroke: Stroke::new(1.0, Color32::from_gray(140)), // Should look grayed out
fg_stroke: Stroke::new(1.0, Color32::from_gray(110)), // Should look grayed out. Also used for "weak" text color.
corner_radius: 4.0,
expansion: 0.0,
},
@ -402,14 +402,14 @@ impl Widgets {
noninteractive: WidgetVisuals {
bg_stroke: Stroke::new(1.0, Color32::from_gray(180)), // window outline
bg_fill: Color32::from_gray(220), // window background
fg_stroke: Stroke::new(1.0, Color32::from_gray(70)), // text color
fg_stroke: Stroke::new(1.0, Color32::from_gray(70)), // normal text color
corner_radius: 4.0,
expansion: 0.0,
},
disabled: WidgetVisuals {
bg_fill: Color32::from_gray(215), // Should look grayed out
bg_stroke: Stroke::new(1.0, Color32::from_gray(185)),
fg_stroke: Stroke::new(1.0, Color32::from_gray(115)), // Should look grayed out
fg_stroke: Stroke::new(1.0, Color32::from_gray(145)), // Should look grayed out. Also used for "weak" text color.
corner_radius: 4.0,
expansion: 0.0,
},

View File

@ -123,6 +123,7 @@ impl CCursorPair {
#[derive(Debug)]
pub struct TextEdit<'t> {
text: &'t mut String,
hint_text: String,
id: Option<Id>,
id_source: Option<Id>,
text_style: Option<TextStyle>,
@ -144,6 +145,7 @@ impl<'t> TextEdit<'t> {
pub fn singleline(text: &'t mut String) -> Self {
TextEdit {
text,
hint_text: Default::default(),
id: None,
id_source: None,
text_style: None,
@ -160,6 +162,7 @@ impl<'t> TextEdit<'t> {
pub fn multiline(text: &'t mut String) -> Self {
TextEdit {
text,
hint_text: Default::default(),
id: None,
id_source: None,
text_style: None,
@ -183,6 +186,12 @@ impl<'t> TextEdit<'t> {
self
}
/// Show a faint hint text when the text field is empty.
pub fn hint_text(mut self, hint_text: impl Into<String>) -> Self {
self.hint_text = hint_text.into();
self
}
pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = Some(text_style);
self
@ -268,6 +277,7 @@ impl<'t> TextEdit<'t> {
fn content_ui(self, ui: &mut Ui) -> Response {
let TextEdit {
text,
hint_text,
id,
id_source,
text_style,
@ -511,6 +521,18 @@ impl<'t> TextEdit<'t> {
ui.painter()
.galley(response.rect.min, galley, text_style, text_color);
if text.is_empty() && !hint_text.is_empty() {
let font = &ui.fonts()[text_style];
let galley = if multiline {
font.layout_multiline(hint_text, available_width)
} else {
font.layout_single_line(hint_text)
};
let hint_text_color = ui.visuals().weak_text_color();
ui.painter()
.galley(response.rect.min, galley, text_style, hint_text_color);
}
ui.memory().text_edit.insert(id, state);
Response {

View File

@ -21,7 +21,7 @@ impl Default for WidgetGallery {
boolean: false,
radio: Enum::First,
scalar: 42.0,
string: "Hello World!".to_owned(),
string: Default::default(),
color: egui::Color32::LIGHT_BLUE.linear_multiply(0.5),
}
}
@ -66,7 +66,7 @@ impl super::View for WidgetGallery {
ui.end_row();
ui.label("Text Input:");
ui.text_edit_singleline(string);
ui.add(egui::TextEdit::singleline(string).hint_text("Write something here"));
ui.end_row();
ui.label("Checkbox:");