/
id.rs
485 lines (441 loc) · 16.4 KB
/
id.rs
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! # Ids
//!
//! [Id](id::Id)s are unique identifiers for views.
//! They're used to identify views in the view tree.
//!
//! ## Ids and Id paths
//!
//! These ids are assigned via the [ViewContext](context::ViewContext) and are unique across the entire application.
//!
use std::{any::Any, cell::RefCell, collections::HashMap, num::NonZeroU64, time::Duration};
use glazier::{
kurbo::{Point, Vec2},
FileDialogOptions, FileInfo,
};
use crate::{
animate::Animation,
app_handle::{StyleSelector, UpdateMessage, DEFERRED_UPDATE_MESSAGES, UPDATE_MESSAGES},
context::{EventCallback, ResizeCallback},
event::EventListener,
menu::Menu,
responsive::ScreenSize,
style::Style,
};
thread_local! {
pub(crate) static ID_PATHS: RefCell<HashMap<Id,IdPath>> = Default::default();
/// primarily for debug_tree and tab navigation
pub(crate) static ID_PATHS_CHILDREN: RefCell<HashMap<Id, Vec<Id>>> = Default::default();
/// primarily for debug_tree and tab navigation
pub(crate) static NEXT_SIBLING: RefCell<HashMap<Id, Option<Id>>> = Default::default();
/// primarily for debug_tree and tab navigation
pub(crate) static PREVIOUS_SIBLING: RefCell<HashMap<Id, Option<Id>>> = Default::default();
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Hash)]
/// A stable identifier for an element.
pub struct Id(NonZeroU64);
#[derive(Clone, Default)]
pub struct IdPath(pub(crate) Vec<Id>);
impl Id {
/// Allocate a new, unique `Id`.
pub fn next() -> Id {
use glazier::Counter;
static WIDGET_ID_COUNTER: Counter = Counter::new();
Id(WIDGET_ID_COUNTER.next_nonzero())
}
#[allow(unused)]
pub fn to_raw(self) -> u64 {
self.0.into()
}
pub fn to_nonzero_raw(self) -> NonZeroU64 {
self.0
}
pub fn new(&self) -> Id {
let mut id_path =
ID_PATHS.with(|id_paths| id_paths.borrow().get(self).cloned().unwrap_or_default());
let new_id = Self::next();
id_path.0.push(new_id);
ID_PATHS.with(|id_paths| {
id_paths.borrow_mut().insert(new_id, id_path);
});
ID_PATHS_CHILDREN.with(|children| {
let mut children = children.borrow_mut();
let children = children.entry(*self).or_default();
if let Some(previous_child) = children.last() {
NEXT_SIBLING.with(|next_sibling| {
next_sibling
.borrow_mut()
.insert(*previous_child, Some(new_id))
});
PREVIOUS_SIBLING.with(|previous_sibling| {
previous_sibling
.borrow_mut()
.insert(new_id, Some(*previous_child))
});
}
children.push(new_id);
});
new_id
}
pub fn parent(&self) -> Option<Id> {
ID_PATHS.with(|id_paths| {
id_paths.borrow().get(self).and_then(|id_path| {
let id_path = &id_path.0;
let len = id_path.len();
if len >= 2 {
Some(id_path[len - 2])
} else {
None
}
})
})
}
/// Get the id of the view after this one (but with the same parent and level of nesting)
pub fn next_sibling(&self) -> Option<Id> {
NEXT_SIBLING.with(|next_sibling| next_sibling.borrow().get(self).copied().flatten())
}
/// Get the id of the view before this one (but with the same parent and level of nesting)
pub fn previous_sibling(&self) -> Option<Id> {
PREVIOUS_SIBLING
.with(|previous_sibling| previous_sibling.borrow().get(self).copied().flatten())
}
/// A list of all the direct children of this view (no deep nesting)
pub fn direct_children(&self) -> Vec<Id> {
ID_PATHS_CHILDREN.with(|idpaths_children| {
idpaths_children
.borrow()
.get(self)
.cloned()
.unwrap_or_default()
})
}
/// The first child with this view as a parent. The depth increases only by 1.
pub fn first_child(&self) -> Option<Id> {
ID_PATHS_CHILDREN.with(|id_paths_children| {
id_paths_children
.borrow()
.get(self)
.and_then(|children| children.first())
.copied()
})
}
/// The last child with this view as a parent. The depth increases only by 1.
pub fn last_child(&self) -> Option<Id> {
ID_PATHS_CHILDREN.with(|id_paths_children| {
id_paths_children
.borrow()
.get(self)
.and_then(|children| children.last())
.copied()
})
}
/// Get the next item in the tree, either the first child or the next sibling of this view or of the first parent view
pub fn tree_next(&self) -> Option<Id> {
self.first_child().or_else(|| {
let mut ancestor = *self;
loop {
if let Some(next_sibling) = ancestor.next_sibling() {
return Some(next_sibling);
}
ancestor = ancestor.parent()?;
}
})
}
/// Get the next item in the tree, the deepest last child of the previous sibling of this view or the parent
pub fn tree_previous(&self) -> Option<Id> {
self.previous_sibling()
.map(|id| id.nested_last_child())
.or_else(|| self.parent())
}
/// Repeatedly get the last child until the deepest last child is found
pub fn nested_last_child(&self) -> Id {
let mut last_child = *self;
while let Some(new_last_child) = last_child.last_child() {
last_child = new_last_child;
}
last_child
}
pub fn all_children(&self) -> Vec<Id> {
let mut children = Vec::new();
let mut parents = Vec::new();
parents.push(*self);
ID_PATHS_CHILDREN.with(|id_paths_children| {
let id_paths_children = id_paths_children.borrow();
while !parents.is_empty() {
let parent = parents.pop().unwrap();
if let Some(c) = id_paths_children.get(&parent) {
for child in c {
children.push(*child);
parents.push(*child);
}
}
}
});
children
}
pub fn remove_id_path(&self) {
let id_path = ID_PATHS.with(|id_paths| id_paths.borrow_mut().remove(self));
if let Some(id_path) = id_path.as_ref() {
if let Some(parent) = id_path.0.get(id_path.0.len().saturating_sub(2)) {
ID_PATHS_CHILDREN.with(|id_paths_children| {
if let Some(children) = id_paths_children.borrow_mut().get_mut(parent) {
let index = children.iter().position(|&id| id == *self).unwrap();
children.remove(index);
let previous_child = index.checked_sub(1).map(|index| children[index]);
let next_child = children.get(index + 1).copied();
if let Some(previous_child) = previous_child {
NEXT_SIBLING.with(|next_sibling| {
next_sibling.borrow_mut().insert(previous_child, next_child)
});
}
if let Some(next_child) = next_child {
PREVIOUS_SIBLING.with(|previous_sibling| {
previous_sibling
.borrow_mut()
.insert(next_child, previous_child)
});
}
}
});
}
}
ID_PATHS_CHILDREN.with(|id_paths_children| {
id_paths_children.borrow_mut().remove(self);
});
}
pub fn root_id(&self) -> Option<Id> {
ID_PATHS.with(|id_paths| {
id_paths
.borrow()
.get(self)
.and_then(|path| path.0.first().copied())
})
}
pub fn request_focus(&self) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::Focus(*self));
});
}
}
pub fn request_active(&self) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::Active(*self));
});
}
}
pub fn update_disabled(&self, is_disabled: bool) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::Disabled {
id: *self,
is_disabled,
})
});
}
}
pub fn update_window_scale(&self, window_scale: f64) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::WindowScale(window_scale))
});
}
}
pub fn request_paint(&self) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::RequestPaint);
});
}
}
pub fn request_layout(&self) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::RequestLayout { id: *self });
});
}
}
pub fn update_state(&self, state: impl Any, deferred: bool) {
if let Some(root) = self.root_id() {
if !deferred {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::State {
id: *self,
state: Box::new(state),
})
});
} else {
DEFERRED_UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push((*self, Box::new(state)));
});
}
}
}
pub fn update_base_style(&self, style: Style) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::BaseStyle { id: *self, style });
});
}
}
pub fn update_style(&self, style: Style) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::Style { id: *self, style });
});
}
}
pub fn update_style_selector(&self, style: Style, selector: StyleSelector) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::StyleSelector {
id: *self,
style,
selector,
})
});
}
}
pub fn keyboard_navigatable(&self) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::KeyboardNavigable { id: *self })
})
}
}
pub fn draggable(&self) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::Draggable { id: *self })
})
}
}
pub fn update_responsive_style(&self, style: Style, size: ScreenSize) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::ResponsiveStyle {
id: *self,
style,
size,
})
});
}
}
pub fn set_handle_titlebar(&self, val: bool) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::HandleTitleBar(val))
});
}
}
pub fn set_window_delta(&self, delta: Vec2) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::SetWindowDelta(delta))
});
}
}
pub fn update_event_listener(&self, listener: EventListener, action: Box<EventCallback>) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::EventListener {
id: *self,
listener,
action,
})
});
}
}
pub fn update_resize_listener(&self, action: Box<ResizeCallback>) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::ResizeListener { id: *self, action })
});
}
}
pub fn update_animation(&self, animation: Animation) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::Animation {
id: *self,
animation,
})
});
}
}
pub fn exec_after(&self, deadline: Duration, action: impl FnOnce() + 'static) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::RequestTimer {
deadline,
action: Box::new(action),
})
});
}
}
pub fn open_file(
&self,
options: FileDialogOptions,
file_info_action: impl Fn(Option<FileInfo>) + 'static,
) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::OpenFile {
options,
file_info_action: Box::new(file_info_action),
})
});
}
}
pub fn show_context_menu(&self, menu: Menu, pos: Point) {
if let Some(root) = self.root_id() {
UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
let msgs = msgs.entry(root).or_default();
msgs.push(UpdateMessage::ShowContextMenu { menu, pos })
});
}
}
}