Add TextEdit::hint_text for showing a weak hint text when empty
This commit is contained in:
parent
4e7e128b2b
commit
0f37b009d6
|
|
@ -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 support for secondary and middle mouse buttons.
|
||||||
* Add `Label` methods for code, strong, strikethrough, underline and italics.
|
* Add `Label` methods for code, strong, strikethrough, underline and italics.
|
||||||
* Add `ui.group(|ui| { … })` to visually group some widgets within a frame
|
* 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.
|
* `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 `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()`.
|
* 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 🔧
|
### 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).
|
* `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() {`.
|
* 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.
|
* `Response::active` is now gone. You can use `response.dragged()` or `response.clicked()` instead.
|
||||||
|
|
|
||||||
|
|
@ -362,14 +362,14 @@ impl Widgets {
|
||||||
noninteractive: WidgetVisuals {
|
noninteractive: WidgetVisuals {
|
||||||
bg_stroke: Stroke::new(1.0, Color32::from_gray(65)), // window outline
|
bg_stroke: Stroke::new(1.0, Color32::from_gray(65)), // window outline
|
||||||
bg_fill: Color32::from_gray(30), // window background
|
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,
|
corner_radius: 4.0,
|
||||||
expansion: 0.0,
|
expansion: 0.0,
|
||||||
},
|
},
|
||||||
disabled: WidgetVisuals {
|
disabled: WidgetVisuals {
|
||||||
bg_fill: Color32::from_gray(40), // Should look grayed out
|
bg_fill: Color32::from_gray(40), // Should look grayed out
|
||||||
bg_stroke: Stroke::new(1.0, Color32::from_gray(70)),
|
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,
|
corner_radius: 4.0,
|
||||||
expansion: 0.0,
|
expansion: 0.0,
|
||||||
},
|
},
|
||||||
|
|
@ -402,14 +402,14 @@ impl Widgets {
|
||||||
noninteractive: WidgetVisuals {
|
noninteractive: WidgetVisuals {
|
||||||
bg_stroke: Stroke::new(1.0, Color32::from_gray(180)), // window outline
|
bg_stroke: Stroke::new(1.0, Color32::from_gray(180)), // window outline
|
||||||
bg_fill: Color32::from_gray(220), // window background
|
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,
|
corner_radius: 4.0,
|
||||||
expansion: 0.0,
|
expansion: 0.0,
|
||||||
},
|
},
|
||||||
disabled: WidgetVisuals {
|
disabled: WidgetVisuals {
|
||||||
bg_fill: Color32::from_gray(215), // Should look grayed out
|
bg_fill: Color32::from_gray(215), // Should look grayed out
|
||||||
bg_stroke: Stroke::new(1.0, Color32::from_gray(185)),
|
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,
|
corner_radius: 4.0,
|
||||||
expansion: 0.0,
|
expansion: 0.0,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -123,6 +123,7 @@ impl CCursorPair {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TextEdit<'t> {
|
pub struct TextEdit<'t> {
|
||||||
text: &'t mut String,
|
text: &'t mut String,
|
||||||
|
hint_text: String,
|
||||||
id: Option<Id>,
|
id: Option<Id>,
|
||||||
id_source: Option<Id>,
|
id_source: Option<Id>,
|
||||||
text_style: Option<TextStyle>,
|
text_style: Option<TextStyle>,
|
||||||
|
|
@ -144,6 +145,7 @@ impl<'t> TextEdit<'t> {
|
||||||
pub fn singleline(text: &'t mut String) -> Self {
|
pub fn singleline(text: &'t mut String) -> Self {
|
||||||
TextEdit {
|
TextEdit {
|
||||||
text,
|
text,
|
||||||
|
hint_text: Default::default(),
|
||||||
id: None,
|
id: None,
|
||||||
id_source: None,
|
id_source: None,
|
||||||
text_style: None,
|
text_style: None,
|
||||||
|
|
@ -160,6 +162,7 @@ impl<'t> TextEdit<'t> {
|
||||||
pub fn multiline(text: &'t mut String) -> Self {
|
pub fn multiline(text: &'t mut String) -> Self {
|
||||||
TextEdit {
|
TextEdit {
|
||||||
text,
|
text,
|
||||||
|
hint_text: Default::default(),
|
||||||
id: None,
|
id: None,
|
||||||
id_source: None,
|
id_source: None,
|
||||||
text_style: None,
|
text_style: None,
|
||||||
|
|
@ -183,6 +186,12 @@ impl<'t> TextEdit<'t> {
|
||||||
self
|
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 {
|
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
||||||
self.text_style = Some(text_style);
|
self.text_style = Some(text_style);
|
||||||
self
|
self
|
||||||
|
|
@ -268,6 +277,7 @@ impl<'t> TextEdit<'t> {
|
||||||
fn content_ui(self, ui: &mut Ui) -> Response {
|
fn content_ui(self, ui: &mut Ui) -> Response {
|
||||||
let TextEdit {
|
let TextEdit {
|
||||||
text,
|
text,
|
||||||
|
hint_text,
|
||||||
id,
|
id,
|
||||||
id_source,
|
id_source,
|
||||||
text_style,
|
text_style,
|
||||||
|
|
@ -511,6 +521,18 @@ impl<'t> TextEdit<'t> {
|
||||||
ui.painter()
|
ui.painter()
|
||||||
.galley(response.rect.min, galley, text_style, text_color);
|
.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);
|
ui.memory().text_edit.insert(id, state);
|
||||||
|
|
||||||
Response {
|
Response {
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ impl Default for WidgetGallery {
|
||||||
boolean: false,
|
boolean: false,
|
||||||
radio: Enum::First,
|
radio: Enum::First,
|
||||||
scalar: 42.0,
|
scalar: 42.0,
|
||||||
string: "Hello World!".to_owned(),
|
string: Default::default(),
|
||||||
color: egui::Color32::LIGHT_BLUE.linear_multiply(0.5),
|
color: egui::Color32::LIGHT_BLUE.linear_multiply(0.5),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,7 +66,7 @@ impl super::View for WidgetGallery {
|
||||||
ui.end_row();
|
ui.end_row();
|
||||||
|
|
||||||
ui.label("Text Input:");
|
ui.label("Text Input:");
|
||||||
ui.text_edit_singleline(string);
|
ui.add(egui::TextEdit::singleline(string).hint_text("Write something here"));
|
||||||
ui.end_row();
|
ui.end_row();
|
||||||
|
|
||||||
ui.label("Checkbox:");
|
ui.label("Checkbox:");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue