Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

Minimize to the tray by default, like Discord, from any way of closing

VRonGitHubd9c129d4a848
diff --git a/README.md b/README.md
index 0dbec5d..8cf3be9 100644
--- a/README.md
+++ b/README.md
@@ -55,9 +55,9 @@ administrator rights needed.
`sudo dnf install webkit2gtk4.1` on Fedora, `sudo pacman -S webkit2gtk-4.1` on Arch.
- Chat, voice calls and everything else work; Go Live screen sharing and the camera are
Windows-only for now. On a Wayland desktop RustCord runs through XWayland.
-- RustCord has a notification-area icon, and clicking X can leave it running there instead of
- quitting (System → General → Minimize RustCord to System Tray). KDE, XFCE and Cinnamon show
- the icon out of the box; on GNOME, install the AppIndicator extension first.
+- RustCord has a notification-area icon, and closing the window leaves it running there, as
+ Discord does (turn that off in System → General → Minimize RustCord to System Tray). KDE, XFCE
+ and Cinnamon show the icon out of the box; on GNOME, install the AppIndicator extension first.
- New on Linux, so please [report problems](https://github.com/VRonGitHub/RustCord/issues).
Check your download with `sha256sum -c RustCord-<version>-linux-x86_64.tar.gz.sha256`.
@@ -226,8 +226,8 @@ GitHub uses for the releases, and starts the app on a Mac to check it.
- DMs and group DMs, unread state and mention badges synced with Discord
- Friends page (online, all, pending, blocked), quick switcher, invites, muting and notification levels
- Remembers where you were in every server and chat, also after a restart
-- Desktop notifications, and a tray icon that clicking X can leave RustCord running in
- (System → General → Minimize RustCord to System Tray)
+- Desktop notifications, and a tray icon: closing the window leaves RustCord running there, as in
+ Discord (System → General → Minimize RustCord to System Tray)
- Opens on the monitor, at the place and size it was closed at (Windows)
</details>
diff --git a/apps/rustcord/src/app.rs b/apps/rustcord/src/app.rs
index 86714e6..ae1a429 100644
--- a/apps/rustcord/src/app.rs
+++ b/apps/rustcord/src/app.rs
@@ -455,6 +455,12 @@ pub struct RustCord {
tray_usable: bool,
/// The window is hidden in the notification area.
window_hidden: bool,
+ /// The window's size while it was last shown (see `raw_input_hook`).
+ laid_out_at: Option<egui::Rect>,
+ /// RustCord is quitting for real (the tray's Quit, a restart, another
+ /// RustCord asking it to): the window's close isn't turned into a trip
+ /// to the tray.
+ quitting: bool,
/// Where the window was last closed (`window.json`), and later where it
/// was when it went to the tray; written again on exit.
window_file: Option<std::path::PathBuf>,
@@ -770,6 +776,8 @@ impl RustCord {
tray,
tray_usable,
window_hidden: false,
+ quitting: false,
+ laid_out_at: None,
window_file,
window_place,
updater: crate::updater::Updater::start(&cc.egui_ctx),
@@ -946,10 +954,29 @@ impl RustCord {
cfg!(any(windows, target_os = "linux")) && window_known && self.tray.is_some() && self.tray_usable
}
- /// "Minimize RustCord to System Tray": the title bar's X hides the window
- /// (only that X: a close from outside, like the updater's or a second
- /// launch's, still quits, so settings get saved). On Windows it leaves
- /// the taskbar and minimizes instead of hiding (see `tray::to_tray`).
+ /// "Minimize RustCord to System Tray", as Discord does it: closing the
+ /// window any way (the title bar's X, Alt+F4, the taskbar's "Close
+ /// window") hides it instead. Only the tray's Quit, a restart or another
+ /// RustCord asking (a second launch, the installer: see
+ /// `tray::ask_others_to_quit`) really quits.
+ fn close_to_tray(&mut self, ctx: &egui::Context) {
+ let asked = ctx.input(|i| i.viewport().close_requested());
+ if asked && !self.quitting && self.prefs.minimize_to_tray && self.tray_available() {
+ ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
+ if !self.window_hidden {
+ self.hide_to_tray(ctx);
+ }
+ }
+ }
+
+ /// Close the window for good.
+ pub fn quit(&mut self, ctx: &egui::Context) {
+ self.quitting = true;
+ ctx.send_viewport_cmd(egui::ViewportCommand::Close);
+ }
+
+ /// Off to the notification area. On Windows the window leaves the
+ /// taskbar and minimizes instead of hiding (see `tray::to_tray`).
pub fn hide_to_tray(&mut self, ctx: &egui::Context) {
if cfg!(windows) {
// Its bounds now: once it's a tool window they read differently.
@@ -1015,7 +1042,7 @@ impl RustCord {
self.select_channel(Some(channel));
}
}
- TrayAction::Quit => ctx.send_viewport_cmd(egui::ViewportCommand::Close),
+ TrayAction::Quit => self.quit(ctx),
TrayAction::Restart => self.restart(ctx),
// Linux: the desktop's notification-area host went away or
// came back. Never leave the window hidden without an icon.
@@ -1069,7 +1096,7 @@ impl RustCord {
Err(e) => log::warn!("tray: couldn't restart: {e}"),
}
}
- ctx.send_viewport_cmd(egui::ViewportCommand::Close);
+ self.quit(ctx);
}
/// Ctrl + R, like Discord: reload the client where it stands. The session
@@ -3904,6 +3931,16 @@ impl RustCord {
impl eframe::App for RustCord {
fn raw_input_hook(&mut self, _ctx: &egui::Context, raw: &mut egui::RawInput) {
+ // Minimized or in the tray, the window is a 160 x 28 sliver: lay out
+ // at the size it had instead, so nothing gets squeezed (or dropped
+ // and loaded again when it comes back).
+ if self.window_hidden || raw.viewport().minimized == Some(true) {
+ if self.laid_out_at.is_some() {
+ raw.screen_rect = self.laid_out_at;
+ }
+ } else if raw.screen_rect.is_some_and(|r| r.width() >= 1.0 && r.height() >= 1.0) {
+ self.laid_out_at = raw.screen_rect;
+ }
self.sync_window_focus(raw);
// A test window driven by dev input sits in the background; it takes
// typing as if it were in front (egui drops the text caret otherwise).
@@ -3954,6 +3991,7 @@ impl eframe::App for RustCord {
crate::linux::pump(ctx, web_page_open);
}
self.tray_actions(ctx);
+ self.close_to_tray(ctx);
self.handle_shortcuts(ctx);
self.pump_events();
if let (Some(action), Some(ch)) = (DEV_ACTION.lock().ok().and_then(|mut a| a.take()), self.current_channel) {
diff --git a/apps/rustcord/src/diagnostics.rs b/apps/rustcord/src/diagnostics.rs
index a8ab748..c61619f 100644
--- a/apps/rustcord/src/diagnostics.rs
+++ b/apps/rustcord/src/diagnostics.rs
@@ -185,8 +185,8 @@ pub fn close_other_instances() {
if std::env::var_os("RUSTCORD_KEEP_OTHERS").is_some() {
return;
}
- // Through their tray icon first: the one polite close that reaches a
- // RustCord whose window is in the tray (see `tray::ask_others_to_quit`).
+ // Through their tray icon first: the one polite close that makes a
+ // RustCord quit rather than go to the tray (see `tray::ask_others_to_quit`).
let quitting = crate::tray::ask_others_to_quit();
if quitting > 0 {
log::info!("asked {quitting} older RustCord window(s) to close");
diff --git a/apps/rustcord/src/features.rs b/apps/rustcord/src/features.rs
index e548e04..12cd725 100644
--- a/apps/rustcord/src/features.rs
+++ b/apps/rustcord/src/features.rs
@@ -394,8 +394,11 @@ pub struct LocalPrefs {
pub share_rustcord: bool,
/// The emoji picker's skin tone: 0 none, 1 light … 5 dark.
pub skin_tone: u8,
- /// Closing the window keeps RustCord running in the notification area.
- pub close_to_tray: bool,
+ /// Closing the window keeps RustCord running in the notification area:
+ /// Discord's "Minimize to Tray", on out of the box as in Discord. (0.4.40
+ /// had it off as `close_to_tray`; a new name, so the default reaches
+ /// everyone that file already says "off" for.)
+ pub minimize_to_tray: bool,
// ---- Notifications ----
/// Windows notifications for DMs and mentions while you're elsewhere.
pub desktop_notifications: bool,
@@ -520,7 +523,7 @@ impl Default for LocalPrefs {
// Off until switched on: it contacts RustCord's own server.
share_rustcord: false,
skin_tone: 0,
- close_to_tray: false,
+ minimize_to_tray: true,
desktop_notifications: true,
unread_badge: true,
sound_message: true,
diff --git a/apps/rustcord/src/tray.rs b/apps/rustcord/src/tray.rs
index ecc5322..b3cc4a4 100644
--- a/apps/rustcord/src/tray.rs
+++ b/apps/rustcord/src/tray.rs
@@ -240,9 +240,10 @@ mod win {
/// What another RustCord program (a second launch, the installer) sends
/// this window to have RustCord quit as the tray menu's Quit does, with
/// its settings saved; the answer 1 means it is quitting (versions before
- /// 0.4.41 answer 0). taskkill's polite close can't do that while the
- /// window is in the tray: it doesn't reach a window that's off the
- /// taskbar. (The setup sends it too: apps/setup/src/lib.rs.)
+ /// 0.4.41 answer 0). taskkill's polite close can't do that: it only
+ /// sends the window to the tray ("Minimize RustCord to System Tray"),
+ /// and once it's there it doesn't reach it at all. (The setup sends it
+ /// too: apps/setup/src/lib.rs.)
const QUIT_MESSAGE: &str = "RustCordQuit";
const CLASS: &str = "RustCordTrayWindow";
static QUIT: AtomicIsize = AtomicIsize::new(0);
@@ -516,11 +517,24 @@ mod win {
pub fn to_tray(hwnd: isize) {
unsafe {
let h = hwnd as HWND;
+ let maximized = IsZoomed(h) != 0;
let ex = GetWindowLongW(h, GWL_EXSTYLE);
EX_STYLE.store(ex as isize, Ordering::Relaxed);
ShowWindow(h, SW_HIDE);
SetWindowLongW(h, GWL_EXSTYLE, (ex | WS_EX_TOOLWINDOW as i32) & !(WS_EX_APPWINDOW as i32));
- ShowWindow(h, SW_SHOWMINNOACTIVE);
+ // Minimized off the screen, where Windows keeps minimized windows
+ // that are on the taskbar: one that isn't would otherwise sit as
+ // a small bar above the taskbar.
+ let mut wp: WINDOWPLACEMENT = std::mem::zeroed();
+ wp.length = std::mem::size_of::<WINDOWPLACEMENT>() as u32;
+ if GetWindowPlacement(h, &mut wp) == 0 {
+ ShowWindow(h, SW_SHOWMINNOACTIVE);
+ return;
+ }
+ wp.showCmd = SW_SHOWMINNOACTIVE as u32;
+ wp.flags = WPF_SETMINPOSITION | if maximized { WPF_RESTORETOMAXIMIZED } else { 0 };
+ wp.ptMinPosition = POINT { x: -32000, y: -32000 };
+ SetWindowPlacement(h, &wp);
}
}
diff --git a/apps/rustcord/src/ui/mod.rs b/apps/rustcord/src/ui/mod.rs
index ad25494..128a6e4 100644
--- a/apps/rustcord/src/ui/mod.rs
+++ b/apps/rustcord/src/ui/mod.rs
@@ -132,7 +132,6 @@ impl RustCord {
paint_caption_glyph(ui.painter(), ui.painter().round_pos_to_pixels(r.center()), cmd, maximized, c);
if b.on_hover_text(tip).clicked() {
match cmd {
- 0 if self.prefs.close_to_tray && self.tray_available() => self.hide_to_tray(ctx),
0 => ctx.send_viewport_cmd(ViewportCommand::Close),
1 => ctx.send_viewport_cmd(ViewportCommand::Maximized(!maximized)),
_ => ctx.send_viewport_cmd(ViewportCommand::Minimized(true)),
diff --git a/apps/rustcord/src/ui/settings.rs b/apps/rustcord/src/ui/settings.rs
index 038c9c3..c79d357 100644
--- a/apps/rustcord/src/ui/settings.rs
+++ b/apps/rustcord/src/ui/settings.rs
@@ -2752,8 +2752,8 @@ impl RustCord {
if general == 1 {
section(ui, t, 0, "General");
if self.tray_available() {
- if let Some(v) = toggle_row(ui, t, "Minimize RustCord to System Tray", "Clicking X minimizes RustCord instead of closing it completely.", self.prefs.close_to_tray) {
- self.prefs.close_to_tray = v;
+ if let Some(v) = toggle_row(ui, t, "Minimize RustCord to System Tray", "Clicking X minimizes RustCord instead of closing it completely.", self.prefs.minimize_to_tray) {
+ self.prefs.minimize_to_tray = v;
self.save_prefs();
}
} else {
diff --git a/apps/setup/src/lib.rs b/apps/setup/src/lib.rs
index 7746c6a..4740841 100644
--- a/apps/setup/src/lib.rs
+++ b/apps/setup/src/lib.rs
@@ -310,8 +310,9 @@ pub fn running_from(dir: &Path) -> Vec<u32> {
/// Ask the RustCords with these process ids to quit as their tray menu's
/// Quit does, saving their settings: a message to their tray window (see
/// `QUIT_MESSAGE` in apps/rustcord/src/tray.rs). taskkill's polite close
-/// can't do that while RustCord's window is in the tray. Whether any is
-/// quitting (versions before 0.4.41 don't know the message).
+/// can't do that: it only sends RustCord to the tray, and doesn't reach it
+/// there. Whether any is quitting (versions before 0.4.41 don't know the
+/// message).
fn ask_to_quit(pids: &[u32]) -> bool {
let mut quitting = false;
// SAFETY: plain window lookups and a message with no pointers in it.