5.3 KiB · text History 59d9458
//! `/shaders`: live previews of the shader backgrounds, and the controls for
//! this browser's own (from party-slut's, without per-party backgrounds).
//! The drawing and the settings live in the browser
//! (assets/f/js/shader_background.js, shader_field.js); the server lists the
//! patterns and sets the default a browser starts with.
use axum::{extract::State, http::StatusCode, response::Html};
use minijinja::context;
use serde::Serialize;

use super::render;
use crate::middleware::UserContext;
use crate::AppState;

/// The background a browser gets until it picks one: Oil on glass, thin.
/// Put on `<body data-shader-default>` (root.html) for the script.
pub const DEFAULT_PATTERN: Shader = Shader::Oil;
pub const DEFAULT_OIL_COVERAGE: u32 = 7;

/// The default as the script reads it: a partial settings object.
pub fn default_settings_json() -> String {
    serde_json::json!({ "pattern": DEFAULT_PATTERN, "oilCoverage": DEFAULT_OIL_COVERAGE }).to_string()
}

/// The patterns, by the names shader_field.js knows them by. The fragment
/// shaders are neiam-device's moire/shaders/*.frag, byte for byte (via
/// party-slut), in assets/f/js/shaders/.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Shader {
    Both,
    Moire,
    Halftone,
    Bokeh,
    Stars,
    Prism,
    Oil,
    Bubbles,
    Spheres,
    Alternate,
}

impl Shader {
    pub const ALL: [Shader; 10] = [
        Shader::Oil,
        Shader::Both,
        Shader::Moire,
        Shader::Halftone,
        Shader::Bokeh,
        Shader::Stars,
        Shader::Prism,
        Shader::Bubbles,
        Shader::Spheres,
        Shader::Alternate,
    ];

    pub fn value(self) -> &'static str {
        match self {
            Self::Both => "both",
            Self::Moire => "moire",
            Self::Halftone => "halftone",
            Self::Bokeh => "bokeh",
            Self::Stars => "stars",
            Self::Prism => "prism",
            Self::Oil => "oil",
            Self::Bubbles => "bubbles",
            Self::Spheres => "spheres",
            Self::Alternate => "alternate",
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Self::Both => "Moire + Halftone",
            Self::Moire => "Moire",
            Self::Halftone => "Halftone",
            Self::Bokeh => "Bokeh",
            Self::Stars => "Starfield",
            Self::Prism => "Prism",
            Self::Oil => "Oil on glass",
            Self::Bubbles => "Bubbles",
            Self::Spheres => "Absorbing spheres",
            Self::Alternate => "Alternate",
        }
    }

    pub fn about(self) -> &'static str {
        match self {
            Self::Both => "The wallpaper's default: rings and dots together, fading out from a drifting hub.",
            Self::Moire => "Two ring families slightly apart; where they interleave and coincide, the moire beats.",
            Self::Halftone => "Dots on a rotated grid, sized as in print by the fade and a travelling wave.",
            Self::Bokeh => {
                "Soft out-of-focus discs drifting upward, fading in and out as they rise, each its own shade."
            }
            Self::Stars => "Sparse stars, mostly faint and a few bright, each twinkling at its own rate.",
            Self::Prism => {
                "A glowing beam strikes a turning shape and splits into rays, each its own width, slowly swelling and thinning."
            }
            Self::Oil => "Slicks of oil on dark glass, their colours the bands of a thin film, swirling slowly.",
            Self::Bubbles => {
                "Soap bubbles drifting about and bouncing off the edges and each other, their films swirling."
            }
            Self::Spheres => {
                "Spheres launched every which way; where two meet, the larger absorbs the smaller. At three, a new round."
            }
            Self::Alternate => "Fades back and forth between moire and halftone every few minutes.",
        }
    }
}

pub async fn show_shaders(
    State(state): State<AppState>,
    user: Option<UserContext>,
) -> Result<Html<String>, StatusCode> {
    let patterns: Vec<_> = Shader::ALL
        .into_iter()
        .map(|s| context! { value => s.value(), label => s.label(), about => s.about(), default => s == DEFAULT_PATTERN })
        .collect();
    let meta = crate::meta::PageMeta::new(&state, "/shaders")
        .title(format!("Shaders on {}", state.config.app_name))
        .description(format!(
            "The moving backgrounds {} can draw behind its pages: {}. Pick one for this browser, or none.",
            state.config.app_name,
            crate::meta::name_list(&Shader::ALL.iter().map(|s| s.label().to_string()).collect::<Vec<_>>(), 4)
        ));
    render(
        &state,
        "shaders.html",
        context! {
            meta => meta,
            title => "Shaders",
            app_name => state.config.app_name,
            user => user.map(|u| u.user),
            patterns => patterns,
            default_pattern => DEFAULT_PATTERN.value(),
            default_coverage => DEFAULT_OIL_COVERAGE,
        },
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serde_names_are_the_script_names() {
        for s in Shader::ALL {
            assert_eq!(serde_json::to_value(s).unwrap(), s.value());
        }
    }
}