Skip to content

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

4.8 KB
Raw Blame
//! Settings → Experimental → Spam Warnings: the card that appears when what
//! you do looks like spam or a bot to Discord (see `discord::guard`). It stays
//! until dismissed (or a minute), and each kind can be silenced for the
//! session.

use crate::app::RustCord;
use discord::guard::{Kind, Warning};
use eframe::egui::{self, pos2, vec2, Align2, Color32, FontFamily, FontId, Order, RichText, Sense, Shape, Stroke};
use std::collections::HashSet;
use std::time::{Duration, Instant};

const AMBER: Color32 = Color32::from_rgb(0xf0, 0xb2, 0x32);
/// A card goes by itself after this long.
const STAYS: Duration = Duration::from_secs(60);

#[derive(Default)]
pub struct SpamWarnings {
    shown: Vec<(Warning, Instant)>,
    /// Kinds the person said not to warn about again this session.
    muted: HashSet<Kind>,
}

impl SpamWarnings {
    /// Show a sample warning (`RUSTCORD_DEV_VIEW=spam-warning`).
    pub fn dev_show(&mut self) {
        self.shown.push((discord::guard::warning(Kind::Messages, 6), Instant::now()));
    }
}

impl RustCord {
    /// Pick up what `discord::guard` noticed (every frame; free when nothing did).
    pub fn spam_warnings_tick(&mut self) {
        let new = discord::guard::take();
        if new.is_empty() || !self.prefs.experiments.spam_warnings {
            return;
        }
        let sw = &mut self.spam_warnings;
        for w in new.into_iter().filter(|w| !sw.muted.contains(&w.kind)) {
            sw.shown.retain(|(old, _)| old.kind != w.kind);
            sw.shown.push((w, Instant::now()));
        }
        self.ctx.request_repaint();
    }

    pub fn spam_warnings_ui(&mut self, ctx: &egui::Context) {
        let sw = &mut self.spam_warnings;
        sw.shown.retain(|(_, at)| at.elapsed() < STAYS);
        if sw.shown.is_empty() {
            return;
        }
        ctx.request_repaint_after(Duration::from_secs(1));
        let t = self.theme.clone();
        let mut dismiss: Option<Kind> = None;
        let mut mute: Option<Kind> = None;
        egui::Area::new(egui::Id::new("spam_warnings")).order(Order::Tooltip).anchor(Align2::CENTER_TOP, vec2(0.0, 52.0)).show(ctx, |ui| {
            ui.spacing_mut().item_spacing.y = 8.0;
            for (w, _) in &sw.shown {
                egui::Frame::none()
                    .fill(t.bg_floating)
                    .rounding(8.0)
                    .stroke(Stroke::new(1.0_f32, AMBER.gamma_multiply(0.6)))
                    .shadow(egui::epaint::Shadow { offset: vec2(0.0, 6.0), blur: 18.0, spread: 0.0, color: Color32::from_black_alpha(120) })
                    .inner_margin(egui::Margin { left: 48.0, right: 16.0, top: 12.0, bottom: 12.0 })
                    .show(ui, |ui| {
                        ui.set_width(400.0);
                        // A warning triangle in the margin.
                        let top = ui.min_rect().left_top() + vec2(-32.0, 2.0);
                        let tri = vec![pos2(top.x + 10.0, top.y), pos2(top.x + 20.0, top.y + 18.0), pos2(top.x, top.y + 18.0)];
                        ui.painter().add(Shape::convex_polygon(tri, AMBER, Stroke::NONE));
                        ui.painter().text(pos2(top.x + 10.0, top.y + 11.0), Align2::CENTER_CENTER, "!", FontId::new(12.0, FontFamily::Name(crate::style::BOLD.into())), Color32::BLACK);

                        ui.label(RichText::new(w.title).font(FontId::new(15.0, FontFamily::Name(crate::style::BOLD.into()))).color(t.header_primary));
                        ui.add_space(2.0);
                        ui.add(egui::Label::new(RichText::new(&w.text).size(13.0).color(t.text_normal)).wrap());
                        ui.add_space(4.0);
                        ui.horizontal(|ui| {
                            let got = egui::Button::new(RichText::new("Got it").font(crate::style::medium(13.0)).color(Color32::BLACK)).fill(AMBER).rounding(6.0);
                            if ui.add(got).on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
                                dismiss = Some(w.kind);
                            }
                            let quiet = ui
                                .add(egui::Label::new(RichText::new("Don't warn me about this again").size(12.0).color(t.text_muted)).sense(Sense::click()))
                                .on_hover_text("For the rest of this session. Spam Warnings can be switched off in Settings → Experimental.")
                                .on_hover_cursor(egui::CursorIcon::PointingHand);
                            if quiet.clicked() {
                                mute = Some(w.kind);
                            }
                        });
                    });
            }
        });
        if let Some(k) = dismiss.or(mute) {
            sw.shown.retain(|(w, _)| w.kind != k);
        }
        if let Some(k) = mute {
            sw.muted.insert(k);
        }
    }
}