Lines
97.27 %
Functions
75 %
Branches
100 %
//! Window identity and the views the windows own.
use crate::event::Intent;
use crate::tabs::config::ConfigTab;
use crate::tabs::fetch::Fetch;
use crate::tabs::list::ListTab;
use crate::tabs::nms::ConsoleState;
use crate::tabs::reports::ReportsTab;
use crate::tabs::scripts::ScriptsTab;
use crate::tabs::seq::Seq;
use crate::tabs::ssh_keys::SshKeysTab;
use crate::tabs::tags::TagsTab;
use crate::tabs::templates::TemplatesTab;
use crate::view::{DrawCtx, Handled, ViewId};
use crate::viewer::Viewer;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use tvui::Caps;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WindowId(pub(crate) u32);
/// The window a dialog or action started from and what it showed, so a window closed since can
/// be named.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Origin {
pub window: WindowId,
pub kind: ViewId,
}
/// What a window shows, owned by the window.
pub enum View {
List(ViewId, ListTab),
Reports(ReportsTab),
Config(ConfigTab),
SshKeys(SshKeysTab),
Tags(TagsTab),
Templates(TemplatesTab),
Scripts(ScriptsTab),
Console(ConsoleState),
Viewer(Viewer),
impl View {
pub fn kind(&self) -> ViewId {
match self {
View::List(kind, _) => *kind,
View::Reports(_) => ViewId::Reports,
View::Config(_) => ViewId::Config,
View::SshKeys(_) => ViewId::SshKeys,
View::Tags(_) => ViewId::Tags,
View::Templates(_) => ViewId::Templates,
View::Scripts(_) => ViewId::Scripts,
View::Console(_) => ViewId::Console,
View::Viewer(_) => ViewId::Viewer,
/// What the window can do besides move and close.
pub fn caps(&self) -> Caps {
View::Viewer(viewer) => viewer.caps(),
_ => Caps::ALL,
/// The title of the window's frame with `suffix`, as the window list shows it.
pub fn title(&self, suffix: &str) -> String {
let (name, tail) = self.title_parts(suffix);
format!("{name}{tail}")
/// The title as the frame draws it, which shortens the name and keeps the tail.
pub fn framed_title(&self, suffix: &str) -> String {
tvui::joined_title(&name, &tail)
/// The name of the view and the tail that tells its windows apart: the ordinal and the filter.
fn title_parts(&self, suffix: &str) -> (String, String) {
View::List(kind, tab) => (
kind.label().to_string(),
format!("{suffix}{}", tab.title("")),
),
View::Reports(tab) => (tab.title(), suffix.to_string()),
View::Viewer(viewer) => (viewer.title(), suffix.to_string()),
other => (other.kind().label().to_string(), suffix.to_string()),
pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
View::List(_, tab) => tab.draw(buf, area, ctx),
View::Reports(tab) => tab.draw(buf, area, ctx),
View::Config(tab) => tab.draw(buf, area, ctx),
View::SshKeys(tab) => tab.draw(buf, area, ctx),
View::Tags(tab) => tab.draw(buf, area, ctx),
View::Templates(tab) => tab.draw(buf, area, ctx),
View::Scripts(tab) => tab.draw(buf, area, ctx),
View::Console(tab) => tab.draw(buf, area, ctx),
View::Viewer(viewer) => viewer.draw(buf, area, ctx),
/// Whether a request of this window is in flight.
pub fn loading(&self) -> bool {
View::List(_, tab) => matches!(tab.state, Fetch::Loading { .. }),
View::Reports(tab) => matches!(tab.state, Fetch::Loading { .. }),
View::Config(tab) => tab.any_loading(),
View::SshKeys(tab) => tab.any_loading(),
View::Tags(tab) => tab.any_loading(),
View::Templates(tab) => tab.any_loading(),
View::Scripts(tab) => tab.any_loading(),
View::Console(_) | View::Viewer(_) => false,
/// Whether the window has not fetched what it shows yet.
pub fn idle(&self) -> bool {
View::List(_, tab) => matches!(tab.state, Fetch::Idle),
View::Config(tab) => tab.is_idle(),
View::SshKeys(tab) => tab.is_idle(),
View::Tags(tab) => tab.is_idle(),
View::Templates(tab) => tab.is_idle(),
View::Scripts(tab) => tab.is_idle(),
View::Reports(_) | View::Console(_) | View::Viewer(_) => false,
/// Back to idle so the window fetches again; a list keeps its focused row.
pub fn invalidate(&mut self) {
View::List(_, tab) => tab.refresh(),
View::Config(tab) => tab.reset(),
View::SshKeys(tab) => tab.reset(),
View::Tags(tab) => tab.reset(),
View::Templates(tab) => tab.reset(),
View::Scripts(tab) => tab.reset(),
View::Reports(_) | View::Console(_) | View::Viewer(_) => {}
pub fn handle(&mut self, intent: Intent) -> Handled {
View::List(_, tab) => tab.handle(intent),
View::Reports(tab) => tab.handle(intent),
View::Config(tab) => tab.handle(intent),
View::SshKeys(tab) => tab.handle(intent),
View::Tags(tab) => tab.handle(intent),
View::Templates(tab) => tab.handle(intent),
View::Scripts(tab) => tab.handle(intent),
View::Console(tab) => tab.handle(intent),
View::Viewer(viewer) => viewer.handle(intent),
/// A view type a window can show: the typed way to reach it through a [`View`].
pub trait Pane: Sized {
fn of(view: &View) -> Option<&Self>;
fn of_mut(view: &mut View) -> Option<&mut Self>;
macro_rules! panes {
($($variant:ident($tab:ty))*) => {
$(
impl Pane for $tab {
fn of(view: &View) -> Option<&Self> {
match view {
View::$variant(tab) => Some(tab),
_ => None,
fn of_mut(view: &mut View) -> Option<&mut Self> {
)*
};
panes! {
Reports(ReportsTab)
Config(ConfigTab)
SshKeys(SshKeysTab)
Tags(TagsTab)
Templates(TemplatesTab)
Scripts(ScriptsTab)
Console(ConsoleState)
Viewer(Viewer)
impl Pane for ListTab {
View::List(_, tab) => Some(tab),
pub struct Win {
pub(crate) id: WindowId,
pub(crate) view: View,
/// The TV window number 1..=9, the lowest free one when it opened.
pub(crate) number: Option<u8>,
/// Which window of its view this is, 1 for the first; kept when others close.
pub(crate) ordinal: u32,
/// Counts the palette report lookups of this window; a reply of an older one is dropped.
pub(crate) report_seq: Seq,
/// Where a window opens: its rect once unzoomed and whether it starts zoomed.
pub(crate) struct Placement {
pub rect: Rect,
pub zoomed: bool,