Skip to main content

cosmic_applet_avestruz/views/components/
reaction_menu.rs

1// SPDX-License-Identifier: MIT
2
3//! What a note can be reacted to with, on offer under its like.
4//!
5//! First a row: the like itself, the emoji this reader reacts with most
6//! (see [`crate::emoji`]), and a way to all the others. That way swaps the
7//! row for every emoji there is: a field to find one by what it is
8//! called, the groups they come in, and the emoji of the group chosen, or
9//! those found, in a grid that scrolls. Pressing any emoji reacts with it.
10
11use std::sync::LazyLock;
12
13use cosmic::Element;
14use cosmic::iced::widget::scrollable;
15use cosmic::iced::widget::text::Shaping;
16use cosmic::iced::{Alignment, Color, Length};
17use cosmic::widget::{self, button, container, icon, text, text_input};
18use nostr_sdk::prelude::EventId;
19
20use crate::emoji::{self, Group};
21use crate::fl;
22use crate::messages::Message;
23use crate::nostr::engagement::Reaction;
24use crate::state::{EmojiPicker, ReactionMenu};
25use crate::views::components::like_icon;
26
27/// How large an emoji to react with is set, the room each has to be
28/// pressed in, and the room around them all.
29const EMOJI_SIZE: u16 = 20;
30const EMOJI_CELL: f32 = 34.0;
31const REACTIONS_PADDING: u16 = 4;
32
33/// How many emoji the grid of all of them has across, and how tall it is
34/// before it scrolls.
35const PICKER_COLUMNS: usize = 8;
36const PICKER_HEIGHT: f32 = 6.0 * EMOJI_CELL;
37
38/// The room beside the grid for the bar it scrolls by, which is drawn
39/// over what it scrolls: without it the last emoji of each row is under
40/// the bar.
41const PICKER_SCROLLBAR_ROOM: f32 = 14.0;
42
43/// The field an emoji is found by, by which it is told to take the
44/// keyboard.
45static EMOJI_SEARCH: LazyLock<widget::Id> = LazyLock::new(widget::Id::unique);
46
47/// The id of the field an emoji is found by.
48pub fn emoji_search_id() -> widget::Id {
49    EMOJI_SEARCH.clone()
50}
51
52/// `shown`, in the room one thing to react with has, doing `on_press`
53/// when pressed.
54fn cell<'a>(shown: impl Into<Element<'a, Message>>, on_press: Message) -> Element<'a, Message> {
55    button::custom(container(shown).center(Length::Fill))
56        .padding(0)
57        .width(Length::Fixed(EMOJI_CELL))
58        .height(Length::Fixed(EMOJI_CELL))
59        .class(cosmic::theme::Button::Text)
60        .on_press(on_press)
61        .into()
62}
63
64/// `emoji`, to be pressed: it reacts to the note `note` with itself. It
65/// is set in the colour of text, as it is in a note: an emoji drawn in
66/// one colour would take the colour of a button otherwise, where it is
67/// what is said and not what is done.
68fn emoji_cell<'a>(note: EventId, emoji: &str) -> Element<'a, Message> {
69    let color: Color = cosmic::theme::active().cosmic().on_bg_color().into();
70    let shown = text(emoji.to_string()).size(EMOJI_SIZE).shaping(Shaping::Advanced).class(cosmic::theme::Text::Color(color));
71    cell(shown, Message::React(note, Reaction::Emoji(emoji.to_string())))
72}
73
74/// What is offered, set apart from what it is over as a menu is.
75fn offered<'a>(content: impl Into<Element<'a, Message>>) -> Element<'a, Message> {
76    container(content).padding(REACTIONS_PADDING).class(cosmic::theme::Container::Dropdown).into()
77}
78
79/// The row offered first: the like, each of `suggested`, and the way to
80/// every other emoji.
81fn reaction_row<'a>(note: EventId, suggested: &[String]) -> Element<'a, Message> {
82    let like = widget::tooltip(
83        cell(icon::icon(like_icon(false)).size(EMOJI_SIZE), Message::React(note, Reaction::Like)),
84        text(fl!("note-like")).size(12),
85        widget::tooltip::Position::Top,
86    );
87    let more = widget::tooltip(
88        cell(icon::from_name("view-more-symbolic").size(16), Message::OpenEmojiPicker),
89        text(fl!("note-react-more")).size(12),
90        widget::tooltip::Position::Top,
91    );
92    let emoji = suggested.iter().map(|emoji| emoji_cell(note, emoji));
93    offered(widget::Row::with_children(std::iter::once(like.into()).chain(emoji).chain([more.into()])).align_y(Alignment::Center))
94}
95
96/// Every emoji there is, to pick one from: the field to find one by, the
97/// groups, and the emoji of the group chosen or those found.
98fn emoji_picker<'a>(note: EventId, picker: &'a EmojiPicker) -> Element<'a, Message> {
99    let across = PICKER_COLUMNS as f32 * EMOJI_CELL + PICKER_SCROLLBAR_ROOM;
100    let search =
101        text_input(fl!("emoji-search-placeholder"), &picker.typed).id(emoji_search_id()).on_input(Message::EmojiSearchTyped);
102
103    // The groups, each stood for by its first emoji; the one on show is
104    // marked, unless what is typed has put a search in its place.
105    let searching = !picker.typed.trim().is_empty();
106    let groups = Group::iter().map(|group| {
107        let shown = text(emoji::standing_for(group)).size(16).shaping(Shaping::Advanced);
108        let chosen = group == picker.group && !searching;
109        button::custom(container(shown).center(Length::Fill))
110            .padding(0)
111            .width(Length::Fill)
112            .height(Length::Fixed(EMOJI_CELL - 4.0))
113            .class(if chosen { cosmic::theme::Button::Suggested } else { cosmic::theme::Button::Text })
114            .on_press(Message::EmojiGroup(group))
115            .into()
116    });
117
118    let found = emoji::found(&picker.typed, picker.group);
119    let grid: Element<'a, Message> = if found.is_empty() {
120        container(text(fl!("emoji-search-nothing")).size(13)).center_x(Length::Fill).padding(12).into()
121    } else {
122        let rows = found
123            .chunks(PICKER_COLUMNS)
124            .map(|row| widget::Row::with_children(row.iter().map(|emoji| emoji_cell(note, emoji))).into());
125        widget::Column::with_children(rows).into()
126    };
127    let grid = scrollable(grid).width(Length::Fill).height(Length::Fixed(PICKER_HEIGHT));
128
129    let picker = widget::Column::new()
130        .push(search)
131        .push(widget::Row::with_children(groups))
132        .push(grid)
133        .spacing(4)
134        .width(Length::Fixed(across));
135    offered(picker)
136}
137
138/// What `menu` offers to react to its note with: the row offered first,
139/// or every emoji there is once those were asked for.
140pub fn reaction_menu<'a>(menu: &'a ReactionMenu, suggested: &[String]) -> Element<'a, Message> {
141    match &menu.picker {
142        Some(picker) => emoji_picker(menu.note, picker),
143        None => reaction_row(menu.note, suggested),
144    }
145}