| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#![forbid(unsafe_code)] |
| 114 |
|
| 115 |
use std::fmt::Write as _; |
| 116 |
|
| 117 |
|
| 118 |
pub const DEFAULT_BASE_PX: u16 = 16; |
| 119 |
|
| 120 |
|
| 121 |
pub const BASE_TOKEN: &str = "geometry-base"; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 128 |
pub struct Ratio { |
| 129 |
|
| 130 |
pub numerator: u16, |
| 131 |
|
| 132 |
pub denominator: u16, |
| 133 |
} |
| 134 |
|
| 135 |
impl Ratio { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#[must_use] |
| 143 |
pub const fn px_at(self, base_px: u16) -> u16 { |
| 144 |
let (n, d) = (self.numerator as u32, self.denominator as u32); |
| 145 |
let scaled = base_px as u32 * n; |
| 146 |
|
| 147 |
((scaled * 2 + d) / (d * 2)) as u16 |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#[must_use] |
| 155 |
pub fn scale(self, base: f32) -> f32 { |
| 156 |
base * f32::from(self.numerator) / f32::from(self.denominator) |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#[must_use] |
| 172 |
pub fn quanta(self, base: f32, quantum: f32) -> u32 { |
| 173 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
let exact = self.scale(base) / quantum; |
| 177 |
if exact <= 0.0 { |
| 178 |
0 |
| 179 |
} else { |
| 180 |
|
| 181 |
exact.round() as u32 |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
#[must_use] |
| 192 |
pub fn quantize(self, base: f32, quantum: f32) -> f32 { |
| 193 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 194 |
return 0.0; |
| 195 |
} |
| 196 |
self.quanta(base, quantum) as f32 * quantum |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[must_use] |
| 204 |
pub fn css(self) -> String { |
| 205 |
match (self.numerator, self.denominator) { |
| 206 |
(n, d) if n == d => format!("var(--{BASE_TOKEN})"), |
| 207 |
(n, 1) => format!("calc(var(--{BASE_TOKEN}) * {n})"), |
| 208 |
(n, d) => format!("calc(var(--{BASE_TOKEN}) * {n} / {d})"), |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 228 |
pub struct Surface { |
| 229 |
|
| 230 |
pub base: f32, |
| 231 |
|
| 232 |
pub quantum: f32, |
| 233 |
} |
| 234 |
|
| 235 |
impl Surface { |
| 236 |
|
| 237 |
#[must_use] |
| 238 |
pub fn web() -> Self { |
| 239 |
Self { |
| 240 |
base: f32::from(DEFAULT_BASE_PX), |
| 241 |
quantum: 1.0, |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
#[must_use] |
| 252 |
pub fn terminal() -> Self { |
| 253 |
Self { |
| 254 |
base: 1.0, |
| 255 |
quantum: 1.0, |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
#[must_use] |
| 261 |
pub fn resolve(self, ratio: Ratio) -> f32 { |
| 262 |
ratio.quantize(self.base, self.quantum) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#[must_use] |
| 269 |
pub fn quanta(self, ratio: Ratio) -> u32 { |
| 270 |
ratio.quanta(self.base, self.quantum) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
#[must_use] |
| 278 |
pub fn gap(self, gap: Gap, density: Density) -> u32 { |
| 279 |
self.quanta(gap.step_at(density).ratio()) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 290 |
pub enum Density { |
| 291 |
|
| 292 |
#[default] |
| 293 |
Pointer, |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
Touch, |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 306 |
pub enum Gap { |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
Bound, |
| 311 |
|
| 312 |
|
| 313 |
Peer, |
| 314 |
|
| 315 |
|
| 316 |
Group, |
| 317 |
|
| 318 |
|
| 319 |
Section, |
| 320 |
|
| 321 |
Pane, |
| 322 |
|
| 323 |
Page, |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 332 |
pub enum Step { |
| 333 |
|
| 334 |
Hair, |
| 335 |
|
| 336 |
Tight, |
| 337 |
|
| 338 |
Snug, |
| 339 |
|
| 340 |
Base, |
| 341 |
|
| 342 |
Roomy, |
| 343 |
|
| 344 |
Wide, |
| 345 |
|
| 346 |
Loose, |
| 347 |
|
| 348 |
Broad, |
| 349 |
|
| 350 |
Vast, |
| 351 |
|
| 352 |
Colossal, |
| 353 |
} |
| 354 |
|
| 355 |
impl Step { |
| 356 |
|
| 357 |
#[must_use] |
| 358 |
pub const fn ratio(self) -> Ratio { |
| 359 |
let (numerator, denominator) = match self { |
| 360 |
Self::Hair => (1, 8), |
| 361 |
Self::Tight => (1, 4), |
| 362 |
Self::Snug => (3, 8), |
| 363 |
Self::Base => (1, 2), |
| 364 |
Self::Roomy => (5, 8), |
| 365 |
Self::Wide => (3, 4), |
| 366 |
Self::Loose => (1, 1), |
| 367 |
Self::Broad => (3, 2), |
| 368 |
Self::Vast => (2, 1), |
| 369 |
Self::Colossal => (3, 1), |
| 370 |
}; |
| 371 |
Ratio { |
| 372 |
numerator, |
| 373 |
denominator, |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
#[must_use] |
| 379 |
pub const fn px(self) -> u16 { |
| 380 |
self.ratio().px_at(DEFAULT_BASE_PX) |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
#[must_use] |
| 385 |
pub const fn token(self) -> &'static str { |
| 386 |
match self { |
| 387 |
Self::Hair => "step-hair", |
| 388 |
Self::Tight => "step-tight", |
| 389 |
Self::Snug => "step-snug", |
| 390 |
Self::Base => "step-base", |
| 391 |
Self::Roomy => "step-roomy", |
| 392 |
Self::Wide => "step-wide", |
| 393 |
Self::Loose => "step-loose", |
| 394 |
Self::Broad => "step-broad", |
| 395 |
Self::Vast => "step-vast", |
| 396 |
Self::Colossal => "step-colossal", |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
#[must_use] |
| 402 |
pub const fn all() -> [Self; 10] { |
| 403 |
[ |
| 404 |
Self::Hair, |
| 405 |
Self::Tight, |
| 406 |
Self::Snug, |
| 407 |
Self::Base, |
| 408 |
Self::Roomy, |
| 409 |
Self::Wide, |
| 410 |
Self::Loose, |
| 411 |
Self::Broad, |
| 412 |
Self::Vast, |
| 413 |
Self::Colossal, |
| 414 |
] |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
impl Gap { |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
#[must_use] |
| 425 |
pub const fn step_at(self, density: Density) -> Step { |
| 426 |
match self { |
| 427 |
|
| 428 |
|
| 429 |
Self::Bound => Step::Tight, |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
Self::Peer => match density { |
| 435 |
Density::Pointer => Step::Snug, |
| 436 |
Density::Touch => Step::Roomy, |
| 437 |
}, |
| 438 |
Self::Group => match density { |
| 439 |
Density::Pointer => Step::Roomy, |
| 440 |
Density::Touch => Step::Wide, |
| 441 |
}, |
| 442 |
Self::Section => match density { |
| 443 |
Density::Pointer => Step::Wide, |
| 444 |
Density::Touch => Step::Loose, |
| 445 |
}, |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
Self::Pane => Step::Broad, |
| 450 |
Self::Page => Step::Vast, |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
#[must_use] |
| 456 |
pub const fn step(self) -> Step { |
| 457 |
self.step_at(Density::Pointer) |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
#[must_use] |
| 462 |
pub const fn px_at(self, density: Density) -> u16 { |
| 463 |
self.step_at(density).px() |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
#[must_use] |
| 468 |
pub const fn px(self) -> u16 { |
| 469 |
self.step().px() |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
#[must_use] |
| 474 |
pub const fn token(self) -> &'static str { |
| 475 |
match self { |
| 476 |
Self::Bound => "gap-bound", |
| 477 |
Self::Peer => "gap-peer", |
| 478 |
Self::Group => "gap-group", |
| 479 |
Self::Section => "gap-section", |
| 480 |
Self::Pane => "gap-pane", |
| 481 |
Self::Page => "gap-page", |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
#[must_use] |
| 487 |
pub const fn all() -> [Self; 6] { |
| 488 |
[ |
| 489 |
Self::Bound, |
| 490 |
Self::Peer, |
| 491 |
Self::Group, |
| 492 |
Self::Section, |
| 493 |
Self::Pane, |
| 494 |
Self::Page, |
| 495 |
] |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
#[must_use] |
| 504 |
pub fn scale_css_declarations() -> String { |
| 505 |
let mut out = String::new(); |
| 506 |
let _ = writeln!( |
| 507 |
out, |
| 508 |
" /* Every size below is a ratio of this. Scale it and the whole\n \ |
| 509 |
layout scales with it, including for a user who has asked for\n \ |
| 510 |
larger text. */\n --{BASE_TOKEN}: 1rem;\n" |
| 511 |
); |
| 512 |
out.push_str(" /* Raw scale. Prefer a --gap-* below; reach here only when\n"); |
| 513 |
out.push_str(" no relationship describes the distance. */\n"); |
| 514 |
for step in Step::all() { |
| 515 |
let _ = writeln!(out, " --{}: {};", step.token(), step.ratio().css()); |
| 516 |
} |
| 517 |
out |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
#[must_use] |
| 525 |
pub fn gap_css_declarations(density: Density) -> String { |
| 526 |
let mut out = String::new(); |
| 527 |
for gap in Gap::all() { |
| 528 |
let _ = writeln!( |
| 529 |
out, |
| 530 |
" --{}: var(--{});", |
| 531 |
gap.token(), |
| 532 |
gap.step_at(density).token() |
| 533 |
); |
| 534 |
} |
| 535 |
out |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
#[must_use] |
| 544 |
pub fn geometry_css_vars(density: Density) -> String { |
| 545 |
format!( |
| 546 |
":root {{\n{}\n{}}}\n", |
| 547 |
scale_css_declarations(), |
| 548 |
gap_css_declarations(density) |
| 549 |
) |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
#[must_use] |
| 572 |
pub fn density_css(explicit_touch: Option<&str>) -> String { |
| 573 |
let mut css = geometry_css_vars(Density::Pointer); |
| 574 |
css.push_str("\n/* Touch: targets separate, shells hold. */\n"); |
| 575 |
css.push_str("@media (hover: none), (pointer: coarse) {\n"); |
| 576 |
for line in gap_css_overrides(":root", Density::Touch).lines() { |
| 577 |
css.push_str(" "); |
| 578 |
css.push_str(line); |
| 579 |
css.push('\n'); |
| 580 |
} |
| 581 |
css.push_str("}\n"); |
| 582 |
if let Some(selector) = explicit_touch { |
| 583 |
css.push_str("\n/* An explicit user choice, last so it wins over detection. */\n"); |
| 584 |
css.push_str(&gap_css_overrides(selector, Density::Touch)); |
| 585 |
} |
| 586 |
css |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
#[must_use] |
| 601 |
pub fn gap_css_overrides(selector: &str, density: Density) -> String { |
| 602 |
format!("{selector} {{\n{}}}\n", gap_css_declarations(density)) |
| 603 |
} |
| 604 |
|
| 605 |
#[cfg(test)] |
| 606 |
mod tests { |
| 607 |
use super::*; |
| 608 |
|
| 609 |
#[test] |
| 610 |
fn density_is_selected_by_capability_not_by_width_or_agent() { |
| 611 |
let css = density_css(None); |
| 612 |
assert!(css.contains("@media (hover: none), (pointer: coarse)")); |
| 613 |
|
| 614 |
assert!(!css.contains("max-width"), "a breakpoint crept in"); |
| 615 |
assert!(!css.contains("min-width"), "a breakpoint crept in"); |
| 616 |
assert!(!css.contains("ui-mode"), "a device mode crept in"); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn an_explicit_choice_is_emitted_after_the_detection() { |
| 621 |
let css = density_css(Some(".ui-mode-mobile")); |
| 622 |
let media = css.find("@media").expect("media query"); |
| 623 |
let explicit = css.find(".ui-mode-mobile").expect("explicit selector"); |
| 624 |
|
| 625 |
|
| 626 |
assert!(explicit > media, "the explicit selector must come last"); |
| 627 |
} |
| 628 |
|
| 629 |
#[test] |
| 630 |
fn without_an_explicit_selector_there_are_exactly_two_presets() { |
| 631 |
assert_eq!(density_css(None).matches("--gap-peer").count(), 2); |
| 632 |
} |
| 633 |
|
| 634 |
#[test] |
| 635 |
fn the_hig_relationships_land_on_the_hig_values() { |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
assert_eq!(Gap::Bound.px(), 4); |
| 640 |
assert_eq!(Gap::Peer.px(), 6); |
| 641 |
assert_eq!(Gap::Group.px(), 10); |
| 642 |
assert_eq!(Gap::Section.px(), 12); |
| 643 |
} |
| 644 |
|
| 645 |
#[test] |
| 646 |
fn every_ratio_divides_the_default_base_exactly() { |
| 647 |
for step in Step::all() { |
| 648 |
let r = step.ratio(); |
| 649 |
assert_eq!( |
| 650 |
u32::from(DEFAULT_BASE_PX) * u32::from(r.numerator) % u32::from(r.denominator), |
| 651 |
0, |
| 652 |
"{step:?} is fractional at the default base" |
| 653 |
); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
#[test] |
| 658 |
fn ratios_scale_linearly() { |
| 659 |
for step in Step::all() { |
| 660 |
assert_eq!( |
| 661 |
step.ratio().px_at(DEFAULT_BASE_PX * 2), |
| 662 |
step.px() * 2, |
| 663 |
"{step:?} does not double with the base" |
| 664 |
); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
#[test] |
| 669 |
fn steps_ascend_and_never_repeat() { |
| 670 |
let px: Vec<u16> = Step::all().iter().map(|s| s.px()).collect(); |
| 671 |
let mut sorted = px.clone(); |
| 672 |
sorted.sort_unstable(); |
| 673 |
sorted.dedup(); |
| 674 |
assert_eq!(px, sorted, "steps must be strictly ascending"); |
| 675 |
} |
| 676 |
|
| 677 |
#[test] |
| 678 |
fn gaps_ascend_with_their_relationships_at_every_density() { |
| 679 |
for density in [Density::Pointer, Density::Touch] { |
| 680 |
let px: Vec<u16> = Gap::all().iter().map(|g| g.px_at(density)).collect(); |
| 681 |
let mut sorted = px.clone(); |
| 682 |
sorted.sort_unstable(); |
| 683 |
assert_eq!(px, sorted, "{density:?}: a looser relationship is tighter"); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
#[test] |
| 688 |
fn touch_separates_targets_and_holds_the_shells() { |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
for gap in [Gap::Peer, Gap::Group, Gap::Section] { |
| 694 |
assert!( |
| 695 |
gap.px_at(Density::Touch) > gap.px_at(Density::Pointer), |
| 696 |
"{gap:?} separates tap targets and must open on touch" |
| 697 |
); |
| 698 |
} |
| 699 |
for gap in [Gap::Bound, Gap::Pane, Gap::Page] { |
| 700 |
assert_eq!( |
| 701 |
gap.px_at(Density::Touch), |
| 702 |
gap.px_at(Density::Pointer), |
| 703 |
"{gap:?} is not a tap target and must not move with the input device" |
| 704 |
); |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
#[test] |
| 709 |
fn touch_never_resolves_tighter_than_pointer() { |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
for gap in Gap::all() { |
| 720 |
assert!( |
| 721 |
gap.px_at(Density::Touch) >= gap.px_at(Density::Pointer), |
| 722 |
"{gap:?}: Touch resolved tighter than Pointer" |
| 723 |
); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
#[test] |
| 728 |
fn the_pointer_retune_is_not_blocked_by_touch() { |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
assert!( |
| 733 |
Gap::Section.px_at(Density::Touch) <= Gap::Pane.px_at(Density::Touch), |
| 734 |
"Touch inverted internally, which is what set the old floor" |
| 735 |
); |
| 736 |
|
| 737 |
|
| 738 |
assert!(Gap::Section.px_at(Density::Touch) >= 16); |
| 739 |
} |
| 740 |
|
| 741 |
#[test] |
| 742 |
fn a_terminal_resolves_the_vocabulary_to_whole_cells() { |
| 743 |
let t = Surface::terminal(); |
| 744 |
let cells: Vec<u32> = Gap::all() |
| 745 |
.iter() |
| 746 |
.map(|g| t.gap(*g, Density::Pointer)) |
| 747 |
.collect(); |
| 748 |
|
| 749 |
assert_eq!(cells, vec![0, 0, 1, 1, 2, 2]); |
| 750 |
} |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn collapsing_is_allowed_but_inverting_is_not() { |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
for quantum in [0.5_f32, 1.0, 2.0, 3.0, 7.0] { |
| 758 |
for density in [Density::Pointer, Density::Touch] { |
| 759 |
let s = Surface { |
| 760 |
base: 16.0, |
| 761 |
quantum, |
| 762 |
}; |
| 763 |
let v: Vec<u32> = Gap::all().iter().map(|g| s.gap(*g, density)).collect(); |
| 764 |
let mut sorted = v.clone(); |
| 765 |
sorted.sort_unstable(); |
| 766 |
assert_eq!(v, sorted, "quantum {quantum} {density:?} inverted: {v:?}"); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
#[test] |
| 772 |
fn the_web_surface_agrees_with_the_pixel_helper() { |
| 773 |
let w = Surface::web(); |
| 774 |
for step in Step::all() { |
| 775 |
assert_eq!( |
| 776 |
w.resolve(step.ratio()) as u16, |
| 777 |
step.px(), |
| 778 |
"{step:?} disagrees between surface and px_at" |
| 779 |
); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
#[test] |
| 784 |
fn quantising_is_monotonic_in_the_ratio() { |
| 785 |
let (base, quantum) = (16.0, 1.0); |
| 786 |
let mut previous = 0; |
| 787 |
for step in Step::all() { |
| 788 |
let q = step.ratio().quanta(base, quantum); |
| 789 |
assert!(q >= previous, "{step:?} went backwards"); |
| 790 |
previous = q; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
#[test] |
| 795 |
fn a_degenerate_quantum_yields_nothing_rather_than_panicking() { |
| 796 |
let r = Step::Loose.ratio(); |
| 797 |
for bad in [0.0_f32, -1.0, f32::NAN] { |
| 798 |
assert_eq!(r.quanta(16.0, bad), 0); |
| 799 |
assert!(r.quantize(16.0, bad).abs() < f32::EPSILON); |
| 800 |
} |
| 801 |
assert_eq!(r.quanta(f32::INFINITY, 1.0), 0); |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
fn px_at_rounds_rather_than_truncating() { |
| 806 |
|
| 807 |
|
| 808 |
assert_eq!(Step::Snug.ratio().px_at(15), 6); |
| 809 |
assert_eq!(Step::Snug.ratio().px_at(DEFAULT_BASE_PX), 6); |
| 810 |
} |
| 811 |
|
| 812 |
#[test] |
| 813 |
fn tokens_are_unique() { |
| 814 |
let mut names: Vec<&str> = Step::all().iter().map(|s| s.token()).collect(); |
| 815 |
names.extend(Gap::all().iter().map(|g| g.token())); |
| 816 |
let count = names.len(); |
| 817 |
names.sort_unstable(); |
| 818 |
names.dedup(); |
| 819 |
assert_eq!(names.len(), count, "token names collide"); |
| 820 |
} |
| 821 |
|
| 822 |
#[test] |
| 823 |
fn css_is_expressed_over_the_base_never_in_pixels() { |
| 824 |
let css = geometry_css_vars(Density::Pointer); |
| 825 |
assert!(css.starts_with(":root {\n")); |
| 826 |
assert!(css.trim_end().ends_with('}')); |
| 827 |
assert!(css.contains("--geometry-base: 1rem;")); |
| 828 |
for step in Step::all() { |
| 829 |
let line = format!("--{}: {}", step.token(), step.ratio().css()); |
| 830 |
assert!(css.contains(&line), "missing or wrong: {line}"); |
| 831 |
} |
| 832 |
|
| 833 |
let scale = scale_css_declarations(); |
| 834 |
assert!( |
| 835 |
!scale.contains("px;"), |
| 836 |
"the scale must not emit pixel literals:\n{scale}" |
| 837 |
); |
| 838 |
} |
| 839 |
|
| 840 |
#[test] |
| 841 |
fn ratio_css_drops_redundant_arithmetic() { |
| 842 |
assert_eq!(Step::Loose.ratio().css(), "var(--geometry-base)"); |
| 843 |
assert_eq!(Step::Vast.ratio().css(), "calc(var(--geometry-base) * 2)"); |
| 844 |
assert_eq!( |
| 845 |
Step::Snug.ratio().css(), |
| 846 |
"calc(var(--geometry-base) * 3 / 8)" |
| 847 |
); |
| 848 |
} |
| 849 |
|
| 850 |
#[test] |
| 851 |
fn gaps_reference_steps_rather_than_repeating_values() { |
| 852 |
let css = geometry_css_vars(Density::Pointer); |
| 853 |
assert!(css.contains("--gap-peer: var(--step-snug);")); |
| 854 |
assert!(!css.contains("--gap-peer: calc")); |
| 855 |
} |
| 856 |
|
| 857 |
#[test] |
| 858 |
fn a_density_override_emits_only_the_relational_layer() { |
| 859 |
let css = gap_css_overrides(".ui-mode-mobile", Density::Touch); |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
assert!(css.contains("--gap-peer: var(--step-")); |
| 864 |
|
| 865 |
|
| 866 |
let declared: Vec<&str> = css |
| 867 |
.lines() |
| 868 |
.filter_map(|l| l.trim().strip_prefix("--")) |
| 869 |
.filter_map(|l| l.split(':').next()) |
| 870 |
.collect(); |
| 871 |
assert!( |
| 872 |
declared.iter().all(|t| t.starts_with("gap-")), |
| 873 |
"only the relational layer may be overridden, got {declared:?}" |
| 874 |
); |
| 875 |
} |
| 876 |
} |
| 877 |
|