Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
Leave group DMs from their menus, with a confirmation (#28)
Matías Sánchez González829521dd8a69
diff --git a/apps/rustcord/src/app.rs b/apps/rustcord/src/app.rsindex 5149b3b..8fca593 100644--- a/apps/rustcord/src/app.rs+++ b/apps/rustcord/src/app.rs@@ -274,6 +274,8 @@ pub struct RustCord {pub show_group_members: bool,/// The "Invite to Group DM" / "Add Friends to DM" dialog.pub group_invite: Option<crate::ui::group_dm::GroupInvite>,+ /// The "Leave '<group>'" confirmation dialog.+ pub group_leave: Option<crate::ui::group_dm::LeaveGroup>,/// People being added to a group DM, until Discord answers.pub group_adds: Vec<Id>,/// How many 100-member windows we've subscribed to, per channel.@@ -642,6 +644,7 @@ impl RustCord {show_members: true,show_group_members: true,group_invite: None,+ group_leave: None,group_adds: Vec::new(),member_windows: HashMap::new(),pins: None,@@ -1056,6 +1059,8 @@ impl RustCord {self.switcher = None;self.friend_menu = None;self.dm_menu = None;+ self.group_invite = None;+ self.group_leave = None;self.badge_window = None;self.badges_view = None;self.search = None;@@ -1355,6 +1360,7 @@ impl RustCord {}}Event::Info(text) => self.toast(text, false),+ Event::GroupLeft(channel) => self.remove_channel_and_navigate(channel, None),Event::Apps(apps) => {for a in apps {self.apps.insert(a.id, a);@@ -1642,12 +1648,9 @@ impl RustCord {"CHANNEL_RECIPIENT_ADD" => self.store.group_recipient(&d, true),"CHANNEL_RECIPIENT_REMOVE" => self.store.group_recipient(&d, false),"CHANNEL_DELETE" | "THREAD_DELETE" => {- // An open thread that was deleted: back to its channel.- let id = Id::from_value(&d["id"]);- if id.is_some() && id == self.current_channel && d["parent_id"].is_string() {- self.select_channel(Id::from_value(&d["parent_id"]));+ if let Some(id) = Id::from_value(&d["id"]) {+ self.remove_channel_and_navigate(id, Id::from_value(&d["parent_id"]));}- self.store.remove_channel(&d)}"THREAD_LIST_SYNC" => self.store.thread_list_sync(&d),"THREAD_MEMBERS_UPDATE" => self.store.thread_members_update(&d),@@ -1945,6 +1948,83 @@ impl RustCord {}}+ /// Remove a deleted or left channel from local state and navigate away if+ /// it was currently open.+ pub fn remove_channel_and_navigate(&mut self, id: Id, parent_id: Option<Id>) {+ let was_open = self.current_channel == Some(id);+ let removed_kind = self.store.channels.get(&id).map(|c| c.kind);+ self.store.remove_channel_by_id(id);+ self.drafts.remove(&id);+ self.uploads.remove(&id);+ self.unread_marker.remove(&id);+ self.calls.remove(&id);+ self.dismissed_calls.remove(&id);+ self.member_windows.remove(&id);+ self.request_previews.remove(&id);+ self.last_channel.retain(|_, ch| *ch != id);+ self.scroll_marks.borrow_mut().remove(&id);+ if self.scroll_restore.get().is_some_and(|(ch, ..)| ch == id) {+ self.scroll_restore.set(None);+ }+ if self.reply.as_ref().is_some_and(|r| r.channel == id) {+ self.reply = None;+ }+ if self.editing.as_ref().is_some_and(|e| e.channel == id) {+ self.editing = None;+ }+ if self.context_menu.is_some_and(|m| m.channel == id) {+ self.context_menu = None;+ }+ if self.dm_menu.is_some_and(|(ch, _, _)| ch == id) {+ self.dm_menu = None;+ }+ if self.channel_menu.is_some_and(|(ch, _, _)| ch == id) {+ self.channel_menu = None;+ }+ if self.group_invite_open(id) {+ self.group_invite = None;+ }+ if self.group_leave.as_ref().is_some_and(|d| d.channel == id) {+ self.group_leave = None;+ }+ if self.pins.as_ref().is_some_and(|p| p.0 == id) {+ self.pins = None;+ }+ if self.new_since.is_some_and(|n| n.channel == id) {+ self.new_since = None;+ }+ if self.ring_when_connected == Some(id) {+ self.ring_when_connected = None;+ }+ if self.camera_when_connected == Some(id) {+ self.camera_when_connected = None;+ }+ if self.call_check == Some(id) {+ self.call_check = None;+ }+ if self.voice.as_ref().is_some_and(|v| v.channel == id) {+ self.leave_voice();+ }+ if was_open {+ let thread_parent = parent_id.filter(|p| {+ removed_kind.is_none_or(discord::model::channel_kind::is_thread)+ && self.store.channels.get(p).is_some_and(|c| c.kind != discord::model::channel_kind::GUILD_CATEGORY)+ });+ if let Some(parent) = thread_parent {+ self.select_channel(Some(parent));+ } else if let Some(guild) = self.current_guild {+ let next = self.store.first_text_channel(Some(guild));+ self.select_channel(next);+ } else {+ self.select_channel(None);+ self.search = None;+ self.friends_open = true;+ self.requests_open = false;+ self.store_page = None;+ }+ }+ }+fn nav_here(&self) -> NavEntry {NavEntry { guild: self.current_guild, channel: self.current_channel, friends: self.friends_open }}diff --git a/apps/rustcord/src/backend.rs b/apps/rustcord/src/backend.rsindex 54b71ad..a0b386f 100644--- a/apps/rustcord/src/backend.rs+++ b/apps/rustcord/src/backend.rs@@ -84,6 +84,8 @@ pub enum Command {SendInvite { user: Id, dm: Option<Id>, content: String, nonce: String },GuildSettings { guild: Id, patch: serde_json::Value },LeaveGuild(Id),+ /// Leave a group DM (or close a DM); `silent` suppresses the system leave message.+ LeaveGroup { channel: Id, silent: bool },/// Accept (true) / ignore a message request.AnswerRequest { channel: Id, accept: bool },FetchRequestPreviews(Vec<Id>),@@ -226,6 +228,8 @@ pub enum Event {Ignored { user: Id, on: bool, result: Result<(), String> },/// Something worked: tell the user.Info(String),+ /// Left a group DM (REST succeeded; gateway `CHANNEL_DELETE` may also follow).+ GroupLeft(Id),/// First message of each message request.RequestPreviews(Vec<(Id, Message)>),/// A friend request by username: Ok(name) or why not.@@ -1477,6 +1481,15 @@ async fn run(mut rx: UnboundedReceiver<Command>, base: Emitter) {}});}+ Command::LeaveGroup { channel, silent } => {+ let (http, em) = (http.clone(), em.clone());+ tokio::spawn(async move {+ match http.leave_group(channel, silent).await {+ Ok(()) => em.emit(Event::GroupLeft(channel)),+ Err(e) => em.emit(Event::Error(format!("Couldn't leave the group: {e:#}"))),+ }+ });+ }Command::FetchGuildProfile(guild) => {let (http, em) = (http.clone(), em.clone());tokio::spawn(async move {diff --git a/apps/rustcord/src/demo.rs b/apps/rustcord/src/demo.rsindex 9a3c09e..447b206 100644--- a/apps/rustcord/src/demo.rs+++ b/apps/rustcord/src/demo.rs@@ -984,6 +984,9 @@ pub async fn run(mut rx: UnboundedReceiver<Command>, em: Emitter) {| Command::LeaveGuild(_) => {em.emit(Event::Info("Demo mode: nothing is changed on Discord.".into()));}+ Command::LeaveGroup { channel, .. } => {+ dispatch(&em, "CHANNEL_DELETE", json!({ "id": channel.to_string(), "type": 3 }));+ }// A made-up player, so the controller can be tried without Spotify.Command::Spotify(request) => {use crate::backend::SpotifyRequest;diff --git a/apps/rustcord/src/store.rs b/apps/rustcord/src/store.rsindex 140f6a0..f31d908 100644--- a/apps/rustcord/src/store.rs+++ b/apps/rustcord/src/store.rs@@ -658,6 +658,12 @@ impl Store {pub fn remove_channel(&mut self, v: &Value) {let Some(id) = Id::from_value(&v["id"]) else { return };+ self.remove_channel_by_id(id);+ }++ /// Drop a channel and its cached per-channel state (messages, read state,+ /// DM pin, typing, voice roster) from the local store.+ pub fn remove_channel_by_id(&mut self, id: Id) {if let Some(ch) = self.channels.remove(&id) {if let Some(g) = ch.guild_id.and_then(|g| self.guilds.get_mut(&g)) {g.channels.retain(|c| *c != id);@@ -665,6 +671,11 @@ impl Store {}self.messages.remove(&id);self.joined_threads.remove(&id);+ self.pinned_dms.remove(&id);+ self.dm_flags.remove(&id);+ self.read_states.remove(&id);+ self.typing.remove(&id);+ self.voice.remove(&id);}/// THREAD_LIST_SYNC: the active threads of a server (or of the channels@@ -2028,4 +2039,40 @@ mod tests {assert_eq!(store.read_states[&ch].last_read, Some(Id(30)));assert_eq!(store.read_states[&ch].mentions, 1);}++ #[test]+ fn leaving_group_dm_removes_channel_and_state() {+ let mut store = Store::default();+ store.me = Some(User { id: Id(1), ..Default::default() });+ store.add_private_channel(&json!({+ "id": "30", "type": 1, "last_message_id": "100",+ "recipients": [{ "id": "2", "username": "ferris" }]+ }));+ store.add_private_channel(&json!({+ "id": "31", "type": 3, "name": "crab rave", "last_message_id": "200",+ "recipients": [{ "id": "2", "username": "ferris" }, { "id": "3", "username": "corro" }]+ }));+ let group = Id(31);+ store.pinned_dms.insert(group);+ store.dm_flags.insert(group, DM_PINNED);+ store.messages.entry(group).or_default().loaded = true;+ store.message_ack(&json!({ "channel_id": "31", "message_id": "150", "mention_count": 2, "manual": true }));+ store.typing_start(&json!({ "channel_id": "31", "user_id": "2" }));+ store.voice_state_update(&json!({ "user_id": "2", "channel_id": "31" }));++ assert_eq!(store.dm_list(), [Id(31), Id(30)]);+ store.remove_channel_by_id(group);+ assert!(!store.channels.contains_key(&group));+ assert_eq!(store.dm_list(), [Id(30)]);+ assert!(!store.pinned_dms.contains(&group));+ assert!(!store.dm_flags.contains_key(&group));+ assert!(!store.messages.contains_key(&group));+ assert!(!store.read_states.contains_key(&group));+ assert!(!store.typing.contains_key(&group));+ assert!(!store.voice.contains_key(&group));++ // Idempotent if CHANNEL_DELETE arrives after local removal.+ store.remove_channel(&json!({ "id": "31", "type": 3 }));+ assert_eq!(store.dm_list(), [Id(30)]);+ }}diff --git a/apps/rustcord/src/ui/channel_menu.rs b/apps/rustcord/src/ui/channel_menu.rsindex df871e9..45bb8d6 100644--- a/apps/rustcord/src/ui/channel_menu.rs+++ b/apps/rustcord/src/ui/channel_menu.rs@@ -35,6 +35,7 @@ impl RustCord {pub fn open_channel_menu(&mut self, channel: Id, at: Pos2) {self.server_menu = None;self.guild_menu = None;+ self.dm_menu = None;self.channel_menu = Some((channel, at, self.ctx.cumulative_pass_nr()));self.ctx.data_mut(|d| d.remove::<Vec<usize>>(sub_path_id()));}diff --git a/apps/rustcord/src/ui/group_dm.rs b/apps/rustcord/src/ui/group_dm.rsindex 2a4be1b..a6d7b8a 100644--- a/apps/rustcord/src/ui/group_dm.rs+++ b/apps/rustcord/src/ui/group_dm.rs@@ -4,21 +4,35 @@use super::{paint_avatar, paint_status_dot};use crate::app::RustCord;-use crate::style::{icon, icon_font, medium, rgba};+use crate::style::{icon, icon_font, medium, overlay, rgba};use discord::model::{channel_kind, Id, User};-use eframe::egui::{self, pos2, vec2, Align2, Color32, CursorIcon, FontId, Key, Margin, Rect, ScrollArea, Sense, Stroke, TextEdit, Ui};+use eframe::egui::{self, pos2, vec2, Align2, Color32, CursorIcon, FontId, Key, Margin, Modifiers, Rect, ScrollArea, Sense, Stroke, TextEdit, Ui};use serde_json::json;/// Discord's limit: ten people in a group DM, you included.-const MAX_MEMBERS: usize = 10;+pub(super) const MAX_MEMBERS: usize = 10;/// The owner's crown (`--icon-feedback-warning`, read from the page).const CROWN: Color32 = Color32::from_rgb(206, 129, 0);const BRAND: Color32 = Color32::from_rgb(0x58, 0x65, 0xf2);const BRAND_HOVER: Color32 = Color32::from_rgb(0x47, 0x52, 0xc4);+const CRITICAL: Color32 = Color32::from_rgb(0xd2, 0x2d, 0x39);+const CRITICAL_HOVER: Color32 = Color32::from_rgb(0xa9, 0x23, 0x2e);/// The dialog's width and the API tag of a new group.const DIALOG_W: f32 = 478.0;+const LEAVE_W: f32 = 440.0;const CREATE_TAG: &str = "group-dm-create";+/// The "Leave '<group>'" confirmation dialog while it is up.+pub struct LeaveGroup {+ pub channel: Id,+ pub silent: bool,+}++/// Discord's title for it (the same shape as Leave Server's).+fn leave_group_title(name: &str) -> String {+ format!("Leave '{name}'")+}+/// The invite dialog while it is up.pub struct GroupInvite {/// The DM or group DM it was opened from.@@ -366,4 +380,114 @@ impl RustCord {None => {}}}++ /// Open the "Leave '<group>'" confirmation dialog for a group DM.+ pub fn open_leave_group(&mut self, channel: Id) {+ if self.store.channels.get(&channel).is_some_and(|c| c.kind == channel_kind::GROUP_DM) {+ self.group_leave = Some(LeaveGroup { channel, silent: false });+ }+ }++ /// Discord's confirmation before leaving a group DM, with its "Leave+ /// without notifying other members" box.+ pub fn leave_group_ui(&mut self, ctx: &egui::Context) {+ let Some(mut dialog) = self.group_leave.take() else { return };+ let Some(channel) = self.store.channels.get(&dialog.channel).filter(|c| c.kind == channel_kind::GROUP_DM) else {+ return;+ };+ let name = self.store.label(channel);+ let t = self.theme.clone();+ let mut cancel = ctx.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Escape));+ let mut confirm = false;+ cancel |= super::guild_pages::modal_frame(ctx, &t, "leave_group", LEAVE_W, |ui| {+ let plain = egui::TextFormat {+ font_id: FontId::proportional(16.0),+ color: t.header_secondary,+ line_height: Some(20.0),+ ..Default::default()+ };+ let bold_fmt = egui::TextFormat {+ font_id: FontId::new(16.0, egui::FontFamily::Name(crate::style::BOLD.into())),+ ..plain.clone()+ };+ let mut job = egui::text::LayoutJob::default();+ job.append("Are you sure you want to leave ", 0.0, plain.clone());+ job.append(&name, 0.0, bold_fmt);+ job.append("? You won't be able to rejoin this group unless you are re-invited.", 0.0, plain);+ job.wrap.max_width = LEAVE_W - 48.0;+ let body = ui.fonts(|f| f.layout_job(job));+ let check_label = ui.painter().layout_no_wrap(+ "Leave without notifying other members".into(),+ FontId::proportional(14.0),+ t.text_normal,+ );+ let body_h = body.size().y;+ let h = 56.0 + body_h + 12.0 + 24.0 + 16.0 + 40.0 + 24.0;+ let (r, _) = ui.allocate_exact_size(vec2(LEAVE_W, h), Sense::hover());+ let left = r.left() + 24.0;++ let title_font = super::guild_pages::strong(20.0);+ let title_text = leave_group_title(&name);+ let title_elided = super::elide_text_emoji(ui, &title_text, &title_font, LEAVE_W - 24.0 - 56.0);+ super::paint_text_emoji(ui, ui.painter(), pos2(left, r.top() + 36.0), &title_elided, title_font, t.header_primary);++ let x = Rect::from_min_size(pos2(r.right() - 8.0 - 40.0, r.top() + 8.0), vec2(40.0, 40.0));+ let xr = ui.interact(x, egui::Id::new("leave_group_close"), Sense::click()).on_hover_cursor(CursorIcon::PointingHand);+ if xr.hovered() {+ ui.painter().rect_filled(x, 8.0, t.bg_hover);+ }+ ui.painter().text(x.center(), Align2::CENTER_CENTER, icon::CLOSE, icon_font(14.0), if xr.hovered() { t.header_primary } else { t.interactive_normal });+ cancel |= xr.clicked();++ ui.painter().galley(pos2(left, r.top() + 56.0), body, t.header_secondary);++ let check_w = (24.0 + 8.0 + check_label.size().x).min(LEAVE_W - 48.0);+ let check_row = Rect::from_min_size(pos2(left, r.top() + 56.0 + body_h + 12.0), vec2(check_w, 24.0));+ let cr = ui.interact(check_row, egui::Id::new("leave_group_silent"), Sense::click()).on_hover_cursor(CursorIcon::PointingHand);+ let b = Rect::from_min_size(check_row.min + vec2(2.0, 2.0), vec2(20.0, 20.0));+ if dialog.silent {+ ui.painter().rect_filled(b, 4.0, BRAND);+ let p = |px: f32, py: f32| b.min + vec2(px, py);+ ui.painter().line_segment([p(5.0, 10.5), p(8.5, 14.0)], Stroke::new(2.0_f32, Color32::WHITE));+ ui.painter().line_segment([p(8.5, 14.0), p(15.0, 6.5)], Stroke::new(2.0_f32, Color32::WHITE));+ } else {+ ui.painter().rect_stroke(b, 4.0, Stroke::new(1.5_f32, t.interactive_normal));+ }+ ui.painter().galley(pos2(b.right() + 10.0, check_row.center().y - check_label.size().y / 2.0), check_label, t.text_normal);+ if cr.clicked() {+ dialog.silent = !dialog.silent;+ }++ let bw = (LEAVE_W - 48.0 - 8.0) / 2.0;+ let no = Rect::from_min_size(pos2(left, r.bottom() - 24.0 - 40.0), vec2(bw, 40.0));+ let yes = no.translate(vec2(bw + 8.0, 0.0));+ let nr = ui.interact(no, egui::Id::new("leave_group_cancel"), Sense::click()).on_hover_cursor(CursorIcon::PointingHand);+ ui.painter().rect(no, 8.0, overlay(if nr.hovered() { 0.2 } else { 0.12 }), Stroke::new(1.0_f32, rgba(151, 151, 159, 10)));+ ui.painter().text(no.center(), Align2::CENTER_CENTER, "Cancel", medium(16.0), t.header_primary);+ cancel |= nr.clicked();++ let yr = ui.interact(yes, egui::Id::new("leave_group_ok"), Sense::click()).on_hover_cursor(CursorIcon::PointingHand);+ let fill = if yr.hovered() { CRITICAL_HOVER } else { CRITICAL };+ ui.painter().rect(yes, 8.0, fill, Stroke::new(1.0_f32, rgba(255, 255, 255, 20)));+ ui.painter().text(yes.center(), Align2::CENTER_CENTER, "Leave Group", medium(16.0), Color32::WHITE);+ confirm |= yr.clicked();+ });+ if confirm {+ log::info!("group dm: leaving {} (silent={})", dialog.channel, dialog.silent);+ self.backend.send(crate::backend::Command::LeaveGroup { channel: dialog.channel, silent: dialog.silent });+ } else if !cancel {+ self.group_leave = Some(dialog);+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn leave_title_quotes_group_name() {+ assert_eq!(leave_group_title("crab rave planning"), "Leave 'crab rave planning'");+ assert_eq!(leave_group_title("Ferris, Corro"), "Leave 'Ferris, Corro'");+ }}diff --git a/apps/rustcord/src/ui/guild_menu.rs b/apps/rustcord/src/ui/guild_menu.rsindex a2f2a18..b6c6597 100644--- a/apps/rustcord/src/ui/guild_menu.rs+++ b/apps/rustcord/src/ui/guild_menu.rs@@ -132,6 +132,7 @@ impl RustCord {pub fn open_guild_menu(&mut self, guild: Id, at: Pos2) {self.server_menu = None;self.channel_menu = None;+ self.dm_menu = None;self.guild_menu = Some((guild, at, self.ctx.cumulative_pass_nr()));self.ctx.data_mut(|d| d.remove::<Vec<usize>>(sub_path_id()));}diff --git a/apps/rustcord/src/ui/mod.rs b/apps/rustcord/src/ui/mod.rsindex 00e9eba..7fa0800 100644--- a/apps/rustcord/src/ui/mod.rs+++ b/apps/rustcord/src/ui/mod.rs@@ -410,12 +410,14 @@ impl RustCord {self.update_confirm_ui(ctx);self.friend_nickname_ui(ctx);self.group_invite_ui(ctx);+ self.leave_group_ui(ctx);self.bot_modal_ui(ctx);self.threads_popout_ui(ctx);self.inbox_popout(ctx);self.context_menu_ui(ctx);self.moderation_ui(ctx);self.guild_dialog_ui(ctx);+ self.dm_menu_ui(ctx);self.app_directory_ui(ctx);self.compose_tick();self.plus_menu_ui(ctx);diff --git a/apps/rustcord/src/ui/sidebar.rs b/apps/rustcord/src/ui/sidebar.rsindex e95f89e..bea4d16 100644--- a/apps/rustcord/src/ui/sidebar.rs+++ b/apps/rustcord/src/ui/sidebar.rs@@ -442,6 +442,7 @@ impl RustCord {let mut add_server = false;// A server icon right-clicked: Discord's server context menu.let mut guild_menu_at: Option<(Id, egui::Pos2)> = None;+ let mut dm_menu_at: Option<(Id, egui::Pos2)> = None;// Dragging a server to move it: each icon's place, for the drop.let mut drops: Vec<(Rect, RailDrop)> = Vec::new();// Each folder's rows and what it looks like there (its tile, or the open@@ -515,9 +516,13 @@ impl RustCord {voice: false,},);- if rail_tip(r, &t, &name).clicked() {+ let r = rail_tip(r, &t, &name);+ if r.clicked() {open_dm = Some(id);}+ if r.secondary_clicked() {+ dm_menu_at = Some((id, r.interact_pointer_pos().unwrap_or(r.rect.right_top())));+ }}// Favourites server (your favourited channels), right under Home.if self.store.has_favorites_server() {@@ -721,6 +726,9 @@ impl RustCord {if let Some((g, at)) = guild_menu_at {self.open_guild_menu(g, at);}+ if let Some((dm, at)) = dm_menu_at {+ self.open_dm_menu(dm, at);+ }if let Some(dm) = open_dm {self.select_guild(None);self.select_channel(Some(dm));@@ -1585,6 +1593,7 @@ impl RustCord {let mut menu_at: Option<(Id, egui::Pos2)> = None;let mut hovered_dm = None;let mut close_dm: Option<(Id, bool)> = None;+ let mut leave_group: Option<Id> = None;let mut wave_dm: Option<Id> = None;let mut order = pinned.clone();order.extend(self.store.dm_list().into_iter().filter(|id| !pinned.contains(id)));@@ -1592,6 +1601,7 @@ impl RustCord {let is_pinned = pinned.contains(&id);let Some(c) = self.store.channels.get(&id) else { continue };let name = self.store.label(c);+ let is_group = c.kind == channel_kind::GROUP_DM;let recipient = (c.kind == channel_kind::DM).then(|| c.recipients.first()).flatten();// A group's own picture when it has one (else its initial).let group_icon = c.icon.as_ref().filter(|_| recipient.is_none()).map(|h| format!("https://cdn.discordapp.com/channel-icons/{}/{h}.png?size=64", id.0));@@ -1675,8 +1685,13 @@ impl RustCord {let x = ui.interact(close_slot, egui::Id::new(("dm_close", id.0)), Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);let c = if x.hovered() { t.header_primary } else { t.interactive_normal };ui.painter().text(close_slot.center(), Align2::CENTER_CENTER, icon::CLOSE, icon_font(12.0), c);- if x.clicked() {- close_dm = Some((id, selected));+ let tip = if is_group { "Leave Group" } else { "Close DM" };+ if x.on_hover_text(tip).clicked() {+ if is_group {+ leave_group = Some(id);+ } else {+ close_dm = Some((id, selected));+ }}} else if is_pinned && mentions == 0 {let c = if resp.hovered() { t.header_primary } else { t.interactive_normal };@@ -1706,105 +1721,16 @@ impl RustCord {}}if let Some((id, at)) = menu_at {- self.dm_menu = Some((id, at, ui.ctx().cumulative_pass_nr()));- }- // The DM's right-click menu.- if let Some((id, at, opened)) = self.dm_menu {- let is_pinned = self.store.pinned_dms.contains(&id);- let person = self.store.channels.get(&id).filter(|c| c.kind == channel_kind::DM).and_then(|c| c.recipients.first().cloned());- let unread = self.store.is_unread(id);- let last_msg = self.store.channels.get(&id).and_then(|c| c.last_message_id);- let nickname_row = person.as_ref().and_then(|u| self.friend_nickname_row(u.id));- let mut pick: Option<u8> = None;- let area = egui::Area::new(egui::Id::new("dm_menu")).order(egui::Order::Foreground).fixed_pos(at).constrain(true).show(ui.ctx(), |ui| {- // Discord's DM menu (2026-11-22): "Mark As Read" alone, then Pin,- // the person's rows, and the ID row with its icon on the left.- super::ctx_menu_frame(t).show(ui, |ui| {- ui.spacing_mut().item_spacing.y = 0.0;- ui.set_width(180.0);- let mut row = |ui: &mut Ui, glyph: Option<&str>, label: &str, enabled: bool, n: u8| {- if super::ctx_row(ui, t, glyph, label, None, false, enabled).clicked() {- pick = Some(n);- }- };- // Discord: "Mark As Read" while unread, else "Mark Unread" (its- // newest message becomes the first unread one).- if unread {- row(ui, None, "Mark As Read", true, 0);- } else {- row(ui, None, "Mark Unread", last_msg.is_some(), 6);- }- super::ctx_separator(ui, t);- row(ui, Some(icon::PIN), if is_pinned { "Unpin" } else { "Pin" }, true, 1);- super::ctx_separator(ui, t);- if person.is_some() {- row(ui, None, "Profile", true, 2);- }- row(ui, None, "Call", true, 3);- if let Some(label) = nickname_row {- row(ui, None, label, true, 7);- }- row(ui, None, "Close DM", true, 4);- super::ctx_separator(ui, t);- row(ui, Some(icon::ID), "Copy Channel ID", true, 5);- });- });- let now = ui.ctx().cumulative_pass_nr();- let outside = now > opened + 1 && ui.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| !area.response.rect.contains(p)));- if pick.is_some() || outside || ui.input(|i| i.key_pressed(egui::Key::Escape)) {- self.dm_menu = None;- }- match pick {- Some(0) => self.mark_channel_read(id),- Some(6) => {- if let Some(m) = last_msg {- self.mark_unread(id, m);- }- }- Some(1) => {- // Flip Discord's FAVORITED bit on this DM's override, here and on the account.- let flags = self.store.dm_flags.get(&id).copied().unwrap_or(0) ^ crate::store::DM_PINNED;- self.store.dm_flags.insert(id, flags);- if flags & crate::store::DM_PINNED != 0 {- self.store.pinned_dms.insert(id);- } else {- self.store.pinned_dms.remove(&id);- }- let patch = serde_json::json!({ "channel_overrides": { id.to_string(): { "flags": flags } } });- self.api("dm-pin", reqwest::Method::PATCH, "/users/@me/guilds/@me/settings", Some(patch));- }- Some(2) => {- if let Some(user) = person {- self.open_profile(user, None, at + vec2(196.0, 0.0));- }- }- Some(3) => self.start_dm_call(id),- Some(7) => {- if let Some(user) = &person {- self.open_friend_nickname(user.id);- }- }- Some(4) => close_dm = Some((id, self.current_channel == Some(id) && !self.friends_open)),- Some(5) => ui.ctx().copy_text(id.to_string()),- _ => {}- }- }- if let Some(Err(e)) = self.api_take("dm-pin") {- self.toast(format!("Couldn't save the pin: {e}"), true);+ self.open_dm_menu(id, at);}if let Some(id) = hovered_dm {self.hover_prefetch(id);}+ if let Some(id) = leave_group {+ self.open_leave_group(id);+ }if let Some((id, was_open)) = close_dm {- // Discord's "Close DM": the channel leaves the list (the gateway's- // CHANNEL_DELETE removes it); an open one drops back to Friends.- self.api("dm:close", reqwest::Method::DELETE, format!("/channels/{id}?silent=false"), None);- if was_open {- self.friends_open = true;- self.requests_open = false;- self.store_page = None;- self.current_channel = None;- }+ self.close_dm_channel(id, was_open);}if let Some(id) = wave_dm.filter(|_| self.dev_wave.is_some()) {self.toast("Wave (test): nothing sent.", false);@@ -1825,6 +1751,136 @@ impl RustCord {plus}+ /// Right-click on a DM or group DM (in the DM list or on the server rail).+ pub fn open_dm_menu(&mut self, channel: Id, at: egui::Pos2) {+ self.server_menu = None;+ self.guild_menu = None;+ self.channel_menu = None;+ self.dm_menu = Some((channel, at, self.ctx.cumulative_pass_nr()));+ }++ /// Close a 1:1 DM from the sidebar (stays in history).+ fn close_dm_channel(&mut self, id: Id, was_open: bool) {+ // Discord's "Close DM": the channel leaves the list (the gateway's+ // CHANNEL_DELETE removes it); an open one drops back to Friends.+ self.api("dm:close", reqwest::Method::DELETE, format!("/channels/{id}?silent=false"), None);+ if was_open {+ self.friends_open = true;+ self.requests_open = false;+ self.store_page = None;+ self.current_channel = None;+ }+ }++ /// The DM / group DM right-click menu.+ pub fn dm_menu_ui(&mut self, ctx: &egui::Context) {+ if let Some(Err(e)) = self.api_take("dm-pin") {+ self.toast(format!("Couldn't save the pin: {e}"), true);+ }+ let Some((id, at, opened)) = self.dm_menu else { return };+ let Some(chan) = self.store.channels.get(&id) else {+ self.dm_menu = None;+ return;+ };+ let t = self.theme.clone();+ let is_pinned = self.store.pinned_dms.contains(&id);+ let is_group = chan.kind == channel_kind::GROUP_DM;+ let person = (!is_group).then(|| chan.recipients.first().cloned()).flatten();+ let last_msg = chan.last_message_id;+ let can_invite = is_group && chan.recipients.len() + 1 < super::group_dm::MAX_MEMBERS;+ let unread = self.store.is_unread(id);+ let nickname_row = person.as_ref().and_then(|u| self.friend_nickname_row(u.id));+ let mut pick: Option<u8> = None;+ let area = egui::Area::new(egui::Id::new("dm_menu")).order(egui::Order::Foreground).fixed_pos(at).constrain(true).show(ctx, |ui| {+ // Discord's DM menu (2026-11-22): "Mark As Read" alone, then Pin,+ // the person's / group's rows, and the ID row with its icon on the left.+ super::ctx_menu_frame(&t).show(ui, |ui| {+ ui.spacing_mut().item_spacing.y = 0.0;+ ui.set_width(180.0);+ let mut row = |ui: &mut Ui, glyph: Option<&str>, label: &str, danger: bool, enabled: bool, n: u8| {+ if super::ctx_row(ui, &t, glyph, label, None, danger, enabled).clicked() {+ pick = Some(n);+ }+ };+ // Discord: "Mark As Read" while unread, else "Mark Unread" (its+ // newest message becomes the first unread one).+ if unread {+ row(ui, None, "Mark As Read", false, true, 0);+ } else {+ row(ui, None, "Mark Unread", false, last_msg.is_some(), 6);+ }+ super::ctx_separator(ui, &t);+ row(ui, Some(icon::PIN), if is_pinned { "Unpin" } else { "Pin" }, false, true, 1);+ super::ctx_separator(ui, &t);+ if person.is_some() {+ row(ui, None, "Profile", false, true, 2);+ }+ row(ui, None, "Call", false, true, 3);+ if let Some(label) = nickname_row {+ row(ui, None, label, false, true, 7);+ }+ if is_group {+ if can_invite {+ row(ui, None, "Invite to Group DM", false, true, 8);+ }+ super::ctx_separator(ui, &t);+ row(ui, None, "Leave Group", true, true, 4);+ } else {+ row(ui, None, "Close DM", false, true, 4);+ }+ super::ctx_separator(ui, &t);+ row(ui, Some(icon::ID), "Copy Channel ID", false, true, 5);+ });+ });+ let now = ctx.cumulative_pass_nr();+ let outside = now > opened + 1 && ctx.input(|i| i.pointer.any_pressed() && i.pointer.interact_pos().is_some_and(|p| !area.response.rect.contains(p)));+ if pick.is_some() || outside || ctx.input(|i| i.key_pressed(egui::Key::Escape)) {+ self.dm_menu = None;+ }+ match pick {+ Some(0) => self.mark_channel_read(id),+ Some(6) => {+ if let Some(m) = last_msg {+ self.mark_unread(id, m);+ }+ }+ Some(1) => {+ // Flip Discord's FAVORITED bit on this DM's override, here and on the account.+ let flags = self.store.dm_flags.get(&id).copied().unwrap_or(0) ^ crate::store::DM_PINNED;+ self.store.dm_flags.insert(id, flags);+ if flags & crate::store::DM_PINNED != 0 {+ self.store.pinned_dms.insert(id);+ } else {+ self.store.pinned_dms.remove(&id);+ }+ let patch = serde_json::json!({ "channel_overrides": { id.to_string(): { "flags": flags } } });+ self.api("dm-pin", reqwest::Method::PATCH, "/users/@me/guilds/@me/settings", Some(patch));+ }+ Some(2) => {+ if let Some(user) = person {+ self.open_profile(user, None, at + vec2(196.0, 0.0));+ }+ }+ Some(3) => self.start_dm_call(id),+ Some(7) => {+ if let Some(user) = &person {+ self.open_friend_nickname(user.id);+ }+ }+ Some(8) => self.open_group_invite(id),+ Some(4) if is_group => self.open_leave_group(id),+ Some(4) => {+ let was_open = self.current_channel == Some(id) && !self.friends_open;+ self.close_dm_channel(id, was_open);+ }+ Some(5) => {+ ctx.copy_text(id.to_string());+ self.toast("Channel ID copied.", false);+ }+ _ => {}+ }+ }+/// The account row at the bottom of the floating panel, laid out like/// Discord's (`row` is the 58 px row including the panel's border): avatar/// + name button on the left, mic / headphones split buttons and the gear ondiff --git a/crates/discord/src/http.rs b/crates/discord/src/http.rsindex c9819fc..b729b32 100644--- a/crates/discord/src/http.rs+++ b/crates/discord/src/http.rs@@ -68,6 +68,11 @@ fn log_path(path: &str) -> &str {path.split('?').next().unwrap_or(path)}+/// Endpoint path for closing a DM or leaving a group DM.+pub fn leave_group_path(channel: Id, silent: bool) -> String {+ format!("/channels/{channel}?silent={silent}")+}+/// A file on disk as a request body, read a piece at a time while it is sent.fn file_body(path: std::path::PathBuf) -> reqwest::Body {use tokio::io::AsyncReadExt;@@ -683,6 +688,12 @@ impl Http {Ok(())}+ /// Close a DM or leave a group DM (`silent`: suppress the leave system message).+ pub async fn leave_group(&self, channel: Id, silent: bool) -> Result<()> {+ self.request(Method::DELETE, &leave_group_path(channel, silent), None).await?;+ Ok(())+ }+/// Create a channel or category (`kind` 0 text, 2 voice, 4 category).pub async fn create_channel(&self, guild: Id, body: Value) -> Result<Value> {self.request(Method::POST, &format!("/guilds/{guild}/channels"), Some(body)).await@@ -1107,6 +1118,12 @@ mod tests {assert_eq!(log_path("/users/@me"), "/users/@me");}+ #[test]+ fn leave_group_path_formats_silent_flag() {+ assert_eq!(leave_group_path(Id(42), false), "/channels/42?silent=false");+ assert_eq!(leave_group_path(Id(42), true), "/channels/42?silent=true");+ }+#[test]fn api_errors_keep_their_text_and_code() {let e: anyhow::Error = ApiError { status: 400, code: Some(STRANGER_REQUEST_NEEDS_CONFIRMATION), message: "Confirm it".into() }.into();