Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
Lay out popups off screen first; keep profile badges until loaded
VRonGitHubd4f80f862d83
diff --git a/apps/rustcord/src/ui/badges.rs b/apps/rustcord/src/ui/badges.rsindex 9ddc09c..bae378a 100644--- a/apps/rustcord/src/ui/badges.rs+++ b/apps/rustcord/src/ui/badges.rs@@ -208,6 +208,18 @@ fn art_uri(b: &BadgeInfo) -> Option<String> {b.big_icon_animated.as_deref().and_then(|u| crate::video::frame_uri(u, super::animating())).or_else(|| b.big_icon.clone()).or_else(|| b.small_icon())}+/// Whether the small icons of `badges` are ready to draw at `size`, starting+/// the loads of those that aren't. One that failed counts as ready: it won't+/// come, and mustn't hold the others back.+pub fn icons_loaded<'a>(ctx: &egui::Context, badges: impl Iterator<Item = &'a BadgeInfo>, size: egui::Vec2) -> bool {+ use egui::load::TexturePoll;+ // (Every icon is asked for, not just up to the first that isn't ready.)+ badges+ .filter_map(BadgeInfo::small_icon)+ .map(|icon| !matches!(egui::Image::new(icon).load_for_size(ctx, size), Ok(TexturePoll::Pending { .. })))+ .fold(true, |all, ready| all && ready)+}+/// Paint `src` inside `rect` keeping its aspect ratio (CSS object-fit: contain).fn paint_contain(ui: &Ui, src: &str, rect: Rect, tint: Color32) {let size = egui::Image::new(src.to_string()).load_for_size(ui.ctx(), rect.size()).ok().and_then(|p| p.size());diff --git a/apps/rustcord/src/ui/compose_tools.rs b/apps/rustcord/src/ui/compose_tools.rsindex 2f70e4e..2a51f09 100644--- a/apps/rustcord/src/ui/compose_tools.rs+++ b/apps/rustcord/src/ui/compose_tools.rs@@ -5,6 +5,7 @@//! picker: expression.rs.)use super::chat::UiAction;+use super::SizedOffScreen;use crate::app::RustCord;use crate::features::{CommandDraft, PickedEmoji, PickerTarget, PollDraft, ScheduleDraft, ThreadDraft};use crate::style::{icon, icon_font, overlay, Theme};@@ -493,6 +494,7 @@ impl RustCord {.order(Order::Foreground).pivot(Align2::LEFT_BOTTOM).fixed_pos(button.left_top() + vec2(0.0, -8.0))+ .sized_off_screen(ctx).show(ctx, |ui| {Frame::none().fill(t.bg_floating)diff --git a/apps/rustcord/src/ui/mod.rs b/apps/rustcord/src/ui/mod.rsindex 2591738..5ea69aa 100644--- a/apps/rustcord/src/ui/mod.rs+++ b/apps/rustcord/src/ui/mod.rs@@ -1180,6 +1180,29 @@ pub fn paint_guild_badge(p: &egui::Painter, c: egui::Pos2, badge: GuildBadge) {p.text(c, Align2::CENTER_CENTER, glyph, icon_font(10.0), fg);}+/// For floating areas pinned by their bottom edge above what opens them.+pub trait SizedOffScreen {+ /// The area's first frame laid out off screen.+ ///+ /// egui lays out an area it hasn't shown before once, invisibly, to learn+ /// its size, placing it as though it were 400 px tall. An area pinned by+ /// its bottom then lands too low, over the button that was just clicked.+ /// egui works out what the pointer is over from the last frame's widgets,+ /// so on the next frame a row of the area takes the hover from that+ /// button: a row that opens a flyout on hover opens it (the account+ /// menu's status picker), and the button loses its hover for a frame.+ fn sized_off_screen(self, ctx: &egui::Context) -> Self;+}++impl SizedOffScreen for egui::Area {+ fn sized_off_screen(self, ctx: &egui::Context) -> Self {+ if ctx.memory(|m| m.area_rect(self.layer().id).is_some()) {+ return self;+ }+ self.fixed_pos(pos2(-100_000.0, -100_000.0)).constrain(false)+ }+}+/// Discord's server-icon shape ("blob mask"): a squircle whose corners are/// three cubic curves, from its 40 × 40 SVG path, as a convex outline of `r`.pub fn squircle(r: Rect) -> Vec<egui::Pos2> {diff --git a/apps/rustcord/src/ui/profile.rs b/apps/rustcord/src/ui/profile.rsindex cc63e18..6a0bdc7 100644--- a/apps/rustcord/src/ui/profile.rs+++ b/apps/rustcord/src/ui/profile.rs@@ -7,7 +7,7 @@use super::chat::UiAction;use super::motion::{self, Motion};use super::profile_theme as theme;-use super::{paint_avatar, paint_status_dot, status_label};+use super::{paint_avatar, paint_status_dot, status_label, SizedOffScreen};use crate::app::RustCord;use crate::store::ChannelRow;use crate::style::{bold, icon, icon_font, overlay};@@ -1354,19 +1354,24 @@ impl RustCord {out.note_focus |= resp.on_hover_text(tip).on_hover_cursor(egui::CursorIcon::PointingHand).clicked();}// Server tag chip + badges chip.+ // Like Discord: six badges, then "+N" (hover lists the rest).+ const SHOWN: usize = 6;let badges: Vec<_> = profile.map(|p| p.badges.iter().chain(p.guild_badges.iter()).cloned().collect()).unwrap_or_default();+ // The badge API's list is drawn with its own icons, other pictures+ // than the profile's: the profile's stay until those have loaded,+ // so the row doesn't go blank when the list arrives.let rich_badges: Option<Vec<super::badges::BadgeInfo>> = if badges.is_empty() {None} else {- self.badges_of(user.id).map(|all| all.into_iter().filter(|b| b.owned && !b.hidden).collect())+ self.badges_of(user.id)+ .map(|all| all.into_iter().filter(|b| b.owned && !b.hidden).collect::<Vec<_>>())+ .filter(|rich| super::badges::icons_loaded(ui.ctx(), rich.iter().take(SHOWN), vec2(20.0, 20.0)))};// RustCord's own badge (from its registry), after Discord's.let rustcord = if user.bot || user.system { None } else { self.rustcord_member(user.id) };let tag = user.server_tag();// Your own full profile: a ▾ to choose which server's tag you wear.let pick_tag = is_me && !expandable;- // Like Discord: six badges, then "+N" (hover lists the rest).- const SHOWN: usize = 6;let badge_count = rich_badges.as_ref().filter(|r| !r.is_empty()).map_or(badges.len(), |r| r.len());// Discord's badge chip: --background-mod-subtle with a faint edge,// 8 px corners, 20 px badges 2 px apart (an app's have no chip).@@ -2598,6 +2603,8 @@ impl RustCord {let mut orbs_open = menu.orbs_open;// Your Orbs balance (Discord's GET /users/@me/virtual-currency/balance).let orbs: Option<u64> = self.api_results.get("orbs-balance").and_then(|r| r.as_ref().ok()).and_then(|v| v["balance"].as_u64());+ // Not answered yet (it's asked for the first time the menu opens).+ let orbs_loading = !self.api_results.contains_key("orbs-balance");let mut actions = Vec::new();let mut out = CardOut::default();@@ -2651,6 +2658,7 @@ impl RustCord {.pivot(Align2::LEFT_BOTTOM).fixed_pos(anchor - vec2(0.0, 8.0)).constrain(true)+ .sized_off_screen(ctx).show(ctx, |ui| {// (Discord's opens above its anchor: it comes down into place.)motion::tag(ui, Motion::Slide(vec2(0.0, -10.0)), 0);@@ -2712,6 +2720,19 @@ impl RustCord {} else if orbs_open {flyout_at = Some(r.rect.right_top());}+ } else if orbs_loading {+ // Its place is kept while the balance loads, with a+ // placeholder line for the amount: the rows below+ // would jump when it came.+ divider(ui);+ let r = row(ui, Some(icon::ORBS), None, "", false, subtle);+ let line = Rect::from_min_size(r.rect.left_center() + vec2(32.0, -6.0), vec2(72.0, 12.0));+ ui.painter().rect_filled(line, 6.0, overlay(0.1));+ if r.hovered() {+ status_open = false;+ switch_open = false;+ orbs_open = false;+ }}});group(ui, &mut |ui| {diff --git a/apps/rustcord/src/ui/server_tag.rs b/apps/rustcord/src/ui/server_tag.rsindex 835fb0a..ae84304 100644--- a/apps/rustcord/src/ui/server_tag.rs+++ b/apps/rustcord/src/ui/server_tag.rs@@ -6,6 +6,7 @@use super::*;use crate::style::{medium, rgba};use crate::ui::tag_badges::{self, DEFAULT_BADGES, PACKS, PRESETS};+use crate::ui::SizedOffScreen;use eframe::egui::{text::LayoutJob, Galley, Painter, Pos2};/// Discord's --brand-500 (buttons, the switch) and --brand-400 (selection).@@ -681,6 +682,7 @@ impl RustCord {.pivot(Align2::CENTER_BOTTOM).fixed_pos(custom.center_top() - vec2(0.0, 8.0)).constrain(true)+ .sized_off_screen(&ctx).show(&ctx, |ui| {Frame::none().fill(t.surface_high).stroke(Stroke::new(1.0_f32, border_strong())).rounding(8.0).inner_margin(Margin::same(12.0)).show(ui, |ui| {ui.spacing_mut().item_spacing = vec2(16.0, 8.0);diff --git a/apps/rustcord/src/ui/soundboard.rs b/apps/rustcord/src/ui/soundboard.rsindex 3f1e77a..3351f13 100644--- a/apps/rustcord/src/ui/soundboard.rs+++ b/apps/rustcord/src/ui/soundboard.rs@@ -4,6 +4,7 @@//! client downloads and plays them itself, so without this RustCord users heard//! nothing when someone used the soundboard.+use super::SizedOffScreen;use crate::app::RustCord;use crate::style::{icon, icon_font, overlay, Theme};use discord::model::Id;@@ -238,7 +239,7 @@ impl RustCord {// makes the range empty, and f32::clamp panics on that.let x = (anchor.center().x - PICKER_W / 2.0).min(screen.right() - PICKER_W - 8.0).max(screen.left() + 8.0);let pos = pos2(x, anchor.top() - 8.0);- let area = egui::Area::new(egui::Id::new("soundboard_picker")).order(Order::Foreground).pivot(Align2::LEFT_BOTTOM).fixed_pos(pos).constrain(true).show(ctx, |ui| {+ let area = egui::Area::new(egui::Id::new("soundboard_picker")).order(Order::Foreground).pivot(Align2::LEFT_BOTTOM).fixed_pos(pos).constrain(true).sized_off_screen(ctx).show(ctx, |ui| {Frame::none().fill(t.bg_floating).rounding(8.0)diff --git a/apps/rustcord/src/ui/voice_menu.rs b/apps/rustcord/src/ui/voice_menu.rsindex 90beb04..fb1e7ac 100644--- a/apps/rustcord/src/ui/voice_menu.rs+++ b/apps/rustcord/src/ui/voice_menu.rs@@ -9,6 +9,7 @@//! submenu on hover (devices, input profile); sliders apply while dragged and//! save when let go.+use super::SizedOffScreen;use crate::app::{RustCord, SettingsTab};use crate::style::{icon, icon_font, overlay, rgba, Theme};use discord::model::{permissions, Id};@@ -404,12 +405,15 @@ impl RustCord {let mut new_sub: Option<(Sub, f32)> = None;let mut clear_sub = false;let open_sub = menu.sub.map(|(s, _)| s);- let area = egui::Area::new(egui::Id::new("voice_menu"))+ // One area per kind of menu, each placed by its own size: sharing one,+ // a menu would open placed by the size of the last kind shown.+ let area = egui::Area::new(egui::Id::new(("voice_menu", std::mem::discriminant(&kind))))// Above the full-screen stream too..order(if matches!(kind, VoiceMenuKind::Stream(_)) { Order::Tooltip } else { Order::Foreground }).pivot(pivot).fixed_pos(pos).constrain(true)+ .sized_off_screen(ctx).show(ctx, |ui| {menu_frame(&t).show(ui, |ui| {// Discord's "More Options" is a little wider (its labels are long).diff --git a/apps/rustcord/src/voice_call.rs b/apps/rustcord/src/voice_call.rsindex 903254a..2a2f0ae 100644--- a/apps/rustcord/src/voice_call.rs+++ b/apps/rustcord/src/voice_call.rs@@ -7,6 +7,7 @@use crate::app::RustCord;use crate::backend::Command;use crate::style::{icon_font, rgba, Theme};+use crate::ui::SizedOffScreen;use discord::model::Id;use eframe::egui::{self, pos2, vec2, Align2, Color32, FontId, Frame, Margin, Order, Rect, RichText, Sense, Stroke, Ui};use serde_json::Value;@@ -1217,6 +1218,7 @@ impl RustCord {.order(Order::Foreground).pivot(Align2::LEFT_BOTTOM).fixed_pos(pos2(panel.left() - 6.0, panel.top() - 14.0))+ .sized_off_screen(ctx).show(ctx, |ui| {Frame::none().fill(t.bg_floating)