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")]
74 pub fn set_tooltip(&self, text: impl AsRef<str>) {
75 let text_ptr = self.scratch_txt(text);
76 unsafe {
77 sys::igSetTooltip(text_ptr);
78 }
79 }
80
81 #[doc(alias = "SetTooltip")]
83 pub fn set_tooltip_formatted(&self, text: impl AsRef<str>) {
84 self.set_tooltip(text);
85 }
86
87 #[doc(alias = "SetItemTooltip")]
90 pub fn set_item_tooltip(&self, text: impl AsRef<str>) {
91 let text_ptr = self.scratch_txt(text);
92 unsafe { sys::igSetItemTooltip(text_ptr) }
93 }
94}
95
96impl Ui {
98 #[doc(alias = "IsItemHovered")]
101 pub fn is_item_hovered(&self) -> bool {
102 unsafe { sys::igIsItemHovered(crate::HoveredFlags::NONE.bits()) }
103 }
104
105 #[doc(alias = "IsItemHovered")]
107 pub fn is_item_hovered_with_flags(&self, flags: crate::HoveredFlags) -> bool {
108 unsafe { sys::igIsItemHovered(flags.bits()) }
109 }
110
111 #[doc(alias = "IsItemActive")]
113 pub fn is_item_active(&self) -> bool {
114 unsafe { sys::igIsItemActive() }
115 }
116
117 #[doc(alias = "IsItemFocused")]
119 pub fn is_item_focused(&self) -> bool {
120 unsafe { sys::igIsItemFocused() }
121 }
122
123 #[doc(alias = "IsItemClicked")]
125 pub fn is_item_clicked(&self) -> bool {
126 unsafe { sys::igIsItemClicked(crate::input::MouseButton::Left as i32) }
127 }
128
129 #[doc(alias = "IsItemClicked")]
131 pub fn is_item_clicked_with_button(&self, mouse_button: MouseButton) -> bool {
132 unsafe { sys::igIsItemClicked(mouse_button as i32) }
133 }
134
135 #[doc(alias = "IsItemVisible")]
137 pub fn is_item_visible(&self) -> bool {
138 unsafe { sys::igIsItemVisible() }
139 }
140
141 #[doc(alias = "IsItemActivated")]
143 pub fn is_item_activated(&self) -> bool {
144 unsafe { sys::igIsItemActivated() }
145 }
146
147 #[doc(alias = "IsItemDeactivated")]
149 pub fn is_item_deactivated(&self) -> bool {
150 unsafe { sys::igIsItemDeactivated() }
151 }
152
153 #[doc(alias = "IsItemDeactivatedAfterEdit")]
155 pub fn is_item_deactivated_after_edit(&self) -> bool {
156 unsafe { sys::igIsItemDeactivatedAfterEdit() }
157 }
158
159 #[doc(alias = "IsAnyItemActive")]
161 pub fn is_any_item_active(&self) -> bool {
162 unsafe { sys::igIsAnyItemActive() }
163 }
164
165 #[doc(alias = "IsAnyItemFocused")]
167 pub fn is_any_item_focused(&self) -> bool {
168 unsafe { sys::igIsAnyItemFocused() }
169 }
170
171 #[doc(alias = "IsAnyItemHovered")]
173 pub fn is_any_item_hovered(&self) -> bool {
174 unsafe { sys::igIsAnyItemHovered() }
175 }
176
177 #[doc(alias = "GetItemRectMin", alias = "GetItemRectMax")]
179 pub fn item_rect(&self) -> ([f32; 2], [f32; 2]) {
180 let min = unsafe { sys::igGetItemRectMin() };
181 let max = unsafe { sys::igGetItemRectMax() };
182 ([min.x, min.y], [max.x, max.y])
183 }
184
185 #[doc(alias = "GetItemRectSize")]
187 pub fn item_rect_size(&self) -> [f32; 2] {
188 let size = unsafe { sys::igGetItemRectSize() };
189 [size.x, size.y]
190 }
191}
192
193#[must_use]
195pub struct TooltipToken<'ui> {
196 ui: &'ui Ui,
197}
198
199impl<'ui> TooltipToken<'ui> {
200 fn new(ui: &'ui Ui) -> Self {
202 TooltipToken { ui }
203 }
204
205 pub fn end(self) {
207 }
209}
210
211impl<'ui> Drop for TooltipToken<'ui> {
212 fn drop(&mut self) {
213 unsafe {
214 sys::igEndTooltip();
215 }
216 }
217}