egui/containers/
old_popup.rs

1//! Old and deprecated API for popups. Use [`Popup`] instead.
2#![allow(deprecated)]
3
4use crate::containers::tooltip::Tooltip;
5use crate::{
6    Align, Context, Id, LayerId, Layout, Popup, PopupAnchor, PopupCloseBehavior, Pos2, Rect,
7    Response, Ui, Widget as _, WidgetText,
8};
9use emath::RectAlign;
10// ----------------------------------------------------------------------------
11
12/// Show a tooltip at the current pointer position (if any).
13///
14/// Most of the time it is easier to use [`Response::on_hover_ui`].
15///
16/// See also [`show_tooltip_text`].
17///
18/// Returns `None` if the tooltip could not be placed.
19///
20/// ```
21/// # egui::__run_test_ui(|ui| {
22/// # #[expect(deprecated)]
23/// if ui.ui_contains_pointer() {
24///     egui::show_tooltip(ui.ctx(), ui.layer_id(), egui::Id::new("my_tooltip"), |ui| {
25///         ui.label("Helpful text");
26///     });
27/// }
28/// # });
29/// ```
30#[deprecated = "Use `egui::Tooltip` instead"]
31pub fn show_tooltip<R>(
32    ctx: &Context,
33    parent_layer: LayerId,
34    widget_id: Id,
35    add_contents: impl FnOnce(&mut Ui) -> R,
36) -> Option<R> {
37    show_tooltip_at_pointer(ctx, parent_layer, widget_id, add_contents)
38}
39
40/// Show a tooltip at the current pointer position (if any).
41///
42/// Most of the time it is easier to use [`Response::on_hover_ui`].
43///
44/// See also [`show_tooltip_text`].
45///
46/// Returns `None` if the tooltip could not be placed.
47///
48/// ```
49/// # egui::__run_test_ui(|ui| {
50/// if ui.ui_contains_pointer() {
51///     egui::show_tooltip_at_pointer(ui.ctx(), ui.layer_id(), egui::Id::new("my_tooltip"), |ui| {
52///         ui.label("Helpful text");
53///     });
54/// }
55/// # });
56/// ```
57#[deprecated = "Use `egui::Tooltip` instead"]
58pub fn show_tooltip_at_pointer<R>(
59    ctx: &Context,
60    parent_layer: LayerId,
61    widget_id: Id,
62    add_contents: impl FnOnce(&mut Ui) -> R,
63) -> Option<R> {
64    Tooltip::always_open(ctx.clone(), parent_layer, widget_id, PopupAnchor::Pointer)
65        .gap(12.0)
66        .show(add_contents)
67        .map(|response| response.inner)
68}
69
70/// Show a tooltip under the given area.
71///
72/// If the tooltip does not fit under the area, it tries to place it above it instead.
73#[deprecated = "Use `egui::Tooltip` instead"]
74pub fn show_tooltip_for<R>(
75    ctx: &Context,
76    parent_layer: LayerId,
77    widget_id: Id,
78    widget_rect: &Rect,
79    add_contents: impl FnOnce(&mut Ui) -> R,
80) -> Option<R> {
81    Tooltip::always_open(ctx.clone(), parent_layer, widget_id, *widget_rect)
82        .show(add_contents)
83        .map(|response| response.inner)
84}
85
86/// Show a tooltip at the given position.
87///
88/// Returns `None` if the tooltip could not be placed.
89#[deprecated = "Use `egui::Tooltip` instead"]
90pub fn show_tooltip_at<R>(
91    ctx: &Context,
92    parent_layer: LayerId,
93    widget_id: Id,
94    suggested_position: Pos2,
95    add_contents: impl FnOnce(&mut Ui) -> R,
96) -> Option<R> {
97    Tooltip::always_open(ctx.clone(), parent_layer, widget_id, suggested_position)
98        .show(add_contents)
99        .map(|response| response.inner)
100}
101
102/// Show some text at the current pointer position (if any).
103///
104/// Most of the time it is easier to use [`Response::on_hover_text`].
105///
106/// See also [`show_tooltip`].
107///
108/// Returns `None` if the tooltip could not be placed.
109///
110/// ```
111/// # egui::__run_test_ui(|ui| {
112/// if ui.ui_contains_pointer() {
113///     egui::show_tooltip_text(ui.ctx(), ui.layer_id(), egui::Id::new("my_tooltip"), "Helpful text");
114/// }
115/// # });
116/// ```
117#[deprecated = "Use `egui::Tooltip` instead"]
118pub fn show_tooltip_text(
119    ctx: &Context,
120    parent_layer: LayerId,
121    widget_id: Id,
122    text: impl Into<WidgetText>,
123) -> Option<()> {
124    show_tooltip(ctx, parent_layer, widget_id, |ui| {
125        crate::widgets::Label::new(text).ui(ui);
126    })
127}
128
129/// Was this tooltip visible last frame?
130#[deprecated = "Use `Tooltip::was_tooltip_open_last_frame` instead"]
131pub fn was_tooltip_open_last_frame(ctx: &Context, widget_id: Id) -> bool {
132    Tooltip::was_tooltip_open_last_frame(ctx, widget_id)
133}
134
135/// Indicate whether a popup will be shown above or below the box.
136#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
137pub enum AboveOrBelow {
138    Above,
139    Below,
140}
141
142/// Helper for [`popup_above_or_below_widget`].
143#[deprecated = "Use `egui::Popup` instead"]
144pub fn popup_below_widget<R>(
145    ui: &Ui,
146    popup_id: Id,
147    widget_response: &Response,
148    close_behavior: PopupCloseBehavior,
149    add_contents: impl FnOnce(&mut Ui) -> R,
150) -> Option<R> {
151    popup_above_or_below_widget(
152        ui,
153        popup_id,
154        widget_response,
155        AboveOrBelow::Below,
156        close_behavior,
157        add_contents,
158    )
159}
160
161/// Shows a popup above or below another widget.
162///
163/// Useful for drop-down menus (combo boxes) or suggestion menus under text fields.
164///
165/// The opened popup will have a minimum width matching its parent.
166///
167/// You must open the popup with [`crate::Memory::open_popup`] or  [`crate::Memory::toggle_popup`].
168///
169/// Returns `None` if the popup is not open.
170///
171/// ```
172/// # egui::__run_test_ui(|ui| {
173/// let response = ui.button("Open popup");
174/// let popup_id = ui.make_persistent_id("my_unique_id");
175/// if response.clicked() {
176///     ui.memory_mut(|mem| mem.toggle_popup(popup_id));
177/// }
178/// let below = egui::AboveOrBelow::Below;
179/// let close_on_click_outside = egui::PopupCloseBehavior::CloseOnClickOutside;
180/// # #[expect(deprecated)]
181/// egui::popup_above_or_below_widget(ui, popup_id, &response, below, close_on_click_outside, |ui| {
182///     ui.set_min_width(200.0); // if you want to control the size
183///     ui.label("Some more info, or things you can select:");
184///     ui.label("…");
185/// });
186/// # });
187/// ```
188#[deprecated = "Use `egui::Popup` instead"]
189pub fn popup_above_or_below_widget<R>(
190    _parent_ui: &Ui,
191    popup_id: Id,
192    widget_response: &Response,
193    above_or_below: AboveOrBelow,
194    close_behavior: PopupCloseBehavior,
195    add_contents: impl FnOnce(&mut Ui) -> R,
196) -> Option<R> {
197    let response = Popup::from_response(widget_response)
198        .layout(Layout::top_down_justified(Align::LEFT))
199        .open_memory(None)
200        .close_behavior(close_behavior)
201        .id(popup_id)
202        .align(match above_or_below {
203            AboveOrBelow::Above => RectAlign::TOP_START,
204            AboveOrBelow::Below => RectAlign::BOTTOM_START,
205        })
206        .width(widget_response.rect.width())
207        .show(|ui| {
208            ui.set_min_width(ui.available_width());
209            add_contents(ui)
210        })?;
211    Some(response.inner)
212}