dear_imgui_rs/widget/
misc.rs1#![allow(
7 clippy::cast_possible_truncation,
8 clippy::cast_sign_loss,
9 clippy::as_conversions
10)]
11use crate::Ui;
12use crate::sys;
13
14bitflags::bitflags! {
15 #[repr(transparent)]
17 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18 pub struct ButtonFlags: i32 {
19 const NONE = 0;
21 const ALLOW_OVERLAP = sys::ImGuiButtonFlags_AllowOverlap as i32;
23 const MOUSE_BUTTON_LEFT = sys::ImGuiButtonFlags_MouseButtonLeft as i32;
25 const MOUSE_BUTTON_RIGHT = sys::ImGuiButtonFlags_MouseButtonRight as i32;
27 const MOUSE_BUTTON_MIDDLE = sys::ImGuiButtonFlags_MouseButtonMiddle as i32;
29 }
30}
31
32pub use crate::Direction as ArrowDirection;
34
35impl Ui {
36 #[doc(alias = "Bullet")]
38 pub fn bullet(&self) {
39 unsafe {
40 sys::igBullet();
41 }
42 }
43
44 #[doc(alias = "BulletText")]
46 pub fn bullet_text(&self, text: impl AsRef<str>) {
47 let text_ptr = self.scratch_txt(text);
48 unsafe {
49 const FMT: &[u8; 3] = b"%s\0";
51 sys::igBulletText(FMT.as_ptr() as *const std::os::raw::c_char, text_ptr);
52 }
53 }
54}
55
56impl Ui {
57 #[doc(alias = "SmallButton")]
59 pub fn small_button(&self, label: impl AsRef<str>) -> bool {
60 let label_ptr = self.scratch_txt(label);
61 unsafe { sys::igSmallButton(label_ptr) }
62 }
63
64 #[doc(alias = "InvisibleButton")]
66 pub fn invisible_button(&self, str_id: impl AsRef<str>, size: impl Into<[f32; 2]>) -> bool {
67 self.invisible_button_flags(str_id, size, crate::widget::ButtonFlags::NONE)
68 }
69
70 #[doc(alias = "InvisibleButton")]
72 pub fn invisible_button_flags(
73 &self,
74 str_id: impl AsRef<str>,
75 size: impl Into<[f32; 2]>,
76 flags: crate::widget::ButtonFlags,
77 ) -> bool {
78 let id_ptr = self.scratch_txt(str_id);
79 let size_vec: sys::ImVec2 = size.into().into();
80 unsafe { sys::igInvisibleButton(id_ptr, size_vec, flags.bits()) }
81 }
82
83 #[doc(alias = "ArrowButton")]
85 pub fn arrow_button(&self, str_id: impl AsRef<str>, dir: crate::Direction) -> bool {
86 let id_ptr = self.scratch_txt(str_id);
87 unsafe { sys::igArrowButton(id_ptr, dir as i32) }
88 }
89}
90
91#[must_use]
97pub struct DisabledToken<'ui> {
98 _ui: &'ui Ui,
99}
100
101impl<'ui> DisabledToken<'ui> {
102 fn new(ui: &'ui Ui) -> Self {
103 DisabledToken { _ui: ui }
104 }
105
106 pub fn end(self) {
108 }
110}
111
112impl<'ui> Drop for DisabledToken<'ui> {
113 fn drop(&mut self) {
114 unsafe { sys::igEndDisabled() }
115 }
116}
117
118impl Ui {
119 #[doc(alias = "BeginDisabled")]
124 pub fn begin_disabled(&self) -> DisabledToken<'_> {
125 unsafe { sys::igBeginDisabled(true) }
126 DisabledToken::new(self)
127 }
128
129 #[doc(alias = "BeginDisabled")]
134 pub fn begin_disabled_with_cond(&self, disabled: bool) -> DisabledToken<'_> {
135 unsafe { sys::igBeginDisabled(disabled) }
136 DisabledToken::new(self)
137 }
138}
139
140impl Ui {
145 #[doc(alias = "PushButtonRepeat")]
149 pub fn push_button_repeat(&self, repeat: bool) {
150 unsafe { sys::igPushItemFlag(sys::ImGuiItemFlags_ButtonRepeat as i32, repeat) }
151 }
152
153 #[doc(alias = "PopButtonRepeat")]
155 pub fn pop_button_repeat(&self) {
156 unsafe { sys::igPopItemFlag() }
157 }
158}
159
160impl Ui {
165 #[doc(alias = "SetItemKeyOwner")]
167 pub fn set_item_key_owner(&self, key: crate::input::Key) {
168 let k: sys::ImGuiKey = key as sys::ImGuiKey;
169 unsafe { sys::igSetItemKeyOwner_Nil(k) }
170 }
171
172 #[doc(alias = "SetItemKeyOwner")]
174 pub fn set_item_key_owner_with_flags(
175 &self,
176 key: crate::input::Key,
177 flags: crate::input::ItemKeyOwnerFlags,
178 ) {
179 let k: sys::ImGuiKey = key as sys::ImGuiKey;
180 unsafe { sys::igSetItemKeyOwner_InputFlags(k, flags.raw()) }
181 }
182}