Skip to main content

cosmic_applet_avestruz/views/
media.rs

1// SPDX-License-Identifier: MIT
2
3//! The media servers view for Avestruz: the servers the account keeps
4//! what it attaches to notes on, the one files are sent to first at the
5//! top, with a server to add under them. Save, above them, writes them
6//! to the account's list.
7
8use cosmic::Element;
9use cosmic::iced::widget::text::Wrapping;
10use cosmic::iced::{Alignment, Length};
11use cosmic::widget::{self, button, container, text, text_input};
12
13use crate::fl;
14use crate::messages::Message;
15use crate::nostr::blossom::server_name;
16use crate::state::AppModel;
17use crate::views::relays::small;
18
19impl AppModel {
20    /// The line above the servers: the way back, what this is, and Save.
21    fn view_media_header(&self) -> Element<'_, Message> {
22        let back = button::icon(widget::icon::from_name("go-previous-symbolic"))
23            .tooltip(fl!("media-back"))
24            .on_press(Message::CloseMedia);
25        let label = if self.media.saving { fl!("media-saving") } else { fl!("media-save") };
26        let save = button::suggested(label).on_press_maybe(self.media_can_save().then_some(Message::SaveMediaServers));
27        widget::Row::new()
28            .push(back)
29            .push(text(fl!("media-title")).size(16).font(cosmic::font::bold()).width(Length::Fill))
30            .push(save)
31            .spacing(8)
32            .align_y(Alignment::Center)
33            .padding([4, 12])
34            .into()
35    }
36
37    /// The servers, each with what it is for and the ways to move it and
38    /// to take it out, and a server to add.
39    fn view_media_servers(&self) -> Element<'_, Message> {
40        let can_change = self.media_can_change();
41        let mut shown = widget::Column::new().push(small(fl!("media-description"))).spacing(6);
42
43        let servers = &self.media.edited;
44        if servers.is_empty() && self.media.read {
45            shown = shown.push(small(fl!("media-none")));
46        }
47        let last = servers.len().saturating_sub(1);
48        for (place, server) in servers.iter().enumerate() {
49            let moving = |up: bool, at_end: bool| (can_change && !at_end).then(|| Message::ShiftMediaServer(server.clone(), up));
50            let up = button::icon(widget::icon::from_name("go-up-symbolic"))
51                .tooltip(fl!("media-up"))
52                .on_press_maybe(moving(true, place == 0));
53            let down = button::icon(widget::icon::from_name("go-down-symbolic"))
54                .tooltip(fl!("media-down"))
55                .on_press_maybe(moving(false, place == last));
56            let remove = button::icon(widget::icon::from_name("edit-delete-symbolic"))
57                .tooltip(fl!("media-remove"))
58                .on_press_maybe(can_change.then(|| Message::RemoveMediaServer(server.clone())));
59            // A long address is cut rather than wrapped.
60            let name = container(text(server_name(server)).size(13).wrapping(Wrapping::None)).clip(true);
61            let what_for = if place == 0 { fl!("media-first") } else { fl!("media-copy") };
62            let named = widget::Column::new()
63                .push(name)
64                .push(text(what_for).size(11).wrapping(Wrapping::None))
65                .spacing(2)
66                .width(Length::Fill);
67            shown =
68                shown.push(widget::Row::new().push(named).push(up).push(down).push(remove).spacing(4).align_y(Alignment::Center));
69        }
70        if can_change {
71            let typed = text_input(fl!("media-add-placeholder"), self.media.typed.as_str())
72                .on_input(Message::MediaServerTyped)
73                .on_submit(|_| Message::AddMediaServer)
74                .width(Length::Fill);
75            let add = button::standard(fl!("media-add"))
76                .on_press_maybe((!self.media.typed.trim().is_empty()).then_some(Message::AddMediaServer));
77            shown = shown.push(widget::Row::new().push(typed).push(add).spacing(8).align_y(Alignment::Center));
78        }
79        shown.into()
80    }
81
82    /// Render the media servers view.
83    pub fn view_media(&self) -> Element<'_, Message> {
84        let mut body = widget::Column::new().spacing(16);
85        if !self.can_publish() {
86            body = body.push(small(fl!("media-read-only")));
87        } else if !self.media.from_relays {
88            body = body.push(small(fl!("media-reading")));
89        }
90        body = body.push(self.view_media_servers()).push(small(fl!("media-privacy")));
91
92        widget::Column::new()
93            .push(self.view_media_header())
94            .push(widget::divider::horizontal::default())
95            .push(widget::scrollable(container(body).padding([8, 12]).width(Length::Fill)))
96            .into()
97    }
98}