1
//! The windows listed at the end of the Window menu.
2
use super::App;
3
use crate::event::Intent;
4
use crate::window::{Win, WindowId};
5

            
6
impl App {
7
    /// The Window menu's list: the windows by number, the unnumbered ones last in opening order.
8
119
    pub(super) fn window_entries(&self) -> Vec<(Intent, String)> {
9
119
        let mut listed: Vec<&Win> = self.windows.iter().collect();
10
2366
        listed.sort_by_key(|win| win.number.unwrap_or(u8::MAX));
11
119
        listed
12
119
            .into_iter()
13
1291
            .map(|win| (Intent::RaiseWindow(win.id), self.window_label(win)))
14
            .collect()
15
119
    }
16

            
17
    /// `n Title` for a numbered window, the title alone otherwise.
18
1291
    fn window_label(&self, win: &Win) -> String {
19
1291
        let title = win.view.title(&self.suffix_of(win));
20
1291
        match win.number {
21
982
            Some(number) => format!("{number} {title}"),
22
309
            None => title,
23
        }
24
1291
    }
25

            
26
    /// What the frame of window `id` appends to its title.
27
1523
    pub(crate) fn window_suffix(&self, id: WindowId) -> String {
28
1523
        self.win_by(id)
29
1523
            .map(|win| self.suffix_of(win))
30
            .unwrap_or_default()
31
1523
    }
32

            
33
    /// The ordinal of a later window of its view, when another window is titled the same.
34
2814
    fn suffix_of(&self, win: &Win) -> String {
35
2814
        let title = win.view.title("");
36
2814
        let clash = win.ordinal > 1
37
234
            && self
38
234
                .windows
39
234
                .iter()
40
1595
                .any(|other| other.id != win.id && other.view.title("") == title);
41
2814
        match clash {
42
231
            true => format!(" ({})", win.ordinal),
43
2583
            false => String::new(),
44
        }
45
2814
    }
46

            
47
    /// Raises window `id` and loads it; a window that has closed since the list was drawn is left.
48
7
    pub(crate) fn raise_listed(&mut self, id: WindowId) {
49
7
        if self.win_by(id).is_some() {
50
7
            self.activate_window(id);
51
        }
52
7
    }
53

            
54
    /// Raises the window numbered `number`; says so when there is none.
55
3
    pub(crate) fn select_window_number(&mut self, number: u8) {
56
15
        let found = self.windows.iter().find(|win| win.number == Some(number));
57
3
        match found.map(|win| win.id) {
58
2
            Some(id) => self.activate_window(id),
59
1
            None => self.set_status(format!("no window {number}")),
60
        }
61
3
    }
62
}