1
//! Loading and refreshing windows: each window of a kind is treated alike.
2

            
3
use super::App;
4
use crate::view::ViewId;
5
use crate::window::WindowId;
6

            
7
impl App {
8
    /// Fetches what window `window` shows, if it has not been fetched.
9
1743
    pub(super) fn load_window(&mut self, window: WindowId) {
10
1743
        let Some(view) = self.win_by(window).map(|win| &win.view) else {
11
            return;
12
        };
13
1743
        if !view.idle() {
14
785
            return;
15
958
        }
16
958
        match view.kind() {
17
            ViewId::Accounts | ViewId::Transactions | ViewId::Commodities => {
18
572
                self.ensure_list_loaded(window);
19
            }
20
69
            ViewId::Config => self.fetch_config(window),
21
51
            ViewId::SshKeys => self.fetch_ssh_keys(window),
22
87
            ViewId::Tags => self.fetch_tags(window),
23
65
            ViewId::Templates => self.fetch_templates(window),
24
114
            ViewId::Scripts => self.fetch_scripts(window),
25
            ViewId::Reports | ViewId::Console | ViewId::Viewer => {}
26
        }
27
1743
    }
28

            
29
    /// Fetches again what every window of `kind` shows, whether or not its dialog's window is
30
    /// still there.
31
118
    pub fn refresh_windows(&mut self, kind: ViewId) {
32
118
        if kind == ViewId::Transactions {
33
21
            self.refetch_account_balances();
34
21
            self.refresh_details();
35
97
        }
36
118
        let windows = self.window_ids(kind);
37
118
        if kind == ViewId::Reports {
38
8
            return self.rerun_reports(windows);
39
110
        }
40
110
        for window in windows {
41
117
            self.refresh_window(window);
42
        }
43
118
    }
44

            
45
    /// Reruns the reports of the windows that have one; says so once when none has.
46
8
    fn rerun_reports(&mut self, windows: Vec<WindowId>) {
47
8
        let ran: Vec<WindowId> = windows
48
8
            .into_iter()
49
10
            .filter(|window| self.last_report(*window).is_some())
50
            .collect();
51
8
        if ran.is_empty() {
52
3
            self.set_status("no report run yet");
53
5
        }
54
8
        for window in ran {
55
5
            self.refresh_window(window);
56
        }
57
8
    }
58

            
59
122
    fn refresh_window(&mut self, window: WindowId) {
60
122
        let Some(view) = self.view_mut(window) else {
61
            return;
62
        };
63
122
        if view.loading() {
64
6
            return;
65
116
        }
66
116
        if view.kind() == ViewId::Reports {
67
5
            return self.rerun_report(window);
68
111
        }
69
111
        view.invalidate();
70
111
        self.load_window(window);
71
122
    }
72
}