raui_core/widget/
context.rs1use crate::{
2 animator::{Animator, AnimatorStates},
3 messenger::{MessageSender, Messenger},
4 props::Props,
5 signals::SignalSender,
6 state::State,
7 view_model::ViewModelCollectionView,
8 widget::{WidgetId, WidgetLifeCycle, WidgetRef, node::WidgetNode},
9};
10use std::collections::HashMap;
11
12pub struct WidgetContext<'a> {
13 pub id: &'a WidgetId,
14 pub idref: Option<&'a WidgetRef>,
15 pub key: &'a str,
16 pub props: &'a mut Props,
17 pub shared_props: &'a mut Props,
18 pub state: State<'a>,
19 pub animator: &'a AnimatorStates,
20 pub life_cycle: &'a mut WidgetLifeCycle,
21 pub named_slots: HashMap<String, WidgetNode>,
22 pub listed_slots: Vec<WidgetNode>,
23 pub view_models: ViewModelCollectionView<'a>,
24}
25
26impl WidgetContext<'_> {
27 pub fn take_named_slots(&mut self) -> HashMap<String, WidgetNode> {
28 std::mem::take(&mut self.named_slots)
29 }
30
31 pub fn take_named_slot(&mut self, name: &str) -> WidgetNode {
32 self.named_slots.remove(name).unwrap_or_default()
33 }
34
35 pub fn take_listed_slots(&mut self) -> Vec<WidgetNode> {
36 std::mem::take(&mut self.listed_slots)
37 }
38
39 pub fn use_hook<F>(&mut self, mut f: F) -> &mut Self
40 where
41 F: FnMut(&mut Self),
42 {
43 (f)(self);
44 self
45 }
46}
47
48impl std::fmt::Debug for WidgetContext<'_> {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 f.debug_struct("WidgetContext")
51 .field("id", &self.id)
52 .field("key", &self.key)
53 .field("props", &self.props)
54 .field("shared_props", &self.shared_props)
55 .field("named_slots", &self.named_slots)
56 .field("listed_slots", &self.listed_slots)
57 .finish()
58 }
59}
60
61pub struct WidgetMountOrChangeContext<'a> {
62 pub id: &'a WidgetId,
63 pub props: &'a Props,
64 pub shared_props: &'a Props,
65 pub state: State<'a>,
66 pub messenger: Messenger<'a>,
67 pub signals: SignalSender,
68 pub animator: Animator<'a>,
69 pub view_models: ViewModelCollectionView<'a>,
70}
71
72pub struct WidgetUnmountContext<'a> {
73 pub id: &'a WidgetId,
74 pub state: &'a Props,
75 pub messenger: &'a MessageSender,
76 pub signals: SignalSender,
77 pub view_models: ViewModelCollectionView<'a>,
78}