Skip to main content

cosmic_applet_avestruz/views/
keys.rs

1// SPDX-License-Identifier: MIT
2
3//! The view that says what the keys do, on a list and elsewhere (see
4//! [`crate::handlers::keys`]): they are nowhere else to be found out. It
5//! is what `?` opens, and Keyboard in the menu above the feed.
6
7use cosmic::Element;
8use cosmic::iced::widget::text::Wrapping;
9use cosmic::iced::{Alignment, Border, Length};
10use cosmic::widget::{self, button, container, text};
11
12use crate::fl;
13use crate::messages::Message;
14use crate::state::AppModel;
15
16/// How wide the keys are set, before what they do: the widest of them,
17/// two keys and what is between them.
18const KEYS_WIDTH: f32 = 150.0;
19
20/// A key, as a key looks: its name in a box of its own.
21fn key_cap<'a>(name: &str) -> Element<'a, Message> {
22    container(text(name.to_string()).size(12).font(cosmic::font::mono()).wrapping(Wrapping::None))
23        .padding([1, 6])
24        .class(cosmic::theme::Container::custom(|theme| {
25            let cosmic = theme.cosmic();
26            container::Style {
27                border: Border { color: cosmic.bg_divider().into(), width: 1.0, radius: cosmic.corner_radii.radius_xs.into() },
28                ..container::Style::default()
29            }
30        }))
31        .into()
32}
33
34/// One line of what the keys do: the keys `keys`, any of which does it,
35/// and what that is.
36fn key_line<'a>(keys: &[&str], does: String) -> Element<'a, Message> {
37    let caps = keys.iter().fold(widget::Row::new().spacing(4).align_y(Alignment::Center), |caps, key| caps.push(key_cap(key)));
38    widget::Row::new()
39        .push(container(caps).width(Length::Fixed(KEYS_WIDTH)))
40        .push(text(does).size(12).wrapping(Wrapping::WordOrGlyph).width(Length::Fill))
41        .spacing(8)
42        .align_y(Alignment::Center)
43        .into()
44}
45
46/// Some of the keys, under what they have in common: `lines`, each the
47/// keys that do a thing and what that is, under `heading`.
48fn key_group<'a>(heading: String, lines: Vec<(&[&str], String)>) -> Element<'a, Message> {
49    lines
50        .into_iter()
51        .fold(widget::Column::new().push(text(heading).size(14)).spacing(8), |group, (keys, does)| {
52            group.push(key_line(keys, does))
53        })
54        .into()
55}
56
57impl AppModel {
58    /// The line above the keys: the way back, and what this is.
59    fn view_keys_header(&self) -> Element<'_, Message> {
60        let back =
61            button::icon(widget::icon::from_name("go-previous-symbolic")).tooltip(fl!("keys-back")).on_press(Message::CloseKeys);
62        widget::Row::new()
63            .push(back)
64            .push(text(fl!("keys-title")).size(16).font(cosmic::font::bold()))
65            .spacing(8)
66            .align_y(Alignment::Center)
67            .padding([4, 12])
68            .into()
69    }
70
71    /// Render the view of what the keys do: those that go about a list
72    /// and from it, those that do something to the marked note, and
73    /// those that are not a list's.
74    pub fn view_keys(&self) -> Element<'_, Message> {
75        let right_click = fl!("keys-right-click");
76        let about = key_group(
77            fl!("keys-about"),
78            vec![
79                (&["j", "k"], fl!("keys-step")),
80                (&["Home"], fl!("keys-top")),
81                (&["l", "Enter"], fl!("keys-open")),
82                (&["h", "Backspace"], fl!("keys-go-back")),
83                (&["."], fl!("keys-new")),
84                (&["B"], fl!("keys-bookmarks")),
85                (&["s"], fl!("keys-search")),
86                (&["?"], fl!("keys-help")),
87            ],
88        );
89        let note = key_group(
90            fl!("keys-note"),
91            vec![
92                (&["r"], fl!("keys-reply")),
93                (&["R"], fl!("keys-repost")),
94                (&["z"], fl!("keys-zap")),
95                (&["b"], fl!("keys-bookmark")),
96                (&["y"], fl!("keys-share")),
97                (&["Space"], fl!("keys-play")),
98            ],
99        );
100        let elsewhere = key_group(
101            fl!("keys-elsewhere"),
102            vec![
103                // One key to a box: these two are pressed together.
104                (&["Ctrl+Enter"], fl!("keys-publish")),
105                (&["Ctrl+P"], fl!("keys-preview")),
106                (&["Backspace"], fl!("keys-leave-composer")),
107                (&["Esc"], fl!("keys-escape")),
108                (&["Ctrl+Shift+S"], fl!("keys-screenshot")),
109                (&[right_click.as_str()], fl!("keys-copy-picture")),
110                (&[right_click.as_str()], fl!("keys-who")),
111            ],
112        );
113        let body = widget::Column::new()
114            .push(about)
115            .push(note)
116            .push(elsewhere)
117            .push(text(fl!("keys-description")).size(11).wrapping(Wrapping::WordOrGlyph))
118            .spacing(16);
119
120        // In a scroller: on a small screen there is more of it than room.
121        widget::Column::new()
122            .push(self.view_keys_header())
123            .push(widget::divider::horizontal::default())
124            .push(widget::scrollable(container(body).padding([8, 12]).width(Length::Fill)))
125            .into()
126    }
127}