Skip to main content

cosmic_applet_avestruz/views/components/
folded_reply.rs

1// SPDX-License-Identifier: MIT
2
3//! The row of a note that is folded: one from outside the account's
4//! network, in a thread (see [`crate::nostr::thread`]) or among what the
5//! account is told of (see [`crate::nostr::notifications`]).
6//!
7//! It says who wrote the note and why it is folded, and nothing of what
8//! it says: an answer
9//! from outside the network is, as often as not, somebody selling
10//! something. A click anywhere on it shows it in full.
11//!
12//! Nor is the picture of whoever wrote it fetched, which would tell
13//! their server that the answer was seen: they have the disc with their
14//! initial that everybody has until their picture is there.
15//!
16//! The row is one height, whoever wrote it and whatever is under it,
17//! and that is known before it is laid out
18//! ([`folded_reply_height`]): a list that counts on it never moves.
19
20use cosmic::iced::core::text::Wrapping;
21use cosmic::iced::widget::svg;
22use cosmic::iced::{Alignment, Color, Length, mouse};
23use cosmic::prelude::*;
24use cosmic::widget::{self, container, icon, text};
25
26use crate::fl;
27use crate::messages::Message;
28use crate::nostr::models::{Note, display_name};
29use crate::views::components::note_row::{
30    NOTE_ACTION_DIM, NOTE_AVATAR_GAP, NOTE_AVATAR_SIZE, NOTE_DIVIDER_HEIGHT, NOTE_PADDING_TOP, NOTE_SPACING, RowContext,
31    RowSizes, line_height,
32};
33use crate::views::components::{avatar, shield_icon};
34
35/// How tall what a folded row says is, set in `sizes`: the picture, or
36/// the two lines beside it, whichever is taller.
37fn said_height(sizes: RowSizes) -> f32 {
38    NOTE_AVATAR_SIZE.max(line_height(sizes.name()) + line_height(sizes.time()))
39}
40
41/// How tall the row of a folded answer is, set in `sizes`: what it says,
42/// with the room over it and the line under it that every row has.
43pub fn folded_reply_height(sizes: RowSizes) -> f32 {
44    f32::from(NOTE_PADDING_TOP) + said_height(sizes) + f32::from(NOTE_SPACING) + NOTE_DIVIDER_HEIGHT
45}
46
47/// The row of `note`, which somebody outside the account's network
48/// wrote: who wrote it, that it is from outside the network, and at its
49/// end `beside`, in small print: in a thread, how many answers are out
50/// of sight under it; among the notifications, when it was written. A
51/// click shows it in full, but not while a menu is open over the list.
52pub fn folded_reply<'a>(note: &'a Note, beside: String, context: &RowContext<'a>) -> Element<'a, Message> {
53    let RowContext { profiles, images, sizes, .. } = *context;
54    let on_background: Color = cosmic::theme::active().cosmic().on_bg_color().into();
55    let quiet = Color { a: NOTE_ACTION_DIM, ..on_background };
56    let quietly = move |handle: icon::Handle| {
57        icon::icon(handle).size(sizes.time()).class(cosmic::theme::Svg::custom(move |_theme| svg::Style { color: Some(quiet) }))
58    };
59    let small = |said: String| text(said).size(sizes.time()).wrapping(Wrapping::None).class(cosmic::theme::Text::Color(quiet));
60
61    let name = display_name(profiles, &note.author);
62    let picture = avatar(images, None, &note.author, &name, NOTE_AVATAR_SIZE);
63    let why = widget::Row::new()
64        .push(quietly(shield_icon()))
65        .push(small(fl!("thread-outside-network")))
66        .spacing(4)
67        .align_y(Alignment::Center);
68    let who =
69        widget::Column::new().push(text(name).size(sizes.name()).font(cosmic::font::bold()).wrapping(Wrapping::None)).push(why);
70    let said = widget::Row::new()
71        .push(picture)
72        // The name takes the room the rest leaves, and is cut rather
73        // than wrapped when it doesn't fit.
74        .push(container(who).width(Length::Fill).clip(true))
75        .push(small(beside))
76        .push(quietly(icon::from_name("pan-down-symbolic").handle()))
77        .spacing(NOTE_AVATAR_GAP)
78        .align_y(Alignment::Center);
79
80    let row = widget::Column::new()
81        .push(container(said).height(Length::Fixed(said_height(sizes))).align_y(Alignment::Center).clip(true))
82        .push(widget::divider::horizontal::default())
83        .spacing(NOTE_SPACING)
84        .padding([NOTE_PADDING_TOP, 0, 0, 0])
85        .width(Length::Fill);
86    let area = widget::mouse_area(row);
87    if context.under_a_menu() {
88        area.into()
89    } else {
90        area.on_press(Message::ShowReply(note.id)).interaction(mouse::Interaction::Pointer).into()
91    }
92}