Skip to content

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

Settings buttons drawn like Discord's (#46)

youtsuhoc757714ce224
diff --git a/apps/rustcord/src/style.rs b/apps/rustcord/src/style.rs
index 4729385..b4552f5 100644
--- a/apps/rustcord/src/style.rs
+++ b/apps/rustcord/src/style.rs
@@ -46,6 +46,22 @@ pub struct Theme {
pub light: bool,
/// Code-block highlighting colours (Discord's --text-code-*).
pub syntax: Syntax,
+ /// Button colours that differ by theme (Discord's --control-*).
+ pub controls: Controls,
+}
+
+/// The theme's share of Discord's button tokens (--control-secondary-* and
+/// --control-critical-secondary-*); the solid brand and red buttons are the
+/// same in every theme.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Controls {
+ /// The grey fill while hovered and while pressed (it's `overlay(0.12)` at rest).
+ pub hover: Color32,
+ pub active: Color32,
+ /// The grey buttons' hairline edge.
+ pub border: Color32,
+ /// The red label of a grey "critical" button (Disable Account).
+ pub critical_text: Color32,
}
/// Discord's code-block colours (its highlight.js theme, --text-code-*).
@@ -324,6 +340,8 @@ struct Tokens {
mod_strong: f32,
notice_bg: u32,
notice_text: u32,
+ control_border: f32,
+ critical_text: u32,
}
fn tokens(name: &str) -> Tokens {
@@ -333,21 +351,25 @@ fn tokens(name: &str) -> Tokens {
lowest: 0xf3f3f4, lower: 0xfbfbfb, low: 0xfbfbfb, surface_high: 0xffffff, surface_higher: 0xffffff,
chat_input: 0xf7f7f8, text_default: 0x2e2e34, text_muted: 0x6d6f78, text_strong: 0x28282d,
channels: 0x666872, interactive: 0x595a63, link: 0x006dd4, mod_normal: 0.16, mod_strong: 0.24, notice_bg: 0xc8ffd8, notice_text: 0x00461f,
+ control_border: 0.2, critical_text: 0xb92733,
},
"ash" => Tokens {
lowest: 0x2c2d32, lower: 0x323339, low: 0x36373e, surface_high: 0x393a41, surface_higher: 0x3c3d45,
chat_input: 0x393a41, text_default: 0xf3f3f4, text_muted: 0xabacb2, text_strong: 0xffffff,
channels: 0x999aa1, interactive: 0xc5c6ca, link: 0x76aff6, mod_normal: 0.16, mod_strong: 0.2, notice_bg: 0x00431e, notice_text: 0xc1fcd1,
+ control_border: 0.04, critical_text: 0xffa09b,
},
"onyx" => Tokens {
lowest: 0x000000, lower: 0x000000, low: 0x000000, surface_high: 0x0a0a0c, surface_higher: 0x121214,
chat_input: 0x101013, text_default: 0xd4d5d8, text_muted: 0x81828a, text_strong: 0xdcdcdf,
channels: 0x7a7b83, interactive: 0x96979e, link: 0x2781e7, mod_normal: 0.2, mod_strong: 0.24, notice_bg: 0x001002, notice_text: 0x7ecb94,
+ control_border: 0.16, critical_text: 0xeb5f5e,
},
_ => Tokens {
lowest: 0x121214, lower: 0x1a1a1e, low: 0x202024, surface_high: 0x242429, surface_higher: 0x28282d,
chat_input: 0x222327, text_default: 0xefeff1, text_muted: 0x96979e, text_strong: 0xfbfbfb,
channels: 0x81828a, interactive: 0xabacb2, link: 0x4d96ee, mod_normal: 0.16, mod_strong: 0.2, notice_bg: 0x002b0e, notice_text: 0x99e0ad,
+ control_border: 0.04, critical_text: 0xf87e7a,
},
}
}
@@ -392,6 +414,7 @@ impl Theme {
overlays: Overlays::default(),
light: name == "light",
syntax: Syntax::for_theme(name),
+ controls: Controls { hover: modc(k.mod_normal), active: modc(k.mod_strong), border: modc(k.control_border), critical_text: hex(k.critical_text) },
}
}
diff --git a/apps/rustcord/src/ui/settings.rs b/apps/rustcord/src/ui/settings.rs
index 4aa42d4..2eff9fb 100644
--- a/apps/rustcord/src/ui/settings.rs
+++ b/apps/rustcord/src/ui/settings.rs
@@ -328,19 +328,21 @@ fn soon(ui: &mut Ui, t: &Theme, title: &str, desc: &str) {
locked_row(ui, t, title, desc, Locked::Soon);
}
-/// Title and description with a button on the right. True when clicked.
-fn button_row(ui: &mut Ui, t: &Theme, title: &str, desc: &str, label: &str, danger: bool) -> bool {
+/// Title and description with a button on the right, centred on the text
+/// beside it as Discord's are. True when clicked.
+fn button_row(ui: &mut Ui, t: &Theme, title: &str, desc: &str, label: &str, kind: ButtonKind) -> bool {
+ let size = button_size(ui, label);
let mut clicked = false;
ui.horizontal_top(|ui| {
- let w = ui.available_width() - 180.0;
- row_text(ui, t, title, desc, w, false);
- ui.with_layout(Layout::right_to_left(Align::Min), |ui| {
- clicked = if danger {
- ui.add(Button::new(RichText::new(label).size(14.0).color(Color32::WHITE)).fill(t.danger).rounding(8.0).min_size(vec2(0.0, 36.0))).clicked()
- } else {
- secondary_button(ui, t, label).clicked()
- };
- });
+ let right = ui.max_rect().right();
+ // The text keeps clear of the button, with a 16 px gap.
+ let w = (ui.available_width() - size.x - 16.0).max(120.0);
+ let text = ui.scope(|ui| row_text(ui, t, title, desc, w, false)).response.rect;
+ let top = (text.center().y - size.y / 2.0).max(text.top());
+ let rect = Rect::from_min_size(pos2(right - size.x, top).round(), size);
+ let resp = ui.allocate_rect(rect, Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);
+ paint_button(ui, t, &resp, label, kind);
+ clicked = resp.clicked();
});
ui.add_space(16.0);
clicked
@@ -460,12 +462,74 @@ fn keycap(ui: &mut Ui, t: &Theme, key: &str) {
ui.painter().galley(rect.center() - g.size() / 2.0 - vec2(0.0, 1.0), g, t.header_primary);
}
+/// Discord's button looks (its --control-* tokens).
+#[derive(Clone, Copy, PartialEq)]
+pub(super) enum ButtonKind {
+ /// Blurple, white label.
+ Primary,
+ /// The translucent grey fill with a faint edge.
+ Secondary,
+ /// Solid red, white label ("critical-primary": Delete Account).
+ Critical,
+ /// The grey fill with a red label ("critical-secondary": Disable Account).
+ CriticalSecondary,
+}
+
+/// Fills of Discord's solid buttons, the same in every theme: at rest,
+/// hovered, pressed.
+const BRAND_FILLS: [Color32; 3] = [BRAND, Color32::from_rgb(68, 82, 187), Color32::from_rgb(58, 72, 163)];
+const CRITICAL_FILLS: [Color32; 3] = [Color32::from_rgb(210, 45, 57), Color32::from_rgb(169, 35, 46), Color32::from_rgb(151, 29, 40)];
+
+/// Height of Discord's settings buttons, and their side padding.
+const BUTTON_H: f32 = 36.0;
+const BUTTON_PAD: f32 = 16.0;
+
+/// The size `label` needs as a button.
+fn button_size(ui: &Ui, label: &str) -> egui::Vec2 {
+ let w = ui.painter().layout_no_wrap(label.to_string(), crate::style::medium(14.0), Color32::WHITE).size().x;
+ vec2((w + 2.0 * BUTTON_PAD).max(60.0), BUTTON_H)
+}
+
+/// Paint a button over `resp`'s rect, label centred: 8 px corners, 14 px
+/// medium label, Discord's own colours at rest, hovered and pressed.
+fn paint_button(ui: &Ui, t: &Theme, resp: &egui::Response, label: &str, kind: ButtonKind) {
+ let state = if resp.is_pointer_button_down_on() {
+ 2
+ } else if resp.hovered() {
+ 1
+ } else {
+ 0
+ };
+ let c = &t.controls;
+ let grey = [overlay(0.12), c.hover, c.active][state];
+ let (fill, edge, text) = match kind {
+ ButtonKind::Primary => (BRAND_FILLS[state], Stroke::NONE, Color32::WHITE),
+ ButtonKind::Critical => (CRITICAL_FILLS[state], Stroke::new(1.0_f32, crate::style::rgba(255, 255, 255, 20)), Color32::WHITE),
+ ButtonKind::Secondary => (grey, Stroke::new(1.0_f32, c.border), t.header_primary),
+ ButtonKind::CriticalSecondary => (grey, Stroke::new(1.0_f32, c.border), c.critical_text),
+ };
+ let p = ui.painter();
+ p.rect(resp.rect, 8.0, fill, edge);
+ let g = p.layout_no_wrap(label.to_string(), crate::style::medium(14.0), text);
+ // (Whole pixels, a hair up: the font's line box sits low, see `keycap`.)
+ let at = (resp.rect.center() - g.size() / 2.0 - vec2(0.0, 0.5)).round();
+ p.galley(at, g, text);
+}
+
+/// A button as wide as its label, where the layout puts it.
+pub(super) fn action_button(ui: &mut Ui, t: &Theme, label: &str, kind: ButtonKind) -> egui::Response {
+ let (_, resp) = ui.allocate_exact_size(button_size(ui, label), Sense::click());
+ let resp = resp.on_hover_cursor(egui::CursorIcon::PointingHand);
+ paint_button(ui, t, &resp, label, kind);
+ resp
+}
+
pub(super) fn secondary_button(ui: &mut Ui, t: &Theme, label: &str) -> egui::Response {
- ui.add(Button::new(RichText::new(label).font(crate::style::medium(14.0)).color(t.header_primary)).fill(overlay(0.12)).rounding(8.0).min_size(vec2(60.0, 36.0)))
+ action_button(ui, t, label, ButtonKind::Secondary)
}
-fn primary_button(ui: &mut Ui, label: &str) -> egui::Response {
- ui.add(Button::new(RichText::new(label).font(crate::style::medium(14.0)).color(Color32::WHITE)).fill(BRAND).rounding(8.0).min_size(vec2(0.0, 36.0)))
+fn primary_button(ui: &mut Ui, t: &Theme, label: &str) -> egui::Response {
+ action_button(ui, t, label, ButtonKind::Primary)
}
/// One page of the sidebar: its sections are listed under it while it's open.
@@ -1136,15 +1200,15 @@ impl RustCord {
}
section(ui, t, 3, "Family Centre");
- if button_row(ui, t, "Set up Family Centre", "Connect with a parent or guardian so they can stay in the loop. Your messages stay private and you'll always see exactly what they see.", "Open on discord.com", false) {
+ if button_row(ui, t, "Set up Family Centre", "Connect with a parent or guardian so they can stay in the loop. Your messages stay private and you'll always see exactly what they see.", "Open on discord.com", ButtonKind::Secondary) {
self.open_discord(ui, "set up Family Centre");
}
ui.add_space(8.0);
// Never done from here: both need your password on discord.com.
- if button_row(ui, t, "Disable your account", "Temporarily disable your account.", "Disable Account", true) {
+ if button_row(ui, t, "Disable your account", "Temporarily disable your account.", "Disable Account", ButtonKind::CriticalSecondary) {
self.open_discord(ui, "disable your account");
}
- if button_row(ui, t, "Close your account", "Permanently close your account.", "Delete Account", true) {
+ if button_row(ui, t, "Close your account", "Permanently close your account.", "Delete Account", ButtonKind::Critical) {
self.open_discord(ui, "delete your account");
}
if self.demo {
@@ -1194,7 +1258,7 @@ impl RustCord {
ui.label(RichText::new("Avatar").size(12.0).color(t.header_secondary));
ui.add_space(4.0);
ui.horizontal(|ui| {
- if primary_button(ui, "Change Avatar").clicked() {
+ if primary_button(ui, t, "Change Avatar").clicked() {
self.pick_avatar();
}
});
@@ -1387,7 +1451,7 @@ impl RustCord {
};
field(ui, label, &mut e.mfa_data, secret);
ui.add_enabled_ui(!busy && !e.mfa_data.trim().is_empty(), |ui| {
- verify = primary_button(ui, "Confirm").clicked();
+ verify = primary_button(ui, t, "Confirm").clicked();
});
} else {
match kind {
@@ -1395,7 +1459,7 @@ impl RustCord {
field(ui, "NEW USERNAME", &mut e.value, false);
field(ui, "CURRENT PASSWORD", &mut e.password, true);
ui.add_enabled_ui(!busy && !e.value.trim().is_empty() && !e.password.is_empty(), |ui| {
- if primary_button(ui, "Done").clicked() {
+ if primary_button(ui, t, "Done").clicked() {
send = Some((1, reqwest::Method::PATCH, "/users/@me", Some(json!({ "username": e.value.trim().to_lowercase(), "password": e.password }))));
}
});
@@ -1404,7 +1468,7 @@ impl RustCord {
ui.add(egui::Label::new(RichText::new("To change your email, Discord first sends a verification code to your current address.").size(14.0).color(t.text_normal)).wrap());
ui.add_space(10.0);
ui.add_enabled_ui(!busy, |ui| {
- if primary_button(ui, "Send Verification Code").clicked() {
+ if primary_button(ui, t, "Send Verification Code").clicked() {
send = Some((10, reqwest::Method::PUT, "/users/@me/email", None));
}
});
@@ -1414,7 +1478,7 @@ impl RustCord {
ui.add_space(10.0);
field(ui, "VERIFICATION CODE", &mut e.code, false);
ui.add_enabled_ui(!busy && !e.code.trim().is_empty(), |ui| {
- if primary_button(ui, "Next").clicked() {
+ if primary_button(ui, t, "Next").clicked() {
send = Some((11, reqwest::Method::POST, "/users/@me/email/verify-code", Some(json!({ "code": e.code.trim() }))));
}
});
@@ -1423,7 +1487,7 @@ impl RustCord {
field(ui, "NEW EMAIL", &mut e.value, false);
field(ui, "CURRENT PASSWORD", &mut e.password, true);
ui.add_enabled_ui(!busy && e.value.contains('@') && !e.password.is_empty(), |ui| {
- if primary_button(ui, "Done").clicked() {
+ if primary_button(ui, t, "Done").clicked() {
send = Some((12, reqwest::Method::PATCH, "/users/@me", Some(json!({ "email": e.value.trim(), "email_token": e.email_token, "password": e.password }))));
}
});
@@ -1438,7 +1502,7 @@ impl RustCord {
ui.add_space(6.0);
}
ui.add_enabled_ui(!busy && matches && e.value.len() >= 8 && !e.password.is_empty(), |ui| {
- if primary_button(ui, "Done").clicked() {
+ if primary_button(ui, t, "Done").clicked() {
send = Some((3, reqwest::Method::PATCH, "/users/@me", Some(json!({ "password": e.password, "new_password": e.value }))));
}
});
@@ -1823,7 +1887,7 @@ impl RustCord {
};
match any_marketing {
Some(any) => {
- if button_row(ui, t, "Unsubscribe from all marketing emails", if any { "Turns off announcement, tip and recommendation emails." } else { "You get no marketing emails." }, "Unsubscribe", false) && any {
+ if button_row(ui, t, "Unsubscribe from all marketing emails", if any { "Turns off announcement, tip and recommendation emails." } else { "You get no marketing emails." }, "Unsubscribe", ButtonKind::Secondary) && any {
email_patch = Some(json!({ "updates_and_announcements": false, "tips": false, "recommendations_and_events": false }));
}
}
@@ -2022,7 +2086,7 @@ impl RustCord {
let r = ui.add(TextEdit::singleline(&mut text).frame(false).hint_text("WUMP-USWU-MPUS-WUMP").desired_width(ui.available_width() - 130.0));
look_up |= r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter));
});
- look_up |= primary_button(ui, "Check Code").clicked();
+ look_up |= primary_button(ui, t, "Check Code").clicked();
});
ui.data_mut(|d| d.insert_temp(code_id, text));
if look_up && !code.is_empty() && !self.demo {
@@ -2070,7 +2134,7 @@ impl RustCord {
} else if expired {
ui.label(RichText::new("Expired").size(14.0).color(t.text_muted));
} else {
- redeem = primary_button(ui, "Redeem").clicked();
+ redeem = primary_button(ui, t, "Redeem").clicked();
}
});
});
@@ -2192,7 +2256,7 @@ impl RustCord {
ui.add_space(16.0);
return;
}
- if button_row(ui, t, "Request my data", about, if open { "Never mind" } else { "Request Data" }, false) {
+ if button_row(ui, t, "Request my data", about, if open { "Never mind" } else { "Request Data" }, ButtonKind::Secondary) {
ui.data_mut(|d| d.insert_temp(open_id, !open));
}
if !open {
@@ -2213,7 +2277,7 @@ impl RustCord {
}
ui.add_space(10.0);
ui.add_enabled_ui(picks.iter().any(|p| *p) && !self.demo, |ui| {
- send = primary_button(ui, "Request My Data").clicked();
+ send = primary_button(ui, t, "Request My Data").clicked();
});
});
ui.add_space(16.0);
@@ -3058,7 +3122,7 @@ impl RustCord {
if secondary_button(ui, t, "Stop Testing").clicked() {
self.stop_camera_test();
}
- } else if primary_button(ui, "Test Video").clicked() {
+ } else if primary_button(ui, t, "Test Video").clicked() {
self.start_camera_test();
}
ui.add_space(8.0);
@@ -3084,7 +3148,7 @@ impl RustCord {
ui.set_height(40.0);
ui.label(RichText::new("Shortcut").font(crate::style::medium(16.0)).color(t.header_primary));
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
- let b = if recording { primary_button(ui, "Press a key…") } else { secondary_button(ui, t, if key == 0 { "Record Keybind" } else { "Edit Keybind" }) };
+ let b = if recording { primary_button(ui, t, "Press a key…") } else { secondary_button(ui, t, if key == 0 { "Record Keybind" } else { "Edit Keybind" }) };
if b.clicked() {
ui.data_mut(|d| d.insert_temp(rec_id, !recording));
}
@@ -3351,7 +3415,7 @@ impl RustCord {
self.prefs.debug_logging = v;
self.save_prefs();
}
- if button_row(ui, t, "Log files", "This run's log, and the one before it.", "Open Folder", false) {
+ if button_row(ui, t, "Log files", "This run's log, and the one before it.", "Open Folder", ButtonKind::Secondary) {
if let Some(dir) = crate::diagnostics::log_path().and_then(|p| p.parent()) {
#[cfg(windows)]
let _ = std::process::Command::new("explorer").arg(dir).spawn();
@@ -3363,7 +3427,7 @@ impl RustCord {
}
let confirm_id = egui::Id::new("settings_voice_reset");
let sure = ui.data(|d| d.get_temp::<bool>(confirm_id).unwrap_or(false));
- if button_row(ui, t, "Reset all Voice & Video settings", "Return to the default set of Voice & Video settings: devices, volumes, input sensitivity, input profile, input mode and sounds.", if sure { "Click again to confirm" } else { "Reset Voice Settings" }, true) {
+ if button_row(ui, t, "Reset all Voice & Video settings", "Return to the default set of Voice & Video settings: devices, volumes, input sensitivity, input profile, input mode and sounds.", if sure { "Click again to confirm" } else { "Reset Voice Settings" }, ButtonKind::Critical) {
if sure {
let old = std::mem::take(&mut self.voice_ui.prefs);
let p = &mut self.voice_ui.prefs;
@@ -3488,7 +3552,7 @@ impl RustCord {
let status = updater.status();
let ready = matches!(status, crate::updater::Status::Ready(_));
let (title, desc) = update_status(&status, updater.enabled());
- if button_row(ui, t, &title, &desc, if ready { "Restart" } else { "Check for Updates" }, false) && !status.busy() {
+ if button_row(ui, t, &title, &desc, if ready { "Restart" } else { "Check for Updates" }, ButtonKind::Secondary) && !status.busy() {
if ready {
self.install_update(ui.ctx());
} else {