1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

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

@@ -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 {