dear_imgui_rs/widget/
tooltip.rs1#![allow(
7 clippy::cast_possible_truncation,
8 clippy::cast_sign_loss,
9 clippy::as_conversions
10)]
11use crate::input::MouseButton;
12use crate::sys;
13use crate::ui::Ui;
14
15impl Ui {
17 #[doc(alias = "BeginTooltip", alias = "EndTooltip")]
35 pub fn tooltip<F: FnOnce()>(&self, f: F) {
36 if let Some(_token) = self.begin_tooltip() {
37 f();
38 }
39 }
40
41 #[doc(alias = "BeginTooltip")]
45 pub fn begin_tooltip(&self) -> Option<TooltipToken<'_>> {
46 if self.run_with_bound_context(|| unsafe { sys::igBeginTooltip() }) {
47 Some(TooltipToken::new(self))
48 } else {
49 None
50 }
51 }
52
53 #[doc(alias = "BeginTooltip", alias = "EndTooltip", alias = "SetTooltip")]
67 pub fn tooltip_text(&self, text: impl AsRef<str>) {
68 self.tooltip(|| self.text(text));
69 }
70
71 #[doc(alias = "SetTooltip")]
75 pub fn set_tooltip(&self, text: impl AsRef<str>) {
76 let s = text.as_ref();
77 self.run_with_bound_context(|| unsafe {
78 if sys::igBeginTooltip() {
80 let begin = s.as_ptr() as *const std::os::raw::c_char;
81 let end = begin.add(s.len());
82 sys::igTextUnformatted(begin, end);
83 sys::igEndTooltip();
84 }
85 });
86 }
87
88 #[doc(alias = "SetTooltip")]
90 pub fn set_tooltip_formatted(&self, text: impl AsRef<str>) {
91 self.set_tooltip(text);
92 }
93
94 #[doc(alias = "SetItemTooltip")]
98 pub fn set_item_tooltip(&self, text: impl AsRef<str>) {
99 let s = text.as_ref();
100 self.run_with_bound_context(|| unsafe {
101 if sys::igBeginItemTooltip() {
103 let begin = s.as_ptr() as *const std::os::raw::c_char;
104 let end = begin.add(s.len());
105 sys::igTextUnformatted(begin, end);
106 sys::igEndTooltip();
107 }
108 });
109 }
110}
111
112impl Ui {
114 #[doc(alias = "IsItemHovered")]
117 pub fn is_item_hovered(&self) -> bool {
118 self.run_with_bound_context(|| unsafe {
119 sys::igIsItemHovered(crate::ItemHoveredFlags::NONE.bits())
120 })
121 }
122
123 #[doc(alias = "IsItemHovered")]
125 pub fn is_item_hovered_with_flags(&self, flags: crate::ItemHoveredFlags) -> bool {
126 crate::utils::validate_item_hovered_flags("Ui::is_item_hovered_with_flags()", flags);
127 self.run_with_bound_context(|| unsafe { sys::igIsItemHovered(flags.bits()) })
128 }
129
130 #[doc(alias = "IsItemActive")]
132 pub fn is_item_active(&self) -> bool {
133 self.run_with_bound_context(|| unsafe { sys::igIsItemActive() })
134 }
135
136 #[doc(alias = "IsItemFocused")]
138 pub fn is_item_focused(&self) -> bool {
139 self.run_with_bound_context(|| unsafe { sys::igIsItemFocused() })
140 }
141
142 #[doc(alias = "IsItemClicked")]
144 pub fn is_item_clicked(&self) -> bool {
145 self.run_with_bound_context(|| unsafe {
146 sys::igIsItemClicked(crate::input::MouseButton::Left as i32)
147 })
148 }
149
150 #[doc(alias = "IsItemClicked")]
152 pub fn is_item_clicked_with_button(&self, mouse_button: MouseButton) -> bool {
153 self.run_with_bound_context(|| unsafe { sys::igIsItemClicked(mouse_button as i32) })
154 }
155
156 #[doc(alias = "IsItemVisible")]
158 pub fn is_item_visible(&self) -> bool {
159 self.run_with_bound_context(|| unsafe { sys::igIsItemVisible() })
160 }
161
162 #[doc(alias = "IsItemActivated")]
164 pub fn is_item_activated(&self) -> bool {
165 self.run_with_bound_context(|| unsafe { sys::igIsItemActivated() })
166 }
167
168 #[doc(alias = "IsItemDeactivated")]
170 pub fn is_item_deactivated(&self) -> bool {
171 self.run_with_bound_context(|| unsafe { sys::igIsItemDeactivated() })
172 }
173
174 #[doc(alias = "IsItemDeactivatedAfterEdit")]
176 pub fn is_item_deactivated_after_edit(&self) -> bool {
177 self.run_with_bound_context(|| unsafe { sys::igIsItemDeactivatedAfterEdit() })
178 }
179
180 #[doc(alias = "IsItemEdited")]
184 pub fn is_item_edited(&self) -> bool {
185 self.run_with_bound_context(|| unsafe { sys::igIsItemEdited() })
186 }
187
188 #[doc(alias = "IsAnyItemActive")]
190 pub fn is_any_item_active(&self) -> bool {
191 self.run_with_bound_context(|| unsafe { sys::igIsAnyItemActive() })
192 }
193
194 #[doc(alias = "IsAnyItemFocused")]
196 pub fn is_any_item_focused(&self) -> bool {
197 self.run_with_bound_context(|| unsafe { sys::igIsAnyItemFocused() })
198 }
199
200 #[doc(alias = "IsAnyItemHovered")]
202 pub fn is_any_item_hovered(&self) -> bool {
203 self.run_with_bound_context(|| unsafe { sys::igIsAnyItemHovered() })
204 }
205
206 #[doc(alias = "GetItemRectMin", alias = "GetItemRectMax")]
208 pub fn item_rect(&self) -> ([f32; 2], [f32; 2]) {
209 let (min, max) = self.run_with_bound_context(|| unsafe {
210 (sys::igGetItemRectMin(), sys::igGetItemRectMax())
211 });
212 ([min.x, min.y], [max.x, max.y])
213 }
214
215 #[doc(alias = "GetItemRectSize")]
217 pub fn item_rect_size(&self) -> [f32; 2] {
218 let size = self.run_with_bound_context(|| unsafe { sys::igGetItemRectSize() });
219 [size.x, size.y]
220 }
221
222 #[doc(alias = "GetItemID")]
224 pub fn item_id(&self) -> crate::Id {
225 self.run_with_bound_context(|| unsafe { crate::Id::from(sys::igGetItemID()) })
226 }
227}
228
229#[must_use]
231pub struct TooltipToken<'ui> {
232 _ui: &'ui Ui,
233}
234
235impl<'ui> TooltipToken<'ui> {
236 fn new(ui: &'ui Ui) -> Self {
238 TooltipToken { _ui: ui }
239 }
240
241 pub fn end(self) {
243 }
245}
246
247impl<'ui> Drop for TooltipToken<'ui> {
248 fn drop(&mut self) {
249 self._ui
250 .run_with_bound_context(|| unsafe { sys::igEndTooltip() });
251 }
252}