gled-ui 2.34.0

gled's egui interface, shared by the desktop app and the browser
Documentation
//! One line under the rules saying what just happened: an edit's
//! consequence ("Removed 1 rule — Ctrl+Z brings it back") or what a
//! control moved on the hardware did. Each message fades after a while.

use egui::{Color32, RichText, Ui};

/// How long a message shows, in seconds.
const SHOWN: f64 = 5.0;

#[derive(Debug, Default)]
pub(crate) struct StatusLine {
    edit: Option<(String, f64)>,
    hardware: Option<(String, f64)>,
}

impl StatusLine {
    pub(crate) fn edit(&mut self, ui: &Ui, message: impl Into<String>) {
        self.edit = Some((message.into(), ui.input(|input| input.time)));
    }

    pub(crate) fn hardware(&mut self, ui: &Ui, message: impl Into<String>) {
        self.hardware = Some((message.into(), ui.input(|input| input.time)));
    }

    pub(crate) fn show(&mut self, ui: &mut Ui) {
        let now = ui.input(|input| input.time);
        ui.horizontal(|ui| {
            for (slot, color) in [
                (&mut self.edit, ui.visuals().text_color()),
                (&mut self.hardware, Color32::from_rgb(94, 194, 122)),
            ] {
                if let Some((message, since)) = slot {
                    let age = now - *since;
                    if age > SHOWN {
                        *slot = None;
                        continue;
                    }
                    let fade = (1.0 - (age - (SHOWN - 1.0)).max(0.0)) as f32;
                    ui.label(RichText::new(message.as_str()).color(color.gamma_multiply(fade)));
                    ui.ctx().request_repaint();
                }
            }
        });
    }
}