raui_core/widget/
context.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
use crate::{
    animator::{Animator, AnimatorStates},
    messenger::{MessageSender, Messenger},
    props::Props,
    signals::SignalSender,
    state::State,
    view_model::ViewModelCollectionView,
    widget::{node::WidgetNode, WidgetId, WidgetLifeCycle, WidgetRef},
};
use std::collections::HashMap;

pub struct WidgetContext<'a> {
    pub id: &'a WidgetId,
    pub idref: Option<&'a WidgetRef>,
    pub key: &'a str,
    pub props: &'a mut Props,
    pub shared_props: &'a mut Props,
    pub state: State<'a>,
    pub animator: &'a AnimatorStates,
    pub life_cycle: &'a mut WidgetLifeCycle,
    pub named_slots: HashMap<String, WidgetNode>,
    pub listed_slots: Vec<WidgetNode>,
    pub view_models: ViewModelCollectionView<'a>,
}

impl<'a> WidgetContext<'a> {
    pub fn take_named_slots(&mut self) -> HashMap<String, WidgetNode> {
        std::mem::take(&mut self.named_slots)
    }

    pub fn take_named_slot(&mut self, name: &str) -> WidgetNode {
        self.named_slots.remove(name).unwrap_or_default()
    }

    pub fn take_listed_slots(&mut self) -> Vec<WidgetNode> {
        std::mem::take(&mut self.listed_slots)
    }

    pub fn use_hook<F>(&mut self, mut f: F) -> &mut Self
    where
        F: FnMut(&mut Self),
    {
        (f)(self);
        self
    }
}

impl<'a> std::fmt::Debug for WidgetContext<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WidgetContext")
            .field("id", &self.id)
            .field("key", &self.key)
            .field("props", &self.props)
            .field("shared_props", &self.shared_props)
            .field("named_slots", &self.named_slots)
            .field("listed_slots", &self.listed_slots)
            .finish()
    }
}

pub struct WidgetMountOrChangeContext<'a> {
    pub id: &'a WidgetId,
    pub props: &'a Props,
    pub shared_props: &'a Props,
    pub state: State<'a>,
    pub messenger: Messenger<'a>,
    pub signals: SignalSender,
    pub animator: Animator<'a>,
    pub view_models: ViewModelCollectionView<'a>,
}

pub struct WidgetUnmountContext<'a> {
    pub id: &'a WidgetId,
    pub state: &'a Props,
    pub messenger: &'a MessageSender,
    pub signals: SignalSender,
    pub view_models: ViewModelCollectionView<'a>,
}