Skip to content

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

5.3 KB
Raw Blame
//! The update that is ready (see `crate::updater`): Discord's green download
//! button in the title bar ("Update Ready!") restarts into it, after Discord's
//! "Briefly leave voice?" when you're in a call.

use crate::app::RustCord;
use crate::style::{icon, icon_font, medium, overlay, rgba};
use eframe::egui::{self, pos2, vec2, Align2, Color32, CursorIcon, FontId, Key, Rect, Sense, Stroke};

/// Discord's `GREEN_360`, the button's colour (Discord web, 2026-12-03).
pub const GREEN: Color32 = Color32::from_rgb(0x3d, 0x9e, 0x60);
/// The "critical-primary" confirm button, and hovered.
const CRITICAL: Color32 = Color32::from_rgb(0xd2, 0x2d, 0x39);
const CRITICAL_HOVER: Color32 = Color32::from_rgb(0xa9, 0x23, 0x2e);

/// Discord's strings (its desktop app's, in its web client), with RustCord
/// for Discord.
pub const READY: &str = "Update Ready!";
pub const DOWNLOADING: &str = "Downloading Update";
const LEAVE_TITLE: &str = "Briefly leave voice?";
const LEAVE_BODY: &str = "Updating RustCord while in a voice channel will cause you to leave briefly. You\u{2019}re probably going to update anyway but, you know, just warning you.";
const LEAVE_CONFIRM: &str = "Update anyway!";

fn strong(size: f32) -> FontId {
    FontId::new(size, egui::FontFamily::Name(crate::style::BOLD.into()))
}

impl RustCord {
    /// The title bar button or Settings' "Restart": restart into the update,
    /// asking first during a call.
    pub fn install_update(&mut self, ctx: &egui::Context) {
        if self.voice.is_some() || self.go_live.is_some() || !self.watching.is_empty() {
            self.update_confirm = true;
        } else {
            log::info!("updater: restarting into the new version");
            self.restart(ctx);
        }
    }

    /// "Briefly leave voice?", laid out as Discord's other confirmations
    /// (see `stranger_confirm_ui`), with its red confirm button. Enter
    /// updates; Esc, ✕ or a click on the dimmed window cancels.
    pub fn update_confirm_ui(&mut self, ctx: &egui::Context) {
        if !self.update_confirm {
            return;
        }
        let t = self.theme.clone();
        const W: f32 = 398.0;
        let ok_id = egui::Id::new("update_confirm_ok");
        let mut cancel = ctx.input(|i| i.key_pressed(Key::Escape));
        let mut confirm = ctx.memory(|m| m.has_focus(ok_id)) && ctx.input(|i| i.key_pressed(Key::Enter));
        cancel |= super::guild_pages::modal_frame(ctx, &t, "update_confirm", W, |ui| {
            let mut job = egui::text::LayoutJob::single_section(
                LEAVE_BODY.into(),
                egui::TextFormat { font_id: FontId::proportional(16.0), color: t.header_secondary, line_height: Some(20.0), ..Default::default() },
            );
            job.wrap.max_width = W - 48.0;
            let body = ui.fonts(|f| f.layout_job(job));
            let h = 24.0 + 24.0 + 8.0 + body.size().y + 8.0 + 16.0 + 40.0 + 8.0 + 16.0;
            let (r, _) = ui.allocate_exact_size(vec2(W, h), Sense::hover());
            ui.painter().text(pos2(r.left() + 24.0, r.top() + 36.0), Align2::LEFT_CENTER, LEAVE_TITLE, strong(20.0), t.header_primary);
            let x = Rect::from_min_size(pos2(r.right() - 8.0 - 40.0, r.top() + 8.0), vec2(40.0, 40.0));
            let xr = ui.interact(x, egui::Id::new("update_confirm_close"), Sense::click()).on_hover_cursor(CursorIcon::PointingHand);
            if xr.hovered() {
                ui.painter().rect_filled(x, 8.0, t.bg_hover);
            }
            ui.painter().text(x.center(), Align2::CENTER_CENTER, icon::CLOSE, icon_font(14.0), if xr.hovered() { t.header_primary } else { t.interactive_normal });
            cancel |= xr.clicked();
            ui.painter().galley(pos2(r.left() + 24.0, r.top() + 56.0), body, t.header_secondary);
            let bw = (W - 48.0 - 8.0) / 2.0;
            let no = Rect::from_min_size(pos2(r.left() + 24.0, r.bottom() - 24.0 - 40.0), vec2(bw, 40.0));
            let yes = no.translate(vec2(bw + 8.0, 0.0));
            let nr = ui.interact(no, egui::Id::new("update_confirm_cancel"), Sense::click()).on_hover_cursor(CursorIcon::PointingHand);
            ui.painter().rect(no, 8.0, overlay(if nr.hovered() { 0.2 } else { 0.12 }), Stroke::new(1.0_f32, rgba(151, 151, 159, 10)));
            ui.painter().text(no.center(), Align2::CENTER_CENTER, "Cancel", medium(16.0), t.header_primary);
            cancel |= nr.clicked();
            let yr = ui.interact(yes, ok_id, Sense::click()).on_hover_cursor(CursorIcon::PointingHand);
            ui.painter().rect(yes, 8.0, if yr.hovered() { CRITICAL_HOVER } else { CRITICAL }, Stroke::new(1.0_f32, rgba(255, 255, 255, 20)));
            ui.painter().text(yes.center(), Align2::CENTER_CENTER, LEAVE_CONFIRM, medium(16.0), Color32::WHITE);
            confirm |= yr.clicked();
        });
        if confirm || cancel {
            ctx.memory_mut(|m| m.surrender_focus(ok_id));
            self.update_confirm = false;
        } else {
            ctx.memory_mut(|m| m.request_focus(ok_id));
        }
        if confirm && crate::diagnostics::dev_var("RUSTCORD_DEV_VIEW").as_deref() == Ok("update-confirm") {
            self.toast("Update anyway (test): nothing restarted.", false);
        } else if confirm {
            log::info!("updater: leaving the call to restart into the new version");
            self.leave_voice();
            self.restart(ctx);
        }
    }
}