nuit_core/compose/view/wrapper/
modified.rs

1use nuit_derive::Bind;
2
3use crate::{Context, Event, Id, IdPath, IdentifyExt, ModifierNode, Node, View};
4
5/// A view that applies a modifier.
6#[derive(Debug, Clone, PartialEq, Bind)]
7pub struct Modified<T> {
8    wrapped: T,
9    modifier: ModifierNode,
10}
11
12impl<T> Modified<T> {
13    pub fn new(wrapped: T, modifier: ModifierNode) -> Self {
14        Self {
15            wrapped,
16            modifier,
17        }
18    }
19}
20
21impl<T> View for Modified<T> where T: View {
22    fn fire(&self, event: &Event, event_path: &IdPath, context: &Context) {
23        if let Some(head) = event_path.head() {
24            match head {
25                Id::Index(0) => self.wrapped.fire(event, &event_path.tail(), &context.child(0)),
26                i => panic!("Cannot fire event for child id {} on Modified which only has one child", i)
27            }
28        }
29    }
30
31    fn render(&self, context: &Context) -> Node {
32        Node::Modified {
33            wrapped: Box::new(self.wrapped.render(&context.child(0)).identify(0)),
34            modifier: self.modifier.clone(),
35        }
36    }
37}