dear_imgui/widget/
button.rs1use crate::Ui;
2use crate::sys;
3
4impl Ui {
5 #[doc(alias = "Button")]
7 pub fn button(&self, label: impl AsRef<str>) -> bool {
8 self.button_config(label).build()
9 }
10
11 #[doc(alias = "Button")]
13 pub fn button_with_size(&self, label: impl AsRef<str>, size: impl Into<[f32; 2]>) -> bool {
14 self.button_config(label).size(size).build()
15 }
16
17 pub fn button_config(&self, label: impl AsRef<str>) -> Button<'_> {
19 Button::new(self, label)
20 }
21}
22
23impl Ui {
24 #[doc(alias = "Checkbox")]
26 pub fn checkbox(&self, label: impl AsRef<str>, value: &mut bool) -> bool {
27 let label_ptr = self.scratch_txt(label);
28 unsafe { sys::igCheckbox(label_ptr, value) }
29 }
30
31 #[doc(alias = "RadioButton")]
33 pub fn radio_button(&self, label: impl AsRef<str>, active: bool) -> bool {
34 let label_ptr = self.scratch_txt(label);
35 unsafe { sys::igRadioButton_Bool(label_ptr, active) }
36 }
37
38 #[doc(alias = "RadioButton")]
40 pub fn radio_button_int(&self, label: impl AsRef<str>, v: &mut i32, v_button: i32) -> bool {
41 let label_ptr = self.scratch_txt(label);
42 unsafe { sys::igRadioButton_IntPtr(label_ptr, v, v_button) }
43 }
44
45 #[doc(alias = "RadioButtonBool")]
49 pub fn radio_button_bool(&self, label: impl AsRef<str>, active: bool) -> bool {
50 let label_ptr = self.scratch_txt(label);
51 unsafe { sys::igRadioButton_Bool(label_ptr, active) }
52 }
53
54 pub fn checkbox_flags<T>(&self, label: impl AsRef<str>, flags: &mut T, mask: T) -> bool
58 where
59 T: Copy
60 + PartialEq
61 + std::ops::BitOrAssign
62 + std::ops::BitAndAssign
63 + std::ops::BitAnd<Output = T>
64 + std::ops::Not<Output = T>,
65 {
66 let mut value = *flags & mask == mask;
67 let pressed = self.checkbox(label, &mut value);
68 if pressed {
69 if value {
70 *flags |= mask;
71 } else {
72 *flags &= !mask;
73 }
74 }
75 pressed
76 }
77}
78
79pub struct Button<'ui> {
81 ui: &'ui Ui,
82 label: String,
83 size: Option<[f32; 2]>,
84}
85
86impl<'ui> Button<'ui> {
87 pub fn new(ui: &'ui Ui, label: impl AsRef<str>) -> Self {
89 Self {
90 ui,
91 label: label.as_ref().to_string(),
92 size: None,
93 }
94 }
95
96 pub fn size(mut self, size: impl Into<[f32; 2]>) -> Self {
98 self.size = Some(size.into());
99 self
100 }
101
102 pub fn build(self) -> bool {
104 let label_ptr = self.ui.scratch_txt(&self.label);
105 let size = self.size.unwrap_or([0.0, 0.0]);
106 let size_vec: sys::ImVec2 = size.into();
107 unsafe { sys::igButton(label_ptr, size_vec) }
108 }
109}