Skip to main content

tui/app/
refresh.rs

1//! Loading and refreshing windows: each window of a kind is treated alike.
2
3use super::App;
4use crate::view::ViewId;
5use crate::window::WindowId;
6
7impl App {
8    /// Fetches what window `window` shows, if it has not been fetched.
9    pub(super) fn load_window(&mut self, window: WindowId) {
10        let Some(view) = self.win_by(window).map(|win| &win.view) else {
11            return;
12        };
13        if !view.idle() {
14            return;
15        }
16        match view.kind() {
17            ViewId::Accounts | ViewId::Transactions | ViewId::Commodities => {
18                self.ensure_list_loaded(window);
19            }
20            ViewId::Config => self.fetch_config(window),
21            ViewId::SshKeys => self.fetch_ssh_keys(window),
22            ViewId::Tags => self.fetch_tags(window),
23            ViewId::Templates => self.fetch_templates(window),
24            ViewId::Scripts => self.fetch_scripts(window),
25            ViewId::Reports | ViewId::Console | ViewId::Viewer => {}
26        }
27    }
28
29    /// Fetches again what every window of `kind` shows, whether or not its dialog's window is
30    /// still there.
31    pub fn refresh_windows(&mut self, kind: ViewId) {
32        if kind == ViewId::Transactions {
33            self.refetch_account_balances();
34            self.refresh_details();
35        }
36        let windows = self.window_ids(kind);
37        if kind == ViewId::Reports {
38            return self.rerun_reports(windows);
39        }
40        for window in windows {
41            self.refresh_window(window);
42        }
43    }
44
45    /// Reruns the reports of the windows that have one; says so once when none has.
46    fn rerun_reports(&mut self, windows: Vec<WindowId>) {
47        let ran: Vec<WindowId> = windows
48            .into_iter()
49            .filter(|window| self.last_report(*window).is_some())
50            .collect();
51        if ran.is_empty() {
52            self.set_status("no report run yet");
53        }
54        for window in ran {
55            self.refresh_window(window);
56        }
57    }
58
59    fn refresh_window(&mut self, window: WindowId) {
60        let Some(view) = self.view_mut(window) else {
61            return;
62        };
63        if view.loading() {
64            return;
65        }
66        if view.kind() == ViewId::Reports {
67            return self.rerun_report(window);
68        }
69        view.invalidate();
70        self.load_window(window);
71    }
72}