Lines
94.64 %
Functions
50 %
Branches
100 %
//! Opening a viewer window, or raising the one that already shows the same source.
use tvui::{Caps, viewer_size};
use super::App;
use crate::viewer::{Source, Viewer};
use crate::window::{Pane, Placement, View, WindowId};
impl App {
/// Shows `lines` in the viewer of `source`: the open one is refilled and raised, else a
/// window fitted to them opens. A dialog stays above it.
pub(crate) fn show_viewer(&mut self, source: Source, lines: Vec<String>) {
match self.viewer_of(&source) {
Some(id) => {
self.fill_viewer(id, lines);
self.raise_window(id);
}
None => {
let lines = split_lines(&lines);
let rect = self
.desktop
.fitted_rect(viewer_size(&lines), self.desktop_rect());
let placement = Placement {
rect,
zoomed: false,
};
self.open_window(View::Viewer(Viewer::new(source, lines)), placement, None);
/// Closes every open Help: its keys were listed from a keymap that is gone.
pub(super) fn close_helps(&mut self) {
let helps: Vec<WindowId> = self
.windows
.iter()
.filter(|win| {
Viewer::of(&win.view)
.is_some_and(|viewer| matches!(viewer.source, Source::Help { .. }))
})
.map(|win| win.id)
.collect();
for id in helps {
self.close_window(id);
pub(super) fn viewer_of(&self, source: &Source) -> Option<WindowId> {
self.windows
.find(|win| Viewer::of(&win.view).is_some_and(|viewer| viewer.source == *source))
/// Replaces the lines of viewer `id` and refits it to them; neither raises it nor moves focus.
pub(super) fn fill_viewer(&mut self, id: WindowId, lines: Vec<String>) {
let size = viewer_size(&lines);
let Some(viewer) = self.win_mut::<Viewer>(id) else {
return;
viewer.lines = lines;
if viewer.caps() == Caps::FIXED {
let area = self.desktop_rect();
self.desktop.set_size(id, size, area);
/// Open the edit form of the transaction the front detail window shows; the window stays.
pub(crate) fn edit_from_detail(&mut self) {
let Some(window) = self.front_window() else {
let Some(Source::Detail { id }) = self.front::<Viewer>().map(|v| v.source.clone()) else {
self.open_transaction_edit_from(window, &id);
fn split_lines(lines: &[String]) -> Vec<String> {
lines
.flat_map(|line| line.split('\n'))
.map(str::to_string)
.collect()