dear_imgui_rs/widget/
list_box.rs1use std::borrow::Cow;
6
7use crate::Ui;
8use crate::sys;
9
10fn assert_finite_vec2(caller: &str, name: &str, value: [f32; 2]) {
11 assert!(
12 value[0].is_finite() && value[1].is_finite(),
13 "{caller} {name} must contain finite values"
14 );
15}
16
17impl Ui {
19 pub fn list_box_config<T: AsRef<str>>(&self, label: T) -> ListBox<T> {
21 ListBox::new(label)
22 }
23}
24
25#[derive(Clone, Debug)]
27#[must_use]
28pub struct ListBox<T> {
29 label: T,
30 size: [f32; 2],
31}
32
33impl<T: AsRef<str>> ListBox<T> {
34 #[doc(alias = "ListBoxHeaderVec2", alias = "ListBoxHeaderInt")]
36 pub fn new(label: T) -> ListBox<T> {
37 ListBox {
38 label,
39 size: [0.0, 0.0],
40 }
41 }
42
43 #[inline]
50 pub fn size(mut self, size: impl Into<[f32; 2]>) -> Self {
51 self.size = size.into();
52 self
53 }
54 #[must_use]
61 pub fn begin(self, ui: &Ui) -> Option<ListBoxToken<'_>> {
62 assert_finite_vec2("ListBox::begin()", "size", self.size);
63 let size_vec = sys::ImVec2 {
64 x: self.size[0],
65 y: self.size[1],
66 };
67 let label_ptr = ui.scratch_txt(self.label);
68 let should_render =
69 ui.run_with_bound_context(|| unsafe { sys::igBeginListBox(label_ptr, size_vec) });
70 if should_render {
71 Some(ListBoxToken::new(ui))
72 } else {
73 None
74 }
75 }
76 pub fn build<R, F: FnOnce() -> R>(self, ui: &Ui, f: F) -> Option<R> {
81 self.begin(ui).map(|_list| f())
82 }
83}
84
85pub struct ListBoxToken<'ui> {
88 _ui: &'ui Ui,
89}
90
91impl<'ui> ListBoxToken<'ui> {
92 pub fn new(ui: &'ui Ui) -> Self {
94 Self { _ui: ui }
95 }
96
97 pub fn end(self) {
99 }
101}
102
103impl<'ui> Drop for ListBoxToken<'ui> {
104 fn drop(&mut self) {
105 self._ui
106 .run_with_bound_context(|| unsafe { sys::igEndListBox() });
107 }
108}
109
110impl<T: AsRef<str>> ListBox<T> {
112 pub fn build_simple<V, L>(
114 self,
115 ui: &Ui,
116 current_item: &mut usize,
117 items: &[V],
118 label_fn: &L,
119 ) -> bool
120 where
121 for<'b> L: Fn(&'b V) -> Cow<'b, str>,
122 {
123 let mut result = false;
124 let lb = self;
125 if let Some(_cb) = lb.begin(ui) {
126 for (idx, item) in items.iter().enumerate() {
127 let text = label_fn(item);
128 let selected = idx == *current_item;
129 if ui.selectable_config(&text).selected(selected).build() {
130 *current_item = idx;
131 result = true;
132 }
133 }
134 }
135 result
136 }
137
138 pub fn build_simple_i32<V, L>(
143 self,
144 ui: &Ui,
145 current_item: &mut i32,
146 items: &[V],
147 label_fn: &L,
148 ) -> bool
149 where
150 for<'b> L: Fn(&'b V) -> Cow<'b, str>,
151 {
152 let mut result = false;
153 let lb = self;
154 if let Some(_cb) = lb.begin(ui) {
155 for (idx, item) in items.iter().enumerate() {
156 if idx > i32::MAX as usize {
157 break;
158 }
159 let idx_i32 = idx as i32;
160 let text = label_fn(item);
161 let selected = idx_i32 == *current_item;
162 if ui.selectable_config(&text).selected(selected).build() {
163 *current_item = idx_i32;
164 result = true;
165 }
166 }
167 }
168 result
169 }
170}