| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#![forbid(unsafe_code)] |
| 43 |
|
| 44 |
use makeover_layout::{Bevel, Depth, Fill, Intent}; |
| 45 |
use std::fmt::Write as _; |
| 46 |
|
| 47 |
|
| 48 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 49 |
pub struct Emit { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pub border_width: &'static str, |
| 55 |
|
| 56 |
pub class_prefix: &'static str, |
| 57 |
} |
| 58 |
|
| 59 |
impl Default for Emit { |
| 60 |
fn default() -> Self { |
| 61 |
Self { |
| 62 |
border_width: "1px", |
| 63 |
class_prefix: "", |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
#[must_use] |
| 70 |
pub fn bevel_var(bevel: Bevel) -> &'static str { |
| 71 |
match bevel { |
| 72 |
Bevel::Raised => "--bevel-raised", |
| 73 |
Bevel::Inset => "--bevel-inset", |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#[must_use] |
| 83 |
pub fn fill_var(fill: Fill) -> String { |
| 84 |
match fill { |
| 85 |
Fill::Well => format!("var(--{}, var(--{}))", fill.token(), Fill::Page.token()), |
| 86 |
other => format!("var(--{})", other.token()), |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[must_use] |
| 97 |
pub fn bevel_shadow(bevel: Bevel, opts: &Emit) -> String { |
| 98 |
let (top_left, bottom_right) = bevel.edges(); |
| 99 |
let w = opts.border_width; |
| 100 |
format!( |
| 101 |
"inset {w} {w} 0 var(--{}), inset -{w} -{w} 0 var(--{})", |
| 102 |
top_left.token(), |
| 103 |
bottom_right.token() |
| 104 |
) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
#[must_use] |
| 113 |
pub fn bevel_properties(opts: &Emit) -> String { |
| 114 |
let mut css = String::new(); |
| 115 |
for bevel in [Bevel::Raised, Bevel::Inset] { |
| 116 |
let _ = writeln!( |
| 117 |
css, |
| 118 |
" {}: {};", |
| 119 |
bevel_var(bevel), |
| 120 |
bevel_shadow(bevel, opts) |
| 121 |
); |
| 122 |
} |
| 123 |
css |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
#[must_use] |
| 128 |
pub fn depth_class(depth: Depth, opts: &Emit) -> Option<String> { |
| 129 |
let name = match depth { |
| 130 |
Depth::Flat => return None, |
| 131 |
Depth::Raised => "raised", |
| 132 |
Depth::Well => "well", |
| 133 |
}; |
| 134 |
Some(format!("{}{name}", opts.class_prefix)) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#[must_use] |
| 146 |
pub fn depth_rules(opts: &Emit) -> String { |
| 147 |
let mut css = String::new(); |
| 148 |
for depth in [Depth::Raised, Depth::Well] { |
| 149 |
let Some(class) = depth_class(depth, opts) else { |
| 150 |
continue; |
| 151 |
}; |
| 152 |
let (Some(fill), Some(bevel)) = (depth.fill(), depth.bevel()) else { |
| 153 |
continue; |
| 154 |
}; |
| 155 |
let _ = writeln!( |
| 156 |
css, |
| 157 |
".{class} {{\n background: {};\n box-shadow: var({});\n}}", |
| 158 |
fill_var(fill), |
| 159 |
bevel_var(bevel) |
| 160 |
); |
| 161 |
} |
| 162 |
if let (Some(raised), Some(pressed)) = ( |
| 163 |
depth_class(Depth::Raised, opts), |
| 164 |
Depth::Raised.pressed().bevel(), |
| 165 |
) { |
| 166 |
let _ = writeln!( |
| 167 |
css, |
| 168 |
".{raised}:active {{\n box-shadow: var({});\n}}", |
| 169 |
bevel_var(pressed) |
| 170 |
); |
| 171 |
} |
| 172 |
css |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
#[must_use] |
| 178 |
pub fn stylesheet(opts: &Emit) -> String { |
| 179 |
format!( |
| 180 |
"/* Generated by makeover-webview from makeover-layout. Do not edit.\n \ |
| 181 |
Depth is a fill and an edge together; naming them apart is what let\n \ |
| 182 |
them disagree. See the crate's README and wiki note makeover-layout. */\n\ |
| 183 |
:root {{\n{}}}\n\n{}", |
| 184 |
bevel_properties(opts), |
| 185 |
depth_rules(opts) |
| 186 |
) |
| 187 |
} |
| 188 |
|
| 189 |
#[cfg(test)] |
| 190 |
mod tests { |
| 191 |
use super::*; |
| 192 |
use makeover_layout::Edge; |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn the_emitted_bevel_matches_what_the_apps_already_hand_write() { |
| 196 |
|
| 197 |
|
| 198 |
let opts = Emit::default(); |
| 199 |
assert_eq!( |
| 200 |
bevel_shadow(Bevel::Raised, &opts), |
| 201 |
"inset 1px 1px 0 var(--bevel-light), inset -1px -1px 0 var(--bevel-dark)" |
| 202 |
); |
| 203 |
assert_eq!( |
| 204 |
bevel_shadow(Bevel::Inset, &opts), |
| 205 |
"inset 1px 1px 0 var(--bevel-dark), inset -1px -1px 0 var(--bevel-light)" |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn no_colour_ever_reaches_the_output() { |
| 211 |
let css = stylesheet(&Emit::default()); |
| 212 |
assert!(!css.contains('#'), "a hex literal escaped into the CSS"); |
| 213 |
assert!( |
| 214 |
!css.contains("rgb"), |
| 215 |
"a colour function escaped into the CSS" |
| 216 |
); |
| 217 |
|
| 218 |
assert!(css.contains("var(--surface-raised)")); |
| 219 |
assert!(css.contains("var(--bevel-light)")); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn a_well_falls_back_through_css_rather_than_through_rust() { |
| 224 |
assert_eq!( |
| 225 |
fill_var(Fill::Well), |
| 226 |
"var(--surface-well, var(--surface-page))" |
| 227 |
); |
| 228 |
|
| 229 |
assert_eq!(fill_var(Fill::Raised), "var(--surface-raised)"); |
| 230 |
assert_eq!(fill_var(Fill::Page), "var(--surface-page)"); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn raised_and_well_do_not_collapse_onto_each_other() { |
| 235 |
let css = depth_rules(&Emit::default()); |
| 236 |
assert!(css.contains(".raised {")); |
| 237 |
assert!(css.contains(".well {")); |
| 238 |
assert!(css.contains("var(--bevel-raised)")); |
| 239 |
assert!(css.contains("var(--bevel-inset)")); |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn the_cascade_carries_the_pressed_state() { |
| 244 |
let css = depth_rules(&Emit::default()); |
| 245 |
|
| 246 |
|
| 247 |
assert!(css.contains(".raised:active {")); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn flat_emits_nothing_at_all() { |
| 252 |
assert_eq!(depth_class(Depth::Flat, &Emit::default()), None); |
| 253 |
assert!(!depth_rules(&Emit::default()).contains("flat")); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn a_prefix_namespaces_every_class() { |
| 258 |
let opts = Emit { |
| 259 |
class_prefix: "mo-", |
| 260 |
..Emit::default() |
| 261 |
}; |
| 262 |
let css = depth_rules(&opts); |
| 263 |
assert!(css.contains(".mo-raised {")); |
| 264 |
assert!(css.contains(".mo-well {")); |
| 265 |
assert!(!css.contains(".raised {")); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn the_border_width_is_the_callers() { |
| 270 |
let opts = Emit { |
| 271 |
border_width: "2px", |
| 272 |
..Emit::default() |
| 273 |
}; |
| 274 |
assert!(bevel_shadow(Bevel::Raised, &opts).contains("inset 2px 2px 0")); |
| 275 |
} |
| 276 |
|
| 277 |
#[test] |
| 278 |
fn edges_agree_with_the_description() { |
| 279 |
|
| 280 |
|
| 281 |
let (tl, br) = Bevel::Raised.edges(); |
| 282 |
assert_eq!(tl.token(), Edge::Light.token()); |
| 283 |
assert_eq!(br.token(), Edge::Dark.token()); |
| 284 |
} |
| 285 |
} |
| 286 |
|