cosmic_applet_avestruz/views/components/
reaction_menu.rs1use 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
27const EMOJI_SIZE: u16 = 20;
30const EMOJI_CELL: f32 = 34.0;
31const REACTIONS_PADDING: u16 = 4;
32
33const PICKER_COLUMNS: usize = 8;
36const PICKER_HEIGHT: f32 = 6.0 * EMOJI_CELL;
37
38const PICKER_SCROLLBAR_ROOM: f32 = 14.0;
42
43static EMOJI_SEARCH: LazyLock<widget::Id> = LazyLock::new(widget::Id::unique);
46
47pub fn emoji_search_id() -> widget::Id {
49 EMOJI_SEARCH.clone()
50}
51
52fn 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
64fn 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
74fn 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
79fn 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
96fn 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 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
138pub 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}