Skip to content

Commit 06760e1

Browse files
authored
Change API of Tooltip slightly (#7151)
We try to be consistent with our parameter order to reduce surprise for users. I also renamed a few things to clarify what is what
1 parent 699be07 commit 06760e1

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ You can test your code locally by running `./scripts/check.sh`.
3535
There are snapshots test that might need to be updated.
3636
Run the tests with `UPDATE_SNAPSHOTS=true cargo test --workspace --all-features` to update all of them.
3737
If CI keeps complaining about snapshots (which could happen if you don't use macOS, snapshots in CI are currently
38-
rendered with macOS), you can instead run `./scripts/update_snapshots_from_ci.sh` to update your local snapshots from
38+
rendered with macOS), you can instead run `./scripts/update_snapshots_from_ci.sh` to update your local snapshots from
3939
the last CI run of your PR (which will download the `test_results` artefact).
4040
For more info about the tests see [egui_kittest](./crates/egui_kittest/README.md).
4141
Snapshots and other big files are stored with git lfs. See [Working with git lfs](#working-with-git-lfs) for more info.
@@ -125,6 +125,7 @@ While using an immediate mode gui is simple, implementing one is a lot more tric
125125
* Flip `if !condition {} else {}`
126126
* Sets of things should be lexicographically sorted (e.g. crate dependencies in `Cargo.toml`)
127127
* Put each type in their own file, unless they are trivial (e.g. a `struct` with no `impl`)
128+
* Put most generic arguments first (e.g. `Context`), and most specific last
128129
* Break the above rules when it makes sense
129130

130131

crates/egui/src/containers/old_popup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn show_tooltip_at_pointer<R>(
6161
widget_id: Id,
6262
add_contents: impl FnOnce(&mut Ui) -> R,
6363
) -> Option<R> {
64-
Tooltip::new(widget_id, ctx.clone(), PopupAnchor::Pointer, parent_layer)
64+
Tooltip::new(ctx.clone(), parent_layer, widget_id, PopupAnchor::Pointer)
6565
.gap(12.0)
6666
.show(add_contents)
6767
.map(|response| response.inner)
@@ -78,7 +78,7 @@ pub fn show_tooltip_for<R>(
7878
widget_rect: &Rect,
7979
add_contents: impl FnOnce(&mut Ui) -> R,
8080
) -> Option<R> {
81-
Tooltip::new(widget_id, ctx.clone(), *widget_rect, parent_layer)
81+
Tooltip::new(ctx.clone(), parent_layer, widget_id, *widget_rect)
8282
.show(add_contents)
8383
.map(|response| response.inner)
8484
}
@@ -94,7 +94,7 @@ pub fn show_tooltip_at<R>(
9494
suggested_position: Pos2,
9595
add_contents: impl FnOnce(&mut Ui) -> R,
9696
) -> Option<R> {
97-
Tooltip::new(widget_id, ctx.clone(), suggested_position, parent_layer)
97+
Tooltip::new(ctx.clone(), parent_layer, widget_id, suggested_position)
9898
.show(add_contents)
9999
.map(|response| response.inner)
100100
}

crates/egui/src/containers/tooltip.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ use emath::Vec2;
77

88
pub struct Tooltip<'a> {
99
pub popup: Popup<'a>,
10-
layer_id: LayerId,
11-
widget_id: Id,
10+
11+
/// The layer of the parent widget.
12+
parent_layer: LayerId,
13+
14+
/// The id of the widget that owns this tooltip.
15+
parent_widget: Id,
1216
}
1317

1418
impl Tooltip<'_> {
15-
/// Show a tooltip that is always open
19+
/// Show a tooltip that is always open.
1620
pub fn new(
17-
widget_id: Id,
1821
ctx: Context,
22+
parent_layer: LayerId,
23+
parent_widget: Id,
1924
anchor: impl Into<PopupAnchor>,
20-
layer_id: LayerId,
2125
) -> Self {
26+
let width = ctx.style().spacing.tooltip_width;
2227
Self {
23-
// TODO(lucasmerlin): Set width somehow (we're missing context here)
24-
popup: Popup::new(widget_id, ctx, anchor.into(), layer_id)
28+
popup: Popup::new(parent_widget, ctx, anchor.into(), parent_layer)
2529
.kind(PopupKind::Tooltip)
2630
.gap(4.0)
31+
.width(width)
2732
.sense(Sense::hover()),
28-
layer_id,
29-
widget_id,
33+
parent_layer,
34+
parent_widget,
3035
}
3136
}
3237

@@ -39,8 +44,8 @@ impl Tooltip<'_> {
3944
.sense(Sense::hover());
4045
Self {
4146
popup,
42-
layer_id: response.layer_id,
43-
widget_id: response.id,
47+
parent_layer: response.layer_id,
48+
parent_widget: response.id,
4449
}
4550
}
4651

@@ -96,8 +101,8 @@ impl Tooltip<'_> {
96101
pub fn show<R>(self, content: impl FnOnce(&mut crate::Ui) -> R) -> Option<InnerResponse<R>> {
97102
let Self {
98103
mut popup,
99-
layer_id: parent_layer,
100-
widget_id,
104+
parent_layer,
105+
parent_widget,
101106
} = self;
102107

103108
if !popup.is_open() {
@@ -111,19 +116,19 @@ impl Tooltip<'_> {
111116
fs.layers
112117
.entry(parent_layer)
113118
.or_default()
114-
.widget_with_tooltip = Some(widget_id);
119+
.widget_with_tooltip = Some(parent_widget);
115120

116121
fs.tooltips
117122
.widget_tooltips
118-
.get(&widget_id)
123+
.get(&parent_widget)
119124
.copied()
120125
.unwrap_or(PerWidgetTooltipState {
121126
bounding_rect: rect,
122127
tooltip_count: 0,
123128
})
124129
});
125130

126-
let tooltip_area_id = Self::tooltip_id(widget_id, state.tooltip_count);
131+
let tooltip_area_id = Self::tooltip_id(parent_widget, state.tooltip_count);
127132
popup = popup.anchor(state.bounding_rect).id(tooltip_area_id);
128133

129134
let response = popup.show(|ui| {
@@ -144,7 +149,7 @@ impl Tooltip<'_> {
144149
response
145150
.response
146151
.ctx
147-
.pass_state_mut(|fs| fs.tooltips.widget_tooltips.insert(widget_id, state));
152+
.pass_state_mut(|fs| fs.tooltips.widget_tooltips.insert(parent_widget, state));
148153
Self::remember_that_tooltip_was_shown(&response.response.ctx);
149154
}
150155

0 commit comments

Comments
 (0)