mirror of
https://github.com/emilk/egui.git
synced 2026-06-28 07:23:13 -04:00
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? ```
56 lines
2.1 KiB
Rust
56 lines
2.1 KiB
Rust
#![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::{CentralPanel, ComboBox, Popup, PopupCloseBehavior};
|
|
|
|
fn main() -> Result<(), eframe::Error> {
|
|
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
|
let options = eframe::NativeOptions::default();
|
|
|
|
eframe::run_native("Popups", options, Box::new(|_| Ok(Box::<MyApp>::default())))
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct MyApp {
|
|
checkbox: bool,
|
|
number: u8,
|
|
numbers: [bool; 10],
|
|
}
|
|
|
|
impl eframe::App for MyApp {
|
|
fn update(&mut self, ctx: &eframe::egui::Context, _frame: &mut eframe::Frame) {
|
|
CentralPanel::default().show(ctx, |ui| {
|
|
ui.label("PopupCloseBehavior::CloseOnClick popup");
|
|
ComboBox::from_label("ComboBox")
|
|
.selected_text(format!("{}", self.number))
|
|
.show_ui(ui, |ui| {
|
|
for num in 0..10 {
|
|
ui.selectable_value(&mut self.number, num, format!("{num}"));
|
|
}
|
|
});
|
|
|
|
ui.label("PopupCloseBehavior::CloseOnClickOutside popup");
|
|
ComboBox::from_label("Ignore Clicks")
|
|
.close_behavior(PopupCloseBehavior::CloseOnClickOutside)
|
|
.selected_text("Select Numbers")
|
|
.show_ui(ui, |ui| {
|
|
ui.label("This popup will be open even if you click the checkboxes");
|
|
for (i, num) in self.numbers.iter_mut().enumerate() {
|
|
ui.checkbox(num, format!("Checkbox {}", i + 1));
|
|
}
|
|
});
|
|
|
|
ui.label("PopupCloseBehavior::IgnoreClicks popup");
|
|
let response = ui.button("Open");
|
|
|
|
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");
|
|
});
|
|
});
|
|
}
|
|
}
|