| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#![forbid(unsafe_code)] |
| 135 |
|
| 136 |
use std::fmt::Write as _; |
| 137 |
|
| 138 |
|
| 139 |
pub const DEFAULT_BASE_PX: u16 = 16; |
| 140 |
|
| 141 |
|
| 142 |
pub const BASE_TOKEN: &str = "geometry-base"; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 149 |
pub struct Ratio { |
| 150 |
|
| 151 |
pub numerator: u16, |
| 152 |
|
| 153 |
pub denominator: u16, |
| 154 |
} |
| 155 |
|
| 156 |
impl Ratio { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#[must_use] |
| 164 |
pub const fn px_at(self, base_px: u16) -> u16 { |
| 165 |
let (n, d) = (self.numerator as u32, self.denominator as u32); |
| 166 |
let scaled = base_px as u32 * n; |
| 167 |
|
| 168 |
((scaled * 2 + d) / (d * 2)) as u16 |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#[must_use] |
| 176 |
pub fn scale(self, base: f32) -> f32 { |
| 177 |
base * f32::from(self.numerator) / f32::from(self.denominator) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#[must_use] |
| 193 |
pub fn quanta(self, base: f32, quantum: f32) -> u32 { |
| 194 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 195 |
return 0; |
| 196 |
} |
| 197 |
let exact = self.scale(base) / quantum; |
| 198 |
if exact <= 0.0 { |
| 199 |
0 |
| 200 |
} else { |
| 201 |
|
| 202 |
exact.round() as u32 |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[must_use] |
| 213 |
pub fn quantize(self, base: f32, quantum: f32) -> f32 { |
| 214 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 215 |
return 0.0; |
| 216 |
} |
| 217 |
self.quanta(base, quantum) as f32 * quantum |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#[must_use] |
| 225 |
pub fn css(self) -> String { |
| 226 |
match (self.numerator, self.denominator) { |
| 227 |
(n, d) if n == d => format!("var(--{BASE_TOKEN})"), |
| 228 |
(n, 1) => format!("calc(var(--{BASE_TOKEN}) * {n})"), |
| 229 |
(n, d) => format!("calc(var(--{BASE_TOKEN}) * {n} / {d})"), |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 249 |
pub struct Surface { |
| 250 |
|
| 251 |
pub base: f32, |
| 252 |
|
| 253 |
pub quantum: f32, |
| 254 |
} |
| 255 |
|
| 256 |
impl Surface { |
| 257 |
|
| 258 |
#[must_use] |
| 259 |
pub fn web() -> Self { |
| 260 |
Self { |
| 261 |
base: f32::from(DEFAULT_BASE_PX), |
| 262 |
quantum: 1.0, |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#[must_use] |
| 273 |
pub fn terminal() -> Self { |
| 274 |
Self { |
| 275 |
base: 1.0, |
| 276 |
quantum: 1.0, |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
#[must_use] |
| 282 |
pub fn resolve(self, ratio: Ratio) -> f32 { |
| 283 |
ratio.quantize(self.base, self.quantum) |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#[must_use] |
| 290 |
pub fn quanta(self, ratio: Ratio) -> u32 { |
| 291 |
ratio.quanta(self.base, self.quantum) |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
#[must_use] |
| 299 |
pub fn gap(self, gap: Gap, density: Density) -> u32 { |
| 300 |
self.quanta(gap.step_at(density).ratio()) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 311 |
pub enum Density { |
| 312 |
|
| 313 |
#[default] |
| 314 |
Pointer, |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
Touch, |
| 320 |
} |
| 321 |
|
| 322 |
impl Density { |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
#[must_use] |
| 341 |
pub const fn media_condition(self) -> &'static str { |
| 342 |
match self { |
| 343 |
Self::Pointer => "(hover: hover) and (pointer: fine)", |
| 344 |
Self::Touch => "(hover: none), (pointer: coarse)", |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 372 |
pub enum SizeClass { |
| 373 |
|
| 374 |
|
| 375 |
Compact, |
| 376 |
|
| 377 |
#[default] |
| 378 |
Medium, |
| 379 |
|
| 380 |
Expanded, |
| 381 |
} |
| 382 |
|
| 383 |
impl SizeClass { |
| 384 |
|
| 385 |
#[must_use] |
| 386 |
pub const fn min_px(self) -> u16 { |
| 387 |
match self { |
| 388 |
Self::Compact => 0, |
| 389 |
Self::Medium => 600, |
| 390 |
Self::Expanded => 840, |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
#[must_use] |
| 400 |
pub fn media_condition(self) -> String { |
| 401 |
match self { |
| 402 |
Self::Compact => format!("(max-width: {}px)", Self::Medium.min_px() - 1), |
| 403 |
Self::Medium => format!( |
| 404 |
"(min-width: {}px) and (max-width: {}px)", |
| 405 |
Self::Medium.min_px(), |
| 406 |
Self::Expanded.min_px() - 1 |
| 407 |
), |
| 408 |
Self::Expanded => format!("(min-width: {}px)", Self::Expanded.min_px()), |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
#[must_use] |
| 414 |
pub const fn token(self) -> &'static str { |
| 415 |
match self { |
| 416 |
Self::Compact => "size-compact", |
| 417 |
Self::Medium => "size-medium", |
| 418 |
Self::Expanded => "size-expanded", |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
#[must_use] |
| 424 |
pub const fn all() -> [Self; 3] { |
| 425 |
[Self::Compact, Self::Medium, Self::Expanded] |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
#[must_use] |
| 430 |
pub const fn at_width(px: u16) -> Self { |
| 431 |
if px >= Self::Expanded.min_px() { |
| 432 |
Self::Expanded |
| 433 |
} else if px >= Self::Medium.min_px() { |
| 434 |
Self::Medium |
| 435 |
} else { |
| 436 |
Self::Compact |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 446 |
pub enum Gap { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
Bound, |
| 451 |
|
| 452 |
|
| 453 |
Peer, |
| 454 |
|
| 455 |
|
| 456 |
Group, |
| 457 |
|
| 458 |
|
| 459 |
Section, |
| 460 |
|
| 461 |
Pane, |
| 462 |
|
| 463 |
Page, |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 472 |
pub enum Step { |
| 473 |
|
| 474 |
Hair, |
| 475 |
|
| 476 |
Tight, |
| 477 |
|
| 478 |
Snug, |
| 479 |
|
| 480 |
Base, |
| 481 |
|
| 482 |
Roomy, |
| 483 |
|
| 484 |
Wide, |
| 485 |
|
| 486 |
Loose, |
| 487 |
|
| 488 |
Broad, |
| 489 |
|
| 490 |
Vast, |
| 491 |
|
| 492 |
Colossal, |
| 493 |
} |
| 494 |
|
| 495 |
impl Step { |
| 496 |
|
| 497 |
#[must_use] |
| 498 |
pub const fn ratio(self) -> Ratio { |
| 499 |
let (numerator, denominator) = match self { |
| 500 |
Self::Hair => (1, 8), |
| 501 |
Self::Tight => (1, 4), |
| 502 |
Self::Snug => (3, 8), |
| 503 |
Self::Base => (1, 2), |
| 504 |
Self::Roomy => (5, 8), |
| 505 |
Self::Wide => (3, 4), |
| 506 |
Self::Loose => (1, 1), |
| 507 |
Self::Broad => (3, 2), |
| 508 |
Self::Vast => (2, 1), |
| 509 |
Self::Colossal => (3, 1), |
| 510 |
}; |
| 511 |
Ratio { |
| 512 |
numerator, |
| 513 |
denominator, |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
#[must_use] |
| 519 |
pub const fn px(self) -> u16 { |
| 520 |
self.ratio().px_at(DEFAULT_BASE_PX) |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
#[must_use] |
| 525 |
pub const fn token(self) -> &'static str { |
| 526 |
match self { |
| 527 |
Self::Hair => "step-hair", |
| 528 |
Self::Tight => "step-tight", |
| 529 |
Self::Snug => "step-snug", |
| 530 |
Self::Base => "step-base", |
| 531 |
Self::Roomy => "step-roomy", |
| 532 |
Self::Wide => "step-wide", |
| 533 |
Self::Loose => "step-loose", |
| 534 |
Self::Broad => "step-broad", |
| 535 |
Self::Vast => "step-vast", |
| 536 |
Self::Colossal => "step-colossal", |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
#[must_use] |
| 542 |
pub const fn all() -> [Self; 10] { |
| 543 |
[ |
| 544 |
Self::Hair, |
| 545 |
Self::Tight, |
| 546 |
Self::Snug, |
| 547 |
Self::Base, |
| 548 |
Self::Roomy, |
| 549 |
Self::Wide, |
| 550 |
Self::Loose, |
| 551 |
Self::Broad, |
| 552 |
Self::Vast, |
| 553 |
Self::Colossal, |
| 554 |
] |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
impl Gap { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
#[must_use] |
| 565 |
pub const fn step_at(self, density: Density) -> Step { |
| 566 |
match self { |
| 567 |
|
| 568 |
|
| 569 |
Self::Bound => Step::Tight, |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
Self::Peer => match density { |
| 575 |
Density::Pointer => Step::Snug, |
| 576 |
Density::Touch => Step::Roomy, |
| 577 |
}, |
| 578 |
Self::Group => match density { |
| 579 |
Density::Pointer => Step::Roomy, |
| 580 |
Density::Touch => Step::Wide, |
| 581 |
}, |
| 582 |
Self::Section => match density { |
| 583 |
Density::Pointer => Step::Wide, |
| 584 |
Density::Touch => Step::Loose, |
| 585 |
}, |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
Self::Pane => Step::Broad, |
| 590 |
Self::Page => Step::Vast, |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
#[must_use] |
| 596 |
pub const fn step(self) -> Step { |
| 597 |
self.step_at(Density::Pointer) |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
#[must_use] |
| 602 |
pub const fn px_at(self, density: Density) -> u16 { |
| 603 |
self.step_at(density).px() |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
#[must_use] |
| 608 |
pub const fn px(self) -> u16 { |
| 609 |
self.step().px() |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
#[must_use] |
| 614 |
pub const fn token(self) -> &'static str { |
| 615 |
match self { |
| 616 |
Self::Bound => "gap-bound", |
| 617 |
Self::Peer => "gap-peer", |
| 618 |
Self::Group => "gap-group", |
| 619 |
Self::Section => "gap-section", |
| 620 |
Self::Pane => "gap-pane", |
| 621 |
Self::Page => "gap-page", |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
#[must_use] |
| 627 |
pub const fn all() -> [Self; 6] { |
| 628 |
[ |
| 629 |
Self::Bound, |
| 630 |
Self::Peer, |
| 631 |
Self::Group, |
| 632 |
Self::Section, |
| 633 |
Self::Pane, |
| 634 |
Self::Page, |
| 635 |
] |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
#[must_use] |
| 644 |
pub fn scale_css_declarations() -> String { |
| 645 |
let mut out = String::new(); |
| 646 |
let _ = writeln!( |
| 647 |
out, |
| 648 |
" /* Every size below is a ratio of this. Scale it and the whole\n \ |
| 649 |
layout scales with it, including for a user who has asked for\n \ |
| 650 |
larger text. */\n --{BASE_TOKEN}: 1rem;\n" |
| 651 |
); |
| 652 |
out.push_str(" /* Raw scale. Prefer a --gap-* below; reach here only when\n"); |
| 653 |
out.push_str(" no relationship describes the distance. */\n"); |
| 654 |
for step in Step::all() { |
| 655 |
let _ = writeln!(out, " --{}: {};", step.token(), step.ratio().css()); |
| 656 |
} |
| 657 |
out |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
#[must_use] |
| 665 |
pub fn gap_css_declarations(density: Density) -> String { |
| 666 |
let mut out = String::new(); |
| 667 |
for gap in Gap::all() { |
| 668 |
let _ = writeln!( |
| 669 |
out, |
| 670 |
" --{}: var(--{});", |
| 671 |
gap.token(), |
| 672 |
gap.step_at(density).token() |
| 673 |
); |
| 674 |
} |
| 675 |
out |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
#[must_use] |
| 684 |
pub fn geometry_css_vars(density: Density) -> String { |
| 685 |
format!( |
| 686 |
":root {{\n{}\n{}}}\n", |
| 687 |
scale_css_declarations(), |
| 688 |
gap_css_declarations(density) |
| 689 |
) |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
#[must_use] |
| 712 |
pub fn density_css(explicit_touch: Option<&str>) -> String { |
| 713 |
let mut css = geometry_css_vars(Density::Pointer); |
| 714 |
css.push_str("\n/* Touch: targets separate, shells hold. */\n"); |
| 715 |
css.push_str("@media "); |
| 716 |
css.push_str(Density::Touch.media_condition()); |
| 717 |
css.push_str(" {\n"); |
| 718 |
for line in gap_css_overrides(":root", Density::Touch).lines() { |
| 719 |
css.push_str(" "); |
| 720 |
css.push_str(line); |
| 721 |
css.push('\n'); |
| 722 |
} |
| 723 |
css.push_str("}\n"); |
| 724 |
if let Some(selector) = explicit_touch { |
| 725 |
css.push_str("\n/* An explicit user choice, last so it wins over detection. */\n"); |
| 726 |
css.push_str(&gap_css_overrides(selector, Density::Touch)); |
| 727 |
} |
| 728 |
css |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
#[must_use] |
| 743 |
pub fn gap_css_overrides(selector: &str, density: Density) -> String { |
| 744 |
format!("{selector} {{\n{}}}\n", gap_css_declarations(density)) |
| 745 |
} |
| 746 |
|
| 747 |
#[cfg(test)] |
| 748 |
mod tests { |
| 749 |
use super::*; |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn density_is_selected_by_capability_not_by_width_or_agent() { |
| 753 |
let css = density_css(None); |
| 754 |
assert!(css.contains("@media (hover: none), (pointer: coarse)")); |
| 755 |
|
| 756 |
assert!(!css.contains("max-width"), "a breakpoint crept in"); |
| 757 |
assert!(!css.contains("min-width"), "a breakpoint crept in"); |
| 758 |
assert!(!css.contains("ui-mode"), "a device mode crept in"); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn the_two_density_conditions_are_complements_and_not_negations() { |
| 763 |
let pointer = Density::Pointer.media_condition(); |
| 764 |
let touch = Density::Touch.media_condition(); |
| 765 |
|
| 766 |
|
| 767 |
assert!(pointer.contains("hover: hover") && touch.contains("hover: none")); |
| 768 |
assert!(pointer.contains("pointer: fine") && touch.contains("pointer: coarse")); |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
assert!(touch.contains(", "), "touch must be an OR"); |
| 774 |
assert!(pointer.contains(" and "), "pointer must be an AND"); |
| 775 |
assert!(!pointer.contains(','), "pointer must not be an OR"); |
| 776 |
} |
| 777 |
|
| 778 |
#[test] |
| 779 |
fn the_emitted_touch_block_is_the_condition_and_not_a_second_copy_of_it() { |
| 780 |
|
| 781 |
let css = density_css(None); |
| 782 |
assert!(css.contains(&format!("@media {}", Density::Touch.media_condition()))); |
| 783 |
} |
| 784 |
|
| 785 |
#[test] |
| 786 |
fn an_explicit_choice_is_emitted_after_the_detection() { |
| 787 |
let css = density_css(Some(".ui-mode-mobile")); |
| 788 |
let media = css.find("@media").expect("media query"); |
| 789 |
let explicit = css.find(".ui-mode-mobile").expect("explicit selector"); |
| 790 |
|
| 791 |
|
| 792 |
assert!(explicit > media, "the explicit selector must come last"); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
fn without_an_explicit_selector_there_are_exactly_two_presets() { |
| 797 |
assert_eq!(density_css(None).matches("--gap-peer").count(), 2); |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
fn the_hig_relationships_land_on_the_hig_values() { |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
assert_eq!(Gap::Bound.px(), 4); |
| 806 |
assert_eq!(Gap::Peer.px(), 6); |
| 807 |
assert_eq!(Gap::Group.px(), 10); |
| 808 |
assert_eq!(Gap::Section.px(), 12); |
| 809 |
} |
| 810 |
|
| 811 |
#[test] |
| 812 |
fn every_ratio_divides_the_default_base_exactly() { |
| 813 |
for step in Step::all() { |
| 814 |
let r = step.ratio(); |
| 815 |
assert_eq!( |
| 816 |
u32::from(DEFAULT_BASE_PX) * u32::from(r.numerator) % u32::from(r.denominator), |
| 817 |
0, |
| 818 |
"{step:?} is fractional at the default base" |
| 819 |
); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
#[test] |
| 824 |
fn ratios_scale_linearly() { |
| 825 |
for step in Step::all() { |
| 826 |
assert_eq!( |
| 827 |
step.ratio().px_at(DEFAULT_BASE_PX * 2), |
| 828 |
step.px() * 2, |
| 829 |
"{step:?} does not double with the base" |
| 830 |
); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
#[test] |
| 835 |
fn steps_ascend_and_never_repeat() { |
| 836 |
let px: Vec<u16> = Step::all().iter().map(|s| s.px()).collect(); |
| 837 |
let mut sorted = px.clone(); |
| 838 |
sorted.sort_unstable(); |
| 839 |
sorted.dedup(); |
| 840 |
assert_eq!(px, sorted, "steps must be strictly ascending"); |
| 841 |
} |
| 842 |
|
| 843 |
#[test] |
| 844 |
fn gaps_ascend_with_their_relationships_at_every_density() { |
| 845 |
for density in [Density::Pointer, Density::Touch] { |
| 846 |
let px: Vec<u16> = Gap::all().iter().map(|g| g.px_at(density)).collect(); |
| 847 |
let mut sorted = px.clone(); |
| 848 |
sorted.sort_unstable(); |
| 849 |
assert_eq!(px, sorted, "{density:?}: a looser relationship is tighter"); |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
#[test] |
| 854 |
fn touch_separates_targets_and_holds_the_shells() { |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
for gap in [Gap::Peer, Gap::Group, Gap::Section] { |
| 860 |
assert!( |
| 861 |
gap.px_at(Density::Touch) > gap.px_at(Density::Pointer), |
| 862 |
"{gap:?} separates tap targets and must open on touch" |
| 863 |
); |
| 864 |
} |
| 865 |
for gap in [Gap::Bound, Gap::Pane, Gap::Page] { |
| 866 |
assert_eq!( |
| 867 |
gap.px_at(Density::Touch), |
| 868 |
gap.px_at(Density::Pointer), |
| 869 |
"{gap:?} is not a tap target and must not move with the input device" |
| 870 |
); |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
#[test] |
| 875 |
fn touch_never_resolves_tighter_than_pointer() { |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
for gap in Gap::all() { |
| 886 |
assert!( |
| 887 |
gap.px_at(Density::Touch) >= gap.px_at(Density::Pointer), |
| 888 |
"{gap:?}: Touch resolved tighter than Pointer" |
| 889 |
); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
#[test] |
| 894 |
fn the_pointer_retune_is_not_blocked_by_touch() { |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
assert!( |
| 899 |
Gap::Section.px_at(Density::Touch) <= Gap::Pane.px_at(Density::Touch), |
| 900 |
"Touch inverted internally, which is what set the old floor" |
| 901 |
); |
| 902 |
|
| 903 |
|
| 904 |
assert!(Gap::Section.px_at(Density::Touch) >= 16); |
| 905 |
} |
| 906 |
|
| 907 |
#[test] |
| 908 |
fn size_classes_partition_every_width_exactly_once() { |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
for px in 0..=4000u16 { |
| 913 |
let hits: Vec<SizeClass> = SizeClass::all() |
| 914 |
.into_iter() |
| 915 |
.filter(|c| { |
| 916 |
let lo = c.min_px(); |
| 917 |
let hi = match c { |
| 918 |
SizeClass::Compact => SizeClass::Medium.min_px() - 1, |
| 919 |
SizeClass::Medium => SizeClass::Expanded.min_px() - 1, |
| 920 |
SizeClass::Expanded => u16::MAX, |
| 921 |
}; |
| 922 |
px >= lo && px <= hi |
| 923 |
}) |
| 924 |
.collect(); |
| 925 |
assert_eq!(hits.len(), 1, "{px}px matched {hits:?}"); |
| 926 |
assert_eq!(hits[0], SizeClass::at_width(px), "{px}px disagrees"); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn the_quoted_boundaries_are_the_ones_material_publishes() { |
| 932 |
|
| 933 |
|
| 934 |
assert_eq!(SizeClass::Compact.min_px(), 0); |
| 935 |
assert_eq!(SizeClass::Medium.min_px(), 600); |
| 936 |
assert_eq!(SizeClass::Expanded.min_px(), 840); |
| 937 |
} |
| 938 |
|
| 939 |
#[test] |
| 940 |
fn the_media_conditions_do_not_overlap_at_the_boundary() { |
| 941 |
|
| 942 |
|
| 943 |
assert_eq!( |
| 944 |
SizeClass::Compact.media_condition(), |
| 945 |
"(max-width: 599px)", |
| 946 |
"Compact must stop one pixel below Medium" |
| 947 |
); |
| 948 |
assert_eq!( |
| 949 |
SizeClass::Medium.media_condition(), |
| 950 |
"(min-width: 600px) and (max-width: 839px)" |
| 951 |
); |
| 952 |
assert_eq!(SizeClass::Expanded.media_condition(), "(min-width: 840px)"); |
| 953 |
} |
| 954 |
|
| 955 |
#[test] |
| 956 |
fn size_class_does_not_reach_the_gap_scale() { |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
let _ = geometry_css_vars(Density::Pointer); |
| 964 |
let _ = Gap::Page.px_at(Density::Touch); |
| 965 |
assert_eq!(SizeClass::all().len(), 3); |
| 966 |
} |
| 967 |
|
| 968 |
#[test] |
| 969 |
fn a_terminal_resolves_the_vocabulary_to_whole_cells() { |
| 970 |
let t = Surface::terminal(); |
| 971 |
let cells: Vec<u32> = Gap::all() |
| 972 |
.iter() |
| 973 |
.map(|g| t.gap(*g, Density::Pointer)) |
| 974 |
.collect(); |
| 975 |
|
| 976 |
assert_eq!(cells, vec![0, 0, 1, 1, 2, 2]); |
| 977 |
} |
| 978 |
|
| 979 |
#[test] |
| 980 |
fn collapsing_is_allowed_but_inverting_is_not() { |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
for quantum in [0.5_f32, 1.0, 2.0, 3.0, 7.0] { |
| 985 |
for density in [Density::Pointer, Density::Touch] { |
| 986 |
let s = Surface { |
| 987 |
base: 16.0, |
| 988 |
quantum, |
| 989 |
}; |
| 990 |
let v: Vec<u32> = Gap::all().iter().map(|g| s.gap(*g, density)).collect(); |
| 991 |
let mut sorted = v.clone(); |
| 992 |
sorted.sort_unstable(); |
| 993 |
assert_eq!(v, sorted, "quantum {quantum} {density:?} inverted: {v:?}"); |
| 994 |
} |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
#[test] |
| 999 |
fn the_web_surface_agrees_with_the_pixel_helper() { |
| 1000 |
let w = Surface::web(); |
| 1001 |
for step in Step::all() { |
| 1002 |
assert_eq!( |
| 1003 |
w.resolve(step.ratio()) as u16, |
| 1004 |
step.px(), |
| 1005 |
"{step:?} disagrees between surface and px_at" |
| 1006 |
); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
#[test] |
| 1011 |
fn quantising_is_monotonic_in_the_ratio() { |
| 1012 |
let (base, quantum) = (16.0, 1.0); |
| 1013 |
let mut previous = 0; |
| 1014 |
for step in Step::all() { |
| 1015 |
let q = step.ratio().quanta(base, quantum); |
| 1016 |
assert!(q >= previous, "{step:?} went backwards"); |
| 1017 |
previous = q; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
#[test] |
| 1022 |
fn a_degenerate_quantum_yields_nothing_rather_than_panicking() { |
| 1023 |
let r = Step::Loose.ratio(); |
| 1024 |
for bad in [0.0_f32, -1.0, f32::NAN] { |
| 1025 |
assert_eq!(r.quanta(16.0, bad), 0); |
| 1026 |
assert!(r.quantize(16.0, bad).abs() < f32::EPSILON); |
| 1027 |
} |
| 1028 |
assert_eq!(r.quanta(f32::INFINITY, 1.0), 0); |
| 1029 |
} |
| 1030 |
|
| 1031 |
#[test] |
| 1032 |
fn px_at_rounds_rather_than_truncating() { |
| 1033 |
|
| 1034 |
|
| 1035 |
assert_eq!(Step::Snug.ratio().px_at(15), 6); |
| 1036 |
assert_eq!(Step::Snug.ratio().px_at(DEFAULT_BASE_PX), 6); |
| 1037 |
} |
| 1038 |
|
| 1039 |
#[test] |
| 1040 |
fn tokens_are_unique() { |
| 1041 |
let mut names: Vec<&str> = Step::all().iter().map(|s| s.token()).collect(); |
| 1042 |
names.extend(Gap::all().iter().map(|g| g.token())); |
| 1043 |
let count = names.len(); |
| 1044 |
names.sort_unstable(); |
| 1045 |
names.dedup(); |
| 1046 |
assert_eq!(names.len(), count, "token names collide"); |
| 1047 |
} |
| 1048 |
|
| 1049 |
#[test] |
| 1050 |
fn css_is_expressed_over_the_base_never_in_pixels() { |
| 1051 |
let css = geometry_css_vars(Density::Pointer); |
| 1052 |
assert!(css.starts_with(":root {\n")); |
| 1053 |
assert!(css.trim_end().ends_with('}')); |
| 1054 |
assert!(css.contains("--geometry-base: 1rem;")); |
| 1055 |
for step in Step::all() { |
| 1056 |
let line = format!("--{}: {}", step.token(), step.ratio().css()); |
| 1057 |
assert!(css.contains(&line), "missing or wrong: {line}"); |
| 1058 |
} |
| 1059 |
|
| 1060 |
let scale = scale_css_declarations(); |
| 1061 |
assert!( |
| 1062 |
!scale.contains("px;"), |
| 1063 |
"the scale must not emit pixel literals:\n{scale}" |
| 1064 |
); |
| 1065 |
} |
| 1066 |
|
| 1067 |
#[test] |
| 1068 |
fn ratio_css_drops_redundant_arithmetic() { |
| 1069 |
assert_eq!(Step::Loose.ratio().css(), "var(--geometry-base)"); |
| 1070 |
assert_eq!(Step::Vast.ratio().css(), "calc(var(--geometry-base) * 2)"); |
| 1071 |
assert_eq!( |
| 1072 |
Step::Snug.ratio().css(), |
| 1073 |
"calc(var(--geometry-base) * 3 / 8)" |
| 1074 |
); |
| 1075 |
} |
| 1076 |
|
| 1077 |
#[test] |
| 1078 |
fn gaps_reference_steps_rather_than_repeating_values() { |
| 1079 |
let css = geometry_css_vars(Density::Pointer); |
| 1080 |
assert!(css.contains("--gap-peer: var(--step-snug);")); |
| 1081 |
assert!(!css.contains("--gap-peer: calc")); |
| 1082 |
} |
| 1083 |
|
| 1084 |
#[test] |
| 1085 |
fn a_density_override_emits_only_the_relational_layer() { |
| 1086 |
let css = gap_css_overrides(".ui-mode-mobile", Density::Touch); |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
assert!(css.contains("--gap-peer: var(--step-")); |
| 1091 |
|
| 1092 |
|
| 1093 |
let declared: Vec<&str> = css |
| 1094 |
.lines() |
| 1095 |
.filter_map(|l| l.trim().strip_prefix("--")) |
| 1096 |
.filter_map(|l| l.split(':').next()) |
| 1097 |
.collect(); |
| 1098 |
assert!( |
| 1099 |
declared.iter().all(|t| t.starts_with("gap-")), |
| 1100 |
"only the relational layer may be overridden, got {declared:?}" |
| 1101 |
); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|