Files
capri-core-rust/crates/capri-ui/src/settings.rs
T

236 lines
8.5 KiB
Rust

//! Shell settings stored as `sections.ui` in settings.json (Capri's `companion.json`, `dimming.json`
//! and `window.json`). Out-of-range values are clamped on read.
use capri_core::Section;
use eframe::egui::Color32;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
#[serde(default, rename_all = "camelCase")]
pub struct UiSettings {
pub always_on_top: bool,
pub minimized_chip: bool,
pub restore_from_tray_on_wake: bool,
pub start_hidden: bool,
pub startup_intro: bool,
pub launch_at_login: bool,
/// `character` or `dialogue`.
pub detached_style: String,
pub reply_auto_hide: bool,
pub reply_auto_hide_seconds: u32,
/// `system`, `on` or `off`.
pub reduced_motion: String,
pub float_enabled: bool,
pub float_amplitude: u32,
pub float_period_seconds: u32,
pub glitch_enabled: bool,
pub glitch_strength: u32,
pub glitch_interval_seconds: u32,
pub glitch_events: bool,
pub crt: bool,
pub dimming_enabled: bool,
pub dimming_level: u32,
pub dimming_softness: u32,
pub teleport_intensity: u32,
pub teleport_jaggedness: u32,
pub teleport_branches: u32,
pub teleport_reduced_flash: bool,
/// `off`, `spark`, `thunder` or `pulse`.
pub teleport_sound: String,
pub avatar_drag_tether: bool,
pub avatar_drag_tether_intensity: u32,
pub avatar_drag_tether_chaos: u32,
pub avatar_drag_tether_dead_zone: u32,
pub avatar_snap_radius: u32,
pub avatar_tap_gap_seconds: u32,
pub interface_theme: Theme,
}
impl Default for UiSettings {
fn default() -> Self {
Self {
always_on_top: true,
minimized_chip: true,
restore_from_tray_on_wake: true,
start_hidden: false,
startup_intro: true,
launch_at_login: false,
detached_style: "character".into(),
reply_auto_hide: true,
reply_auto_hide_seconds: 8,
reduced_motion: "system".into(),
float_enabled: false,
float_amplitude: 6,
float_period_seconds: 10,
glitch_enabled: true,
glitch_strength: 65,
glitch_interval_seconds: 35,
glitch_events: true,
crt: true,
dimming_enabled: true,
dimming_level: 35,
dimming_softness: 140,
teleport_intensity: 75,
teleport_jaggedness: 78,
teleport_branches: 75,
teleport_reduced_flash: false,
teleport_sound: "spark".into(),
avatar_drag_tether: true,
avatar_drag_tether_intensity: 45,
avatar_drag_tether_chaos: 55,
avatar_drag_tether_dead_zone: 30,
avatar_snap_radius: 36,
avatar_tap_gap_seconds: 10,
interface_theme: Theme::default(),
}
}
}
impl Section for UiSettings {
const NAME: &'static str = "ui";
}
fn pick(value: &mut String, allowed: &[&str]) {
if !allowed.contains(&value.as_str()) {
*value = allowed[0].into();
}
}
impl UiSettings {
/// Capri's validation ranges, applied by clamping instead of rejecting the whole object.
pub fn sanitized(mut self) -> Self {
pick(&mut self.detached_style, &["character", "dialogue"]);
pick(&mut self.reduced_motion, &["system", "on", "off"]);
pick(&mut self.teleport_sound, &["spark", "off", "thunder", "pulse"]);
for (value, lo, hi) in [
(&mut self.reply_auto_hide_seconds, 2, 60),
(&mut self.float_amplitude, 0, 24),
(&mut self.float_period_seconds, 4, 30),
(&mut self.glitch_strength, 0, 100),
(&mut self.glitch_interval_seconds, 10, 120),
(&mut self.dimming_level, 0, 85),
(&mut self.dimming_softness, 0, 320),
(&mut self.teleport_intensity, 0, 100),
(&mut self.teleport_jaggedness, 0, 100),
(&mut self.teleport_branches, 0, 100),
(&mut self.avatar_drag_tether_intensity, 0, 100),
(&mut self.avatar_drag_tether_chaos, 0, 100),
(&mut self.avatar_drag_tether_dead_zone, 0, 120),
(&mut self.avatar_snap_radius, 12, 100),
(&mut self.avatar_tap_gap_seconds, 1, 15),
] {
*value = (*value).clamp(lo, hi);
}
let t = &mut self.interface_theme;
pick(&mut t.preset, &["capri", "blackwall", "amethyst", "overdrive", "custom"]);
t.hue = t.hue.min(359);
t.saturation = t.saturation.clamp(35, 100);
t.background_hue = t.background_hue.min(359);
t.background_saturation = t.background_saturation.min(100);
self
}
/// Reduced motion: explicit choice, else the desktop preference read at startup.
pub fn reduced(&self, system: bool) -> bool {
match self.reduced_motion.as_str() {
"on" => true,
"off" => false,
_ => system,
}
}
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
#[serde(default, rename_all = "camelCase")]
pub struct Theme {
pub preset: String,
pub hue: u32,
pub saturation: u32,
pub background_hue: u32,
pub background_saturation: u32,
}
impl Default for Theme {
fn default() -> Self {
Self { preset: "capri".into(), hue: 174, saturation: 84, background_hue: 174, background_saturation: 40 }
}
}
/// Colours derived from the theme.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Palette {
pub capri: bool,
pub cyan: Color32,
pub pink: Color32,
pub panel: Color32,
pub background: Color32,
pub border: Color32,
pub text: Color32,
}
pub fn hsl(h: f32, s: f32, l: f32) -> Color32 {
let (s, l) = (s / 100.0, l / 100.0);
let a = s * l.min(1.0 - l);
let f = |n: f32| {
let k = (n + h / 30.0) % 12.0;
let v = l - a * (k - 3.0).min(9.0 - k).clamp(-1.0, 1.0);
(v * 255.0).round() as u8
};
Color32::from_rgb(f(0.0), f(8.0), f(4.0))
}
impl Theme {
pub fn palette(&self) -> Palette {
let (accent, vivid, highlight) = match self.preset.as_str() {
"capri" => (174, 84, 344),
"blackwall" => (346, 91, 284),
"amethyst" => (264, 79, 190),
"overdrive" => (46, 95, 338),
_ => (self.hue, self.saturation, self.hue),
};
let custom = self.preset == "custom";
let capri = self.preset == "capri";
let (surface, surface_sat) = if custom { (self.background_hue, self.background_saturation) } else { (accent, 40) };
let (accent, vivid, highlight, surface, surface_sat) = (accent as f32, vivid as f32, highlight as f32, surface as f32, surface_sat as f32);
Palette {
capri,
cyan: if capri { Color32::from_rgb(0x8e, 0xf5, 0xed) } else { hsl(accent, vivid, 76.0) },
pink: if capri { Color32::from_rgb(0xff, 0x3c, 0x68) } else { hsl(highlight, if custom { vivid } else { 88.0 }, 61.0) },
panel: hsl(surface, (surface_sat * 1.1).round().min(100.0), 13.0),
background: hsl(surface, surface_sat, 8.0),
border: hsl(accent, (vivid * 0.42).round(), 38.0),
text: hsl(accent, (vivid * 0.54).round(), 92.0),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clamps_and_defaults() {
let s: UiSettings = serde_json::from_str(r#"{"glitchStrength":500,"teleportSound":"boom","interfaceTheme":{"preset":"x","saturation":2}}"#).unwrap();
let s = s.sanitized();
assert_eq!(s.glitch_strength, 100);
assert_eq!(s.teleport_sound, "spark");
assert_eq!(s.interface_theme.preset, "capri");
assert_eq!(s.interface_theme.saturation, 35);
assert_eq!(s.avatar_snap_radius, 36);
assert!(s.reduced(true) && !UiSettings { reduced_motion: "off".into(), ..s }.reduced(true));
}
#[test]
fn theme_palette() {
assert_eq!(hsl(0.0, 100.0, 50.0), Color32::from_rgb(255, 0, 0));
assert_eq!(hsl(120.0, 100.0, 25.0), Color32::from_rgb(0, 128, 0));
let capri = Theme::default().palette();
assert_eq!(capri.cyan, Color32::from_rgb(0x8e, 0xf5, 0xed));
let blackwall = Theme { preset: "blackwall".into(), ..Theme::default() }.palette();
assert_eq!(blackwall.cyan, hsl(346.0, 91.0, 76.0));
assert_eq!(blackwall.pink, hsl(284.0, 88.0, 61.0));
let custom = Theme { preset: "custom".into(), hue: 10, saturation: 50, background_hue: 200, background_saturation: 20 }.palette();
assert_eq!(custom.background, hsl(200.0, 20.0, 8.0));
assert_eq!(custom.pink, hsl(10.0, 50.0, 61.0));
}
}