Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
//! Switching accounts, like Discord's: the account menu's "Switch Accounts"
//! flyout (each saved account, then "Manage Accounts") and the "Manage
//! Accounts" window (switch, log out, add an account).
use super::guild_pages::{close_button, modal_frame};
use super::paint_avatar;
use crate::accounts::Account;
use crate::app::RustCord;
use crate::style::{icon, overlay, Theme};
use discord::model::Id;
use eframe::egui::{self, pos2, vec2, Align2, Color32, FontFamily, FontId, Frame, Margin, Order, Pos2, Rect, Sense, Stroke, Ui};
/// Discord's --brand-500 (the active account's check).
const BRAND: Color32 = Color32::from_rgb(88, 101, 242);
/// --text-feedback-positive ("Active account").
const POSITIVE_TEXT: Color32 = Color32::from_rgb(94, 180, 121);
/// Width of the "Manage Accounts" window and of the flyout.
const WINDOW_W: f32 = 480.0;
pub const FLYOUT_W: f32 = 220.0;
/// The "Manage Accounts" window.
#[derive(Default)]
pub struct ManageAccounts {
/// An account's "…" menu, open under its button.
menu: Option<(Id, Pos2)>,
}
/// What was picked in the flyout.
pub enum FlyoutPick {
Switch(Id),
Manage,
}
fn semibold(size: f32) -> FontId {
FontId::new(size, FontFamily::Name(crate::style::BOLD.into()))
}
/// Discord's "active" check: a white tick in a brand circle, 18 px.
fn paint_active_check(painter: &egui::Painter, center: Pos2) {
painter.circle_filled(center, 8.25, BRAND);
// The middle of the 24 px icon's tick (2 px thick), scaled to 18 px.
let points = [vec2(-5.0, 1.0), vec2(-2.35, 3.65), vec2(5.0, -3.0)].map(|v| center + v * 0.75);
painter.add(egui::Shape::line(points.to_vec(), Stroke::new(1.5_f32, Color32::WHITE)));
}
/// The accounts to list: the saved ones, or (the demo, which saves none) the
/// running one alone.
pub fn listed_accounts(app: &RustCord) -> Vec<Account> {
if !app.accounts.list.is_empty() {
return app.accounts.list.clone();
}
app.store.me.as_ref().map(|me| Account { id: me.id, username: me.username.clone(), global_name: me.global_name.clone(), avatar: me.avatar.clone() }).into_iter().collect()
}
/// The flyout's rows (inside its 8 px padding): each account (24 px avatar,
/// username, the check on the active one), a separator, "Manage Accounts".
pub fn flyout_rows(ui: &mut Ui, t: &Theme, accounts: &[Account], active: Option<Id>) -> Option<FlyoutPick> {
let mut pick = None;
let width = FLYOUT_W - 2.0 - 16.0;
for a in accounts {
let (r, resp) = ui.allocate_exact_size(vec2(width, 40.0), Sense::click());
if resp.hovered() {
ui.painter().rect_filled(r, 4.0, overlay(0.16));
}
let av = Rect::from_min_size(pos2(r.left() + 8.0, r.center().y - 12.0), vec2(24.0, 24.0));
let user = a.user();
paint_avatar(ui, av, Some(user.avatar_or_default(64)), user.display_name());
let name = ui.painter().text(pos2(av.right() + 8.0, r.center().y), Align2::LEFT_CENTER, &a.username, FontId::proportional(14.0), t.header_primary);
if active == Some(a.id) {
paint_active_check(ui.painter(), pos2(name.right() + 8.0 + 9.0, r.center().y));
} else if resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
pick = Some(FlyoutPick::Switch(a.id));
}
}
ui.add_space(8.0);
let y = ui.cursor().top() + 0.5;
ui.painter().hline(egui::Rangef::new(ui.cursor().left(), ui.cursor().left() + width), y, Stroke::new(1.0_f32, t.border_subtle));
ui.add_space(9.0);
let (r, resp) = ui.allocate_exact_size(vec2(width, 34.0), Sense::click());
if resp.hovered() {
ui.painter().rect_filled(r, 4.0, overlay(0.16));
}
ui.painter().text(pos2(r.left() + 8.0, r.center().y), Align2::LEFT_CENTER, "Manage Accounts", crate::style::medium(14.0), t.header_primary);
if resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
pick = Some(FlyoutPick::Manage);
}
pick
}
enum Action {
Switch(Id),
OpenMenu(Id, Pos2),
LogOut(Id),
Add,
Close,
}
impl RustCord {
pub fn open_manage_accounts(&mut self) {
self.account_menu = None;
self.manage_accounts = Some(ManageAccounts::default());
}
pub fn manage_accounts_ui(&mut self, ctx: &egui::Context) {
let Some(state) = &self.manage_accounts else { return };
let menu = state.menu;
let t = self.theme.clone();
let accounts = listed_accounts(self);
let active = self.account.or(self.store.me_id());
let mut action = None;
let dim_clicked = modal_frame(ctx, &t, "manage_accounts", WINDOW_W, |ui| {
// Header: 16 / 24 / 8, title 20/24 semibold, subtitle 16/20.
let top = ui.cursor().top();
Frame::none().inner_margin(Margin { left: 24.0, right: 24.0, top: 16.0, bottom: 8.0 }).show(ui, |ui| {
ui.set_width(WINDOW_W - 48.0);
let (r, _) = ui.allocate_exact_size(vec2(WINDOW_W - 48.0, 52.0), Sense::hover());
ui.painter().text(pos2(r.left(), r.top() + 12.0), Align2::LEFT_CENTER, "Manage Accounts", semibold(20.0), t.header_primary);
ui.painter().text(pos2(r.left(), r.top() + 42.0), Align2::LEFT_CENTER, "Switch accounts, sign in, sign out, go wild.", FontId::proportional(16.0), t.header_secondary);
});
let right = ui.max_rect().right();
if close_button(ui, &t, pos2(right - 28.0, top + 29.0), "manage_accounts") {
action = Some(Action::Close);
}
// The accounts, in one rounded list.
ui.add_space(16.0);
Frame::none().inner_margin(Margin { left: 24.0, right: 24.0, top: 0.0, bottom: 0.0 }).show(ui, |ui| {
Frame::none().fill(overlay(0.12)).rounding(8.0).show(ui, |ui| {
ui.set_width(WINDOW_W - 48.0);
ui.spacing_mut().item_spacing.y = 0.0;
for (i, a) in accounts.iter().enumerate() {
if i > 0 {
let x = ui.max_rect().x_range();
ui.painter().hline(egui::Rangef::new(x.min + 16.0, x.max - 16.0), ui.cursor().top(), Stroke::new(1.0_f32, t.border_subtle));
}
let (r, resp) = ui.allocate_exact_size(vec2(WINDOW_W - 48.0, 64.0), Sense::click());
let is_active = active == Some(a.id);
let more = Rect::from_center_size(pos2(r.right() - 36.0, r.center().y), vec2(40.0, 40.0));
let more_resp = ui.interact(more, egui::Id::new(("manage_accounts_more", a.id)), Sense::click());
if !is_active && resp.hovered() && !more_resp.hovered() {
ui.painter().rect_filled(r, 8.0, overlay(0.06));
}
let av = Rect::from_min_size(pos2(r.left() + 16.0, r.center().y - 20.0), vec2(40.0, 40.0));
let user = a.user();
paint_avatar(ui, av, Some(user.avatar_or_default(80)), user.display_name());
let x = av.right() + 8.0;
if is_active {
ui.painter().text(pos2(x, r.center().y - 9.0), Align2::LEFT_CENTER, &a.username, semibold(16.0), t.header_primary);
ui.painter().text(pos2(x, r.center().y + 10.0), Align2::LEFT_CENTER, "Active account", semibold(14.0), POSITIVE_TEXT);
} else {
ui.painter().text(pos2(x, r.center().y), Align2::LEFT_CENTER, &a.username, semibold(16.0), t.header_primary);
}
if more_resp.hovered() || menu.is_some_and(|(id, _)| id == a.id) {
ui.painter().rect_filled(more, 8.0, overlay(0.12));
}
let tint = if more_resp.hovered() { t.interactive_hover } else { t.interactive_normal };
super::paint_icon(ui.painter(), Rect::from_center_size(more.center(), vec2(20.0, 20.0)), icon::MORE, tint);
if more_resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
action = Some(Action::OpenMenu(a.id, more.left_bottom() + vec2(20.0, 4.0)));
} else if !is_active && resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
action = Some(Action::Switch(a.id));
}
}
});
});
// "Add an account": a secondary button the width of the list.
ui.add_space(8.0);
Frame::none().inner_margin(Margin { left: 24.0, right: 24.0, top: 16.0, bottom: 16.0 }).show(ui, |ui| {
let (r, resp) = ui.allocate_exact_size(vec2(WINDOW_W - 48.0, 40.0), Sense::click());
ui.painter().rect_filled(r, 8.0, overlay(if resp.hovered() { 0.2 } else { 0.12 }));
ui.painter().rect_stroke(r.shrink(0.5), 8.0, Stroke::new(1.0_f32, overlay(0.04)));
ui.painter().text(r.center(), Align2::CENTER_CENTER, "Add an account", crate::style::medium(16.0), t.header_primary);
if resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
action = Some(Action::Add);
}
});
});
// An account's menu: "Log out".
let mut menu_rect = None;
if let Some((id, at)) = menu {
let area = egui::Area::new(egui::Id::new("manage_accounts_menu")).order(Order::Tooltip).pivot(Align2::RIGHT_TOP).fixed_pos(at).constrain(true).show(ctx, |ui| {
Frame::none()
.fill(t.bg_floating)
.rounding(8.0)
.stroke(Stroke::new(1.0_f32, t.border_subtle))
.inner_margin(8.0)
.show(ui, |ui| {
let (r, resp) = ui.allocate_exact_size(vec2(126.0, 34.0), Sense::click());
let hovered = resp.hovered();
if hovered {
ui.painter().rect_filled(r, 4.0, t.danger);
}
ui.painter().text(pos2(r.left() + 8.0, r.center().y), Align2::LEFT_CENTER, "Log out", crate::style::medium(14.0), if hovered { Color32::WHITE } else { t.danger });
if resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
action = Some(Action::LogOut(id));
}
});
});
menu_rect = Some(area.response.rect);
}
let clicked_off_menu = menu.is_some() && ctx.input(|i| i.pointer.any_click() && i.pointer.interact_pos().is_some_and(|p| !menu_rect.is_some_and(|m| m.contains(p))));
match action {
Some(Action::Close) => self.manage_accounts = None,
Some(Action::Switch(id)) => {
self.manage_accounts = None;
self.switch_account(id);
}
Some(Action::OpenMenu(id, at)) => {
if let Some(s) = self.manage_accounts.as_mut() {
s.menu = if menu.is_some_and(|(m, _)| m == id) { None } else { Some((id, at)) };
}
}
Some(Action::LogOut(id)) => {
if active == Some(id) {
// The next saved account takes over (or the login page shows).
self.manage_accounts = None;
self.logout();
} else {
self.logout_other(id);
if let Some(s) = self.manage_accounts.as_mut() {
s.menu = None;
}
}
}
Some(Action::Add) => self.open_add_account(),
None if ctx.input(|i| i.key_pressed(egui::Key::Escape)) => match self.manage_accounts.as_mut() {
Some(s) if s.menu.is_some() => s.menu = None,
_ => self.manage_accounts = None,
},
None if clicked_off_menu => {
if let Some(s) = self.manage_accounts.as_mut() {
s.menu = None;
}
}
None if dim_clicked => self.manage_accounts = None,
None => {}
}
}
}