1
//! Opening a viewer window, or raising the one that already shows the same source.
2

            
3
use tvui::{Caps, viewer_size};
4

            
5
use super::App;
6
use crate::viewer::{Source, Viewer};
7
use crate::window::{Pane, Placement, View, WindowId};
8

            
9
impl App {
10
    /// Shows `lines` in the viewer of `source`: the open one is refilled and raised, else a
11
    /// window fitted to them opens. A dialog stays above it.
12
80
    pub(crate) fn show_viewer(&mut self, source: Source, lines: Vec<String>) {
13
80
        match self.viewer_of(&source) {
14
            Some(id) => {
15
3
                self.fill_viewer(id, lines);
16
3
                self.raise_window(id);
17
            }
18
            None => {
19
77
                let lines = split_lines(&lines);
20
77
                let rect = self
21
77
                    .desktop
22
77
                    .fitted_rect(viewer_size(&lines), self.desktop_rect());
23
77
                let placement = Placement {
24
77
                    rect,
25
77
                    zoomed: false,
26
77
                };
27
77
                self.open_window(View::Viewer(Viewer::new(source, lines)), placement, None);
28
            }
29
        }
30
80
    }
31

            
32
    /// Closes every open Help: its keys were listed from a keymap that is gone.
33
19
    pub(super) fn close_helps(&mut self) {
34
19
        let helps: Vec<WindowId> = self
35
19
            .windows
36
19
            .iter()
37
92
            .filter(|win| {
38
92
                Viewer::of(&win.view)
39
2
                    .is_some_and(|viewer| matches!(viewer.source, Source::Help { .. }))
40
92
            })
41
1
            .map(|win| win.id)
42
            .collect();
43
19
        for id in helps {
44
1
            self.close_window(id);
45
        }
46
19
    }
47

            
48
177
    pub(super) fn viewer_of(&self, source: &Source) -> Option<WindowId> {
49
177
        self.windows
50
177
            .iter()
51
1747
            .find(|win| Viewer::of(&win.view).is_some_and(|viewer| viewer.source == *source))
52
45
            .map(|win| win.id)
53
177
    }
54

            
55
    /// Replaces the lines of viewer `id` and refits it to them; neither raises it nor moves focus.
56
13
    pub(super) fn fill_viewer(&mut self, id: WindowId, lines: Vec<String>) {
57
13
        let lines = split_lines(&lines);
58
13
        let size = viewer_size(&lines);
59
13
        let Some(viewer) = self.win_mut::<Viewer>(id) else {
60
            return;
61
        };
62
13
        viewer.lines = lines;
63
13
        if viewer.caps() == Caps::FIXED {
64
11
            let area = self.desktop_rect();
65
11
            self.desktop.set_size(id, size, area);
66
2
        }
67
13
    }
68

            
69
    /// Open the edit form of the transaction the front detail window shows; the window stays.
70
4
    pub(crate) fn edit_from_detail(&mut self) {
71
4
        let Some(window) = self.front_window() else {
72
            return;
73
        };
74
4
        let Some(Source::Detail { id }) = self.front::<Viewer>().map(|v| v.source.clone()) else {
75
            return;
76
        };
77
4
        self.open_transaction_edit_from(window, &id);
78
4
    }
79
}
80

            
81
90
fn split_lines(lines: &[String]) -> Vec<String> {
82
90
    lines
83
90
        .iter()
84
2140
        .flat_map(|line| line.split('\n'))
85
90
        .map(str::to_string)
86
        .collect()
87
90
}