Lines
96.05 %
Functions
77.12 %
Branches
100 %
//! The windows of the app and the lookups over them.
use forms::FormValues;
use super::App;
use crate::draw::body_area;
use crate::modal::Modal;
use crate::tabs::config::ConfigTab;
use crate::tabs::list::ListTab;
use crate::tabs::nms::ConsoleState;
use crate::tabs::reports::chart::TerminalEnv;
use crate::tabs::reports::{ReportKind, 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::ViewId;
use crate::viewer::{Source, Viewer};
use crate::window::{Origin, Pane, Placement, View, Win, WindowId};
use cli_core::render::schema;
use ratatui::layout::Rect;
use tvui::{Caps, FrameMode, FrameState, Place};
/// The part of a terminal of `env` the windows sit on.
pub(super) fn desktop_area(env: TerminalEnv) -> Rect {
body_area(Rect::new(0, 0, env.screen.width, env.screen.height))
}
/// A fresh view of `kind`.
pub(super) fn new_view(kind: ViewId) -> View {
match kind {
ViewId::Accounts => View::List(
kind,
ListTab::new("(list-accounts)", &schema::ACCOUNTS).with_balance_column(),
),
ViewId::Transactions => View::List(kind, ListTab::paged()),
ViewId::Commodities => View::List(
ListTab::new("(list-commodities)", &schema::COMMODITIES),
ViewId::Reports => View::Reports(ReportsTab::new()),
ViewId::Config => View::Config(ConfigTab::new()),
ViewId::SshKeys => View::SshKeys(SshKeysTab::new()),
ViewId::Tags => View::Tags(TagsTab::new()),
ViewId::Templates => View::Templates(TemplatesTab::new()),
ViewId::Scripts => View::Scripts(ScriptsTab::new()),
ViewId::Console => View::Console(ConsoleState::new()),
ViewId::Viewer => View::Viewer(Viewer::new(
Source::Detail { id: String::new() },
Vec::new(),
)),
impl App {
/// Opens `view` on top at `placement` with `number`; the only way a window appears.
pub(super) fn open_window(
&mut self,
view: View,
placement: Placement,
number: Option<u8>,
) -> WindowId {
self.leave_front();
let id = WindowId(self.next_window);
self.next_window = self.next_window.saturating_add(1);
let ordinal = self.next_ordinal(view.kind());
let caps = view.caps();
self.windows.push(Win {
id,
view,
number,
ordinal,
report_seq: Seq::default(),
});
self.desktop.open(id, placement.rect, caps);
if placement.zoomed {
self.desktop.toggle_zoom(id);
id
/// Closes window `id`, settling what it has in flight; the only way a window goes.
pub(crate) fn close_window(&mut self, id: WindowId) {
if self.win_by(id).is_none() {
return;
if self.dragged() == Some(id) {
self.hold = None;
self.settle_window(id);
self.close_console_stream(id);
self.delete_image(id);
let uncovers = self.drawn_ids().contains(&id);
self.windows.retain(|win| win.id != id);
self.desktop.close(id);
if uncovers {
self.load_visible();
/// Closes the front window.
pub(crate) fn close_front_window(&mut self) {
if let Some(id) = self.front_window() {
self.close_window(id);
/// Brings window `id` to the front; the only way a window is raised.
pub(super) fn raise_window(&mut self, id: WindowId) {
if self.front_window() == Some(id) {
self.desktop.raise(id);
/// Brings the rearmost window to the front.
pub(crate) fn next_window(&mut self) {
if let Some(id) = self.desktop.rearmost() {
self.activate_window(id);
/// Sends the front window to the back; the one behind it comes to the front.
pub(crate) fn prev_window(&mut self) {
let Some(id) = self
.front_window()
.filter(|id| self.desktop.rearmost() != Some(*id))
else {
};
self.desktop.lower(id);
self.clear_status();
/// Zooms the front window to the desktop or restores it; what it uncovers is loaded and a
/// chart of the new size is fetched.
pub(crate) fn toggle_zoom(&mut self) {
let Some(id) = self.front_window() else {
if self.desktop.toggle_zoom(id) {
self.window_resized(id);
/// The front-most window of `kind`, or a new one that is loaded.
pub(crate) fn window_or_open(&mut self, kind: ViewId) -> Origin {
let window = self.window_id(kind).unwrap_or_else(|| {
let window = self.open_new_window(new_view(kind));
self.load_window(window);
window
Origin { window, kind }
/// Says that there is no window of `kind`, or no window at all, to act on.
pub(crate) fn say_no_window(&mut self, kind: Option<ViewId>) {
Some(kind) => self.set_status(format!("no {} window", kind.label())),
None => self.set_status("no window"),
/// The front window when it shows `kind`; says so otherwise.
pub(crate) fn front_or_say(&mut self, kind: ViewId) -> Option<WindowId> {
let window = self.front_window().filter(|_| self.front_is(kind));
if window.is_none() {
self.say_no_window(Some(kind));
/// Brings window `id` to the front, clears the status and loads it, as choosing it does.
pub(crate) fn activate_window(&mut self, id: WindowId) {
self.raise_window(id);
/// The windows drawn on the desktop; with no terminal geometry yet only the front one is
/// known to be.
fn drawn_ids(&self) -> Vec<WindowId> {
match self.terminal.env() {
Some(env) => {
let windows = self.visible_windows(desktop_area(env));
windows.into_iter().map(|(id, ..)| id).collect()
None => self.front_window().into_iter().collect(),
/// Loads every drawn window that has not been loaded.
pub(super) fn load_visible(&mut self) {
for id in self.drawn_ids() {
self.load_window(id);
/// Shows window `id` unless a dialog is open or awaiting its reply.
pub(crate) fn show_window_if_free(&mut self, id: WindowId) {
if self.pending_form.is_none() && self.overlays.is_empty() {
/// The window `id` and what it shows, or `None` once it is gone.
pub(super) fn origin_of(&self, id: WindowId) -> Option<Origin> {
let kind = self.window_view(id)?;
Some(Origin { window: id, kind })
/// The window of `origin`; says so when it has closed.
pub(super) fn origin_window(&mut self, origin: Origin) -> Option<WindowId> {
if self.window_view(origin.window).is_some() {
return Some(origin.window);
let message = match origin.kind {
ViewId::Viewer => "the window was closed".to_string(),
kind => format!("the {} window was closed", kind.label()),
self.set_status(message);
None
/// Raises the window of `origin` and makes the next dialog belong to it.
pub(super) fn begin_dialog(&mut self, origin: Origin) -> bool {
let Some(window) = self.origin_window(origin) else {
return false;
self.raise_and_load(window);
self.overlays.set_origin(Some(origin));
true
/// Raises `window` and loads it if it has not been.
fn raise_and_load(&mut self, window: WindowId) {
self.raise_window(window);
/// Raises the front-most window of `kind`; says so when there is none.
pub(crate) fn raise_kind(&mut self, kind: ViewId) -> bool {
let Some(window) = self.window_id(kind) else {
/// `begin_dialog` for the window a reply was routed to.
pub(super) fn begin_dialog_of(&mut self, target: Option<WindowId>) -> bool {
match target.and_then(|id| self.origin_of(id)) {
Some(origin) => self.begin_dialog(origin),
None => {
self.say_no_window(None);
false
/// Opens `modal` as a dialog of `origin`; `false` when its window has closed.
pub(super) fn open_dialog_at(&mut self, origin: Origin, modal: Modal) -> bool {
let opened = self.begin_dialog(origin);
if opened {
self.overlays.push(modal);
opened
/// Opens `modal` as a dialog of window `id`.
pub(super) fn open_dialog_in(&mut self, id: WindowId, modal: Modal) {
if let Some(origin) = self.origin_of(id) {
self.open_dialog_at(origin, modal);
/// Opens `modal` as a dialog of the front-most window of `kind`, or of a new one.
pub(crate) fn open_dialog_for(&mut self, kind: ViewId, modal: Modal) {
let origin = self.window_or_open(kind);
/// The window the open dialog started from; says so when there is none.
pub(crate) fn dialog_origin(&mut self) -> Option<Origin> {
let origin = self.overlays.origin();
if origin.is_none() {
origin
/// The window the open dialog belongs to; says so when it has closed or there is none.
pub(crate) fn dialog_window(&mut self) -> Option<WindowId> {
let origin = self.dialog_origin()?;
self.origin_window(origin)
/// What ends when another window takes the front: the kitty image, which only the front
/// window draws. A console keeps its own prompt focus.
fn leave_front(&mut self) {
if let Some(front) = self.front_window() {
self.delete_image(front);
/// Queues the deletion of the kitty image window `id` shows, if any; the chart transmits
/// again when it is next drawn.
pub(super) fn delete_image(&mut self, id: WindowId) {
self.queue_image_delete(id, self.terminal.env());
/// As `delete_image`; with no `env` the picture is not rebuilt, for a chart fetched again.
pub(super) fn queue_image_delete(&mut self, id: WindowId, env: Option<TerminalEnv>) {
let reports = self.win_mut::<ReportsTab>(id);
let image = reports.and_then(|reports| reports.chart.forget_kitty(env));
if let Some(image) = image {
self.outbound.push(tvui::kitty_delete(image));
/// The windows front to back.
fn front_to_back(&self) -> impl Iterator<Item = &Win> {
let order = self.desktop.iter_back_to_front().rev();
order.filter_map(|slot| self.win_by(slot.id))
/// The id of the front window.
#[must_use]
pub fn front_window(&self) -> Option<WindowId> {
self.desktop.front()
fn front_win(&self) -> Option<&Win> {
self.front_to_back().next()
/// The `T` the front window shows; user actions go through it.
pub fn front<T: Pane>(&self) -> Option<&T> {
self.front_win().and_then(|win| T::of(&win.view))
pub fn front_mut<T: Pane>(&mut self) -> Option<&mut T> {
let id = self.front_window()?;
self.win_mut::<T>(id)
/// The `T` shown by window `id`.
pub fn win<T: Pane>(&self, id: WindowId) -> Option<&T> {
self.win_by(id).and_then(|win| T::of(&win.view))
pub fn win_mut<T: Pane>(&mut self, id: WindowId) -> Option<&mut T> {
self.view_mut(id).and_then(T::of_mut)
pub(super) fn win_by(&self, id: WindowId) -> Option<&Win> {
self.windows.iter().find(|win| win.id == id)
pub(super) fn report_seq_of(&mut self, window: WindowId) -> Option<&mut Seq> {
self.win_by_mut(window).map(|win| &mut win.report_seq)
fn win_by_mut(&mut self, id: WindowId) -> Option<&mut Win> {
self.windows.iter_mut().find(|win| win.id == id)
pub(crate) fn view_mut(&mut self, id: WindowId) -> Option<&mut View> {
self.win_by_mut(id).map(|win| &mut win.view)
/// The id of the front-most window of `kind`.
pub fn window_id(&self, kind: ViewId) -> Option<WindowId> {
let win = self.front_to_back().find(|win| win.view.kind() == kind);
win.map(|win| win.id)
/// The ids of every window of `kind`, front first.
pub(super) fn window_ids(&self, kind: ViewId) -> Vec<WindowId> {
let wins = self.front_to_back();
wins.filter(|win| win.view.kind() == kind)
.map(|win| win.id)
.collect()
/// The kind of the front window.
pub fn front_kind(&self) -> Option<ViewId> {
self.front_win().map(|win| win.view.kind())
pub fn front_is(&self, kind: ViewId) -> bool {
self.front_kind() == Some(kind)
/// The windows to draw inside `area`, back to front, without those fully covered.
pub fn visible_windows(&self, area: Rect) -> Vec<(WindowId, &View, Place)> {
let drawn = self.desktop.iter_back_to_front();
drawn
.filter(|slot| !self.desktop.occluded(slot.id, area))
.filter_map(|slot| {
let place = self.desktop.place_of(slot.id, area)?;
let win = self.win_by(slot.id)?;
Some((win.id, &win.view, place))
})
/// The frame of window `id`: dragging while it is moved or sized, else active when it is the front one, numbered by its own number.
pub fn frame_state(&self, id: WindowId) -> FrameState {
let mode = match (self.dragged() == Some(id), self.front_window() == Some(id)) {
(true, _) => FrameMode::Dragging,
(false, true) => FrameMode::Active,
(false, false) => FrameMode::Passive,
let slot = self.desktop.window(id);
FrameState {
mode,
caps: slot.map_or(Caps::ALL, |slot| slot.caps),
zoomed: slot.is_some_and(|slot| slot.zoomed),
number: self.win_by(id).and_then(|win| win.number),
/// Whether the front window is a console whose prompt has the keyboard.
pub fn console_input_focused(&self) -> bool {
self.front::<ConsoleState>()
.is_some_and(|console| console.focused)
/// Gives the front console's prompt the keyboard, or takes it away.
pub(crate) fn set_console_focused(&mut self, focused: bool) {
if let Some(console) = self.front_mut::<ConsoleState>() {
console.focused = focused;
/// The front window's view.
pub fn active_view(&self) -> Option<&View> {
self.front_win().map(|win| &win.view)
pub fn active_view_mut(&mut self) -> Option<&mut View> {
let id = self.desktop.front()?;
self.view_mut(id)
/// The list the front window shows when it shows `kind`.
pub fn front_list(&self, kind: ViewId) -> Option<&ListTab> {
self.front_is(kind)
.then(|| self.front::<ListTab>())
.flatten()
/// The view kind window `id` shows, or `None` once the window is gone.
pub fn window_view(&self, id: WindowId) -> Option<ViewId> {
self.win_by(id).map(|win| win.view.kind())
/// The last report run in Reports window `window` and its values.
pub fn last_report(&self, window: WindowId) -> Option<(ReportKind, FormValues)> {
self.win::<ReportsTab>(window)?.last().cloned()
/// Marks the Reports window that asked request `id` as waiting for it.
pub(crate) fn set_report_loading(&mut self, id: i64, kind: ReportKind, values: &FormValues) {
let Some(window) = self.pending_routes.get(&id).and_then(|route| route.target) else {
if let Some(reports) = self.win_mut::<ReportsTab>(window) {
reports.set_loading(id, kind, values);
/// The selected row id of the front window's list, when it shows `kind`.
pub fn selected_id(&self, kind: ViewId) -> Option<String> {
self.front_list(kind)?.selected_id().map(str::to_string)