1
//! Window identity and the views the windows own.
2

            
3
use crate::event::Intent;
4
use crate::tabs::config::ConfigTab;
5
use crate::tabs::fetch::Fetch;
6
use crate::tabs::list::ListTab;
7
use crate::tabs::nms::ConsoleState;
8
use crate::tabs::reports::ReportsTab;
9
use crate::tabs::scripts::ScriptsTab;
10
use crate::tabs::seq::Seq;
11
use crate::tabs::ssh_keys::SshKeysTab;
12
use crate::tabs::tags::TagsTab;
13
use crate::tabs::templates::TemplatesTab;
14
use crate::view::{DrawCtx, Handled, ViewId};
15
use crate::viewer::Viewer;
16
use ratatui::buffer::Buffer;
17
use ratatui::layout::Rect;
18
use tvui::Caps;
19

            
20
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21
pub struct WindowId(pub(crate) u32);
22

            
23
/// The window a dialog or action started from and what it showed, so a window closed since can
24
/// be named.
25
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26
pub struct Origin {
27
    pub window: WindowId,
28
    pub kind: ViewId,
29
}
30

            
31
/// What a window shows, owned by the window.
32
pub enum View {
33
    List(ViewId, ListTab),
34
    Reports(ReportsTab),
35
    Config(ConfigTab),
36
    SshKeys(SshKeysTab),
37
    Tags(TagsTab),
38
    Templates(TemplatesTab),
39
    Scripts(ScriptsTab),
40
    Console(ConsoleState),
41
    Viewer(Viewer),
42
}
43

            
44
impl View {
45
166903
    pub fn kind(&self) -> ViewId {
46
166903
        match self {
47
73942
            View::List(kind, _) => *kind,
48
19561
            View::Reports(_) => ViewId::Reports,
49
17094
            View::Config(_) => ViewId::Config,
50
14703
            View::SshKeys(_) => ViewId::SshKeys,
51
13877
            View::Tags(_) => ViewId::Tags,
52
10758
            View::Templates(_) => ViewId::Templates,
53
7225
            View::Scripts(_) => ViewId::Scripts,
54
9591
            View::Console(_) => ViewId::Console,
55
152
            View::Viewer(_) => ViewId::Viewer,
56
        }
57
166903
    }
58

            
59
    /// What the window can do besides move and close.
60
20687
    pub fn caps(&self) -> Caps {
61
20687
        match self {
62
77
            View::Viewer(viewer) => viewer.caps(),
63
20610
            _ => Caps::ALL,
64
        }
65
20687
    }
66

            
67
    /// The title of the window's frame with `suffix`, as the window list shows it.
68
5699
    pub fn title(&self, suffix: &str) -> String {
69
5699
        let (name, tail) = self.title_parts(suffix);
70
5699
        format!("{name}{tail}")
71
5699
    }
72

            
73
    /// The title as the frame draws it, which shortens the name and keeps the tail.
74
1516
    pub fn framed_title(&self, suffix: &str) -> String {
75
1516
        let (name, tail) = self.title_parts(suffix);
76
1516
        tvui::joined_title(&name, &tail)
77
1516
    }
78

            
79
    /// The name of the view and the tail that tells its windows apart: the ordinal and the filter.
80
7215
    fn title_parts(&self, suffix: &str) -> (String, String) {
81
7215
        match self {
82
3604
            View::List(kind, tab) => (
83
3604
                kind.label().to_string(),
84
3604
                format!("{suffix}{}", tab.title("")),
85
3604
            ),
86
789
            View::Reports(tab) => (tab.title(), suffix.to_string()),
87
35
            View::Viewer(viewer) => (viewer.title(), suffix.to_string()),
88
2787
            other => (other.kind().label().to_string(), suffix.to_string()),
89
        }
90
7215
    }
91

            
92
1514
    pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
93
1514
        match self {
94
1124
            View::List(_, tab) => tab.draw(buf, area, ctx),
95
172
            View::Reports(tab) => tab.draw(buf, area, ctx),
96
28
            View::Config(tab) => tab.draw(buf, area, ctx),
97
3
            View::SshKeys(tab) => tab.draw(buf, area, ctx),
98
12
            View::Tags(tab) => tab.draw(buf, area, ctx),
99
2
            View::Templates(tab) => tab.draw(buf, area, ctx),
100
12
            View::Scripts(tab) => tab.draw(buf, area, ctx),
101
147
            View::Console(tab) => tab.draw(buf, area, ctx),
102
14
            View::Viewer(viewer) => viewer.draw(buf, area, ctx),
103
        }
104
1514
    }
105

            
106
    /// Whether a request of this window is in flight.
107
178
    pub fn loading(&self) -> bool {
108
178
        match self {
109
67
            View::List(_, tab) => matches!(tab.state, Fetch::Loading { .. }),
110
7
            View::Reports(tab) => matches!(tab.state, Fetch::Loading { .. }),
111
17
            View::Config(tab) => tab.any_loading(),
112
15
            View::SshKeys(tab) => tab.any_loading(),
113
30
            View::Tags(tab) => tab.any_loading(),
114
17
            View::Templates(tab) => tab.any_loading(),
115
25
            View::Scripts(tab) => tab.any_loading(),
116
            View::Console(_) | View::Viewer(_) => false,
117
        }
118
178
    }
119

            
120
    /// Whether the window has not fetched what it shows yet.
121
1743
    pub fn idle(&self) -> bool {
122
1743
        match self {
123
829
            View::List(_, tab) => matches!(tab.state, Fetch::Idle),
124
81
            View::Config(tab) => tab.is_idle(),
125
57
            View::SshKeys(tab) => tab.is_idle(),
126
106
            View::Tags(tab) => tab.is_idle(),
127
78
            View::Templates(tab) => tab.is_idle(),
128
126
            View::Scripts(tab) => tab.is_idle(),
129
466
            View::Reports(_) | View::Console(_) | View::Viewer(_) => false,
130
        }
131
1743
    }
132

            
133
    /// Back to idle so the window fetches again; a list keeps its focused row.
134
111
    pub fn invalidate(&mut self) {
135
111
        match self {
136
51
            View::List(_, tab) => tab.refresh(),
137
9
            View::Config(tab) => tab.reset(),
138
9
            View::SshKeys(tab) => tab.reset(),
139
15
            View::Tags(tab) => tab.reset(),
140
11
            View::Templates(tab) => tab.reset(),
141
16
            View::Scripts(tab) => tab.reset(),
142
            View::Reports(_) | View::Console(_) | View::Viewer(_) => {}
143
        }
144
111
    }
145

            
146
897
    pub fn handle(&mut self, intent: Intent) -> Handled {
147
897
        match self {
148
482
            View::List(_, tab) => tab.handle(intent),
149
168
            View::Reports(tab) => tab.handle(intent),
150
41
            View::Config(tab) => tab.handle(intent),
151
10
            View::SshKeys(tab) => tab.handle(intent),
152
26
            View::Tags(tab) => tab.handle(intent),
153
21
            View::Templates(tab) => tab.handle(intent),
154
52
            View::Scripts(tab) => tab.handle(intent),
155
19
            View::Console(tab) => tab.handle(intent),
156
78
            View::Viewer(viewer) => viewer.handle(intent),
157
        }
158
897
    }
159
}
160

            
161
/// A view type a window can show: the typed way to reach it through a [`View`].
162
pub trait Pane: Sized {
163
    fn of(view: &View) -> Option<&Self>;
164
    fn of_mut(view: &mut View) -> Option<&mut Self>;
165
}
166

            
167
macro_rules! panes {
168
    ($($variant:ident($tab:ty))*) => {
169
        $(
170
            impl Pane for $tab {
171
49892
                fn of(view: &View) -> Option<&Self> {
172
49892
                    match view {
173
2863
                        View::$variant(tab) => Some(tab),
174
47029
                        _ => None,
175
                    }
176
49892
                }
177

            
178
24933
                fn of_mut(view: &mut View) -> Option<&mut Self> {
179
24933
                    match view {
180
4681
                        View::$variant(tab) => Some(tab),
181
20252
                        _ => None,
182
                    }
183
24933
                }
184
            }
185
        )*
186
    };
187
}
188

            
189
panes! {
190
    Reports(ReportsTab)
191
    Config(ConfigTab)
192
    SshKeys(SshKeysTab)
193
    Tags(TagsTab)
194
    Templates(TemplatesTab)
195
    Scripts(ScriptsTab)
196
    Console(ConsoleState)
197
    Viewer(Viewer)
198
}
199

            
200
impl Pane for ListTab {
201
1116
    fn of(view: &View) -> Option<&Self> {
202
1116
        match view {
203
1116
            View::List(_, tab) => Some(tab),
204
            _ => None,
205
        }
206
1116
    }
207

            
208
987
    fn of_mut(view: &mut View) -> Option<&mut Self> {
209
987
        match view {
210
986
            View::List(_, tab) => Some(tab),
211
1
            _ => None,
212
        }
213
987
    }
214
}
215

            
216
pub struct Win {
217
    pub(crate) id: WindowId,
218
    pub(crate) view: View,
219
    /// The TV window number 1..=9, the lowest free one when it opened.
220
    pub(crate) number: Option<u8>,
221
    /// Which window of its view this is, 1 for the first; kept when others close.
222
    pub(crate) ordinal: u32,
223
    /// Counts the palette report lookups of this window; a reply of an older one is dropped.
224
    pub(crate) report_seq: Seq,
225
}
226

            
227
/// Where a window opens: its rect once unzoomed and whether it starts zoomed.
228
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229
pub(crate) struct Placement {
230
    pub rect: Rect,
231
    pub zoomed: bool,
232
}