Browse Source

Refactor: remove extra tooltip id salt (#4594)

pull/4595/head
Emil Ernerfeldt 5 months ago
committed by GitHub
parent
commit
d4ac91ab6d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 29
      crates/egui/src/containers/popup.rs
  2. 22
      crates/egui/src/response.rs

29
crates/egui/src/containers/popup.rs

@ -25,10 +25,10 @@ use crate::*;
/// ```
pub fn show_tooltip<R>(
ctx: &Context,
id: Id,
widget_id: Id,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> Option<R> {
show_tooltip_at_pointer(ctx, id, add_contents)
show_tooltip_at_pointer(ctx, widget_id, add_contents)
}
/// Show a tooltip at the current pointer position (if any).
@ -50,11 +50,12 @@ pub fn show_tooltip<R>(
/// ```
pub fn show_tooltip_at_pointer<R>(
ctx: &Context,
id: Id,
widget_id: Id,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> Option<R> {
ctx.input(|i| i.pointer.hover_pos())
.map(|pointer_pos| show_tooltip_at(ctx, id, pointer_pos + vec2(16.0, 16.0), add_contents))
ctx.input(|i| i.pointer.hover_pos()).map(|pointer_pos| {
show_tooltip_at(ctx, widget_id, pointer_pos + vec2(16.0, 16.0), add_contents)
})
}
/// Show a tooltip under the given area.
@ -62,7 +63,7 @@ pub fn show_tooltip_at_pointer<R>(
/// If the tooltip does not fit under the area, it tries to place it above it instead.
pub fn show_tooltip_for<R>(
ctx: &Context,
id: Id,
widget_id: Id,
widget_rect: &Rect,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> R {
@ -70,7 +71,7 @@ pub fn show_tooltip_for<R>(
let allow_placing_below = !is_touch_screen; // There is a finger below.
show_tooltip_at_avoid_dyn(
ctx,
id,
widget_id,
allow_placing_below,
widget_rect,
Box::new(add_contents),
@ -82,13 +83,19 @@ pub fn show_tooltip_for<R>(
/// Returns `None` if the tooltip could not be placed.
pub fn show_tooltip_at<R>(
ctx: &Context,
id: Id,
widget_id: Id,
suggested_position: Pos2,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> R {
let allow_placing_below = true;
let rect = Rect::from_center_size(suggested_position, Vec2::ZERO);
show_tooltip_at_avoid_dyn(ctx, id, allow_placing_below, &rect, Box::new(add_contents))
show_tooltip_at_avoid_dyn(
ctx,
widget_id,
allow_placing_below,
&rect,
Box::new(add_contents),
)
}
fn show_tooltip_at_avoid_dyn<'c, R>(
@ -212,8 +219,8 @@ fn find_tooltip_position(
/// }
/// # });
/// ```
pub fn show_tooltip_text(ctx: &Context, id: Id, text: impl Into<WidgetText>) -> Option<()> {
show_tooltip(ctx, id, |ui| {
pub fn show_tooltip_text(ctx: &Context, widget_id: Id, text: impl Into<WidgetText>) -> Option<()> {
show_tooltip(ctx, widget_id, |ui| {
crate::widgets::Label::new(text).ui(ui);
})
}

22
crates/egui/src/response.rs

@ -531,12 +531,7 @@ impl Response {
/// Show this UI when hovering if the widget is disabled.
pub fn on_disabled_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if !self.enabled && self.should_show_hover_ui() {
crate::containers::show_tooltip_for(
&self.ctx,
self.id.with("__tooltip"),
&self.rect,
add_contents,
);
crate::containers::show_tooltip_for(&self.ctx, self.id, &self.rect, add_contents);
}
self
}
@ -544,11 +539,7 @@ impl Response {
/// Like `on_hover_ui`, but show the ui next to cursor.
pub fn on_hover_ui_at_pointer(self, add_contents: impl FnOnce(&mut Ui)) -> Self {
if self.enabled && self.should_show_hover_ui() {
crate::containers::show_tooltip_at_pointer(
&self.ctx,
self.id.with("__tooltip"),
add_contents,
);
crate::containers::show_tooltip_at_pointer(&self.ctx, self.id, add_contents);
}
self
}
@ -557,12 +548,7 @@ impl Response {
///
/// This can be used to give attention to a widget during a tutorial.
pub fn show_tooltip_ui(&self, add_contents: impl FnOnce(&mut Ui)) {
crate::containers::show_tooltip_for(
&self.ctx,
self.id.with("__tooltip"),
&self.rect,
add_contents,
);
crate::containers::show_tooltip_for(&self.ctx, self.id, &self.rect, add_contents);
}
/// Always show this tooltip, even if disabled and the user isn't hovering it.
@ -576,7 +562,7 @@ impl Response {
/// Was the tooltip open last frame?
pub fn is_tooltip_open(&self) -> bool {
crate::popup::was_tooltip_open_last_frame(&self.ctx, self.id.with("__tooltip"))
crate::popup::was_tooltip_open_last_frame(&self.ctx, self.id)
}
fn should_show_hover_ui(&self) -> bool {

Loading…
Cancel
Save