1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Origin {
27 pub window: WindowId,
28 pub kind: ViewId,
29}
30
31pub 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 pub fn caps(&self) -> Caps {
61 match self {
62 View::Viewer(viewer) => viewer.caps(),
63 _ => Caps::ALL,
64 }
65 }
66
67 pub fn title(&self, suffix: &str) -> String {
69 let (name, tail) = self.title_parts(suffix);
70 format!("{name}{tail}")
71 }
72
73 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 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 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 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 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
161pub 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 pub(crate) number: Option<u8>,
221 pub(crate) ordinal: u32,
223 pub(crate) report_seq: Seq,
225}
226
227#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub(crate) struct Placement {
230 pub rect: Rect,
231 pub zoomed: bool,
232}