Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
Draw only the server rail rows in view
VRonGitHuba2e4a371f2fa
diff --git a/apps/rustcord/src/store.rs b/apps/rustcord/src/store.rsindex f31d908..b70f2d7 100644--- a/apps/rustcord/src/store.rs+++ b/apps/rustcord/src/store.rs@@ -1517,9 +1517,10 @@ impl Store {}}- /// Someone is in one of `guild`'s voice channels.- pub fn guild_has_voice(&self, guild: Id) -> bool {- self.voice.iter().any(|(c, users)| !users.is_empty() && self.channels.get(c).is_some_and(|ch| ch.guild_id == Some(guild)))+ /// The servers with someone in one of their voice channels (for the+ /// rail, which asks once a frame rather than once per server).+ pub fn guilds_in_voice(&self) -> std::collections::HashSet<Id> {+ self.voice.iter().filter(|(_, users)| !users.is_empty()).filter_map(|(c, _)| self.channels.get(c)?.guild_id).collect()}/// Who is in a server's voice and stage channels, for its rail tooltip:diff --git a/apps/rustcord/src/ui/sidebar.rs b/apps/rustcord/src/ui/sidebar.rsindex bea4d16..5b44629 100644--- a/apps/rustcord/src/ui/sidebar.rs+++ b/apps/rustcord/src/ui/sidebar.rs@@ -186,10 +186,21 @@ fn rail_badge(ui: &Ui, t: &Theme, icon: Rect, count: u32) {/// Discord's mention-badge red (--status-danger in the refresh).const BADGE_RED: Color32 = Color32::from_rgb(210, 45, 57);+/// Whether a rail row is drawn: only those in view, and the five or so just+/// above and below it, so their pictures have loaded before they scroll in.+/// (A long rail has well over a hundred rows, drawn again every frame; the+/// rest still take their place and answer the pointer.)+fn rail_row_shown(ui: &Ui, rect: Rect) -> bool {+ ui.clip_rect().expand2(vec2(0.0, 240.0)).intersects(rect)+}+/// A server (or Home / Favourites) in the rail: Discord's 40 px squircle,/// flush with the row's top, 8 px to the next one.fn server_icon(ui: &mut Ui, t: &Theme, a: IconArgs) -> Response {let (rect, resp) = ui.allocate_exact_size(vec2(72.0, 48.0), Sense::click());+ if !rail_row_shown(ui, rect) {+ return resp;+ }let icon_rect = Rect::from_min_size(pos2(rect.center().x - 20.0, rect.top()), vec2(40.0, 40.0));let hot = a.selected || resp.hovered();let drawn = a.url.as_deref().is_some_and(|url| super::paint_squircle_image(ui, icon_rect, url));@@ -259,6 +270,9 @@ fn rail_button(ui: &mut Ui, t: &Theme, svg: &'static [u8], key: &str) -> Respons/// Collapsed folder: a tinted tile with up to four mini server icons.fn folder_tile(ui: &mut Ui, t: &Theme, color: Color32, icons: &[(Option<String>, String)], unread: bool, mentions: u32) -> Response {let (rect, resp) = ui.allocate_exact_size(vec2(72.0, 48.0), Sense::click_and_drag());+ if !rail_row_shown(ui, rect) {+ return resp;+ }let tile = Rect::from_min_size(pos2(rect.center().x - 20.0, rect.top()), vec2(40.0, 40.0));super::fill_squircle(ui.painter(), tile, color.gamma_multiply(0.4));let mini_px = super::cdn_size(15.0 * ui.ctx().pixels_per_point());@@ -552,26 +566,33 @@ impl RustCord {let icon_for = |store: &Store, g: Id| {store.guilds.get(&g).map(|gs| (gs.guild.icon_url(icon_px), gs.guild.name.clone())).unwrap_or((None, String::new()))};+ let in_voice = self.store.guilds_in_voice();+ // Where the next row goes. A row out of view isn't drawn (see+ // `rail_row_shown`), so its picture, name, unread dot and voice+ // mark aren't looked up; its mentions are, for the "NEW" pill.+ let next_row = |ui: &Ui| rail_row_shown(ui, Rect::from_min_size(ui.cursor().min, vec2(72.0, 48.0)));for (folder_id, name, color, ids) in &rows {// Like Discord: any saved folder (even with one server) is a folder;// loose servers are stored without an id.let is_folder = folder_id.is_some();if !is_folder {for g in ids {- let (url, gname) = icon_for(&self.store, *g);+ let shown = next_row(ui);+ let (url, gname) = if shown { icon_for(&self.store, *g) } else { (None, String::new()) };+ let mentions = self.store.guild_mentions(*g);let r = server_icon(ui,&t,IconArgs {selected: self.current_guild == Some(*g),- unread: self.store.guild_unread(*g),- mentions: self.store.guild_mentions(*g),+ unread: shown && self.store.guild_unread(*g),+ mentions,url,label: &acronym(&gname),- voice: self.store.guild_has_voice(*g),+ voice: shown && in_voice.contains(g),},);- if self.store.guild_mentions(*g) > 0 {+ if mentions > 0 {mention_rects.push(r.rect);}let r = r.interact(Sense::drag());@@ -596,8 +617,6 @@ impl RustCord {.unwrap_or(t.accent);let fname = name.clone().unwrap_or_else(|| "Server Folder".into());let expanded = self.expanded_folders.contains(&fid);- let unread = ids.iter().any(|g| self.store.guild_unread(*g));- let mentions: u32 = ids.iter().map(|g| self.store.guild_mentions(*g)).sum();if expanded {let top = ui.cursor().top();let (rect, r) = ui.allocate_exact_size(vec2(72.0, 48.0), Sense::click_and_drag());@@ -615,20 +634,22 @@ impl RustCord {toggle_folder = Some(fid);}for g in ids {- let (url, gname) = icon_for(&self.store, *g);+ let shown = next_row(ui);+ let (url, gname) = if shown { icon_for(&self.store, *g) } else { (None, String::new()) };+ let mentions = self.store.guild_mentions(*g);let r = server_icon(ui,&t,IconArgs {selected: self.current_guild == Some(*g),- unread: self.store.guild_unread(*g),- mentions: self.store.guild_mentions(*g),+ unread: shown && self.store.guild_unread(*g),+ mentions,url,label: &acronym(&gname),- voice: self.store.guild_has_voice(*g),+ voice: shown && in_voice.contains(g),},);- if self.store.guild_mentions(*g) > 0 {+ if mentions > 0 {mention_rects.push(r.rect);}let r = r.interact(Sense::drag());@@ -651,7 +672,10 @@ impl RustCord {ui.painter().set(bg_idx, egui::Shape::rect_filled(bg, 16.0, fcolor.gamma_multiply(0.18)));spans.push((Rect::from_x_y_ranges(rect.x_range(), top..=bottom), bg, fid));} else {- let icons: Vec<_> = ids.iter().map(|g| icon_for(&self.store, *g)).collect();+ let shown = next_row(ui);+ let unread = shown && ids.iter().any(|g| self.store.guild_unread(*g));+ let mentions: u32 = ids.iter().map(|g| self.store.guild_mentions(*g)).sum();+ let icons: Vec<_> = if shown { ids.iter().map(|g| icon_for(&self.store, *g)).collect() } else { Vec::new() };let r = folder_tile(ui, &t, fcolor, &icons, unread, mentions);if mentions > 0 {mention_rects.push(r.rect);