1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 14:49:06 -04:00

Add Popup and Tooltip, unifying the previous behaviours (#5713)

This introduces new `Tooltip` and `Popup` structs that unify and extend
the old popups and tooltips.

`Popup` handles the positioning and optionally stores state on whether
the popup is open (for click based popups like `ComboBox`, menus,
context menus).
`Tooltip` is based on `Popup` and handles state of whether the tooltip
should be shown (which turns out to be quite complex to handles all the
edge cases).

Both `Popup` and `Tooltip` can easily be constructed from a `Response`
and then customized via builder methods.

This also introduces `PositionAlign`, for aligning something outside of
a `Rect` (in contrast to `Align2` for aligning inside a `Rect`). But I
don't like the name, any suggestions? Inspired by [mui's tooltip
positioning](https://mui.com/material-ui/react-tooltip/#positioned-tooltips).

* Part of #4607 
* [x] I have followed the instructions in the PR template

TODOs:
- [x] Automatic tooltip positioning based on available space
- [x] Review / fix / remove all code TODOs 
- [x] ~Update the helper fns on `Response` to be consistent in naming
and parameters (Some use tooltip, some hover_ui, some take &self, some
take self)~ actually, I think the naming and parameter make sense on
second thought
- [x] Make sure all old code is marked deprecated

For discussion during review:
- the following check in `show_tooltip_for` still necessary?:
```rust
     let is_touch_screen = ctx.input(|i| i.any_touches());
     let allow_placing_below = !is_touch_screen; // There is a finger below. TODO: Needed?
```
This commit is contained in:
lucasmerlin
2025-02-18 15:53:07 +01:00
committed by GitHub
parent 66c73b9cbf
commit a8e98d3f9b
22 changed files with 1738 additions and 715 deletions

View File

@@ -1,7 +1,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui::{popup_below_widget, CentralPanel, ComboBox, Id, PopupCloseBehavior};
use eframe::egui::{CentralPanel, ComboBox, Popup, PopupCloseBehavior};
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
@@ -42,23 +42,14 @@ impl eframe::App for MyApp {
ui.label("PopupCloseBehavior::IgnoreClicks popup");
let response = ui.button("Open");
let popup_id = Id::new("popup_id");
if response.clicked() {
ui.memory_mut(|mem| mem.toggle_popup(popup_id));
}
popup_below_widget(
ui,
popup_id,
&response,
PopupCloseBehavior::IgnoreClicks,
|ui| {
Popup::menu(&response)
.close_behavior(PopupCloseBehavior::IgnoreClicks)
.show(|ui| {
ui.set_min_width(310.0);
ui.label("This popup will be open until you press the button again");
ui.checkbox(&mut self.checkbox, "Checkbox");
},
);
});
});
}
}