1
//! Overlay stack — LIFO focus layer rendered on top of the main view.
2

            
3
use crate::form::Form;
4
use crate::form::FormKind;
5
use crate::modal::{FilterBuilder, Modal};
6
use crate::window::Origin;
7

            
8
mod placement;
9
pub use placement::Placement;
10

            
11
#[cfg(test)]
12
mod tests;
13

            
14
/// An open overlay with where it sits; only the top one has the focus.
15
#[derive(Clone, Copy)]
16
pub struct Layer<'a> {
17
    pub modal: &'a Modal,
18
    pub placement: Placement,
19
    pub top: bool,
20
}
21

            
22
/// LIFO stack of overlays. The topmost overlay owns keyboard focus.
23
#[derive(Debug, Default)]
24
pub struct OverlayStack {
25
    items: Vec<Modal>,
26
    /// Where each of `items` sits against the spot it opened at.
27
    placements: Vec<Placement>,
28
    /// The window the open dialog was opened from; none once the stack is empty.
29
    origin: Option<Origin>,
30
    /// Counts pushes and pops, so a press can tell the dialog it began on is gone.
31
    epoch: u64,
32
}
33

            
34
impl OverlayStack {
35
    #[must_use]
36
2106
    pub const fn new() -> Self {
37
2106
        Self {
38
2106
            items: Vec::new(),
39
2106
            placements: Vec::new(),
40
2106
            origin: None,
41
2106
            epoch: 0,
42
2106
        }
43
2106
    }
44

            
45
    #[must_use]
46
15307
    pub const fn epoch(&self) -> u64 {
47
15307
        self.epoch
48
15307
    }
49

            
50
    #[must_use]
51
448
    pub const fn origin(&self) -> Option<Origin> {
52
448
        self.origin
53
448
    }
54

            
55
680
    pub fn set_origin(&mut self, origin: Option<Origin>) {
56
680
        self.origin = origin;
57
680
    }
58

            
59
1262
    pub fn push(&mut self, mut modal: Modal) {
60
1262
        if let Modal::Form(form) = &mut modal {
61
957
            form.snapshot();
62
957
            form.origin = form.origin.or(self.origin);
63
957
            self.origin = form.origin;
64
305
        }
65
1262
        self.epoch = self.epoch.wrapping_add(1);
66
1262
        self.placements.push(match &modal {
67
957
            Modal::Form(form) => form.placement,
68
305
            _ => Placement::default(),
69
        });
70
1262
        self.items.push(modal);
71
1262
    }
72

            
73
441
    pub fn pop(&mut self) -> Option<Modal> {
74
441
        self.epoch = self.epoch.wrapping_add(1);
75
441
        let mut modal = self.items.pop();
76
441
        let placement = self.placements.pop().unwrap_or_default();
77
441
        if let Some(Modal::Form(form)) = &mut modal {
78
268
            form.placement = placement;
79
173
        }
80
441
        self.release();
81
441
        modal
82
441
    }
83

            
84
    /// Forgets the origin once no dialog is open.
85
717
    pub fn release(&mut self) {
86
717
        if self.items.is_empty() {
87
506
            self.origin = None;
88
211
        }
89
717
    }
90

            
91
    #[must_use]
92
25018
    pub fn top(&self) -> Option<&Modal> {
93
25018
        self.items.last()
94
25018
    }
95

            
96
    #[must_use]
97
2733
    pub fn top_mut(&mut self) -> Option<&mut Modal> {
98
2733
        self.items.last_mut()
99
2733
    }
100

            
101
    /// Every overlay, bottom to top.
102
1649
    pub fn layers(&self) -> impl Iterator<Item = Layer<'_>> {
103
1649
        let last = self.items.len().checked_sub(1);
104
1649
        let sitting = self.items.iter().zip(&self.placements);
105
1649
        sitting
106
1649
            .enumerate()
107
            .map(move |(i, (modal, &placement))| Layer {
108
685
                modal,
109
685
                placement,
110
685
                top: Some(i) == last,
111
685
            })
112
1649
    }
113

            
114
    /// Where the top overlay sits.
115
    #[must_use]
116
73
    pub fn placement(&self) -> Placement {
117
73
        self.placements.last().copied().unwrap_or_default()
118
73
    }
119

            
120
46
    pub fn set_placement(&mut self, placement: Placement) {
121
46
        if let Some(top) = self.placements.last_mut() {
122
46
            *top = placement;
123
        }
124
46
    }
125

            
126
    /// The topmost form, also when only a confirm about it sits above.
127
    #[must_use]
128
263
    pub fn form_mut(&mut self) -> Option<&mut Form> {
129
263
        let skip = usize::from(matches!(self.items.last(), Some(Modal::Confirm { .. })));
130
263
        match self.items.iter_mut().rev().nth(skip) {
131
258
            Some(Modal::Form(form)) => Some(form),
132
5
            _ => None,
133
        }
134
263
    }
135

            
136
    /// The topmost report form, also beneath a filter builder.
137
    #[must_use]
138
2
    pub fn report_form_mut(&mut self) -> Option<&mut Form> {
139
3
        self.items.iter_mut().rev().find_map(|modal| match modal {
140
2
            Modal::Form(form) if matches!(form.kind, FormKind::Report(_)) => Some(form),
141
1
            _ => None,
142
3
        })
143
2
    }
144

            
145
    /// The open filter builder, also when a row form sits above it.
146
    #[must_use]
147
247
    pub fn builder_mut(&mut self) -> Option<&mut FilterBuilder> {
148
249
        self.items.iter_mut().rev().find_map(|modal| match modal {
149
145
            Modal::FilterBuilder(builder) => Some(builder),
150
104
            _ => None,
151
249
        })
152
247
    }
153

            
154
    #[must_use]
155
6
    pub fn len(&self) -> usize {
156
6
        self.items.len()
157
6
    }
158

            
159
    #[must_use]
160
142354
    pub fn is_empty(&self) -> bool {
161
142354
        self.items.is_empty()
162
142354
    }
163
}