ori_core/views/
popup.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::{
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Affine, Size, Space},
    view::{Pod, PodState, View},
};

/// Create a new popup view.
pub fn popup<V, P>(content: V, popup: P) -> Popup<V, P> {
    Popup::new(content, popup)
}

/// A view that can popup.
pub struct Popup<V, P> {
    content: Pod<V>,
    popup: Pod<P>,
}

impl<V, P> Popup<V, P> {
    /// Create a new popup view.
    pub fn new(content: V, popup: P) -> Self {
        Self {
            content: Pod::new(content),
            popup: Pod::new(popup),
        }
    }
}

impl<T, V, P> View<T> for Popup<V, P>
where
    V: View<T>,
    P: View<T>,
{
    type State = (PodState<T, V>, PodState<T, P>);

    fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
        cx.set_focusable(true);

        let content = self.content.build(cx, data);
        let popup = self.popup.build(cx, data);

        (content, popup)
    }

    fn rebuild(
        &mut self,
        (content, popup): &mut Self::State,
        cx: &mut RebuildCx,
        data: &mut T,
        old: &Self,
    ) {
        self.content.rebuild(content, cx, data, &old.content);
        self.popup.rebuild(popup, cx, data, &old.popup);
    }

    fn event(
        &mut self,
        (content, popup): &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        let mut handled = self.content.event(content, cx, data, event);
        handled |= (self.popup).event_maybe(handled, popup, cx, data, event);

        if handled {
            return true;
        }

        match event {
            Event::PointerReleased(e) if e.clicked && content.is_hovered() => {
                if !cx.is_focused() {
                    cx.focus();
                } else {
                    cx.set_focused(false);
                }

                cx.draw();

                false
            }

            Event::PointerReleased(e) if e.clicked && !popup.has_hovered() => {
                cx.set_focused(false);
                cx.draw();

                false
            }

            _ => false,
        }
    }

    fn layout(
        &mut self,
        (content, popup): &mut Self::State,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        let popup_space = Space::max(cx.window().size);
        let _ = self.popup.layout(popup, cx, data, popup_space);
        self.content.layout(content, cx, data, space)
    }

    fn draw(&mut self, (content, popup): &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        // draw the content first
        cx.layer(Affine::IDENTITY, None, Some(content.id()), |cx| {
            self.content.draw(content, cx, data);
        });

        if !cx.is_focused() && !popup.has_focused() {
            return;
        }

        // place the popup just below the content
        let offset = content.rect().bottom_center() - popup.rect().top_center();
        popup.translate(cx.transform() * offset);

        cx.overlay(0, |cx| {
            cx.layer(Affine::IDENTITY, None, Some(popup.id()), |cx| {
                self.popup.draw(popup, cx, data);
            });
        });
    }
}