|
| 1 | +//! Desktop environment appearance profile shared across settings, shelld, and compd. |
| 2 | +
|
| 3 | +use crate::persist; |
| 4 | + |
| 5 | +pub const APPEARANCE_KEY: &str = "de_appearance_v1"; |
| 6 | +pub const APPEARANCE_KEY_LEGACY: &str = "de.appearance.v1"; |
| 7 | +const MAGIC: [u8; 4] = *b"MDE1"; |
| 8 | +const VERSION: u8 = 1; |
| 9 | +const ENCODED_LEN: usize = 26; |
| 10 | + |
| 11 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 12 | +pub struct DesktopAppearance { |
| 13 | + pub dark_mode: bool, |
| 14 | + pub accent_rgb: (u8, u8, u8), |
| 15 | + pub desktop_rgb: (u8, u8, u8), |
| 16 | + pub panel_rgb: (u8, u8, u8), |
| 17 | + pub start_rgb: (u8, u8, u8), |
| 18 | + pub title_focus_rgb: (u8, u8, u8), |
| 19 | + pub border_focus_rgb: (u8, u8, u8), |
| 20 | +} |
| 21 | + |
| 22 | +impl DesktopAppearance { |
| 23 | + pub const fn default_dark() -> Self { |
| 24 | + Self { |
| 25 | + dark_mode: true, |
| 26 | + accent_rgb: (0, 230, 118), |
| 27 | + desktop_rgb: (26, 26, 46), |
| 28 | + panel_rgb: (18, 20, 30), |
| 29 | + start_rgb: (0, 85, 0), |
| 30 | + title_focus_rgb: (0, 85, 0), |
| 31 | + border_focus_rgb: (0, 170, 0), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub const fn default_light() -> Self { |
| 36 | + Self { |
| 37 | + dark_mode: false, |
| 38 | + accent_rgb: (0, 132, 96), |
| 39 | + desktop_rgb: (236, 238, 245), |
| 40 | + panel_rgb: (220, 223, 230), |
| 41 | + start_rgb: (0, 132, 96), |
| 42 | + title_focus_rgb: (0, 132, 96), |
| 43 | + border_focus_rgb: (0, 165, 120), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub fn from_theme_choice(dark_mode: bool, accent_rgb: (u8, u8, u8)) -> Self { |
| 48 | + let (ar, ag, ab) = accent_rgb; |
| 49 | + let title = scale_rgb(accent_rgb, if dark_mode { 38 } else { 68 }); |
| 50 | + let border = scale_rgb(accent_rgb, if dark_mode { 76 } else { 92 }); |
| 51 | + let start = scale_rgb(accent_rgb, if dark_mode { 34 } else { 74 }); |
| 52 | + let desktop = if dark_mode { |
| 53 | + (26, 26, 46) |
| 54 | + } else { |
| 55 | + (236, 238, 245) |
| 56 | + }; |
| 57 | + let panel = if dark_mode { |
| 58 | + (18, 20, 30) |
| 59 | + } else { |
| 60 | + (220, 223, 230) |
| 61 | + }; |
| 62 | + |
| 63 | + Self { |
| 64 | + dark_mode, |
| 65 | + accent_rgb: (ar, ag, ab), |
| 66 | + desktop_rgb: desktop, |
| 67 | + panel_rgb: panel, |
| 68 | + start_rgb: start, |
| 69 | + title_focus_rgb: title, |
| 70 | + border_focus_rgb: border, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + pub fn load() -> Option<Self> { |
| 75 | + let mut buf = [0u8; ENCODED_LEN]; |
| 76 | + if let Ok(n) = persist::get(APPEARANCE_KEY, &mut buf) { |
| 77 | + if n == ENCODED_LEN { |
| 78 | + if let Some(v) = decode(&buf) { |
| 79 | + return Some(v); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if let Ok(n) = persist::get(APPEARANCE_KEY_LEGACY, &mut buf) { |
| 85 | + if n == ENCODED_LEN { |
| 86 | + return decode(&buf); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + None |
| 91 | + } |
| 92 | + |
| 93 | + pub fn store(&self) -> Result<(), u64> { |
| 94 | + let buf = encode(self); |
| 95 | + match persist::put(APPEARANCE_KEY, &buf) { |
| 96 | + Ok(()) => Ok(()), |
| 97 | + Err(_) => persist::put(APPEARANCE_KEY_LEGACY, &buf), |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[inline(always)] |
| 103 | +fn scale8(v: u8, pct: u16) -> u8 { |
| 104 | + let x = (v as u16).saturating_mul(pct) / 100; |
| 105 | + x.min(255) as u8 |
| 106 | +} |
| 107 | + |
| 108 | +#[inline(always)] |
| 109 | +fn scale_rgb(rgb: (u8, u8, u8), pct: u16) -> (u8, u8, u8) { |
| 110 | + (scale8(rgb.0, pct), scale8(rgb.1, pct), scale8(rgb.2, pct)) |
| 111 | +} |
| 112 | + |
| 113 | +fn encode(a: &DesktopAppearance) -> [u8; ENCODED_LEN] { |
| 114 | + let mut out = [0u8; ENCODED_LEN]; |
| 115 | + out[0..4].copy_from_slice(&MAGIC); |
| 116 | + out[4] = VERSION; |
| 117 | + out[5] = if a.dark_mode { 1 } else { 0 }; |
| 118 | + |
| 119 | + write_rgb(&mut out, 8, a.accent_rgb); |
| 120 | + write_rgb(&mut out, 11, a.desktop_rgb); |
| 121 | + write_rgb(&mut out, 14, a.panel_rgb); |
| 122 | + write_rgb(&mut out, 17, a.start_rgb); |
| 123 | + write_rgb(&mut out, 20, a.title_focus_rgb); |
| 124 | + write_rgb(&mut out, 23, a.border_focus_rgb); |
| 125 | + out |
| 126 | +} |
| 127 | + |
| 128 | +fn decode(buf: &[u8; ENCODED_LEN]) -> Option<DesktopAppearance> { |
| 129 | + if buf[0..4] != MAGIC || buf[4] != VERSION { |
| 130 | + return None; |
| 131 | + } |
| 132 | + |
| 133 | + Some(DesktopAppearance { |
| 134 | + dark_mode: buf[5] != 0, |
| 135 | + accent_rgb: read_rgb(buf, 8), |
| 136 | + desktop_rgb: read_rgb(buf, 11), |
| 137 | + panel_rgb: read_rgb(buf, 14), |
| 138 | + start_rgb: read_rgb(buf, 17), |
| 139 | + title_focus_rgb: read_rgb(buf, 20), |
| 140 | + border_focus_rgb: read_rgb(buf, 23), |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +#[inline(always)] |
| 145 | +fn write_rgb(buf: &mut [u8; ENCODED_LEN], i: usize, rgb: (u8, u8, u8)) { |
| 146 | + buf[i] = rgb.0; |
| 147 | + buf[i + 1] = rgb.1; |
| 148 | + buf[i + 2] = rgb.2; |
| 149 | +} |
| 150 | + |
| 151 | +#[inline(always)] |
| 152 | +fn read_rgb(buf: &[u8; ENCODED_LEN], i: usize) -> (u8, u8, u8) { |
| 153 | + (buf[i], buf[i + 1], buf[i + 2]) |
| 154 | +} |
0 commit comments