dear_imgui_rs/widget/
button.rs1use crate::Ui;
6use crate::sys;
7use std::borrow::Cow;
8
9impl Ui {
10 #[doc(alias = "Button")]
12 pub fn button(&self, label: impl AsRef<str>) -> bool {
13 self.button_config(label.as_ref()).build()
14 }
15
16 #[doc(alias = "Button")]
18 pub fn button_with_size(&self, label: impl AsRef<str>, size: impl Into<[f32; 2]>) -> bool {
19 self.button_config(label.as_ref()).size(size).build()
20 }
21
22 pub fn button_config<'ui>(&'ui self, label: impl Into<Cow<'ui, str>>) -> Button<'ui> {
24 Button::new(self, label)
25 }
26}
27
28impl Ui {
29 #[doc(alias = "Checkbox")]
31 pub fn checkbox(&self, label: impl AsRef<str>, value: &mut bool) -> bool {
32 let label_ptr = self.scratch_txt(label);
33 unsafe { sys::igCheckbox(label_ptr, value) }
34 }
35
36 #[doc(alias = "RadioButton")]
38 pub fn radio_button(&self, label: impl AsRef<str>, active: bool) -> bool {
39 let label_ptr = self.scratch_txt(label);
40 unsafe { sys::igRadioButton_Bool(label_ptr, active) }
41 }
42
43 #[doc(alias = "RadioButton")]
45 pub fn radio_button_int(&self, label: impl AsRef<str>, v: &mut i32, v_button: i32) -> bool {
46 let label_ptr = self.scratch_txt(label);
47 unsafe { sys::igRadioButton_IntPtr(label_ptr, v, v_button) }
48 }
49
50 #[doc(alias = "RadioButtonBool")]
54 pub fn radio_button_bool(&self, label: impl AsRef<str>, active: bool) -> bool {
55 let label_ptr = self.scratch_txt(label);
56 unsafe { sys::igRadioButton_Bool(label_ptr, active) }
57 }
58
59 #[doc(alias = "CheckboxFlags")]
67 pub fn checkbox_flags<T>(&self, label: impl AsRef<str>, flags: &mut T, mask: T) -> bool
68 where
69 T: Copy
70 + PartialEq
71 + std::ops::BitOrAssign
72 + std::ops::BitAndAssign
73 + std::ops::BitAnd<Output = T>
74 + std::ops::Not<Output = T>,
75 {
76 let mut value = *flags & mask == mask;
77 let pressed = self.checkbox(label, &mut value);
78 if pressed {
79 if value {
80 *flags |= mask;
81 } else {
82 *flags &= !mask;
83 }
84 }
85 pressed
86 }
87}
88
89pub struct Button<'ui> {
91 ui: &'ui Ui,
92 label: Cow<'ui, str>,
93 size: Option<[f32; 2]>,
94}
95
96impl<'ui> Button<'ui> {
97 pub fn new(ui: &'ui Ui, label: impl Into<Cow<'ui, str>>) -> Self {
99 Self {
100 ui,
101 label: label.into(),
102 size: None,
103 }
104 }
105
106 pub fn size(mut self, size: impl Into<[f32; 2]>) -> Self {
108 self.size = Some(size.into());
109 self
110 }
111
112 pub fn build(self) -> bool {
114 let label_ptr = self.ui.scratch_txt(self.label.as_ref());
115 let size = self.size.unwrap_or([0.0, 0.0]);
116 let size_vec: sys::ImVec2 = size.into();
117 unsafe { sys::igButton(label_ptr, size_vec) }
118 }
119}