Skip to content

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

3.6 KB
Raw Blame
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod accounts;
mod app;
mod cache;
mod clipboard;
mod quest_video;
mod registry;
mod rpc;
mod tray;
mod voice_call;
mod camera;
mod golive;
mod audio;
mod backend;
mod demo;
mod diagnostics;
mod emoji;
mod emoji_data;
mod features;
mod login_view;
mod captcha_view;
#[cfg(feature = "memprof")]
mod memprof;
mod embed_view;
mod video_player;
mod net_images;
mod lottie;
mod palette;
mod shortcodes;
mod spellcheck;
mod highlight;
mod hotkey;
#[cfg(target_os = "linux")]
mod linux;
mod markdown;
mod media_copy;
mod store;
mod style;
mod ui;
mod updater;
mod video;
mod window_place;

use eframe::egui;

/// mimalloc hands freed memory back to Windows (the default heap mostly
/// keeps it), so the big burst while loading 100+ servers doesn't stay
/// resident for the rest of the session.
#[cfg(not(feature = "memprof"))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[cfg(feature = "memprof")]
#[global_allocator]
static GLOBAL: memprof::Prof = memprof::Prof;

/// Window / taskbar icon: the blurple "R" (the .exe gets it from rustcord.rc).
fn app_icon() -> egui::IconData {
    match image::load_from_memory(include_bytes!("../assets/icon-256.png")) {
        Ok(img) => {
            let rgba = img.to_rgba8();
            egui::IconData { width: rgba.width(), height: rgba.height(), rgba: rgba.into_raw() }
        }
        Err(_) => egui::IconData::default(),
    }
}

fn main() -> eframe::Result<()> {
    // Before anything (the log, the saved accounts) is written there.
    cache::make_folders_private();
    #[cfg(target_os = "linux")]
    linux::prefer_x11();
    // The uninstaller's "delete my saved login": end every saved session at
    // Discord and forget it, with no window.
    if std::env::args().any(|a| a == "--logout-all") {
        accounts::log_out_all();
        return Ok(());
    }
    diagnostics::tune_allocator();
    diagnostics::init();
    #[cfg(target_os = "linux")]
    linux::init();
    diagnostics::close_other_instances();
    diagnostics::idle::start();
    let demo = std::env::args().any(|a| a == "--demo");
    // The network thread and the saved login's session start now, while the
    // window, its GPU context and the fonts are being set up.
    let backend = backend::Backend::spawn(demo);
    let launch = app::Launch::begin(&backend, demo);

    let viewport = egui::ViewportBuilder::default()
        .with_title(if demo { "RustCord (demo)" } else { "RustCord" })
        // Linux: matches rustcord.desktop, so the dock shows RustCord's icon.
        .with_app_id("rustcord")
        .with_inner_size([1280.0, 800.0])
        .with_min_inner_size([940.0, 560.0])
        .with_icon(app_icon());
    // Discord's desktop app draws its own title bar. On macOS it keeps the
    // window's traffic lights, over the left end of that bar.
    #[cfg(not(target_os = "macos"))]
    let viewport = viewport.with_decorations(false);
    #[cfg(target_os = "macos")]
    let viewport = viewport.with_fullsize_content_view(true).with_titlebar_shown(false).with_title_shown(false);
    let options = eframe::NativeOptions { viewport, ..Default::default() };

    let result = eframe::run_native(
        "RustCord",
        options,
        Box::new(move |cc| {
            #[cfg(target_os = "linux")]
            linux::keep_x_errors_apart(cc);
            Ok(Box::new(app::RustCord::new(cc, demo, backend, launch)))
        }),
    );
    // No icon left behind in the notification area.
    tray::remove();
    match &result {
        Ok(()) => log::info!("RustCord exited normally (window closed)"),
        Err(e) => log::error!("RustCord exited with an error: {e}"),
    }
    result
}