1
//! Focusable sub-pane model.
2
//!
3
//! Each view declares a static list of its panes; one is always focused.
4
//! Focus is stored by value so reads never need to index.
5

            
6
#[cfg(test)]
7
mod tests;
8

            
9
/// Named pane within a view.
10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11
pub enum PaneId {
12
    /// The nomiscript input prompt line.
13
    Prompt,
14
    /// The scrollback transcript.
15
    Scrollback,
16
}
17

            
18
/// A view's focusable sub-panes with exactly one always focused.
19
///
20
/// The pane list is `'static` (declared per view); focus is stored by value
21
/// so reads never need to index.
22
#[derive(Debug)]
23
pub struct PaneSet {
24
    panes: &'static [PaneId],
25
    focused: PaneId,
26
}
27

            
28
impl PaneSet {
29
    /// Construct a `PaneSet` with the given static pane list and initial focus.
30
2135
    pub const fn new(panes: &'static [PaneId], initial: PaneId) -> Self {
31
2135
        Self {
32
2135
            panes,
33
2135
            focused: initial,
34
2135
        }
35
2135
    }
36

            
37
    /// The currently focused pane.
38
164
    pub fn focused(&self) -> PaneId {
39
164
        self.focused
40
164
    }
41

            
42
    /// Whether `id` is currently focused.
43
19
    pub fn is_focused(&self, id: PaneId) -> bool {
44
19
        self.focused == id
45
19
    }
46

            
47
    /// Move focus to `id`. A no-op if `id` is not in the pane list.
48
16
    pub fn focus(&mut self, id: PaneId) {
49
16
        if self.panes.contains(&id) {
50
15
            self.focused = id;
51
1
        }
52
16
    }
53

            
54
    /// Cycle focus forward or backward through the pane list, wrapping at
55
    /// both ends.
56
12
    pub fn cycle(&mut self, forward: bool) {
57
18
        if let Some(i) = self.panes.iter().position(|p| *p == self.focused) {
58
12
            let len = self.panes.len();
59
12
            let next = if forward {
60
9
                (i + 1) % len
61
            } else {
62
3
                (i + len - 1) % len
63
            };
64
12
            if let Some(p) = self.panes.get(next) {
65
12
                self.focused = *p;
66
            }
67
        }
68
12
    }
69
}