Skip to content

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

3.4 KB
Raw Blame
//! Login screen: Discord's own login page (`login_view`) fills the window
//! under the title bar, with a notice bar above it when there is something to say.

use crate::app::RustCord;
use eframe::egui::{self, vec2, Align2, Button, CentralPanel, Color32, Frame, Rect, RichText, Sense, Stroke, UiBuilder};

/// Discord's notice bar: 36 px, 14 px medium text, centred.
const NOTICE: f32 = 36.0;

impl RustCord {
    pub fn login_ui(&mut self, ctx: &egui::Context) {
        let t = self.theme.clone();
        CentralPanel::default().frame(Frame::none().fill(t.bg_tertiary)).show(ctx, |ui| {
            let mut area = ui.max_rect();
            // Adding an account: a bar that says so, whose × goes back to the running one.
            let adding = self.adding_account && self.login.notice.is_none();
            let notice = self.login.notice.clone().or_else(|| adding.then(|| "Log in to add an account".to_string()));
            if let Some(text) = notice {
                let bar = Rect::from_min_size(area.min, vec2(area.width(), NOTICE));
                area.min.y = bar.bottom();
                ui.painter().rect_filled(bar, 0.0, if adding { t.accent } else { t.danger });
                ui.painter().text(bar.center(), Align2::CENTER_CENTER, text, crate::style::medium(14.0), Color32::WHITE);
                let close = Rect::from_center_size(egui::pos2(bar.right() - NOTICE / 2.0, bar.center().y), vec2(24.0, 24.0));
                let b = ui.interact(close, egui::Id::new("login_notice_close"), Sense::click()).on_hover_cursor(egui::CursorIcon::PointingHand);
                let c = if b.hovered() { Color32::WHITE } else { crate::style::rgba(255, 255, 255, 200) };
                let (m, h) = (ui.painter().round_pos_to_pixels(close.center()), 4.5);
                ui.painter().line_segment([m + vec2(-h, -h), m + vec2(h, h)], Stroke::new(1.5_f32, c));
                ui.painter().line_segment([m + vec2(-h, h), m + vec2(h, -h)], Stroke::new(1.5_f32, c));
                if b.clicked() {
                    if self.adding_account {
                        self.cancel_add_account();
                    } else {
                        self.login.notice = None;
                    }
                }
            }
            match self.login.open_error.clone() {
                // WebView2 couldn't start: say so, and offer another go.
                Some(err) => {
                    self.login.webview_rect = None;
                    let rect = Rect::from_center_size(area.center(), vec2(area.width().min(440.0), 140.0));
                    ui.allocate_new_ui(UiBuilder::new().max_rect(rect), |ui| {
                        ui.vertical_centered(|ui| {
                            ui.label(crate::style::bold("Couldn't open the login page").size(20.0).color(t.header_primary));
                            ui.add_space(8.0);
                            ui.label(RichText::new(err).size(14.0).color(t.header_secondary));
                            ui.add_space(16.0);
                            let retry = Button::new(RichText::new("Try Again").font(crate::style::medium(14.0)).color(Color32::WHITE)).fill(t.accent).min_size(vec2(120.0, 38.0));
                            if ui.add(retry).clicked() {
                                self.login.open_error = None;
                            }
                        });
                    });
                }
                None => self.login.webview_rect = Some(area),
            }
        });
    }
}