cosmic_applet_avestruz/views/components/
quoted_note.rs1use 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
34const QUOTE_PADDING: f32 = 8.0;
36
37const QUOTE_AVATAR_SIZE: f32 = 20.0;
40const QUOTE_AVATAR_GAP: u16 = 8;
41
42const QUOTE_INSET: f32 = 2.0 * (CARD_BORDER + QUOTE_PADDING);
45
46pub 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
53pub 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, ¬e.author);
60 let picture = profiles.get(¬e.author).and_then(|profile| profile.picture.as_deref());
61
62 let header = widget::Row::new()
63 .push(avatar(images, picture, ¬e.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
83pub fn quote_link<'a>(quote: &'a Quote, context: &RowContext<'a>) -> Element<'a, Message> {
86 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}