acrylic 0.3.5

ultra-portable, web-inspired UI toolkit with SIMD graphics
Documentation
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
//! The state of your Application

use super::xml::{XmlNodeTree, XmlNodeKey, AttributeValue, AttributeValueVec, AttributeValueType};
use crate::{Error, error, String, ArcStr, Vec, Box, Rc, HashMap, LiteMap, DEFAULT_FONT_NAME};
use super::visual::{Pixels, Position, Size, write_framebuffer, constrain, Texture as _};
use super::style::{Theme, Style, DEFAULT_STYLE};
use super::layout::{compute_layout, hit_test};
use super::node::{NodeTree, NodeKey, Mutator};
use super::state::{Namespace, root_ns};
use core::{time::Duration, ops::Deref};
use super::event::UserInputEvent;
use super::text_edit::Cursor;
use super::for_each_child;
use super::rgb::RGBA8;

use oakwood::{NodeKey as _};
use lmfu::json::{JsonFile, Value, Path, parse_path};

use super::glyph::FONT_MUTATOR;

use crate::builtin::{
    inflate::INFLATE_MUTATOR,
    import::IMPORT_MUTATOR,
    png::PNG_MUTATOR,
    container::CONTAINERS,
    label::LABEL_MUTATOR,
    paragraph::{PARAGRAPH_MUTATOR, UNBREAKABLE_MUTATOR},
};

#[cfg(doc)]
use super::node::Node;

/// General-purpose callbacks that containers can call based on their attributes.
pub type SimpleCallback = fn(&mut Application, NodeKey) -> Result<(), Error>;

/// General-purpose callbacks that containers can call based on their attributes.
pub type SimpleCallbackMap = HashMap<ArcStr, SimpleCallback>;

struct Request {
    asset: ArcStr,
    parse: bool,
    origin: NodeKey,
}

enum Asset {
    Parsed,
    Raw(Rc<[u8]>),
}

pub struct DebuggingOptions {
    pub skip_glyph_rendering: bool,
    pub skip_container_decoration: bool,
    pub freeze_layout: bool,
    pub draw_layout: bool,
}

/// A Singleton which represents your application.
///
/// Its content includes:
/// - the list of [`Mutator`]s
/// - the XML layout
/// - the JSON state and related triggers
/// - the internal view representation (a Node tree)
/// - the [`Theme`]
/// - a cache of assets
pub struct Application {
    pub root: NodeKey,
    pub view: NodeTree,
    pub xml_tree: XmlNodeTree,
    pub theme: Theme,
    pub callbacks: SimpleCallbackMap,
    pub debug: DebuggingOptions,
    pub state: JsonFile,

    pub(crate) namespaces: LiteMap<NodeKey, Namespace>,
    pub(crate) mutators: Vec<Mutator>,
    pub(crate) text_cursors: Vec<Cursor>,
    implicit_focus: NodeKey,

    focus_coords: Position,
    explicit_focus: Option<NodeKey>,
    must_check_layout: bool,
    _source_files: Vec<String>,
    _age: Duration,
    render_list: Vec<(Position, Size)>,
    assets: HashMap<ArcStr, Asset>,
    requests: Vec<Request>,
}

pub const IMPORT_MUTATOR_INDEX: usize = 0;
pub const FONT_MUTATOR_INDEX: usize = 1;
pub const UNBREAKABLE_MUTATOR_INDEX: usize = 5;

impl Application {
    /// Main constructor
    pub fn new(layout_asset: ArcStr, callbacks: SimpleCallbackMap) -> Self {
        let default_mutators = &[
            IMPORT_MUTATOR,
            FONT_MUTATOR,
            PNG_MUTATOR,
            LABEL_MUTATOR,
            PARAGRAPH_MUTATOR,
            UNBREAKABLE_MUTATOR,
            INFLATE_MUTATOR,
        ];

        assert_eq!(default_mutators[IMPORT_MUTATOR_INDEX].name, "ImportMutator");
        assert_eq!(default_mutators[FONT_MUTATOR_INDEX].name, "FontMutator");
        assert_eq!(default_mutators[UNBREAKABLE_MUTATOR_INDEX].name, "UnbreakableMutator");

        let mut mutators = Vec::with_capacity(default_mutators.len() + CONTAINERS.len());
        mutators.extend_from_slice(default_mutators);
        mutators.extend_from_slice(&CONTAINERS);

        let mut app = Self {
            root: Default::default(),
            view: NodeTree::new(),
            xml_tree: XmlNodeTree::new(),
            state: JsonFile::new(Some(include_str!("default.json"))).unwrap(),
            namespaces: LiteMap::new(),
            // monitors: LiteMap::new(),
            callbacks,
            mutators,
            must_check_layout: false,
            _source_files: Vec::new(),
            text_cursors: Vec::new(),
            focus_coords: Position::zero(),
            implicit_focus: Default::default(),
            explicit_focus: None,
            theme: Theme::parse(include_str!("default-theme.json")).unwrap(),
            _age: Duration::from_secs(0),
            render_list: Vec::new(),
            debug: DebuggingOptions {
                skip_glyph_rendering: false,
                skip_container_decoration: false,
                freeze_layout: false,
                draw_layout: false,
            },

            assets: HashMap::new(),
            requests: Vec::new(),
        };

        for i in 0..app.mutators.len() {
            (app.mutators[i].handlers.initializer)(&mut app, i.into()).unwrap();
        }

        if true {
            let default_font = crate::NOTO_SANS.to_vec().into_boxed_slice();
            let font_parser = app.mutators[FONT_MUTATOR_INDEX].handlers.parser;
            font_parser(
                &mut app,
                FONT_MUTATOR_INDEX.into(),
                Default::default(),
                &DEFAULT_FONT_NAME,
                default_font,
            ).unwrap();
            app.assets.insert((*DEFAULT_FONT_NAME).clone(), Asset::Parsed);
        }

        let factory = Some(IMPORT_MUTATOR_INDEX.into()).into();

        let xml_root = app.xml_tree.create();
        app.xml_tree[xml_root].factory = factory;
        app.xml_tree[xml_root].attributes = AttributeValueVec::new_import(layout_asset);

        app.root = app.view.create();
        app.view[app.root].factory = factory;
        app.view[app.root].xml_node_index = Some(xml_root.index().into()).into();

        app.namespaces.insert(app.root, root_ns());
        app.implicit_focus = app.root;

        app.call_populator(app.root, xml_root).unwrap();

        app
    }

    fn node_path(&self, mut node_key: NodeKey) -> Vec<usize> {
        let mut path = Vec::new();

        while let Some(parent) = self.view.parent(node_key) {
            let index = self.view.child_index(node_key).unwrap();
            path.push(index);
            node_key = parent;
        }

        path
    }

    fn resolve_path(&self, mut path: Vec<usize>) -> NodeKey {
        let mut node_key = self.root;

        while let Some(index) = path.pop() {
            node_key = self.view.first_child(node_key).unwrap();
            for _ in 0..index {
                node_key = self.view.next_sibling(node_key);
            }
        }

        node_key
    }

    /// Reload the view, allowing it to pick up state changes
    pub fn reload_view(&mut self) {
        let backup = &self.view[self.root];
        let factory = backup.factory;
        let xml_node_index = backup.xml_node_index;

        let exf = self.explicit_focus.map(|nk| self.node_path(nk));
        let imf = self.node_path(self.implicit_focus);

        self.view.reset(self.root);
        self.invalidate_layout();
        let root_ns = self.namespaces.remove(&self.root).unwrap();
        self.namespaces.clear();
        self.namespaces.insert(self.root, root_ns);

        let root = &mut self.view[self.root];
        root.factory = factory;
        root.xml_node_index = xml_node_index;

        if let Some(xml_root) = xml_node_index.get() {
            let xml_root = self.xml_tree.node_key(xml_root);
            self.call_populator(self.root, xml_root).unwrap();
        }

        self.explicit_focus = exf.map(|p| self.resolve_path(p));
        self.implicit_focus = self.resolve_path(imf);
    }

    /// Quick way to tell the application to recompute its layout before the next frame
    pub fn invalidate_layout(&mut self) {
        self.must_check_layout = true;
    }

    /// Read an asset from the internal cache
    pub fn get_asset(&self, asset: &ArcStr) -> Result<Rc<[u8]>, Error> {
        match self.assets.get(asset) {
            Some(Asset::Raw(rc)) => Ok(rc.clone()),
            Some(Asset::Parsed) => Err(error!("Asset {} was stored in mutator storage", asset.deref())),
            None => Err(error!("Asset {} was not found", asset.deref())),
        }
    }

    /// Platforms use this method to read the next asset to load.
    pub fn requested(&self) -> Option<ArcStr> {
        self.requests.first().map(|r| r.asset.clone())
    }

    /// Notify the system that an asset is required by some [`Node`]
    ///
    /// If `asset` is already loaded, this will trigger
    /// Handling of an `AssetLoaded` event immediately
    pub fn request(&mut self, asset: &ArcStr, origin: NodeKey, parse: bool) -> Result<(), Error> {
        if let Some(content) = self.assets.get(&asset) {
            let illegal = match (parse, content) {
                (true, Asset::Raw(_)) => true,
                (false, Asset::Parsed) => true,
                _ => false,
            };

            if illegal {
                return Err(error!("Asset {} was previously loaded with a different `parse` flag", asset.deref()));
            }

            self.call_finalizer(origin)
        } else {
            self.requests.push(Request {
                asset: asset.clone(),
                origin,
                parse,
            });
            Ok(())
        }
    }

    /// Platforms use this method to deliver an asset's content
    pub fn data_response(&mut self, asset: ArcStr, data: Box<[u8]>) -> Result<(), Error> {
        let mut data = Some(data);

        let mut i = 0;
        while let Some(request) = self.requests.get(i) {
            if request.asset == asset {
                let request = self.requests.swap_remove(i);
                let node_key = request.origin;

                if let Some(data) = data.take() {
                    let result = if request.parse {
                        self.call_parser(node_key, &asset, data)?;

                        Asset::Parsed
                    } else {
                        Asset::Raw(data.into())
                    };

                    self.assets.insert(asset.clone(), result);
                }

                self.request(&asset, request.origin, request.parse)?;
            } else {
                i += 1;
            }
        }

        Ok(())
    }

    /// Retrieves the style that the nearest parent of `node_key` has set.
    pub fn get_inherited_style(&self, node_key: NodeKey) -> Result<Style, Error> {
        let mut parent_style = match self.theme.resolve(DEFAULT_STYLE) {
            Some(style_index) => Ok(style_index),
            None => Err(error!("Missing default style in theme")),
        }?;

        let mut current = node_key;
        while let Some(parent) = self.view.parent(current) {
            if let Some(p_style) = self.view[parent].style_override.get() {
                parent_style = p_style.into();
                break;
            } else {
                current = parent;
            }
        }

        Ok(self.theme.get(parent_style))
    }

    /// Retrieves a value from the JSON state
    pub fn resolve(
        &self,
        node: NodeKey,
        ns_name: &str,
        ns_path: &str,
    ) -> Result<Path, Error> {
        let mut target = node;
        loop {
            match self.namespaces.get(&target) {
                Some(ns) if &*ns.name == ns_name => {
                    let mut jp = ns.path.clone();
                    (ns.callback)(&self, target, node, &mut jp)?;
                    jp.append(parse_path(ns_path));
                    break Ok(jp);
                },
                _ => match self.view.parent(target) {
                    Some(parent) => target = parent,
                    None => break Err(error!("Missing {} namespace", ns_name)),
                },
            }
        }
    }

    /// Retrieves the XML tag name of a node
    ///
    /// This can return the following special strings:
    /// - `<subnode>` if the node wasn't created from an XML tag
    /// - `<anon>` if the node's [`Mutator`] has no defined XML tag
    pub fn xml_tag(&self, node: NodeKey) -> ArcStr {
        let mutator_index = match self.view[node].factory.get() {
            Some(index) => index,
            None => return "<subnode>".into(),
        };
        let mutator = &self.mutators[usize::from(mutator_index)];
        match &mutator.xml_params {
            Some(params) => params.tag_name.clone(),
            None => "<anon>".into(),
        }
    }

    #[doc(hidden)]
    pub fn attr_state_path(&mut self, node: NodeKey, attr: usize) -> Result<Result<(Path, AttributeValueType), AttributeValue>, Error> {
        let xml_node_index = self.view[node].xml_node_index.get()
            .expect("cannot use Application::attr on nodes without xml_node_index");
        let xml_node_key = self.xml_tree.node_key(xml_node_index);
        let xml_node = &self.xml_tree[xml_node_key];

        let (namespace, path, value_type) = match xml_node.attributes.get(attr).clone() {
            AttributeValue::StateLookup { namespace, path, value_type } => (namespace, path, value_type),
            value => return Ok(Err(value)),
        };

        Ok(Ok((self.resolve(node, &namespace, path.deref())?, value_type)))
    }

    /// Retrieves the value of an XML attribute, resolving optional JSON state dependencies.
    ///
    /// # Attribute syntax
    ///
    /// ## Immediate syntax
    /// 
    /// `<png file="acrylic.png" />`
    ///
    /// Here, the `file` attribute will contain the string `acrylic.png`
    ///
    /// ## JSON state dependency
    ///
    /// `<label root:text="some.json.path.items.3" />`
    ///
    /// Here, the `text` attribute will contain the value of the JSON state at `some` / `json` / `path` / `items` / fourth item.
    ///
    /// `root` specifies the main JSON state namespace. Use [Iterating Containers](http://todo.io/) to create other ones.
    pub fn attr<T: TryFrom<AttributeValue, Error=Error>>(
        &mut self,
        node: NodeKey,
        attr: usize,
    ) -> Result<T, Error> {
        use AttributeValueType::*;

        let (json_path, value_type) = match self.attr_state_path(node, attr)? {
            Ok(tuple) => tuple,
            Err(value) => return T::try_from(value),
        };

        let value = match (&self.state[&json_path], value_type) {
            // String dumps:
            (
                Value::Array  (_) |
                Value::Object (_) |
                Value::Number (_) |
                Value::Boolean(_) |
                Value::Null,

                Other | OptOther,
            ) => {
                let string = self.state.dump(&json_path)
                    .map_err(|e| error!("unexpected fmt error: {:?}", e))?;

                match value_type {
                    OptOther => AttributeValue::OptOther(Some(string)),
                    Other => AttributeValue::Other(string),
                    _ => unreachable!(),
                }
            },

            // Common conversions:
            (Value::String(s), _) => AttributeValue::parse(&s, value_type)?,

            _ => return Err(error!("Invalid Attribute Conversion")),
        };

        // self.subscribe_to_state(node, json_path);

        T::try_from(value)
    }

    fn build_render_list(&mut self, fb_rect: &(Position, Size), key: NodeKey, querying: bool) {
        // let _tag = self.xml_tag(key);
        let node = &mut self.view[key];
        if node.config.get_dirty() {
            // log::info!("node {} ({}) is dirty", _tag, key.index());
            node.config.set_dirty(false);

            if querying {
                let mut rect = (node.position, node.size);
                constrain(fb_rect, &mut rect);
                self.render_list.push(rect);
            }

            // all children that are in the container will also be re-rendered
            for_each_child!(self.view, key, child, {
                self.build_render_list(fb_rect, child, false);
            });
        } else {
            for_each_child!(self.view, key, child, {
                self.build_render_list(fb_rect, child, querying);
            });
        }
    }

    fn paint(
        &mut self,
        key: NodeKey,
        fb: &mut [RGBA8],
        stride: usize,
        restrict: &mut (Position, Size),
    ) -> Result<(), Error> {
        let size = self.view[key].size;
        let position = self.view[key].position;
        let texture_coords = (position, size);

        let backup = *restrict;
        constrain(&texture_coords, restrict);

        for sampling_window in &self.render_list {
            let mut sampling_window = *sampling_window;
            constrain(&texture_coords, &mut sampling_window);
            constrain(restrict, &mut sampling_window);
            self.view[key].background.paint(fb, texture_coords, sampling_window, stride, true, false);
        }

        for_each_child!(self.view, key, child, {
            self.paint(child, fb, stride, restrict)?;
        });


        for sampling_window in &self.render_list {
            let mut sampling_window = *sampling_window;
            constrain(&texture_coords, &mut sampling_window);
            constrain(restrict, &mut sampling_window);
            if self.debug.draw_layout {
                super::visual::debug_framebuffer(fb, stride, sampling_window);
            }

            self.view[key].foreground.paint(fb, texture_coords, sampling_window, stride, true, false);
        }

        *restrict = backup;

        Ok(())
    }

    /// Alias of [`hit_test`]
    pub fn hit_test(&self, position: Position) -> NodeKey {
        hit_test(&self.view, self.root, position)
    }

    /// Retrieves the latest value that was passed to [`Self::set_focus_coords`]
    pub fn get_focus_coords(&self) -> Position {
        self.focus_coords
    }

    fn resize_hover_sensitive_nodes(&mut self, node_key: NodeKey) -> Result<(), Error> {
        let mut current = Some(node_key);
        while let Some(node_key) = current {
            if self.view[node_key].config.get_hover_sensitivity() {
                self.call_resizer(node_key)?;
            }
            current = self.view.parent(node_key);
        }

        Ok(())
    }

    /// Returns true if the current focus coords is over a node.
    pub fn is_hovered(&self, node_key: NodeKey) -> bool {
        let mut current = Some(self.implicit_focus);
        while let Some(c) = current {
            if node_key == c {
                return true;
            }
            current = self.view.parent(c);
        }

        false
    }

    /// Updates the position of the pointer device
    pub fn set_focus_coords(&mut self, focus_coords: Position) -> Result<(), Error> {
        let old = self.implicit_focus;
        let new = self.hit_test(focus_coords);
        self.implicit_focus = new;

        if old != new {
            self.resize_hover_sensitive_nodes(old)?;
            self.resize_hover_sensitive_nodes(new)?;
        }

        self.focus_coords = focus_coords;

        Ok(())
    }

    /// Sets a node as explicitely focused
    ///
    /// This is normally called by nodes which want to grab focus.
    ///
    /// The explicitly focused node will receive a FocusLoss event
    /// when it loses focus.
    pub fn set_focused_node(&mut self, node_key: NodeKey) -> Result<(), Error> {
        self.clear_focused_node()?;
        self.explicit_focus = Some(node_key);

        Ok(())
    }

    /// Unsets the current explicit focus
    pub fn clear_focused_node(&mut self) -> Result<(), Error> {
        if let Some(node_key) = self.explicit_focus {
            let input_event = UserInputEvent::FocusLoss;
            self.call_user_input_handler(node_key, &input_event)?;
            self.explicit_focus = None;
        }

        Ok(())
    }

    /// Retrieves the current implicit focus target, which is under the focus coordinates
    pub fn get_implicit_focus(&mut self) -> NodeKey {
        self.implicit_focus
    }

    /// Retrieves the current explicit focus target, which was set using [`Self::set_focused_node`]
    pub fn get_explicit_focus(&mut self) -> Option<NodeKey> {
        self.explicit_focus
    }

    /// Renders the current view in a `framebuffer`.
    ///
    /// This expects the framebuffer to keep its content between calls.
    ///
    /// TODO: remove temporary input code from this and implement Input Events.
    ///
    /// This methods follows the following steps:
    /// - If the framebuffer size changed: invalidate layout & empty framebuffer.
    /// - Recompute the layout if needed.
    /// - Builds a list of dirty rectangles by locating each dirty node in the view
    /// - repaint each rectangle in this list
    pub fn render(
        &mut self,
        fb_size: (usize, usize),
        framebuffer: &mut [RGBA8],
    ) -> Result<&[(Position, Size)], Error> {
        // log::info!("frame");
        let stride = fb_size.0;
        let new_size = Size::new(Pixels::from_num(stride), Pixels::from_num(fb_size.1));

        /*
        let node_key = super::layout::hit_test(&mut self.view, self.root, _mouse);
        let input_event = super::event::UserInputEvent::WheelY(SignedPixels::from_num(wheel_delta));

        self.handle_user_input(node_key, &input_event).unwrap();
        */

        let transparent = RGBA8::new(0, 0, 0, 0);
        let fb_rect = (Position::zero(), new_size);
        self.render_list.clear();

        if self.view[self.root].size != new_size {
            log::info!("fbsize: {}x{}", stride, fb_size.1);
            self.view[self.root].size = new_size;
            self.must_check_layout = true;
            framebuffer.fill(transparent);
            self.render_list.push(fb_rect);
        }

        if self.must_check_layout && !self.debug.freeze_layout {
            log::warn!("recomputing layout");
            compute_layout(self, self.root)?;
            self.must_check_layout = false;
        }

        if self.render_list.len() == 0 {
            self.build_render_list(&fb_rect, self.root, true);
            let mut dirty_pixels = 0;

            for rect in &self.render_list {
                dirty_pixels += (rect.1.w * rect.1.h).to_num::<usize>();
                write_framebuffer(framebuffer, stride, *rect, transparent);
            }

            if self.render_list.len() > 0 {
                log::warn!("{} repaints, total: {} pixels", self.render_list.len(), dirty_pixels);
            }
        }

        if self.render_list.len() > 0 {
            let mut restrict = fb_rect;
            self.paint(self.root, framebuffer, stride, &mut restrict)?;
        }

        Ok(&self.render_list)
    }
}

impl Application {
    pub fn call_parser(&mut self, node_key: NodeKey, asset: &ArcStr, bytes: Box<[u8]>) -> Result<(), Error> {
        match self.view[node_key].factory.get() {
            Some(i) => (self.mutators[usize::from(i)].handlers.parser)(self, i, node_key, asset, bytes),
            None => Err(error!("Node {:?} cannot parse: it has no factory", node_key)),
        }
    }

    pub fn call_populator(&mut self, node_key: NodeKey, xml_node_key: XmlNodeKey) -> Result<(), Error> {
        match self.view[node_key].factory.get() {
            Some(i) => (self.mutators[usize::from(i)].handlers.populator)(self, i, node_key, xml_node_key),
            None => Err(error!("Node {:?} cannot parse: it has no factory", node_key)),
        }
    }

    pub fn call_finalizer(&mut self, node_key: NodeKey) -> Result<(), Error> {
        match self.view[node_key].factory.get() {
            Some(i) => (self.mutators[usize::from(i)].handlers.finalizer)(self, i, node_key),
            None => Err(error!("Node {:?} cannot parse: it has no factory", node_key)),
        }
    }

    pub fn call_resizer(&mut self, node_key: NodeKey) -> Result<(), Error> {
        match self.view[node_key].factory.get() {
            Some(i) => (self.mutators[usize::from(i)].handlers.resizer)(self, i, node_key),
            None => Err(error!("Node {:?} cannot parse: it has no factory", node_key)),
        }
    }

    pub fn call_user_input_handler(&mut self, target: NodeKey, event: &UserInputEvent) -> Result<bool, Error> {
        let mut node_key = target;
        loop {
            match self.view[node_key].factory.get() {
                Some(i) => {
                    let handler = self.mutators[usize::from(i)].handlers.user_input_handler;
                    if handler(self, i, node_key, target, event)? {
                        break Ok(true);
                    } else if let Some(parent) = self.view.parent(node_key) {
                        node_key = parent;
                    } else {
                        break Ok(false);
                    }
                },
                None => break Err(error!("Node {:?} cannot parse: it has no factory", node_key)),
            }
        }
    }
}