Skip to main content

cosmic_applet_avestruz/views/components/
quoted_note.rs

1// SPDX-License-Identifier: MIT
2
3//! A note another note quotes, shown under what that one says: a card with
4//! who wrote the quoted note and when, and what it says, as a row would
5//! show it: its text, its images, the preview of the page it links to. A
6//! click opens its thread.
7//!
8//! A quoted note shows the notes it quotes in turn the same way, a card in
9//! its card, and there it ends: quoting has no end of its own, so only so
10//! many notes deep are looked for (see
11//! [`QUOTE_DEPTH`](crate::nostr::content::QUOTE_DEPTH)). A quoted note that
12//! is not at hand, for that or because nobody has it, shows as a link to
13//! it, on a line of its own.
14//!
15//! Whether a quoted note is at hand is settled before the note quoting it
16//! is ever shown (see [`Note::quoted`]), so a card doesn't turn up in a row
17//! that is being read.
18
19use cosmic::Element;
20use cosmic::iced::widget::text::{Span, Wrapping};
21use cosmic::iced::widget::{rich_text, span};
22use cosmic::iced::{Alignment, Length, mouse};
23use cosmic::widget::{self, container, text};
24use nostr_sdk::prelude::EventId;
25
26use crate::helpers::relative_time;
27use crate::messages::Message;
28use crate::nostr::content::Quote;
29use crate::nostr::models::{Note, display_name};
30use crate::nostr::outbox::Whereabouts;
31use crate::views::components::note_row::{NOTE_SPACING, Reading, RowContext, RowSizes, line_height, said, said_height};
32use crate::views::components::{CARD_BORDER, avatar, card_style};
33
34/// The room between the card's edge and the note in it.
35const QUOTE_PADDING: f32 = 8.0;
36
37/// How wide the picture of the quoted note's author is, and the gap
38/// between it and their name.
39const QUOTE_AVATAR_SIZE: f32 = 20.0;
40const QUOTE_AVATAR_GAP: u16 = 8;
41
42/// How much narrower than its card a quoted note is: the card's line and
43/// the room inside it, on both sides.
44const QUOTE_INSET: f32 = 2.0 * (CARD_BORDER + QUOTE_PADDING);
45
46/// How tall the card of the quoted note `note` will be, near enough, set
47/// in `sizes` in a row `width` wide.
48pub fn quoted_note_height(note: &Note, sizes: RowSizes, width: f32) -> f32 {
49    let header = QUOTE_AVATAR_SIZE.max(line_height(sizes.name()));
50    QUOTE_INSET + header + said_height(note, sizes, (width - QUOTE_INSET).max(1.0), Reading::Quoted)
51}
52
53/// The card of the quoted note `note`: who wrote it and how long ago, and
54/// under that what it says. It opens the note's thread when it is clicked
55/// anywhere that doesn't lead somewhere of its own; not while a menu is
56/// open over the list, as a row doesn't.
57pub fn quoted_note<'a>(note: &'a Note, context: &RowContext<'a>) -> Element<'a, Message> {
58    let RowContext { profiles, images, now, sizes, .. } = *context;
59    let name = display_name(profiles, &note.author);
60    let picture = profiles.get(&note.author).and_then(|profile| profile.picture.as_deref());
61
62    let header = widget::Row::new()
63        .push(avatar(images, picture, &note.author, &name, QUOTE_AVATAR_SIZE))
64        .push(
65            container(text(name).size(sizes.name()).font(cosmic::font::bold()).wrapping(Wrapping::None))
66                .width(Length::Fill)
67                .clip(true),
68        )
69        .push(text(relative_time(now, note.created_at)).size(sizes.time()).wrapping(Wrapping::None))
70        .spacing(QUOTE_AVATAR_GAP)
71        .align_y(Alignment::Center);
72    let body = said(widget::Column::new().push(header).spacing(NOTE_SPACING).width(Length::Fill), note, context, Reading::Quoted);
73    let card = container(body).padding(CARD_BORDER + QUOTE_PADDING).width(Length::Fill).class(card_style());
74
75    let area = widget::mouse_area(card);
76    if context.under_a_menu() {
77        area.into()
78    } else {
79        area.on_press(Message::OpenThread(note.id, Whereabouts::default())).interaction(mouse::Interaction::Pointer).into()
80    }
81}
82
83/// A quoted note that is not at hand: the short form of its address, on a
84/// line of its own, which opens its thread, where it is looked for again.
85pub fn quote_link<'a>(quote: &'a Quote, context: &RowContext<'a>) -> Element<'a, Message> {
86    // With where its address says it is: that is where it is looked for.
87    let link = (quote.id, quote.whereabouts());
88    let spans: Vec<Span<'a, (EventId, Whereabouts)>> = vec![span(quote.label.as_str()).color(context.accent).link(link)];
89    rich_text(spans)
90        .size(context.sizes.text())
91        .wrapping(Wrapping::WordOrGlyph)
92        .width(Length::Fill)
93        .on_link_click(|(id, whereabouts)| Message::OpenThread(id, whereabouts))
94        .into()
95}