Skip to main content

tui/
window.rs

1//! Window identity and the views the windows own.
2
3use crate::event::Intent;
4use crate::tabs::config::ConfigTab;
5use crate::tabs::fetch::Fetch;
6use crate::tabs::list::ListTab;
7use crate::tabs::nms::ConsoleState;
8use crate::tabs::reports::ReportsTab;
9use crate::tabs::scripts::ScriptsTab;
10use crate::tabs::seq::Seq;
11use crate::tabs::ssh_keys::SshKeysTab;
12use crate::tabs::tags::TagsTab;
13use crate::tabs::templates::TemplatesTab;
14use crate::view::{DrawCtx, Handled, ViewId};
15use crate::viewer::Viewer;
16use ratatui::buffer::Buffer;
17use ratatui::layout::Rect;
18use tvui::Caps;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21pub 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)]
26pub struct Origin {
27    pub window: WindowId,
28    pub kind: ViewId,
29}
30
31/// What a window shows, owned by the window.
32pub 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
44impl View {
45    pub fn kind(&self) -> ViewId {
46        match self {
47            View::List(kind, _) => *kind,
48            View::Reports(_) => ViewId::Reports,
49            View::Config(_) => ViewId::Config,
50            View::SshKeys(_) => ViewId::SshKeys,
51            View::Tags(_) => ViewId::Tags,
52            View::Templates(_) => ViewId::Templates,
53            View::Scripts(_) => ViewId::Scripts,
54            View::Console(_) => ViewId::Console,
55            View::Viewer(_) => ViewId::Viewer,
56        }
57    }
58
59    /// What the window can do besides move and close.
60    pub fn caps(&self) -> Caps {
61        match self {
62            View::Viewer(viewer) => viewer.caps(),
63            _ => Caps::ALL,
64        }
65    }
66
67    /// The title of the window's frame with `suffix`, as the window list shows it.
68    pub fn title(&self, suffix: &str) -> String {
69        let (name, tail) = self.title_parts(suffix);
70        format!("{name}{tail}")
71    }
72
73    /// The title as the frame draws it, which shortens the name and keeps the tail.
74    pub fn framed_title(&self, suffix: &str) -> String {
75        let (name, tail) = self.title_parts(suffix);
76        tvui::joined_title(&name, &tail)
77    }
78
79    /// The name of the view and the tail that tells its windows apart: the ordinal and the filter.
80    fn title_parts(&self, suffix: &str) -> (String, String) {
81        match self {
82            View::List(kind, tab) => (
83                kind.label().to_string(),
84                format!("{suffix}{}", tab.title("")),
85            ),
86            View::Reports(tab) => (tab.title(), suffix.to_string()),
87            View::Viewer(viewer) => (viewer.title(), suffix.to_string()),
88            other => (other.kind().label().to_string(), suffix.to_string()),
89        }
90    }
91
92    pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
93        match self {
94            View::List(_, tab) => tab.draw(buf, area, ctx),
95            View::Reports(tab) => tab.draw(buf, area, ctx),
96            View::Config(tab) => tab.draw(buf, area, ctx),
97            View::SshKeys(tab) => tab.draw(buf, area, ctx),
98            View::Tags(tab) => tab.draw(buf, area, ctx),
99            View::Templates(tab) => tab.draw(buf, area, ctx),
100            View::Scripts(tab) => tab.draw(buf, area, ctx),
101            View::Console(tab) => tab.draw(buf, area, ctx),
102            View::Viewer(viewer) => viewer.draw(buf, area, ctx),
103        }
104    }
105
106    /// Whether a request of this window is in flight.
107    pub fn loading(&self) -> bool {
108        match self {
109            View::List(_, tab) => matches!(tab.state, Fetch::Loading { .. }),
110            View::Reports(tab) => matches!(tab.state, Fetch::Loading { .. }),
111            View::Config(tab) => tab.any_loading(),
112            View::SshKeys(tab) => tab.any_loading(),
113            View::Tags(tab) => tab.any_loading(),
114            View::Templates(tab) => tab.any_loading(),
115            View::Scripts(tab) => tab.any_loading(),
116            View::Console(_) | View::Viewer(_) => false,
117        }
118    }
119
120    /// Whether the window has not fetched what it shows yet.
121    pub fn idle(&self) -> bool {
122        match self {
123            View::List(_, tab) => matches!(tab.state, Fetch::Idle),
124            View::Config(tab) => tab.is_idle(),
125            View::SshKeys(tab) => tab.is_idle(),
126            View::Tags(tab) => tab.is_idle(),
127            View::Templates(tab) => tab.is_idle(),
128            View::Scripts(tab) => tab.is_idle(),
129            View::Reports(_) | View::Console(_) | View::Viewer(_) => false,
130        }
131    }
132
133    /// Back to idle so the window fetches again; a list keeps its focused row.
134    pub fn invalidate(&mut self) {
135        match self {
136            View::List(_, tab) => tab.refresh(),
137            View::Config(tab) => tab.reset(),
138            View::SshKeys(tab) => tab.reset(),
139            View::Tags(tab) => tab.reset(),
140            View::Templates(tab) => tab.reset(),
141            View::Scripts(tab) => tab.reset(),
142            View::Reports(_) | View::Console(_) | View::Viewer(_) => {}
143        }
144    }
145
146    pub fn handle(&mut self, intent: Intent) -> Handled {
147        match self {
148            View::List(_, tab) => tab.handle(intent),
149            View::Reports(tab) => tab.handle(intent),
150            View::Config(tab) => tab.handle(intent),
151            View::SshKeys(tab) => tab.handle(intent),
152            View::Tags(tab) => tab.handle(intent),
153            View::Templates(tab) => tab.handle(intent),
154            View::Scripts(tab) => tab.handle(intent),
155            View::Console(tab) => tab.handle(intent),
156            View::Viewer(viewer) => viewer.handle(intent),
157        }
158    }
159}
160
161/// A view type a window can show: the typed way to reach it through a [`View`].
162pub trait Pane: Sized {
163    fn of(view: &View) -> Option<&Self>;
164    fn of_mut(view: &mut View) -> Option<&mut Self>;
165}
166
167macro_rules! panes {
168    ($($variant:ident($tab:ty))*) => {
169        $(
170            impl Pane for $tab {
171                fn of(view: &View) -> Option<&Self> {
172                    match view {
173                        View::$variant(tab) => Some(tab),
174                        _ => None,
175                    }
176                }
177
178                fn of_mut(view: &mut View) -> Option<&mut Self> {
179                    match view {
180                        View::$variant(tab) => Some(tab),
181                        _ => None,
182                    }
183                }
184            }
185        )*
186    };
187}
188
189panes! {
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
200impl Pane for ListTab {
201    fn of(view: &View) -> Option<&Self> {
202        match view {
203            View::List(_, tab) => Some(tab),
204            _ => None,
205        }
206    }
207
208    fn of_mut(view: &mut View) -> Option<&mut Self> {
209        match view {
210            View::List(_, tab) => Some(tab),
211            _ => None,
212        }
213    }
214}
215
216pub 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)]
229pub(crate) struct Placement {
230    pub rect: Rect,
231    pub zoomed: bool,
232}