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 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 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 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 unsafe { sys::igIsItemHovered(crate::HoveredFlags::NONE.bits()) }
119 }
120
121 #[doc(alias = "IsItemHovered")]
123 pub fn is_item_hovered_with_flags(&self, flags: crate::HoveredFlags) -> bool {
124 crate::utils::validate_item_hovered_flags("Ui::is_item_hovered_with_flags()", flags);
125 unsafe { sys::igIsItemHovered(flags.bits()) }
126 }
127
128 #[doc(alias = "IsItemActive")]
130 pub fn is_item_active(&self) -> bool {
131 unsafe { sys::igIsItemActive() }
132 }
133
134 #[doc(alias = "IsItemFocused")]
136 pub fn is_item_focused(&self) -> bool {
137 unsafe { sys::igIsItemFocused() }
138 }
139
140 #[doc(alias = "IsItemClicked")]
142 pub fn is_item_clicked(&self) -> bool {
143 unsafe { sys::igIsItemClicked(crate::input::MouseButton::Left as i32) }
144 }
145
146 #[doc(alias = "IsItemClicked")]
148 pub fn is_item_clicked_with_button(&self, mouse_button: MouseButton) -> bool {
149 unsafe { sys::igIsItemClicked(mouse_button as i32) }
150 }
151
152 #[doc(alias = "IsItemVisible")]
154 pub fn is_item_visible(&self) -> bool {
155 unsafe { sys::igIsItemVisible() }
156 }
157
158 #[doc(alias = "IsItemActivated")]
160 pub fn is_item_activated(&self) -> bool {
161 unsafe { sys::igIsItemActivated() }
162 }
163
164 #[doc(alias = "IsItemDeactivated")]
166 pub fn is_item_deactivated(&self) -> bool {
167 unsafe { sys::igIsItemDeactivated() }
168 }
169
170 #[doc(alias = "IsItemDeactivatedAfterEdit")]
172 pub fn is_item_deactivated_after_edit(&self) -> bool {
173 unsafe { sys::igIsItemDeactivatedAfterEdit() }
174 }
175
176 #[doc(alias = "IsItemEdited")]
180 pub fn is_item_edited(&self) -> bool {
181 unsafe { sys::igIsItemEdited() }
182 }
183
184 #[doc(alias = "IsAnyItemActive")]
186 pub fn is_any_item_active(&self) -> bool {
187 unsafe { sys::igIsAnyItemActive() }
188 }
189
190 #[doc(alias = "IsAnyItemFocused")]
192 pub fn is_any_item_focused(&self) -> bool {
193 unsafe { sys::igIsAnyItemFocused() }
194 }
195
196 #[doc(alias = "IsAnyItemHovered")]
198 pub fn is_any_item_hovered(&self) -> bool {
199 unsafe { sys::igIsAnyItemHovered() }
200 }
201
202 #[doc(alias = "GetItemRectMin", alias = "GetItemRectMax")]
204 pub fn item_rect(&self) -> ([f32; 2], [f32; 2]) {
205 let min = unsafe { sys::igGetItemRectMin() };
206 let max = unsafe { sys::igGetItemRectMax() };
207 ([min.x, min.y], [max.x, max.y])
208 }
209
210 #[doc(alias = "GetItemRectSize")]
212 pub fn item_rect_size(&self) -> [f32; 2] {
213 let size = unsafe { sys::igGetItemRectSize() };
214 [size.x, size.y]
215 }
216
217 #[doc(alias = "GetItemID")]
219 pub fn item_id(&self) -> crate::Id {
220 unsafe { crate::Id::from(sys::igGetItemID()) }
221 }
222}
223
224#[must_use]
226pub struct TooltipToken<'ui> {
227 _ui: &'ui Ui,
228}
229
230impl<'ui> TooltipToken<'ui> {
231 fn new(ui: &'ui Ui) -> Self {
233 TooltipToken { _ui: ui }
234 }
235
236 pub fn end(self) {
238 }
240}
241
242impl<'ui> Drop for TooltipToken<'ui> {
243 fn drop(&mut self) {
244 unsafe {
245 sys::igEndTooltip();
246 }
247 }
248}