nuit_core/compose/view/widget/
picker.rs

1use nuit_derive::Bind;
2
3use crate::{View, Node, Context, Binding, Event, IdPath, Id, IdentifyExt};
4
5/// A view that lets the user choose a value.
6#[derive(Debug, Clone, Bind)]
7pub struct Picker<C> {
8    title: String,
9    selection: Binding<Id>,
10    content: C,
11}
12
13impl<C> Picker<C> {
14    pub fn new(title: impl Into<String>, selection: Binding<Id>, content: C) -> Self {
15        Self { title: title.into(), selection, content }
16    }
17}
18
19impl<C> View for Picker<C> where C: View {
20    fn fire(&self, event: &Event, event_path: &IdPath, _context: &Context) {
21        assert!(event_path.is_root());
22        if let Event::UpdatePickerSelection { id } = event {
23            self.selection.set(id.clone());
24        }
25    }
26
27    fn render(&self, context: &Context) -> Node {
28        Node::Picker {
29            title: self.title.clone(),
30            selection: self.selection.get(),
31            content: Box::new(self.content.render(&context.child(0)).identify(0)),
32        }
33    }
34}