Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
//! Discord's "Reactions" dialog: the message's reactions down the left, and
//! everyone who used the selected one on the right. Opened by clicking a
//! reaction's tooltip, or "View Reactions" in a message's menu.
use crate::app::RustCord;
use crate::style::{icon, icon_font};
use discord::model::{Id, ReactionEmoji, User};
use eframe::egui::{self, pos2, vec2, Align2, Color32, CursorIcon, FontId, Frame, Key, Margin, Order, Rect, RichText, ScrollArea, Sense, Stroke};
pub struct ReactionsDialog {
pub channel: Id,
pub msg: Id,
/// Which of the message's reactions is shown.
pub selected: usize,
}
/// Where the full list of who used a reaction is kept.
fn list_tag(msg: Id, emoji: &ReactionEmoji, count: u32) -> String {
format!("rxl:{msg}:{}:{}:{count}", emoji.id.map_or(0, |i| i.0), emoji.name.as_deref().unwrap_or(""))
}
fn emoji_image(e: &ReactionEmoji) -> Option<String> {
match (e.id, e.name.as_deref()) {
(Some(id), _) => Some(super::emoji_img(id, e.animated, 48)),
(None, Some(n)) => Some(crate::emoji::twemoji_url(n)),
_ => None,
}
}
impl RustCord {
/// Open the dialog on `emoji` (or the first reaction).
pub fn open_reactions(&mut self, channel: Id, msg: Id, emoji: Option<&ReactionEmoji>) {
let selected = self
.store
.messages
.get(&channel)
.and_then(|l| l.list.iter().find(|m| m.id == msg))
.and_then(|m| emoji.and_then(|e| m.reactions.iter().position(|r| r.emoji.same(e))))
.unwrap_or(0);
self.reactions_dialog = Some(ReactionsDialog { channel, msg, selected });
}
pub fn reactions_dialog_ui(&mut self, ctx: &egui::Context) {
let Some(d) = &self.reactions_dialog else { return };
let (channel, msg, mut selected) = (d.channel, d.msg, d.selected);
let t = self.theme.clone();
let message = self.store.messages.get(&channel).and_then(|l| l.list.iter().find(|m| m.id == msg));
// The message (or its last reaction) is gone: nothing to show.
let Some(reactions) = message.map(|m| m.reactions.clone()).filter(|r| !r.is_empty()) else {
self.reactions_dialog = None;
return;
};
let guild = message.and_then(|m| m.guild_id).or_else(|| self.store.channels.get(&channel).and_then(|c| c.guild_id));
selected = selected.min(reactions.len() - 1);
let current = reactions[selected].clone();
let tag = list_tag(msg, ¤t.emoji, current.count);
// Fetch who reacted (up to Discord's page of 100).
let name = current.emoji.name.clone().unwrap_or_default();
let spec = match current.emoji.id {
Some(id) => format!("{name}:{id}"),
None => name,
};
let spec: String = url::form_urlencoded::byte_serialize(spec.as_bytes()).collect();
self.api_get_once(&tag, format!("/channels/{channel}/messages/{msg}/reactions/{spec}?limit=100&type=0"));
let users: Option<Result<Vec<User>, String>> = self.api_result(&tag).map(|r| match r {
Ok(v) => serde_json::from_value::<Vec<User>>(v.clone()).map_err(|e| e.to_string()),
Err(e) => Err(e.clone()),
});
let screen = ctx.screen_rect();
let mut close = ctx.input(|i| i.key_pressed(Key::Escape));
// Dim the app; a click outside closes.
egui::Area::new(egui::Id::new("reactions_dim")).order(Order::Foreground).fixed_pos(screen.min).show(ctx, |ui| {
let (r, resp) = ui.allocate_exact_size(screen.size(), Sense::click());
ui.painter().rect_filled(r, 0.0, Color32::from_black_alpha(178));
close |= resp.clicked();
});
let mut open_profile: Option<(User, egui::Pos2)> = None;
let size = vec2(480.0_f32.min(screen.width() - 32.0), 440.0_f32.min(screen.height() - 32.0));
egui::Area::new(egui::Id::new("reactions_dialog")).order(Order::Tooltip).anchor(Align2::CENTER_CENTER, vec2(0.0, 0.0)).show(ctx, |ui| {
Frame::none().fill(t.bg_floating).rounding(12.0).stroke(Stroke::new(1.0_f32, t.border_subtle)).inner_margin(Margin::same(0.0)).show(ui, |ui| {
ui.set_width(size.x);
ui.set_height(size.y);
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
// Header: title and ✕.
let (head, _) = ui.allocate_exact_size(vec2(size.x, 56.0), Sense::hover());
ui.painter().text(pos2(head.left() + 16.0, head.center().y), Align2::LEFT_CENTER, "Reactions", FontId::new(20.0, egui::FontFamily::Name(crate::style::BOLD.into())), t.header_primary);
let x = Rect::from_center_size(pos2(head.right() - 28.0, head.center().y), vec2(32.0, 32.0));
let xr = ui.interact(x, ui.id().with("close"), Sense::click());
ui.painter().text(x.center(), Align2::CENTER_CENTER, icon::CLOSE, icon_font(14.0), if xr.hovered() { t.interactive_hover } else { t.interactive_normal });
close |= xr.on_hover_cursor(CursorIcon::PointingHand).clicked();
ui.painter().hline(head.x_range(), head.bottom(), Stroke::new(1.0_f32, t.border_subtle));
let body_h = size.y - 57.0;
ui.horizontal_top(|ui| {
// Left: one pill per reaction.
ui.allocate_ui_with_layout(vec2(104.0, body_h), egui::Layout::top_down(egui::Align::Min), |ui| {
ui.set_width(104.0);
ui.set_height(body_h);
ScrollArea::vertical().id_salt("reactions_left").max_width(104.0).max_height(body_h).auto_shrink([false, false]).show(ui, |ui| {
ui.add_space(8.0);
for (i, r) in reactions.iter().enumerate() {
let (row, resp) = ui.allocate_exact_size(vec2(104.0, 36.0), Sense::click());
let pill = row.shrink2(vec2(8.0, 2.0));
let on = i == selected;
if on || resp.hovered() {
ui.painter().rect_filled(pill, 8.0, crate::style::rgba(151, 151, 159, if on { 61 } else { 31 }));
}
if let Some(url) = emoji_image(&r.emoji) {
egui::Image::new(url).paint_at(ui, Rect::from_center_size(pos2(pill.left() + 20.0, pill.center().y), vec2(20.0, 20.0)));
}
ui.painter().text(
pos2(pill.left() + 38.0, pill.center().y),
Align2::LEFT_CENTER,
r.count.to_string(),
FontId::new(14.0, egui::FontFamily::Name(crate::style::BOLD.into())),
if on { t.header_primary } else { t.interactive_normal },
);
if resp.on_hover_cursor(CursorIcon::PointingHand).clicked() {
selected = i;
}
}
ui.add_space(8.0);
});
});
let (line, _) = ui.allocate_exact_size(vec2(1.0, body_h), Sense::hover());
ui.painter().rect_filled(line, 0.0, t.border_subtle);
// Right: who reacted.
let right_w = size.x - 105.0;
ui.allocate_ui_with_layout(vec2(right_w, body_h), egui::Layout::top_down(egui::Align::Min), |ui| {
ui.set_width(right_w);
ui.set_height(body_h);
ScrollArea::vertical().id_salt(("reactions_right", selected)).max_width(right_w).max_height(body_h).auto_shrink([false, false]).show(ui, |ui| {
let w = right_w;
match &users {
None => {
ui.add_space(24.0);
ui.vertical_centered(|ui| ui.spinner());
}
Some(Err(e)) => {
ui.add_space(16.0);
ui.horizontal(|ui| {
ui.add_space(16.0);
ui.label(RichText::new(format!("Couldn't load who reacted: {e}")).size(14.0).color(t.text_muted));
});
}
Some(Ok(list)) => {
ui.add_space(4.0);
for (i, u) in list.iter().enumerate() {
let (row, resp) = ui.allocate_exact_size(vec2(w, 44.0), Sense::click());
if resp.hovered() {
ui.painter().rect_filled(row.shrink2(vec2(8.0, 1.0)), 4.0, crate::style::rgba(151, 151, 159, 20));
}
if i > 0 {
ui.painter().hline(row.left() + 16.0..=row.right() - 16.0, row.top(), Stroke::new(1.0_f32, t.border_subtle));
}
let av = Rect::from_center_size(pos2(row.left() + 28.0, row.center().y), vec2(24.0, 24.0));
super::paint_avatar(ui, av, Some(u.avatar_or_default(64)), u.display_name());
let nick = self.store.nick_in(guild, Some(channel), u.id);
let shown = nick.unwrap_or_else(|| u.display_name().to_string());
let p = ui.painter_at(row.shrink2(vec2(12.0, 0.0)));
let name = super::paint_text_emoji(ui, &p, pos2(av.right() + 8.0, row.center().y), &shown, crate::style::medium(16.0), t.header_primary);
p.text(pos2(name.right() + 6.0, row.center().y + 1.0), Align2::LEFT_CENTER, &u.username, FontId::proportional(14.0), t.text_muted);
if resp.on_hover_cursor(CursorIcon::PointingHand).clicked() {
open_profile = Some((u.clone(), pos2(row.right() + 8.0, row.top())));
}
}
ui.add_space(8.0);
}
}
});
});
});
});
});
if let Some((user, pos)) = open_profile {
self.reactions_dialog = None;
self.open_profile(user, guild, pos);
return;
}
if close {
self.reactions_dialog = None;
} else if let Some(d) = &mut self.reactions_dialog {
d.selected = selected;
}
}
}