Skip to main content

cosmic_applet_avestruz/views/
popup.rs

1// SPDX-License-Identifier: MIT
2
3//! Popup window view for Avestruz.
4//!
5//! This module renders the popup window: the view [`ViewState`] names, under
6//! an error banner when something went wrong.
7
8use cosmic::Element;
9use cosmic::iced::widget::text::Wrapping;
10use cosmic::iced::window::Id;
11use cosmic::iced::{Alignment, Length, Limits};
12use cosmic::widget::{self, button, container, text};
13use nostr_sdk::prelude::EventId;
14
15use crate::fl;
16use crate::helpers::{truncate_chars, unix_now};
17use crate::messages::Message;
18use crate::state::{AppModel, BookmarkState, Expanded, Page, ViewState};
19use crate::views::components::{Bookmarking, POPUP_MAX_WIDTH, POPUP_MIN_WIDTH, Reacting, RowContext, RowSizes};
20
21/// How much of an error the banner shows. Errors from the network can run
22/// long; the whole of it is in the log.
23const BANNER_MAX_CHARS: usize = 160;
24
25/// A red banner across the top of the content: what went wrong, and a
26/// button to dismiss it.
27fn error_banner(error: &str) -> Element<'_, Message> {
28    let first_line = error.lines().next().unwrap_or_default();
29    let row = widget::Row::new()
30        .push(
31            container(text(truncate_chars(first_line, BANNER_MAX_CHARS)).size(12).wrapping(Wrapping::WordOrGlyph))
32                .width(Length::Fill),
33        )
34        .push(
35            button::icon(widget::icon::from_name("window-close-symbolic"))
36                .tooltip(fl!("error-dismiss"))
37                .on_press(Message::ClearError)
38                .padding(2),
39        )
40        .spacing(8)
41        .align_y(Alignment::Center)
42        .width(Length::Fill);
43
44    container(row)
45        .padding(8)
46        .width(Length::Fill)
47        .class(cosmic::theme::Container::custom(|_theme| container::Style {
48            background: Some(cosmic::iced::Background::Color(cosmic::iced::Color::from_rgb(0.8, 0.2, 0.2))),
49            text_color: Some(cosmic::iced::Color::WHITE),
50            border: cosmic::iced::Border { radius: 4.0.into(), ..Default::default() },
51            ..Default::default()
52        }))
53        .into()
54}
55
56impl AppModel {
57    /// What the rows of a list are built with, as things stand now: the
58    /// list whose long notes in full are `expanded`, and in which the
59    /// keyboard is at the row `marked`.
60    pub(crate) fn row_context<'a>(&'a self, expanded: &'a Expanded, marked: Option<EventId>) -> RowContext<'a> {
61        RowContext {
62            profiles: &self.profiles,
63            images: &self.images,
64            previews: &self.previews,
65            videos: &self.videos,
66            engagement: &self.engagement,
67            seen_on: &self.seen_on,
68            now: unix_now(),
69            // The theme's accent, as text. Read once here rather than for
70            // every row.
71            accent: cosmic::theme::active().cosmic().accent_text_color().into(),
72            can_publish: self.can_publish(),
73            repost_menu: self.repost_menu,
74            who: self.who_menu.as_ref(),
75            reacting: Reacting { menu: self.reaction_menu.as_ref(), suggested: &self.reaction_suggestions },
76            expanded,
77            marked,
78            share_copied: self.share_copied,
79            picture_copied: self.picture_copied.as_deref(),
80            menu_open: self.menu_open,
81            bookmarking: match &self.bookmarks {
82                _ if !self.can_publish() => Bookmarking::Unavailable,
83                BookmarkState::Unknown => Bookmarking::Unknown,
84                BookmarkState::Known(bookmarks) => Bookmarking::Known { bookmarks, pending: self.bookmark_pending },
85                BookmarkState::Unreadable(why) => Bookmarking::Unreadable(why),
86            },
87            sizes: RowSizes::new(self.config.note_text_size()),
88        }
89    }
90
91    /// Dispatch to the appropriate view based on the current [`ViewState`].
92    fn view_page_content(&self) -> Element<'_, Message> {
93        match self.view_state {
94            ViewState::Login => self.view_login(),
95            ViewState::Feed => self.view_feed(),
96            ViewState::Page => match self.pages.last() {
97                Some(Page::Thread(state)) => self.view_thread(state),
98                Some(Page::Profile(state)) => self.view_profile(state),
99                Some(Page::Bookmarks(page)) => self.view_bookmarks(page),
100                Some(Page::Search(page)) => self.view_search(page),
101                Some(Page::Notifications(page)) => self.view_notifications(page),
102                // Nothing is open over the feed.
103                None => self.view_feed(),
104            },
105            ViewState::Compose => self.view_compose(),
106            ViewState::Zap => self.view_zap(),
107            ViewState::Settings => self.view_settings(),
108            ViewState::Keys => self.view_keys(),
109            ViewState::Relays => self.view_relays(),
110            ViewState::Media => self.view_media(),
111        }
112    }
113
114    /// Build the full content tree for the popup: the current view, under
115    /// the error banner if there is something to say.
116    pub fn view_content(&self) -> Element<'_, Message> {
117        let page = self.view_page_content();
118        match &self.error_message {
119            Some(error) => widget::Column::new().push(error_banner(error)).push(page).spacing(8).into(),
120            None => page,
121        }
122    }
123
124    /// Render the main popup window, in the applet popup container, which
125    /// sizes the popup to its content: within these limits, in place of the
126    /// container's own, which are one width and no other. A list longer
127    /// than the popup may be tall takes what room the limit leaves it, and
128    /// scrolls.
129    pub fn view_popup(&self, _id: Id) -> Element<'_, Message> {
130        // A video plays while the rows built for this show it.
131        self.videos.viewing();
132        self.core
133            .applet
134            .popup_container(self.view_content())
135            .limits(
136                Limits::NONE
137                    .min_width(POPUP_MIN_WIDTH)
138                    .max_width(POPUP_MAX_WIDTH)
139                    .min_height(1.0)
140                    .max_height(self.popup_max_height()),
141            )
142            .into()
143    }
144}