1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Trees and collapsing headers
//!
//! Tree nodes and collapsing headers for hierarchical content. See
//! `TreeNodeFlags` for customization options.
//!
#![allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::as_conversions
)]
use crate::Condition;
use crate::sys;
use crate::ui::Ui;
use crate::widget::TreeNodeFlags;
/// Tree node ID that can be constructed from different types
#[derive(Copy, Clone, Debug)]
pub enum TreeNodeId<T> {
Str(T),
Ptr(*const u8),
Int(i32),
}
impl<T> From<T> for TreeNodeId<T>
where
T: AsRef<str>,
{
fn from(s: T) -> Self {
TreeNodeId::Str(s)
}
}
impl From<*const u8> for TreeNodeId<&'static str> {
fn from(ptr: *const u8) -> Self {
TreeNodeId::Ptr(ptr)
}
}
impl From<i32> for TreeNodeId<&'static str> {
fn from(i: i32) -> Self {
TreeNodeId::Int(i)
}
}
/// # Tree Node Widgets
impl Ui {
/// Constructs a new tree node with just a name, and pushes it.
///
/// Use [tree_node_config] to access a builder to put additional
/// configurations on the tree node.
///
/// [tree_node_config]: Self::tree_node_config
pub fn tree_node<I, T>(&self, id: I) -> Option<TreeNodeToken<'_>>
where
I: Into<TreeNodeId<T>>,
T: AsRef<str>,
{
self.tree_node_config(id).push()
}
/// Constructs a new tree node builder.
///
/// Use [tree_node] to build a simple node with just a name.
///
/// [tree_node]: Self::tree_node
pub fn tree_node_config<I, T>(&self, id: I) -> TreeNode<'_, T>
where
I: Into<TreeNodeId<T>>,
T: AsRef<str>,
{
TreeNode {
id: id.into(),
label: None,
opened: false,
opened_cond: None,
flags: TreeNodeFlags::NONE,
ui: self,
}
}
/// Creates a collapsing header widget
#[doc(alias = "CollapsingHeader")]
pub fn collapsing_header(&self, label: impl AsRef<str>, flags: TreeNodeFlags) -> bool {
let label_ptr = self.scratch_txt(label);
unsafe { sys::igCollapsingHeader_TreeNodeFlags(label_ptr, flags.bits()) }
}
/// Creates a collapsing header widget with a visibility tracking variable.
///
/// Passing `visible` enables a close button on the header. When clicked, ImGui will set
/// `*visible = false`. As with other immediate-mode widgets, you should stop submitting the
/// header when `*visible == false`.
#[doc(alias = "CollapsingHeader")]
pub fn collapsing_header_with_visible(
&self,
label: impl AsRef<str>,
visible: &mut bool,
flags: TreeNodeFlags,
) -> bool {
let label_ptr = self.scratch_txt(label);
unsafe { sys::igCollapsingHeader_BoolPtr(label_ptr, visible as *mut bool, flags.bits()) }
}
/// Returns the distance from the start of a tree node to the label text.
#[doc(alias = "GetTreeNodeToLabelSpacing")]
pub fn tree_node_to_label_spacing(&self) -> f32 {
unsafe { sys::igGetTreeNodeToLabelSpacing() }
}
/// Returns whether the tree node identified by `storage_id` is open in storage.
#[doc(alias = "TreeNodeGetOpen")]
pub fn tree_node_get_open(&self, storage_id: crate::Id) -> bool {
unsafe { sys::igTreeNodeGetOpen(storage_id.raw()) }
}
}
/// Builder for a tree node widget
#[derive(Clone, Debug)]
#[must_use]
pub struct TreeNode<'a, T, L = &'static str> {
id: TreeNodeId<T>,
label: Option<L>,
opened: bool,
opened_cond: Option<Condition>,
flags: TreeNodeFlags,
ui: &'a Ui,
}
impl<'a, T: AsRef<str>> TreeNode<'a, T, &'static str> {
/// Sets a custom label for the tree node
pub fn label<L: AsRef<str>>(self, label: L) -> TreeNode<'a, T, L> {
TreeNode {
id: self.id,
label: Some(label),
opened: self.opened,
opened_cond: self.opened_cond,
flags: self.flags,
ui: self.ui,
}
}
}
impl<'a, T: AsRef<str>, L: AsRef<str>> TreeNode<'a, T, L> {
/// Sets the opened state
pub fn opened(mut self, opened: bool, cond: Condition) -> Self {
self.opened = opened;
self.opened_cond = Some(cond);
self
}
/// Draw as selected
pub fn selected(mut self, selected: bool) -> Self {
self.flags.set(TreeNodeFlags::SELECTED, selected);
self
}
/// Draw frame with background (e.g. for CollapsingHeader)
pub fn framed(mut self, framed: bool) -> Self {
self.flags.set(TreeNodeFlags::FRAMED, framed);
self
}
/// Hit testing to allow subsequent widgets to overlap this one
pub fn allow_item_overlap(mut self, allow: bool) -> Self {
self.flags.set(TreeNodeFlags::ALLOW_ITEM_OVERLAP, allow);
self
}
/// Don't do a TreePush() when open (e.g. for CollapsingHeader)
pub fn no_tree_push_on_open(mut self, no_push: bool) -> Self {
self.flags.set(TreeNodeFlags::NO_TREE_PUSH_ON_OPEN, no_push);
self
}
/// Don't automatically and temporarily open node when Logging is active
pub fn no_auto_open_on_log(mut self, no_auto: bool) -> Self {
self.flags.set(TreeNodeFlags::NO_AUTO_OPEN_ON_LOG, no_auto);
self
}
/// Default node to be open
pub fn default_open(mut self, default_open: bool) -> Self {
self.flags.set(TreeNodeFlags::DEFAULT_OPEN, default_open);
self
}
/// Need double-click to open node
pub fn open_on_double_click(mut self, double_click: bool) -> Self {
self.flags
.set(TreeNodeFlags::OPEN_ON_DOUBLE_CLICK, double_click);
self
}
/// Only open when clicking on the arrow part
pub fn open_on_arrow(mut self, arrow_only: bool) -> Self {
self.flags.set(TreeNodeFlags::OPEN_ON_ARROW, arrow_only);
self
}
/// No collapsing, no arrow (use as a convenience for leaf nodes)
pub fn leaf(mut self, leaf: bool) -> Self {
self.flags.set(TreeNodeFlags::LEAF, leaf);
self
}
/// Display a bullet instead of arrow
pub fn bullet(mut self, bullet: bool) -> Self {
self.flags.set(TreeNodeFlags::BULLET, bullet);
self
}
/// Use FramePadding to vertically align text baseline to regular widget height
pub fn frame_padding(mut self, frame_padding: bool) -> Self {
self.flags.set(TreeNodeFlags::FRAME_PADDING, frame_padding);
self
}
/// Extend hit box to the right-most edge
pub fn span_avail_width(mut self, span: bool) -> Self {
self.flags.set(TreeNodeFlags::SPAN_AVAIL_WIDTH, span);
self
}
/// Extend hit box to the left-most and right-most edges
pub fn span_full_width(mut self, span: bool) -> Self {
self.flags.set(TreeNodeFlags::SPAN_FULL_WIDTH, span);
self
}
/// Left direction may move to this tree node from any of its child
pub fn nav_left_jumps_back_here(mut self, nav: bool) -> Self {
self.flags.set(TreeNodeFlags::NAV_LEFT_JUMPS_BACK_HERE, nav);
self
}
/// Pushes a tree node and starts appending to it.
///
/// Returns `Some(TreeNodeToken)` if the tree node is open. After content has been
/// rendered, the token can be popped by calling `.pop()`.
///
/// Returns `None` if the tree node is not open and no content should be rendered.
pub fn push(self) -> Option<TreeNodeToken<'a>> {
let open = unsafe {
if let Some(opened_cond) = self.opened_cond {
sys::igSetNextItemOpen(self.opened, opened_cond as i32);
}
match &self.id {
TreeNodeId::Str(s) => {
if let Some(label) = self.label.as_ref() {
let (id_ptr, label_ptr) = self.ui.scratch_txt_two(s, label);
sys::igPushID_Str(id_ptr);
let open = sys::igTreeNodeEx_Str(label_ptr, self.flags.bits());
sys::igPopID();
open
} else {
let label_ptr = self.ui.scratch_txt(s);
sys::igTreeNodeEx_Str(label_ptr, self.flags.bits())
}
}
TreeNodeId::Ptr(ptr) => {
let label = self.label.as_ref().map_or("", |l| l.as_ref());
let label_ptr = self.ui.scratch_txt(label);
sys::igPushID_Ptr(*ptr as *const std::os::raw::c_void);
let open = sys::igTreeNodeEx_Str(label_ptr, self.flags.bits());
sys::igPopID();
open
}
TreeNodeId::Int(i) => {
let label = self.label.as_ref().map_or("", |l| l.as_ref());
let label_ptr = self.ui.scratch_txt(label);
sys::igPushID_Int(*i);
let open = sys::igTreeNodeEx_Str(label_ptr, self.flags.bits());
sys::igPopID();
open
}
}
};
if open {
Some(TreeNodeToken::new(self.ui))
} else {
None
}
}
}
/// Tracks a tree node that can be popped by calling `.pop()` or by dropping
#[must_use]
pub struct TreeNodeToken<'ui> {
_ui: &'ui Ui,
}
impl<'ui> TreeNodeToken<'ui> {
/// Creates a new tree node token
fn new(ui: &'ui Ui) -> Self {
TreeNodeToken { _ui: ui }
}
/// Pops the tree node
pub fn pop(self) {
// The drop implementation will handle the actual popping
}
}
impl<'ui> Drop for TreeNodeToken<'ui> {
fn drop(&mut self) {
unsafe {
sys::igTreePop();
}
}
}