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
use crate::{View, Node, Bind, Context, Binding, Event, IdPath, Id, IdentifyExt};

#[derive(Debug, Clone)]
pub struct Picker<C> {
    title: String,
    selection: Binding<Id>,
    content: C,
}

impl<C> Picker<C> {
    pub fn new(title: impl Into<String>, selection: Binding<Id>, content: C) -> Self {
        Self { title: title.into(), selection, content }
    }
}

impl<C> Bind for Picker<C> where C: Bind {}

impl<C> View for Picker<C> where C: View {
    fn fire(&self, event: &Event, id_path: &IdPath) {
        assert!(id_path.is_root());
        if let Event::UpdatePickerSelection { id } = event {
            self.selection.set(id.clone());
        }
    }

    fn render(&mut self, context: &Context) -> Node {
        Node::Picker {
            title: self.title.clone(),
            selection: self.selection.get(),
            content: Box::new(self.content.render(&context.child(0)).identify(0)),
        }
    }
}