-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathdom.rs
947 lines (810 loc) · 30.3 KB
/
dom.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
use std::{
fmt,
rc::Rc,
cell::RefCell,
hash::{Hash, Hasher},
sync::atomic::{AtomicUsize, Ordering},
collections::BTreeMap,
};
use glium::{Texture2d, framebuffer::SimpleFrameBuffer};
use {
window::{WindowEvent, WindowInfo},
images::ImageId,
cache::DomHash,
text_cache::TextId,
traits::Layout,
app_state::AppState,
id_tree::{NodeId, Node, Arena},
default_callbacks::{DefaultCallbackId, StackCheckedPointer},
};
static TAG_ID: AtomicUsize = AtomicUsize::new(0);
/// A callback function has to return if the screen should
/// be updated after the function has run.PartialEq
///
/// This is necessary for updating the screen only if it is absolutely necessary.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum UpdateScreen {
/// Redraw the screen
Redraw,
/// Don't redraw the screen
DontRedraw,
}
/// Stores a function pointer that is executed when the given UI element is hit
///
/// Must return an `UpdateScreen` that denotes if the screen should be redrawn.
/// The CSS is not affected by this, so if you push to the windows' CSS inside the
/// function, the screen will not be automatically redrawn, unless you return an
/// `UpdateScreen::Redraw` from the function
pub struct Callback<T: Layout>(pub fn(&mut AppState<T>, WindowEvent) -> UpdateScreen);
impl<T: Layout> fmt::Debug for Callback<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Callback @ 0x{:x}", self.0 as usize)
}
}
impl<T: Layout> Clone for Callback<T> {
fn clone(&self) -> Self {
Callback(self.0.clone())
}
}
/// As a hashing function, we use the function pointer casted to a usize
/// as a unique ID for the function. This way, we can hash and compare DOM nodes
/// (to create diffs between two states). Comparing usizes is more efficient
/// than re-creating the whole DOM and serves as a caching mechanism.
impl<T: Layout> Hash for Callback<T> {
fn hash<H>(&self, state: &mut H) where H: Hasher {
state.write_usize(self.0 as usize);
}
}
/// Basically compares the function pointers and types for equality
impl<T: Layout> PartialEq for Callback<T> {
fn eq(&self, rhs: &Self) -> bool {
self.0 as usize == rhs.0 as usize
}
}
impl<T: Layout> Eq for Callback<T> { }
impl<T: Layout> Copy for Callback<T> { }
/// List of core DOM node types built-into by `azul`.
pub enum NodeType<T: Layout> {
/// Regular div with no particular type of data attached
Div,
/// A small label that can be (optionally) be selectable with the mouse
Label(String),
/// Larger amount of text, that has to be cached
Text(TextId),
/// An image that is rendered by webrender. The id is aquired by the
/// `AppState::add_image()` function
Image(ImageId),
/// OpenGL texture. The `Svg` widget deserizalizes itself into a texture
/// Equality and Hash values are only checked by the OpenGl texture ID,
/// azul does not check that the contents of two textures are the same
GlTexture((GlTextureCallback<T>, StackCheckedPointer<T>)),
/// DOM that gets passed its width / height during the layout
IFrame((IFrameCallback<T>, StackCheckedPointer<T>)),
}
pub struct GlTextureCallback<T: Layout>(pub fn(&StackCheckedPointer<T>, WindowInfo<T>, usize, usize) -> Option<Texture>);
// #[derive(Debug, Clone, PartialEq, Hash, Eq)] for GlTextureCallback<T>
impl<T: Layout> fmt::Debug for GlTextureCallback<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GlTextureCallback @ 0x{:x}", self.0 as usize)
}
}
impl<T: Layout> Clone for GlTextureCallback<T> {
fn clone(&self) -> Self {
GlTextureCallback(self.0.clone())
}
}
impl<T: Layout> Hash for GlTextureCallback<T> {
fn hash<H>(&self, state: &mut H) where H: Hasher {
state.write_usize(self.0 as usize);
}
}
impl<T: Layout> PartialEq for GlTextureCallback<T> {
fn eq(&self, rhs: &Self) -> bool {
self.0 as usize == rhs.0 as usize
}
}
impl<T: Layout> Eq for GlTextureCallback<T> { }
impl<T: Layout> Copy for GlTextureCallback<T> { }
pub struct IFrameCallback<T: Layout>(pub fn(&StackCheckedPointer<T>, WindowInfo<T>, usize, usize) -> Dom<T>);
// #[derive(Debug, Clone, PartialEq, Hash, Eq)] for IFrameCallback<T>
impl<T: Layout> fmt::Debug for IFrameCallback<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "IFrameCallback @ 0x{:x}", self.0 as usize)
}
}
impl<T: Layout> Clone for IFrameCallback<T> {
fn clone(&self) -> Self {
IFrameCallback(self.0.clone())
}
}
impl<T: Layout> Hash for IFrameCallback<T> {
fn hash<H>(&self, state: &mut H) where H: Hasher {
state.write_usize(self.0 as usize);
}
}
impl<T: Layout> PartialEq for IFrameCallback<T> {
fn eq(&self, rhs: &Self) -> bool {
self.0 as usize == rhs.0 as usize
}
}
impl<T: Layout> Eq for IFrameCallback<T> { }
impl<T: Layout> Copy for IFrameCallback<T> { }
// #[derive(Debug, Clone, PartialEq, Hash, Eq)] for NodeType<T>
impl<T: Layout> fmt::Debug for NodeType<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::NodeType::*;
match self {
Div => write!(f, "NodeType::Div"),
Label(a) => write!(f, "NodeType::Label {{ {:?} }}", a),
Text(a) => write!(f, "NodeType::Text {{ {:?} }}", a),
Image(a) => write!(f, "NodeType::Image {{ {:?} }}", a),
GlTexture((ptr, cb)) => write!(f, "NodeType::GlTexture {{ ptr: {:?}, callback: {:?} }}", ptr, cb),
IFrame((ptr, cb)) => write!(f, "NodeType::IFrame {{ ptr: {:?}, callback: {:?} }}", ptr, cb),
}
}
}
impl<T: Layout> Clone for NodeType<T> {
fn clone(&self) -> Self {
use self::NodeType::*;
match self {
Div => Div,
Label(a) => Label(a.clone()),
Text(a) => Text(a.clone()),
Image(a) => Image(a.clone()),
GlTexture((ptr, a)) => GlTexture((ptr.clone(), a.clone())),
IFrame((ptr, a)) => IFrame((ptr.clone(), a.clone())),
}
}
}
impl<T: Layout> Hash for NodeType<T> {
fn hash<H>(&self, state: &mut H) where H: Hasher {
use self::NodeType::*;
use std::mem;
mem::discriminant(&self).hash(state);
match self {
Div => { },
Label(a) => a.hash(state),
Text(a) => a.hash(state),
Image(a) => a.hash(state),
GlTexture((ptr, a)) => {
ptr.hash(state);
a.hash(state);
},
IFrame((ptr, a)) => {
ptr.hash(state);
a.hash(state);
},
}
}
}
impl<T: Layout> PartialEq for NodeType<T> {
fn eq(&self, rhs: &Self) -> bool {
use self::NodeType::*;
match (self, rhs) {
(Div, Div) => true,
(Label(a), Label(b)) => a == b,
(Text(a), Text(b)) => a == b,
(Image(a), Image(b)) => a == b,
(GlTexture((ptr_a, a)), GlTexture((ptr_b, b))) => {
a == b && ptr_a == ptr_b
},
(IFrame((ptr_a, a)), IFrame((ptr_b, b))) => {
a == b && ptr_a == ptr_b
},
_ => false,
}
}
}
impl<T: Layout> Eq for NodeType<T> { }
impl<T: Layout> NodeType<T> {
pub(crate) fn get_css_id(&self) -> &'static str {
use self::NodeType::*;
match self {
Div => "div",
Label(_) | Text(_) => "p",
Image(_) => "image",
GlTexture(_) => "texture",
IFrame(_) => "iframe",
}
}
}
/// OpenGL texture, use `ReadOnlyWindow::create_texture` to create a texture
///
/// **WARNING**: Don't forget to call `ReadOnlyWindow::unbind_framebuffer()`
/// when you are done with your OpenGL drawing, otherwise webrender will render
/// to the texture, not the window, so your texture will actually never show up.
/// If you use a `Texture` and you get a blank screen, this is probably why.
#[derive(Debug, Clone)]
pub struct Texture {
pub(crate) inner: Rc<Texture2d>,
}
impl Texture {
pub(crate) fn new(tex: Texture2d) -> Self {
Self {
inner: Rc::new(tex),
}
}
/// Prepares the texture for drawing - you can only draw
/// on a framebuffer, the texture itself is readonly from the
/// OpenGL drivers point of view.
///
/// **WARNING**: Don't forget to call `ReadOnlyWindow::unbind_framebuffer()`
/// when you are done with your OpenGL drawing, otherwise webrender will render
/// to the texture instead of the window, so your texture will actually
/// never show up on the screen, since it is never rendered.
/// If you use a `Texture` and you get a blank screen, this is probably why.
pub fn as_surface<'a>(&'a self) -> SimpleFrameBuffer<'a> {
self.inner.as_surface()
}
}
impl Hash for Texture {
fn hash<H: Hasher>(&self, state: &mut H) {
use glium::GlObject;
self.inner.get_id().hash(state);
}
}
impl PartialEq for Texture {
/// Note: Comparison uses only the OpenGL ID, it doesn't compare the
/// actual contents of the texture.
fn eq(&self, other: &Texture) -> bool {
use glium::GlObject;
self.inner.get_id() == other.inner.get_id()
}
}
impl Eq for Texture { }
/// When to call a callback action - `On::MouseOver`, `On::MouseOut`, etc.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum On {
/// Mouse cursor is hovering over the element
MouseOver,
/// Mouse cursor has is over element and is pressed
/// (not good for "click" events - use `MouseUp` instead)
MouseDown,
/// (Specialization of `MouseDown`). Fires only if the left mouse button
/// has been pressed while cursor was over the element
LeftMouseDown,
/// (Specialization of `MouseDown`). Fires only if the middle mouse button
/// has been pressed while cursor was over the element
MiddleMouseDown,
/// (Specialization of `MouseDown`). Fires only if the right mouse button
/// has been pressed while cursor was over the element
RightMouseDown,
/// Mouse button has been released while cursor was over the element
MouseUp,
/// (Specialization of `MouseUp`). Fires only if the left mouse button has
/// been released while cursor was over the element
LeftMouseUp,
/// (Specialization of `MouseUp`). Fires only if the middle mouse button has
/// been released while cursor was over the element
MiddleMouseUp,
/// (Specialization of `MouseUp`). Fires only if the right mouse button has
/// been released while cursor was over the element
RightMouseUp,
/// Mouse cursor has entered the element
MouseEnter,
/// Mouse cursor has left the element
MouseLeave,
/// Mousewheel / touchpad scrolling
Scroll,
/// A key was pressed. Check `window.get_keyboard_state().current_chars` for
/// getting the actual key / virtual key / scancode.
///
/// Warning: key repeat is on. When a key is held down, this event fires
/// multiple times, the delay between events depends on the operating system.
KeyDown,
/// A key was released. Check `window.get_keyboard_state().current_chars` for
/// getting the actual key / virtual key / scancode
///
/// Warning: key repeat is on. When a key is held down, this event fires
/// multiple times, the delay between events depends on the operating system.
KeyUp,
}
pub struct NodeData<T: Layout> {
/// `div`
pub node_type: NodeType<T>,
/// `#main`
pub id: Option<String>,
/// `.myclass .otherclass`
pub classes: Vec<String>,
/// `onclick` -> `my_button_click_handler`
pub events: CallbackList<T>,
/// Usually not set by the user directly - `FakeWindow::push_default_callback`
/// returns a callback ID, so that we know which default callback(s) are attached
/// to this node.
///
/// This is only important if this node has any default callbacks.
pub default_callback_ids: BTreeMap<On, DefaultCallbackId>,
}
impl<T: Layout> PartialEq for NodeData<T> {
fn eq(&self, other: &Self) -> bool {
self.node_type == other.node_type &&
self.id == other.id &&
self.classes == other.classes &&
self.events == other.events &&
self.default_callback_ids == other.default_callback_ids
}
}
impl<T: Layout> Eq for NodeData<T> { }
impl<T: Layout> Default for NodeData<T> {
fn default() -> Self {
NodeData {
node_type: NodeType::Div,
id: None,
classes: Vec::new(),
events: CallbackList::default(),
default_callback_ids: BTreeMap::new(),
}
}
}
impl<T: Layout> Hash for NodeData<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.node_type.hash(state);
self.id.hash(state);
for class in &self.classes {
class.hash(state);
}
for default_callback_id in &self.default_callback_ids {
default_callback_id.hash(state);
}
self.events.hash(state);
}
}
impl<T: Layout> NodeData<T> {
pub(crate) fn calculate_node_data_hash(&self) -> DomHash {
use std::hash::Hash;
// Pick hash algorithm based on features
#[cfg(feature = "faster-hashing")]
use twox_hash::XxHash as HashAlgorithm;
#[cfg(not(feature = "faster-hashing"))]
use std::collections::hash_map::DefaultHasher as HashAlgorithm;
let mut hasher = HashAlgorithm::default();
self.hash(&mut hasher);
DomHash(hasher.finish())
}
}
impl<T: Layout> Clone for NodeData<T> {
fn clone(&self) -> Self {
Self {
node_type: self.node_type.clone(),
id: self.id.clone(),
classes: self.classes.clone(),
events: self.events.special_clone(),
default_callback_ids: self.default_callback_ids.clone(),
}
}
}
impl<T: Layout> fmt::Debug for NodeData<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"NodeData {{ \
\tnode_type: {:?}, \
\tid: {:?}, \
\tclasses: {:?}, \
\tevents: {:?}, \
\tdefault_callback_ids: {:?}, \
}}",
self.node_type,
self.id,
self.classes,
self.events,
self.default_callback_ids)
}
}
impl<T: Layout> PartialEq for CallbackList<T> {
fn eq(&self, rhs: &Self) -> bool {
if self.callbacks.len() != rhs.callbacks.len() {
return false;
}
self.callbacks.iter().all(|(key, val)| {
rhs.callbacks.get(key) == Some(val)
})
}
}
impl<T: Layout> CallbackList<T> {
fn special_clone(&self) -> Self {
Self {
callbacks: self.callbacks.clone(),
}
}
}
impl<T: Layout> NodeData<T> {
/// Creates a new NodeData
pub fn new(node_type: NodeType<T>) -> Self {
Self {
node_type: node_type,
id: None,
classes: Vec::new(),
events: CallbackList::<T>::new(),
default_callback_ids: BTreeMap::new(),
}
}
/// Since `#[derive(Clone)]` requires `T: Clone`, we currently
/// have to make our own version
fn special_clone(&self) -> Self {
Self {
node_type: self.node_type.clone(),
id: self.id.clone(),
classes: self.classes.clone(),
events: self.events.special_clone(),
default_callback_ids: self.default_callback_ids.clone(),
}
}
}
/// The document model, similar to HTML. This is a create-only structure, you don't actually read anything back
#[derive(Clone, PartialEq, Eq)]
pub struct Dom<T: Layout> {
pub(crate) arena: Rc<RefCell<Arena<NodeData<T>>>>,
pub(crate) root: NodeId,
pub(crate) head: NodeId,
}
impl<T: Layout> fmt::Debug for Dom<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"Dom {{ \
\tarena: {:?}, \
\troot: {:?}, \
\thead: {:?}, \
}}",
self.arena,
self.root,
self.head)
}
}
#[derive(Clone, Eq)]
pub struct CallbackList<T: Layout> {
pub callbacks: BTreeMap<On, Callback<T>>
}
impl<T: Layout> Default for CallbackList<T> {
fn default() -> Self {
Self {
callbacks: BTreeMap::default(),
}
}
}
impl<T: Layout> Hash for CallbackList<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
for callback in &self.callbacks {
callback.hash(state);
}
}
}
impl<T: Layout> fmt::Debug for CallbackList<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CallbackList (length: {:?})", self.callbacks.len())
}
}
impl<T: Layout> CallbackList<T> {
pub fn new() -> Self {
Self {
callbacks: BTreeMap::new(),
}
}
}
use std::iter::FromIterator;
impl<T: Layout> FromIterator<Dom<T>> for Dom<T> {
fn from_iter<I: IntoIterator<Item=Dom<T>>>(iter: I) -> Self {
let mut c = Dom::new(NodeType::Div);
for i in iter {
c.add_child(i);
}
c
}
}
impl<T: Layout> FromIterator<NodeData<T>> for Dom<T> {
fn from_iter<I: IntoIterator<Item=NodeData<T>>>(iter: I) -> Self {
use id_tree::Node;
let mut nodes = Vec::new();
let mut idx = 0;
for i in iter {
let node = Node {
data: i,
parent: None,
previous_sibling: if idx == 0 { None } else { Some(NodeId::new(idx - 1)) },
next_sibling: Some(NodeId::new(idx + 1)),
last_child: None,
first_child: None,
};
nodes.push(node);
idx += 1;
}
let nodes_len = nodes.len();
if nodes_len > 0 {
if let Some(last) = nodes.get_mut(nodes_len - 1) {
last.next_sibling = None;
}
} else {
// WARNING: nodes can be empty, so the root
// could point to an invalid node!
}
Dom { head: NodeId::new(0), root: NodeId::new(0), arena: Rc::new(RefCell::new(Arena { nodes })) }
}
}
impl<T: Layout> FromIterator<NodeType<T>> for Dom<T> {
fn from_iter<I: IntoIterator<Item=NodeType<T>>>(iter: I) -> Self {
iter.into_iter().map(|i| NodeData { node_type: i, .. Default::default() }).collect()
}
}
impl<T: Layout> Dom<T> {
/// Creates an empty DOM
#[inline]
pub fn new(node_type: NodeType<T>) -> Self {
Self::with_capacity(node_type, 0)
}
/// Returns the number of nodes in this DOM
#[inline]
pub fn len(&self) -> usize {
self.arena.borrow().nodes_len()
}
/// Creates an empty DOM with space reserved for `cap` nodes
#[inline]
pub fn with_capacity(node_type: NodeType<T>, cap: usize) -> Self {
let mut arena = Arena::with_capacity(cap.saturating_add(1));
let root = arena.new_node(NodeData::new(node_type));
Self {
arena: Rc::new(RefCell::new(arena)),
root: root,
head: root,
}
}
/// Adds a sibling to the current DOM
pub fn add_sibling(&mut self, sibling: Self) {
// Note: for a more readable Python version of this algorithm,
// see: https://gist.github.com/fschutt/4b3bd9a2654b548a6eb0b6a8623bdc8a#file-dow_new_2-py-L32-L63
let self_len = self.arena.borrow().nodes_len();
let sibling_len = sibling.arena.borrow().nodes_len();
if sibling_len == 0 {
return; // No nodes to append, nothing to do
}
if self_len == 0 {
*self = sibling;
return;
}
let mut self_arena = self.arena.borrow_mut();
let mut sibling_arena = sibling.arena.borrow_mut();
for node_id in 0..sibling_len {
let node: &mut Node<NodeData<T>> = &mut sibling_arena[NodeId::new(node_id)];
// NOTE: we cannot directly match on the option, since it leads to borrwowing issues
// We can't do `node.parent` in the `None` branch, since Rust thinks we still have access
// to the borrowed value because `node.parent_mut()` lives too long
if node.parent_mut().and_then(|parent| {
// Some(parent) - increase the parent by the current arena length
*parent += self_len;
Some(parent)
}).is_none() {
// No parent - insert the current arenas head as the parent of the node
node.parent = self_arena[self.head].parent;
}
if node.previous_sibling_mut().and_then(|previous_sibling| {
*previous_sibling += self_len;
Some(previous_sibling)
}).is_none() {
node.previous_sibling = Some(self.head);
}
if let Some(next_sibling) = node.next_sibling_mut() {
*next_sibling += self_len;
}
if let Some(first_child) = node.first_child_mut() {
*first_child += self_len;
}
if let Some(last_child) = node.last_child_mut() {
*last_child += self_len;
}
}
let head_node_id = NodeId::new(self_len);
self_arena[self.head].next_sibling = Some(head_node_id);
self.head = head_node_id;
(&mut *self_arena).append(&mut sibling_arena);
}
/// Adds a child DOM to the current DOM
pub fn add_child(&mut self, child: Self) {
// Note: for a more readable Python version of this algorithm,
// see: https://gist.github.com/fschutt/4b3bd9a2654b548a6eb0b6a8623bdc8a#file-dow_new_2-py-L65-L107
let self_len = self.arena.borrow().nodes_len();
let child_len = child.arena.borrow().nodes_len();
if child_len == 0 {
// No nodes to append, nothing to do
return;
}
if self_len == 0 {
// Self has no nodes, therefore all child nodes will
// replace the self nodes, so
*self = child;
return;
}
let mut self_arena = self.arena.borrow_mut();
let mut child_arena = child.arena.borrow_mut();
let mut last_sibling = None;
for node_id in 0..child_len {
let node_id = NodeId::new(node_id);
let node: &mut Node<NodeData<T>> = &mut child_arena[node_id];
// WARNING: Order of these blocks is important!
if node.previous_sibling_mut().and_then(|previous_sibling| {
// Some(previous_sibling) - increase the parent ID by the current arena length
*previous_sibling += self_len;
Some(previous_sibling)
}).is_none() {
// None - set the current heads' last child as the new previous sibling
let last_child = self_arena[self.head].last_child;
if last_child.is_some() && node.parent.is_none() {
node.previous_sibling = last_child;
self_arena[last_child.unwrap()].next_sibling = Some(node_id + self_len);
}
}
if node.parent_mut().and_then(|parent| {
*parent += self_len;
Some(parent)
}).is_none() {
// Have we encountered the last root item?
if node.next_sibling.is_none() {
last_sibling = Some(node_id);
}
node.parent = Some(self.head);
}
if let Some(next_sibling) = node.next_sibling_mut() {
*next_sibling += self_len;
}
if let Some(first_child) = node.first_child_mut() {
*first_child += self_len;
}
if let Some(last_child) = node.last_child_mut() {
*last_child += self_len;
}
}
self_arena[self.head].first_child.get_or_insert(NodeId::new(self_len));
self_arena[self.head].last_child = Some(last_sibling.unwrap() + self_len);
(&mut *self_arena).append(&mut child_arena);
}
/// Same as `id`, but easier to use for method chaining in a builder-style pattern
#[inline]
pub fn with_id<S: Into<String>>(mut self, id: S) -> Self {
self.set_id(id);
self
}
/// Same as `id`, but easier to use for method chaining in a builder-style pattern
#[inline]
pub fn with_class<S: Into<String>>(mut self, class: S) -> Self {
self.push_class(class);
self
}
/// Same as `event`, but easier to use for method chaining in a builder-style pattern
#[inline]
pub fn with_callback(mut self, on: On, callback: Callback<T>) -> Self {
self.push_callback(on, callback);
self
}
#[inline]
pub fn with_child(mut self, child: Self) -> Self {
self.add_child(child);
self
}
#[inline]
pub fn with_sibling(mut self, sibling: Self) -> Self {
self.add_sibling(sibling);
self
}
#[inline]
pub fn set_id<S: Into<String>>(&mut self, id: S) {
self.arena.borrow_mut()[self.head].data.id = Some(id.into());
}
#[inline]
pub fn push_class<S: Into<String>>(&mut self, class: S) {
self.arena.borrow_mut()[self.head].data.classes.push(class.into());
}
#[inline]
pub fn push_callback(&mut self, on: On, callback: Callback<T>) {
self.arena.borrow_mut()[self.head].data.events.callbacks.insert(on, callback);
}
#[inline]
pub fn push_default_callback_id(&mut self, on: On, id: DefaultCallbackId) {
self.arena.borrow_mut()[self.head].data.default_callback_ids.insert(on, id);
}
}
pub type TagId = u64;
fn new_tag_id() -> TagId {
TAG_ID.fetch_add(1, Ordering::SeqCst) as TagId
}
impl<T: Layout> Dom<T> {
pub(crate) fn collect_callbacks(
&self,
tag_ids_to_callback_list: &mut BTreeMap<TagId, BTreeMap<On, Callback<T>>>,
tag_ids_to_default_callback_list: &mut BTreeMap<TagId, BTreeMap<On, DefaultCallbackId>>,
node_ids_to_tag_ids: &mut BTreeMap<NodeId, TagId>,
tag_ids_to_node_ids: &mut BTreeMap<TagId, NodeId>)
{
for item in self.root.traverse(&*self.arena.borrow()) {
let node_id = item.inner_value();
let item = &self.arena.borrow()[node_id];
let mut node_tag_id = None;
if !item.data.events.callbacks.is_empty() {
let tag_id = new_tag_id();
tag_ids_to_callback_list.insert(tag_id, item.data.events.callbacks.clone());
node_tag_id = Some(tag_id);
}
if !item.data.default_callback_ids.is_empty() {
let tag_id = node_tag_id.unwrap_or(new_tag_id());
tag_ids_to_default_callback_list.insert(tag_id, item.data.default_callback_ids.clone());
node_tag_id = Some(tag_id);
}
if let Some(tag_id) = node_tag_id {
tag_ids_to_node_ids.insert(tag_id, node_id);
node_ids_to_tag_ids.insert(node_id, tag_id);
}
}
TAG_ID.swap(0, Ordering::SeqCst);
}
}
#[test]
fn test_dom_sibling_1() {
struct TestLayout { }
impl Layout for TestLayout {
fn layout(&self) -> Dom<Self> {
Dom::new(NodeType::Div)
.with_child(
Dom::new(NodeType::Div)
.with_id("sibling-1")
.with_child(Dom::new(NodeType::Div)
.with_id("sibling-1-child-1")))
.with_child(Dom::new(NodeType::Div)
.with_id("sibling-2")
.with_child(Dom::new(NodeType::Div)
.with_id("sibling-2-child-1")))
}
}
let dom = TestLayout{ }.layout();
let arena = dom.arena.borrow();
assert_eq!(NodeId::new(0), dom.root);
assert_eq!(Some(String::from("sibling-1")),
arena[
arena[dom.root]
.first_child().expect("root has no first child")
].data.id);
assert_eq!(Some(String::from("sibling-2")),
arena[
arena[
arena[dom.root]
.first_child().expect("root has no first child")
].next_sibling().expect("root has no second sibling")
].data.id);
assert_eq!(Some(String::from("sibling-1-child-1")),
arena[
arena[
arena[dom.root]
.first_child().expect("root has no first child")
].first_child().expect("first child has no first child")
].data.id);
assert_eq!(Some(String::from("sibling-2-child-1")),
arena[
arena[
arena[
arena[dom.root]
.first_child().expect("root has no first child")
].next_sibling().expect("first child has no second sibling")
].first_child().expect("second sibling has no first child")
].data.id);
}
#[test]
fn test_dom_from_iter_1() {
use id_tree::Node;
struct TestLayout { }
impl Layout for TestLayout {
fn layout(&self) -> Dom<Self> {
(0..5).map(|e| NodeData::new(NodeType::Label(format!("{}", e + 1)))).collect()
}
}
let dom = TestLayout{ }.layout();
let arena = dom.arena.borrow();
assert_eq!(arena.nodes.last(), Some(&Node {
parent: None,
previous_sibling: Some(NodeId::new(3)),
next_sibling: None,
first_child: None,
last_child: None,
data: NodeData {
node_type: NodeType::Label(String::from("5")),
id: None,
classes: Vec::new(),
default_callback_ids: BTreeMap::new(),
events: CallbackList::default(),
}
}));
}