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
use std::sync::mpsc::{Receiver, Sender};
/// This is the struct that gets returned when a widget is added to the context.
pub struct Response<R> {
    ///This is used to comunicate with the widget
    pub channel: R,
    ///This is the id of the widget, allowing you to remove it from the context
    pub id: WidgetId,
}

pub(crate) type LayerNummerId = u64;
pub(crate) type WidgetNummerId = u64;
pub(crate) type LayerChannelSender = Sender<(LayerNummerId, LayerInstructions)>;
pub(crate) type LayerChannelReceiver = Receiver<(LayerNummerId, LayerInstructions)>;

pub(crate) type WidgetChannelSender = Sender<((LayerNummerId, WidgetNummerId), WidgetInstruction)>;
pub(crate) type WidgetChannelReceiver =
    Receiver<((LayerNummerId, WidgetNummerId), WidgetInstruction)>;
pub(crate) enum LayerInstructions {
    Drop,
    SetIsActive(bool),
}

pub(crate) enum WidgetInstruction {
    Drop,
}

pub struct LayerId {
    pub(crate) id: LayerNummerId,
    channel: LayerChannelSender,
}
impl LayerId {
    pub(crate) fn new(id: LayerNummerId, channel: LayerChannelSender) -> Self {
        Self { id, channel }
    }

    ///Set a layer to active or inactive.
    ///Layers that are inactive won't be rendered or receive updates.
    pub fn set_is_active(&self, is_active: bool) {
        let _ = self
            .channel
            .send((self.id, LayerInstructions::SetIsActive(is_active)));
    }
}
impl Drop for LayerId {
    fn drop(&mut self) {
        let _ = self.channel.send((self.id, LayerInstructions::Drop));
    }
}
pub struct WidgetId {
    pub(crate) id: WidgetNummerId,
    pub(crate) layer: LayerNummerId,
    channel: WidgetChannelSender,
}
impl Drop for WidgetId {
    fn drop(&mut self) {
        let _ = self
            .channel
            .send(((self.layer, self.id), WidgetInstruction::Drop));
    }
}
impl WidgetId {
    pub(crate) fn new(
        layer: LayerNummerId,
        id: WidgetNummerId,
        channel: WidgetChannelSender,
    ) -> Self {
        Self { layer, id, channel }
    }
}