Rename tooltip_text to on_hover_text

This commit is contained in:
Emil Ernerfeldt 2020-10-01 22:53:11 +02:00
parent 5cba44eaa8
commit 7a71ac1a95
15 changed files with 55 additions and 39 deletions

View file

@ -5,7 +5,7 @@ TODO-list for the Egui project. If you looking for something to do, look here.
* Widgets * Widgets
* [ ] Tooltips: * [ ] Tooltips:
* [ ] Tooltip widget: Something that looks like this: (?) :that shows text on hover. * [ ] Tooltip widget: Something that looks like this: (?) :that shows text on hover.
* [ ] ui.info_button().tooltip_text("More info here"); * [ ] ui.info_button().on_hover_text("More info here");
* [ ] Allow adding multiple tooltips to the same widget, showing them all one after the other. * [ ] Allow adding multiple tooltips to the same widget, showing them all one after the other.
* [ ] Text input * [ ] Text input
* [x] Input * [x] Input

View file

@ -553,7 +553,7 @@ impl Context {
if ui if ui
.add(Button::new("Reset all")) .add(Button::new("Reset all"))
.tooltip_text("Reset all Egui state") .on_hover_text("Reset all Egui state")
.clicked .clicked
{ {
*self.memory() = Default::default(); *self.memory() = Default::default();
@ -629,9 +629,9 @@ impl paint::PaintOptions {
impl PaintStats { impl PaintStats {
pub fn ui(&self, ui: &mut Ui) { pub fn ui(&self, ui: &mut Ui) {
ui.add(label!("Jobs: {}", self.num_jobs)) ui.add(label!("Jobs: {}", self.num_jobs))
.tooltip_text("Number of separate clip rectangles"); .on_hover_text("Number of separate clip rectangles");
ui.add(label!("Primitives: {}", self.num_primitives)) ui.add(label!("Primitives: {}", self.num_primitives))
.tooltip_text("Boxes, circles, text areas etc"); .on_hover_text("Boxes, circles, text areas etc");
ui.add(label!("Vertices: {}", self.num_vertices)); ui.add(label!("Vertices: {}", self.num_vertices));
ui.add(label!("Triangles: {}", self.num_triangles)); ui.add(label!("Triangles: {}", self.num_triangles));
} }

View file

@ -83,7 +83,7 @@ impl FrameHistory {
"Mean CPU usage per frame: {:.2} ms / frame", "Mean CPU usage per frame: {:.2} ms / frame",
1e3 * self.frame_times.average().unwrap_or_default() 1e3 * self.frame_times.average().unwrap_or_default()
)) ))
.tooltip_text( .on_hover_text(
"Includes Egui layout and tesselation time.\n\ "Includes Egui layout and tesselation time.\n\
Does not include GPU usage, nor overhead for sending data to GPU.", Does not include GPU usage, nor overhead for sending data to GPU.",
); );
@ -366,9 +366,9 @@ impl DemoApp {
let run_mode = &mut self.run_mode; let run_mode = &mut self.run_mode;
ui.label("Run mode:"); ui.label("Run mode:");
ui.radio_value("Continuous", run_mode, RunMode::Continuous) ui.radio_value("Continuous", run_mode, RunMode::Continuous)
.tooltip_text("Repaint everything each frame"); .on_hover_text("Repaint everything each frame");
ui.radio_value("Reactive", run_mode, RunMode::Reactive) ui.radio_value("Reactive", run_mode, RunMode::Reactive)
.tooltip_text("Repaint when there are animations or input (e.g. mouse movement)"); .on_hover_text("Repaint when there are animations or input (e.g. mouse movement)");
}); });
} }
} }
@ -471,7 +471,7 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, env: &DemoEnvironment)
} }
if ui if ui
.add(Button::new("Clear entire Egui memory")) .add(Button::new("Clear entire Egui memory"))
.tooltip_text("Forget scroll, collapsibles etc") .on_hover_text("Forget scroll, collapsibles etc")
.clicked .clicked
{ {
*ui.ctx().memory() = Default::default(); *ui.ctx().memory() = Default::default();

View file

@ -78,7 +78,7 @@ impl ColorTest {
let texel_offset = 0.5 / (g.0.len() as f32); let texel_offset = 0.5 / (g.0.len() as f32);
let uv = Rect::from_min_max(pos2(texel_offset, 0.0), pos2(1.0 - texel_offset, 1.0)); let uv = Rect::from_min_max(pos2(texel_offset, 0.0), pos2(1.0 - texel_offset, 1.0));
ui.add(Image::new(tex, GRADIENT_SIZE).tint(vertex_color).uv(uv)) ui.add(Image::new(tex, GRADIENT_SIZE).tint(vertex_color).uv(uv))
.tooltip_text(format!("A texture that is {} texels wide", g.0.len())); .on_hover_text(format!("A texture that is {} texels wide", g.0.len()));
ui.label("GPU result"); ui.label("GPU result");
}); });
}); });
@ -233,7 +233,7 @@ impl ColorTest {
let texel_offset = 0.5 / (gradient.0.len() as f32); let texel_offset = 0.5 / (gradient.0.len() as f32);
let uv = Rect::from_min_max(pos2(texel_offset, 0.0), pos2(1.0 - texel_offset, 1.0)); let uv = Rect::from_min_max(pos2(texel_offset, 0.0), pos2(1.0 - texel_offset, 1.0));
ui.add(Image::new(tex, GRADIENT_SIZE).bg_fill(bg_fill).uv(uv)) ui.add(Image::new(tex, GRADIENT_SIZE).bg_fill(bg_fill).uv(uv))
.tooltip_text(format!( .on_hover_text(format!(
"A texture that is {} texels wide", "A texture that is {} texels wide",
gradient.0.len() gradient.0.len()
)); ));
@ -246,7 +246,7 @@ impl ColorTest {
return; return;
} }
ui.horizontal(|ui| { ui.horizontal(|ui| {
vertex_gradient(ui, bg_fill, gradient).tooltip_text(format!( vertex_gradient(ui, bg_fill, gradient).on_hover_text(format!(
"A triangle mesh that is {} vertices wide", "A triangle mesh that is {} vertices wide",
gradient.0.len() gradient.0.len()
)); ));

View file

@ -415,7 +415,7 @@ impl LayoutDemo {
} }
if ui if ui
.add(RadioButton::new(self.align == None, "Justified")) .add(RadioButton::new(self.align == None, "Justified"))
.tooltip_text("Try to fill full width/height (e.g. buttons)") .on_hover_text("Try to fill full width/height (e.g. buttons)")
.clicked .clicked
{ {
self.align = None; self.align = None;

View file

@ -75,7 +75,7 @@ pub fn demo(ui: &mut Ui, on: &mut bool) {
let url = format!("https://github.com/emilk/egui/blob/master/{}", file!()); let url = format!("https://github.com/emilk/egui/blob/master/{}", file!());
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Like this toggle switch:"); ui.label("Like this toggle switch:");
toggle(ui, on).tooltip_text("Click to toggle"); toggle(ui, on).on_hover_text("Click to toggle");
ui.add(Hyperlink::new(url).text("(source code)")); ui.add(Hyperlink::new(url).text("(source code)"));
}); });
} }

View file

@ -53,13 +53,22 @@ impl Widgets {
ui.style_mut().spacing.item_spacing.x = 0.0; ui.style_mut().spacing.item_spacing.x = 0.0;
ui.add(label!("Text can have ").text_color(srgba(110, 255, 110, 255))); ui.add(label!("Text can have ").text_color(srgba(110, 255, 110, 255)));
ui.add(label!("color ").text_color(srgba(128, 140, 255, 255))); ui.add(label!("color ").text_color(srgba(128, 140, 255, 255)));
ui.add(label!("and tooltips")).tooltip_text( ui.add(label!("and tooltips.")).on_hover_text(
"This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.", "This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.",
); );
}); });
ui.add(label!("Tooltips can be more than just simple text."))
.on_hover_ui(|ui| {
ui.heading("The name of the tooltip");
ui.horizontal(|ui| {
ui.label("This tooltip was created with");
ui.monospace(".on_hover_ui(...)");
});
let _ = ui.button("A button you can never press");
});
ui.add(label!("Some non-latin characters: Ευρηκα τ = 2×π")) ui.add(label!("Ευρηκα! τ = 2×π"))
.tooltip_text("The current font supports only a few non-latin characters and Egui does not currently support right-to-left text."); .on_hover_text("The current font supports only a few non-latin characters and Egui does not currently support right-to-left text.");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.radio_value("First", &mut self.radio, Enum::First); ui.radio_value("First", &mut self.radio, Enum::First);
@ -78,7 +87,7 @@ impl Widgets {
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui if ui
.add(Button::new("Click me").enabled(self.button_enabled)) .add(Button::new("Click me").enabled(self.button_enabled))
.tooltip_text("This will just increase a counter.") .on_hover_text("This will just increase a counter.")
.clicked .clicked
{ {
self.count += 1; self.count += 1;
@ -127,7 +136,7 @@ impl Widgets {
.multiline(false) .multiline(false)
.id_source("single line"), .id_source("single line"),
); );
}); // TODO: .tooltip_text("Enter text to edit me") }); // TODO: .on_hover_text("Enter text to edit me")
ui.add(label!("Multiline text input:")); ui.add(label!("Multiline text input:"));
ui.add(TextEdit::new(&mut self.multiline_text_input).id_source("multiline")); ui.add(TextEdit::new(&mut self.multiline_text_input).id_source("multiline"));

View file

@ -349,12 +349,12 @@ impl RawInput {
ui.label(format!("scroll_delta: {:?} points", scroll_delta)); ui.label(format!("scroll_delta: {:?} points", scroll_delta));
ui.label(format!("screen_size: {:?} points", screen_size)); ui.label(format!("screen_size: {:?} points", screen_size));
ui.label(format!("pixels_per_point: {:?}", pixels_per_point)) ui.label(format!("pixels_per_point: {:?}", pixels_per_point))
.tooltip_text( .on_hover_text(
"Also called HDPI factor.\nNumber of physical pixels per each logical pixel.", "Also called HDPI factor.\nNumber of physical pixels per each logical pixel.",
); );
ui.label(format!("time: {:.3} s", time)); ui.label(format!("time: {:.3} s", time));
ui.label(format!("events: {:?}", events)) ui.label(format!("events: {:?}", events))
.tooltip_text("key presses etc"); .on_hover_text("key presses etc");
} }
} }
@ -393,7 +393,7 @@ impl InputState {
)); ));
ui.label(format!("expected dt: {:.1} ms", 1e3 * predicted_dt)); ui.label(format!("expected dt: {:.1} ms", 1e3 * predicted_dt));
ui.label(format!("events: {:?}", events)) ui.label(format!("events: {:?}", events))
.tooltip_text("key presses etc"); .on_hover_text("key presses etc");
} }
} }

View file

@ -336,7 +336,7 @@ impl Spacing {
ui_slider_vec2(ui, window_padding, 0.0..=10.0, "window_padding"); ui_slider_vec2(ui, window_padding, 0.0..=10.0, "window_padding");
ui_slider_vec2(ui, button_padding, 0.0..=10.0, "button_padding"); ui_slider_vec2(ui, button_padding, 0.0..=10.0, "button_padding");
ui_slider_vec2(ui, interact_size, 0.0..=60.0, "interact_size") ui_slider_vec2(ui, interact_size, 0.0..=60.0, "interact_size")
.tooltip_text("Minimum size of an interactive widget"); .on_hover_text("Minimum size of an interactive widget");
ui.add(Slider::f32(indent, 0.0..=100.0).text("indent")); ui.add(Slider::f32(indent, 0.0..=100.0).text("indent"));
ui.add(Slider::f32(slider_width, 0.0..=1000.0).text("slider_width")); ui.add(Slider::f32(slider_width, 0.0..=1000.0).text("slider_width"));
ui.add(Slider::f32(icon_width, 0.0..=60.0).text("icon_width")); ui.add(Slider::f32(icon_width, 0.0..=60.0).text("icon_width"));
@ -441,7 +441,7 @@ impl Stroke {
let Self { width, color } = self; let Self { width, color } = self;
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add(DragValue::f32(width).speed(0.1).range(0.0..=5.0)) ui.add(DragValue::f32(width).speed(0.1).range(0.0..=5.0))
.tooltip_text("Width"); .on_hover_text("Width");
ui.color_edit_button_srgba(color); ui.color_edit_button_srgba(color);
ui.label(text); ui.label(text);

View file

@ -95,19 +95,25 @@ impl std::fmt::Debug for Response {
} }
impl Response { impl Response {
pub fn tooltip(self, add_contents: impl FnOnce(&mut Ui)) -> Self { /// Show this UI if the item was hovered (i.e. a tooltip)
pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.hovered { if self.hovered {
crate::containers::show_tooltip(&self.ctx, add_contents); crate::containers::show_tooltip(&self.ctx, add_contents);
} }
self self
} }
/// Show this text if the item was hovered /// Show this text if the item was hovered (i.e. a tooltip)
pub fn tooltip_text(self, text: impl Into<String>) -> Self { pub fn on_hover_text(self, text: impl Into<String>) -> Self {
self.tooltip(|ui| { self.on_hover_ui(|ui| {
ui.add(crate::widgets::Label::new(text)); ui.add(crate::widgets::Label::new(text));
}) })
} }
#[deprecated = "Deprecated 2020-10-01: use `on_hover_text` instead."]
pub fn tooltip_text(self, text: impl Into<String>) -> Self {
self.on_hover_text(text)
}
} }
impl Response { impl Response {

View file

@ -489,6 +489,7 @@ impl Ui {
} }
/// Shortcut for `add(Button::new(text))` /// Shortcut for `add(Button::new(text))`
#[must_use = "You should check if the user clicked this with `if ui.button(...).clicked { ... } "]
pub fn button(&mut self, text: impl Into<String>) -> Response { pub fn button(&mut self, text: impl Into<String>) -> Response {
self.add(Button::new(text)) self.add(Button::new(text))
} }

View file

@ -194,21 +194,21 @@ fn color_picker_hsvag_2d(ui: &mut Ui, hsva: &mut HsvaGamma) {
ui.style().spacing.interact_size.y * 2.0, ui.style().spacing.interact_size.y * 2.0,
); );
show_color(ui, *hsva, current_color_size).tooltip_text("Current color"); show_color(ui, *hsva, current_color_size).on_hover_text("Current color");
show_color(ui, HsvaGamma { a: 1.0, ..*hsva }, current_color_size) show_color(ui, HsvaGamma { a: 1.0, ..*hsva }, current_color_size)
.tooltip_text("Current color (opaque)"); .on_hover_text("Current color (opaque)");
let opaque = HsvaGamma { a: 1.0, ..*hsva }; let opaque = HsvaGamma { a: 1.0, ..*hsva };
let HsvaGamma { h, s, v, a } = hsva; let HsvaGamma { h, s, v, a } = hsva;
color_slider_2d(ui, h, s, |h, s| HsvaGamma::new(h, s, 1.0, 1.0).into()) color_slider_2d(ui, h, s, |h, s| HsvaGamma::new(h, s, 1.0, 1.0).into())
.tooltip_text("Hue - Saturation"); .on_hover_text("Hue - Saturation");
color_slider_2d(ui, v, s, |v, s| HsvaGamma { v, s, ..opaque }.into()) color_slider_2d(ui, v, s, |v, s| HsvaGamma { v, s, ..opaque }.into())
.tooltip_text("Value - Saturation"); .on_hover_text("Value - Saturation");
color_slider_1d(ui, h, |h| HsvaGamma { h, ..opaque }.into()).tooltip_text("Hue"); color_slider_1d(ui, h, |h| HsvaGamma { h, ..opaque }.into()).on_hover_text("Hue");
color_slider_1d(ui, s, |s| HsvaGamma { s, ..opaque }.into()).tooltip_text("Saturation"); color_slider_1d(ui, s, |s| HsvaGamma { s, ..opaque }.into()).on_hover_text("Saturation");
color_slider_1d(ui, v, |v| HsvaGamma { v, ..opaque }.into()).tooltip_text("Value"); color_slider_1d(ui, v, |v| HsvaGamma { v, ..opaque }.into()).on_hover_text("Value");
color_slider_1d(ui, a, |a| HsvaGamma { a, ..opaque }.into()).tooltip_text("Alpha"); color_slider_1d(ui, a, |a| HsvaGamma { a, ..opaque }.into()).on_hover_text("Alpha");
}); });
} }
@ -220,7 +220,7 @@ fn color_picker_hsva_2d(ui: &mut Ui, hsva: &mut Hsva) {
pub fn color_edit_button_hsva(ui: &mut Ui, hsva: &mut Hsva) -> Response { pub fn color_edit_button_hsva(ui: &mut Ui, hsva: &mut Hsva) -> Response {
let pupup_id = ui.make_position_id().with("popup"); let pupup_id = ui.make_position_id().with("popup");
let button_response = color_button(ui, (*hsva).into()).tooltip_text("Click to edit color"); let button_response = color_button(ui, (*hsva).into()).on_hover_text("Click to edit color");
if button_response.clicked { if button_response.clicked {
ui.memory().toggle_popup(pupup_id); ui.memory().toggle_popup(pupup_id);

View file

@ -138,7 +138,7 @@ impl<'a> Widget for DragValue<'a> {
.sense(Sense::click_and_drag()) .sense(Sense::click_and_drag())
.text_style(TextStyle::Monospace); .text_style(TextStyle::Monospace);
let response = ui.add(button); let response = ui.add(button);
// response.tooltip_text("Drag to edit, click to enter a value"); // TODO: may clash with users own tooltips // response.on_hover_text("Drag to edit, click to enter a value"); // TODO: may clash with users own tooltips
if response.clicked { if response.clicked {
ui.memory().request_kb_focus(kb_edit_id); ui.memory().request_kb_focus(kb_edit_id);
ui.memory().temp_edit_string = None; // Filled in next frame ui.memory().temp_edit_string = None; // Filled in next frame

View file

@ -217,7 +217,7 @@ impl Widget for Hyperlink {
ui.painter() ui.painter()
.galley(response.rect.min, galley, text_style, color); .galley(response.rect.min, galley, text_style, color);
response.tooltip_text(url) response.on_hover_text(url)
} }
} }

View file

@ -257,7 +257,7 @@ impl<'a> Slider<'a> {
.text_style(TextStyle::Monospace) .text_style(TextStyle::Monospace)
.text_color_opt(self.text_color), .text_color_opt(self.text_color),
); );
let response = response.tooltip_text("Click to enter a value"); let response = response.on_hover_text("Click to enter a value");
// let response = ui.interact(response.rect, kb_edit_id, Sense::click()); // let response = ui.interact(response.rect, kb_edit_id, Sense::click());
if response.clicked { if response.clicked {
ui.memory().request_kb_focus(kb_edit_id); ui.memory().request_kb_focus(kb_edit_id);