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 unsafe { sys::igIsItemHovered(flags.bits()) }
125 }
126
127 #[doc(alias = "IsItemActive")]
129 pub fn is_item_active(&self) -> bool {
130 unsafe { sys::igIsItemActive() }
131 }
132
133 #[doc(alias = "IsItemFocused")]
135 pub fn is_item_focused(&self) -> bool {
136 unsafe { sys::igIsItemFocused() }
137 }
138
139 #[doc(alias = "IsItemClicked")]
141 pub fn is_item_clicked(&self) -> bool {
142 unsafe { sys::igIsItemClicked(crate::input::MouseButton::Left as i32) }
143 }
144
145 #[doc(alias = "IsItemClicked")]
147 pub fn is_item_clicked_with_button(&self, mouse_button: MouseButton) -> bool {
148 unsafe { sys::igIsItemClicked(mouse_button as i32) }
149 }
150
151 #[doc(alias = "IsItemVisible")]
153 pub fn is_item_visible(&self) -> bool {
154 unsafe { sys::igIsItemVisible() }
155 }
156
157 #[doc(alias = "IsItemActivated")]
159 pub fn is_item_activated(&self) -> bool {
160 unsafe { sys::igIsItemActivated() }
161 }
162
163 #[doc(alias = "IsItemDeactivated")]
165 pub fn is_item_deactivated(&self) -> bool {
166 unsafe { sys::igIsItemDeactivated() }
167 }
168
169 #[doc(alias = "IsItemDeactivatedAfterEdit")]
171 pub fn is_item_deactivated_after_edit(&self) -> bool {
172 unsafe { sys::igIsItemDeactivatedAfterEdit() }
173 }
174
175 #[doc(alias = "IsAnyItemActive")]
177 pub fn is_any_item_active(&self) -> bool {
178 unsafe { sys::igIsAnyItemActive() }
179 }
180
181 #[doc(alias = "IsAnyItemFocused")]
183 pub fn is_any_item_focused(&self) -> bool {
184 unsafe { sys::igIsAnyItemFocused() }
185 }
186
187 #[doc(alias = "IsAnyItemHovered")]
189 pub fn is_any_item_hovered(&self) -> bool {
190 unsafe { sys::igIsAnyItemHovered() }
191 }
192
193 #[doc(alias = "GetItemRectMin", alias = "GetItemRectMax")]
195 pub fn item_rect(&self) -> ([f32; 2], [f32; 2]) {
196 let min = unsafe { sys::igGetItemRectMin() };
197 let max = unsafe { sys::igGetItemRectMax() };
198 ([min.x, min.y], [max.x, max.y])
199 }
200
201 #[doc(alias = "GetItemRectSize")]
203 pub fn item_rect_size(&self) -> [f32; 2] {
204 let size = unsafe { sys::igGetItemRectSize() };
205 [size.x, size.y]
206 }
207}
208
209#[must_use]
211pub struct TooltipToken<'ui> {
212 ui: &'ui Ui,
213}
214
215impl<'ui> TooltipToken<'ui> {
216 fn new(ui: &'ui Ui) -> Self {
218 TooltipToken { ui }
219 }
220
221 pub fn end(self) {
223 }
225}
226
227impl<'ui> Drop for TooltipToken<'ui> {
228 fn drop(&mut self) {
229 unsafe {
230 sys::igEndTooltip();
231 }
232 }
233}