1#![allow(
7 clippy::cast_possible_truncation,
8 clippy::cast_sign_loss,
9 clippy::as_conversions
10)]
11use crate::Condition;
12use crate::sys;
13use crate::ui::Ui;
14use crate::widget::TreeNodeFlags;
15
16#[derive(Copy, Clone, Debug)]
18pub enum TreeNodeId<T> {
19 Str(T),
20 Ptr(*const u8),
21 Int(i32),
22}
23
24impl<T> From<T> for TreeNodeId<T>
25where
26 T: AsRef<str>,
27{
28 fn from(s: T) -> Self {
29 TreeNodeId::Str(s)
30 }
31}
32
33impl From<*const u8> for TreeNodeId<&'static str> {
34 fn from(ptr: *const u8) -> Self {
35 TreeNodeId::Ptr(ptr)
36 }
37}
38
39impl From<i32> for TreeNodeId<&'static str> {
40 fn from(i: i32) -> Self {
41 TreeNodeId::Int(i)
42 }
43}
44
45impl Ui {
47 pub fn tree_node<I, T>(&self, id: I) -> Option<TreeNodeToken<'_>>
54 where
55 I: Into<TreeNodeId<T>>,
56 T: AsRef<str>,
57 {
58 self.tree_node_config(id).push()
59 }
60
61 pub fn tree_node_config<I, T>(&self, id: I) -> TreeNode<'_, T>
67 where
68 I: Into<TreeNodeId<T>>,
69 T: AsRef<str>,
70 {
71 TreeNode {
72 id: id.into(),
73 label: None,
74 opened: false,
75 opened_cond: Condition::Never,
76 flags: TreeNodeFlags::NONE,
77 ui: self,
78 }
79 }
80
81 #[doc(alias = "CollapsingHeader")]
83 pub fn collapsing_header(&self, label: impl AsRef<str>, flags: TreeNodeFlags) -> bool {
84 let label_ptr = self.scratch_txt(label);
85 unsafe { sys::igCollapsingHeader_TreeNodeFlags(label_ptr, flags.bits()) }
86 }
87
88 #[doc(alias = "CollapsingHeader")]
94 pub fn collapsing_header_with_visible(
95 &self,
96 label: impl AsRef<str>,
97 visible: &mut bool,
98 flags: TreeNodeFlags,
99 ) -> bool {
100 let label_ptr = self.scratch_txt(label);
101 unsafe { sys::igCollapsingHeader_BoolPtr(label_ptr, visible as *mut bool, flags.bits()) }
102 }
103
104 #[doc(alias = "GetTreeNodeToLabelSpacing")]
106 pub fn tree_node_to_label_spacing(&self) -> f32 {
107 unsafe { sys::igGetTreeNodeToLabelSpacing() }
108 }
109}
110
111#[derive(Clone, Debug)]
113#[must_use]
114pub struct TreeNode<'a, T, L = &'static str> {
115 id: TreeNodeId<T>,
116 label: Option<L>,
117 opened: bool,
118 opened_cond: Condition,
119 flags: TreeNodeFlags,
120 ui: &'a Ui,
121}
122
123impl<'a, T: AsRef<str>> TreeNode<'a, T, &'static str> {
124 pub fn label<L: AsRef<str>>(self, label: L) -> TreeNode<'a, T, L> {
126 TreeNode {
127 id: self.id,
128 label: Some(label),
129 opened: self.opened,
130 opened_cond: self.opened_cond,
131 flags: self.flags,
132 ui: self.ui,
133 }
134 }
135}
136
137impl<'a, T: AsRef<str>, L: AsRef<str>> TreeNode<'a, T, L> {
138 pub fn opened(mut self, opened: bool, cond: Condition) -> Self {
140 self.opened = opened;
141 self.opened_cond = cond;
142 self
143 }
144
145 pub fn selected(mut self, selected: bool) -> Self {
147 self.flags.set(TreeNodeFlags::SELECTED, selected);
148 self
149 }
150
151 pub fn framed(mut self, framed: bool) -> Self {
153 self.flags.set(TreeNodeFlags::FRAMED, framed);
154 self
155 }
156
157 pub fn allow_item_overlap(mut self, allow: bool) -> Self {
159 self.flags.set(TreeNodeFlags::ALLOW_ITEM_OVERLAP, allow);
160 self
161 }
162
163 pub fn no_tree_push_on_open(mut self, no_push: bool) -> Self {
165 self.flags.set(TreeNodeFlags::NO_TREE_PUSH_ON_OPEN, no_push);
166 self
167 }
168
169 pub fn no_auto_open_on_log(mut self, no_auto: bool) -> Self {
171 self.flags.set(TreeNodeFlags::NO_AUTO_OPEN_ON_LOG, no_auto);
172 self
173 }
174
175 pub fn default_open(mut self, default_open: bool) -> Self {
177 self.flags.set(TreeNodeFlags::DEFAULT_OPEN, default_open);
178 self
179 }
180
181 pub fn open_on_double_click(mut self, double_click: bool) -> Self {
183 self.flags
184 .set(TreeNodeFlags::OPEN_ON_DOUBLE_CLICK, double_click);
185 self
186 }
187
188 pub fn open_on_arrow(mut self, arrow_only: bool) -> Self {
190 self.flags.set(TreeNodeFlags::OPEN_ON_ARROW, arrow_only);
191 self
192 }
193
194 pub fn leaf(mut self, leaf: bool) -> Self {
196 self.flags.set(TreeNodeFlags::LEAF, leaf);
197 self
198 }
199
200 pub fn bullet(mut self, bullet: bool) -> Self {
202 self.flags.set(TreeNodeFlags::BULLET, bullet);
203 self
204 }
205
206 pub fn frame_padding(mut self, frame_padding: bool) -> Self {
208 self.flags.set(TreeNodeFlags::FRAME_PADDING, frame_padding);
209 self
210 }
211
212 pub fn span_avail_width(mut self, span: bool) -> Self {
214 self.flags.set(TreeNodeFlags::SPAN_AVAIL_WIDTH, span);
215 self
216 }
217
218 pub fn span_full_width(mut self, span: bool) -> Self {
220 self.flags.set(TreeNodeFlags::SPAN_FULL_WIDTH, span);
221 self
222 }
223
224 pub fn nav_left_jumps_back_here(mut self, nav: bool) -> Self {
226 self.flags.set(TreeNodeFlags::NAV_LEFT_JUMPS_BACK_HERE, nav);
227 self
228 }
229
230 pub fn push(self) -> Option<TreeNodeToken<'a>> {
237 let open = unsafe {
238 if self.opened_cond != Condition::Never {
239 sys::igSetNextItemOpen(self.opened, self.opened_cond as i32);
240 }
241
242 match &self.id {
243 TreeNodeId::Str(s) => {
244 if let Some(label) = self.label.as_ref() {
245 let (id_ptr, label_ptr) = self.ui.scratch_txt_two(s, label);
246 sys::igPushID_Str(id_ptr);
247 let open = sys::igTreeNodeEx_Str(label_ptr, self.flags.bits());
248 sys::igPopID();
249 open
250 } else {
251 let label_ptr = self.ui.scratch_txt(s);
252 sys::igTreeNodeEx_Str(label_ptr, self.flags.bits())
253 }
254 }
255 TreeNodeId::Ptr(ptr) => {
256 let label = self.label.as_ref().map_or("", |l| l.as_ref());
257 let label_ptr = self.ui.scratch_txt(label);
258 sys::igPushID_Ptr(*ptr as *const std::os::raw::c_void);
259 let open = sys::igTreeNodeEx_Str(label_ptr, self.flags.bits());
260 sys::igPopID();
261 open
262 }
263 TreeNodeId::Int(i) => {
264 let label = self.label.as_ref().map_or("", |l| l.as_ref());
265 let label_ptr = self.ui.scratch_txt(label);
266 sys::igPushID_Int(*i);
267 let open = sys::igTreeNodeEx_Str(label_ptr, self.flags.bits());
268 sys::igPopID();
269 open
270 }
271 }
272 };
273
274 if open {
275 Some(TreeNodeToken::new(self.ui))
276 } else {
277 None
278 }
279 }
280}
281
282#[must_use]
284pub struct TreeNodeToken<'ui> {
285 ui: &'ui Ui,
286}
287
288impl<'ui> TreeNodeToken<'ui> {
289 fn new(ui: &'ui Ui) -> Self {
291 TreeNodeToken { ui }
292 }
293
294 pub fn pop(self) {
296 }
298}
299
300impl<'ui> Drop for TreeNodeToken<'ui> {
301 fn drop(&mut self) {
302 unsafe {
303 sys::igTreePop();
304 }
305 }
306}