dear_imgui/widget/
tooltip.rs1#![allow(
2 clippy::cast_possible_truncation,
3 clippy::cast_sign_loss,
4 clippy::as_conversions
5)]
6use crate::input::MouseButton;
7use crate::sys;
8use crate::ui::Ui;
9
10impl Ui {
12 #[doc(alias = "BeginTooltip", alias = "EndTooltip")]
30 pub fn tooltip<F: FnOnce()>(&self, f: F) {
31 if let Some(_token) = self.begin_tooltip() {
32 f();
33 }
34 }
35
36 #[doc(alias = "BeginTooltip")]
40 pub fn begin_tooltip(&self) -> Option<TooltipToken<'_>> {
41 if unsafe { sys::igBeginTooltip() } {
42 Some(TooltipToken::new(self))
43 } else {
44 None
45 }
46 }
47
48 #[doc(alias = "BeginTooltip", alias = "EndTooltip", alias = "SetTooltip")]
62 pub fn tooltip_text(&self, text: impl AsRef<str>) {
63 self.tooltip(|| self.text(text));
64 }
65
66 #[doc(alias = "SetTooltip")]
69 pub fn set_tooltip(&self, text: impl AsRef<str>) {
70 let text_ptr = self.scratch_txt(text);
71 unsafe {
72 sys::igSetTooltip(text_ptr);
73 }
74 }
75
76 #[doc(alias = "SetTooltip")]
78 pub fn set_tooltip_formatted(&self, text: impl AsRef<str>) {
79 self.set_tooltip(text);
80 }
81
82 #[doc(alias = "SetItemTooltip")]
85 pub fn set_item_tooltip(&self, text: impl AsRef<str>) {
86 let text_ptr = self.scratch_txt(text);
87 unsafe { sys::igSetItemTooltip(text_ptr) }
88 }
89}
90
91impl Ui {
93 #[doc(alias = "IsItemHovered")]
96 pub fn is_item_hovered(&self) -> bool {
97 unsafe { sys::igIsItemHovered(crate::HoveredFlags::NONE.bits()) }
98 }
99
100 #[doc(alias = "IsItemHovered")]
102 pub fn is_item_hovered_with_flags(&self, flags: crate::HoveredFlags) -> bool {
103 unsafe { sys::igIsItemHovered(flags.bits()) }
104 }
105
106 #[doc(alias = "IsItemActive")]
108 pub fn is_item_active(&self) -> bool {
109 unsafe { sys::igIsItemActive() }
110 }
111
112 #[doc(alias = "IsItemFocused")]
114 pub fn is_item_focused(&self) -> bool {
115 unsafe { sys::igIsItemFocused() }
116 }
117
118 #[doc(alias = "IsItemClicked")]
120 pub fn is_item_clicked(&self) -> bool {
121 unsafe { sys::igIsItemClicked(crate::input::MouseButton::Left as i32) }
122 }
123
124 #[doc(alias = "IsItemClicked")]
126 pub fn is_item_clicked_with_button(&self, mouse_button: MouseButton) -> bool {
127 unsafe { sys::igIsItemClicked(mouse_button as i32) }
128 }
129
130 #[doc(alias = "IsItemVisible")]
132 pub fn is_item_visible(&self) -> bool {
133 unsafe { sys::igIsItemVisible() }
134 }
135
136 #[doc(alias = "IsItemActivated")]
138 pub fn is_item_activated(&self) -> bool {
139 unsafe { sys::igIsItemActivated() }
140 }
141
142 #[doc(alias = "IsItemDeactivated")]
144 pub fn is_item_deactivated(&self) -> bool {
145 unsafe { sys::igIsItemDeactivated() }
146 }
147
148 #[doc(alias = "IsItemDeactivatedAfterEdit")]
150 pub fn is_item_deactivated_after_edit(&self) -> bool {
151 unsafe { sys::igIsItemDeactivatedAfterEdit() }
152 }
153
154 #[doc(alias = "IsAnyItemActive")]
156 pub fn is_any_item_active(&self) -> bool {
157 unsafe { sys::igIsAnyItemActive() }
158 }
159
160 #[doc(alias = "IsAnyItemFocused")]
162 pub fn is_any_item_focused(&self) -> bool {
163 unsafe { sys::igIsAnyItemFocused() }
164 }
165
166 #[doc(alias = "IsAnyItemHovered")]
168 pub fn is_any_item_hovered(&self) -> bool {
169 unsafe { sys::igIsAnyItemHovered() }
170 }
171
172 #[doc(alias = "GetItemRectMin", alias = "GetItemRectMax")]
174 pub fn item_rect(&self) -> ([f32; 2], [f32; 2]) {
175 unsafe {
176 let mut min = sys::ImVec2 { x: 0.0, y: 0.0 };
177 let mut max = sys::ImVec2 { x: 0.0, y: 0.0 };
178 sys::igGetItemRectMin(&mut min);
179 sys::igGetItemRectMax(&mut max);
180 ([min.x, min.y], [max.x, max.y])
181 }
182 }
183
184 #[doc(alias = "GetItemRectSize")]
186 pub fn item_rect_size(&self) -> [f32; 2] {
187 unsafe {
188 let mut size = sys::ImVec2 { x: 0.0, y: 0.0 };
189 sys::igGetItemRectSize(&mut size);
190 [size.x, size.y]
191 }
192 }
193}
194
195#[must_use]
197pub struct TooltipToken<'ui> {
198 ui: &'ui Ui,
199}
200
201impl<'ui> TooltipToken<'ui> {
202 fn new(ui: &'ui Ui) -> Self {
204 TooltipToken { ui }
205 }
206
207 pub fn end(self) {
209 }
211}
212
213impl<'ui> Drop for TooltipToken<'ui> {
214 fn drop(&mut self) {
215 unsafe {
216 sys::igEndTooltip();
217 }
218 }
219}