| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
#![forbid(unsafe_code)] |
| 161 |
|
| 162 |
use std::fmt::Write as _; |
| 163 |
|
| 164 |
|
| 165 |
pub const DEFAULT_BASE_PX: u16 = 16; |
| 166 |
|
| 167 |
|
| 168 |
pub const BASE_TOKEN: &str = "geometry-base"; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 175 |
pub struct Ratio { |
| 176 |
|
| 177 |
pub numerator: u16, |
| 178 |
|
| 179 |
pub denominator: u16, |
| 180 |
} |
| 181 |
|
| 182 |
impl Ratio { |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
#[must_use] |
| 190 |
pub const fn px_at(self, base_px: u16) -> u16 { |
| 191 |
let (n, d) = (self.numerator as u32, self.denominator as u32); |
| 192 |
let scaled = base_px as u32 * n; |
| 193 |
|
| 194 |
((scaled * 2 + d) / (d * 2)) as u16 |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#[must_use] |
| 202 |
pub fn scale(self, base: f32) -> f32 { |
| 203 |
base * f32::from(self.numerator) / f32::from(self.denominator) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
#[must_use] |
| 219 |
pub fn quanta(self, base: f32, quantum: f32) -> u32 { |
| 220 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 221 |
return 0; |
| 222 |
} |
| 223 |
let exact = self.scale(base) / quantum; |
| 224 |
if exact <= 0.0 { |
| 225 |
0 |
| 226 |
} else { |
| 227 |
|
| 228 |
exact.round() as u32 |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#[must_use] |
| 239 |
pub fn quantize(self, base: f32, quantum: f32) -> f32 { |
| 240 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 241 |
return 0.0; |
| 242 |
} |
| 243 |
self.quanta(base, quantum) as f32 * quantum |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
#[must_use] |
| 251 |
pub fn css(self) -> String { |
| 252 |
match (self.numerator, self.denominator) { |
| 253 |
(n, d) if n == d => format!("var(--{BASE_TOKEN})"), |
| 254 |
(n, 1) => format!("calc(var(--{BASE_TOKEN}) * {n})"), |
| 255 |
(n, d) => format!("calc(var(--{BASE_TOKEN}) * {n} / {d})"), |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 275 |
pub struct Surface { |
| 276 |
|
| 277 |
pub base: f32, |
| 278 |
|
| 279 |
pub quantum: f32, |
| 280 |
} |
| 281 |
|
| 282 |
impl Surface { |
| 283 |
|
| 284 |
#[must_use] |
| 285 |
pub fn web() -> Self { |
| 286 |
Self { |
| 287 |
base: f32::from(DEFAULT_BASE_PX), |
| 288 |
quantum: 1.0, |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
#[must_use] |
| 299 |
pub fn terminal() -> Self { |
| 300 |
Self { |
| 301 |
base: 1.0, |
| 302 |
quantum: 1.0, |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
#[must_use] |
| 308 |
pub fn resolve(self, ratio: Ratio) -> f32 { |
| 309 |
ratio.quantize(self.base, self.quantum) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
#[must_use] |
| 316 |
pub fn quanta(self, ratio: Ratio) -> u32 { |
| 317 |
ratio.quanta(self.base, self.quantum) |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
#[must_use] |
| 325 |
pub fn gap(self, gap: Gap, density: Density) -> u32 { |
| 326 |
self.quanta(gap.step_at(density).ratio()) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 337 |
pub enum Density { |
| 338 |
|
| 339 |
#[default] |
| 340 |
Pointer, |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
Touch, |
| 346 |
} |
| 347 |
|
| 348 |
impl Density { |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
#[must_use] |
| 367 |
pub const fn media_condition(self) -> &'static str { |
| 368 |
match self { |
| 369 |
Self::Pointer => "(hover: hover) and (pointer: fine)", |
| 370 |
Self::Touch => "(hover: none), (pointer: coarse)", |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 398 |
pub enum SizeClass { |
| 399 |
|
| 400 |
|
| 401 |
Compact, |
| 402 |
|
| 403 |
#[default] |
| 404 |
Medium, |
| 405 |
|
| 406 |
Expanded, |
| 407 |
} |
| 408 |
|
| 409 |
impl SizeClass { |
| 410 |
|
| 411 |
#[must_use] |
| 412 |
pub const fn min_px(self) -> u16 { |
| 413 |
match self { |
| 414 |
Self::Compact => 0, |
| 415 |
Self::Medium => 600, |
| 416 |
Self::Expanded => 840, |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
#[must_use] |
| 426 |
pub fn media_condition(self) -> String { |
| 427 |
match self { |
| 428 |
Self::Compact => format!("(max-width: {}px)", Self::Medium.min_px() - 1), |
| 429 |
Self::Medium => format!( |
| 430 |
"(min-width: {}px) and (max-width: {}px)", |
| 431 |
Self::Medium.min_px(), |
| 432 |
Self::Expanded.min_px() - 1 |
| 433 |
), |
| 434 |
Self::Expanded => format!("(min-width: {}px)", Self::Expanded.min_px()), |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
#[must_use] |
| 440 |
pub const fn token(self) -> &'static str { |
| 441 |
match self { |
| 442 |
Self::Compact => "size-compact", |
| 443 |
Self::Medium => "size-medium", |
| 444 |
Self::Expanded => "size-expanded", |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
#[must_use] |
| 450 |
pub const fn all() -> [Self; 3] { |
| 451 |
[Self::Compact, Self::Medium, Self::Expanded] |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
#[must_use] |
| 456 |
pub const fn at_width(px: u16) -> Self { |
| 457 |
if px >= Self::Expanded.min_px() { |
| 458 |
Self::Expanded |
| 459 |
} else if px >= Self::Medium.min_px() { |
| 460 |
Self::Medium |
| 461 |
} else { |
| 462 |
Self::Compact |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 472 |
pub enum Gap { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
Bound, |
| 477 |
|
| 478 |
|
| 479 |
Peer, |
| 480 |
|
| 481 |
|
| 482 |
Group, |
| 483 |
|
| 484 |
|
| 485 |
Section, |
| 486 |
|
| 487 |
Pane, |
| 488 |
|
| 489 |
Page, |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 498 |
pub enum Step { |
| 499 |
|
| 500 |
Hair, |
| 501 |
|
| 502 |
Tight, |
| 503 |
|
| 504 |
Snug, |
| 505 |
|
| 506 |
Base, |
| 507 |
|
| 508 |
Roomy, |
| 509 |
|
| 510 |
Wide, |
| 511 |
|
| 512 |
Loose, |
| 513 |
|
| 514 |
Broad, |
| 515 |
|
| 516 |
Vast, |
| 517 |
|
| 518 |
Colossal, |
| 519 |
} |
| 520 |
|
| 521 |
impl Step { |
| 522 |
|
| 523 |
#[must_use] |
| 524 |
pub const fn ratio(self) -> Ratio { |
| 525 |
let (numerator, denominator) = match self { |
| 526 |
Self::Hair => (1, 8), |
| 527 |
Self::Tight => (1, 4), |
| 528 |
Self::Snug => (3, 8), |
| 529 |
Self::Base => (1, 2), |
| 530 |
Self::Roomy => (5, 8), |
| 531 |
Self::Wide => (3, 4), |
| 532 |
Self::Loose => (1, 1), |
| 533 |
Self::Broad => (3, 2), |
| 534 |
Self::Vast => (2, 1), |
| 535 |
Self::Colossal => (3, 1), |
| 536 |
}; |
| 537 |
Ratio { |
| 538 |
numerator, |
| 539 |
denominator, |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
#[must_use] |
| 545 |
pub const fn px(self) -> u16 { |
| 546 |
self.ratio().px_at(DEFAULT_BASE_PX) |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
#[must_use] |
| 551 |
pub const fn token(self) -> &'static str { |
| 552 |
match self { |
| 553 |
Self::Hair => "step-hair", |
| 554 |
Self::Tight => "step-tight", |
| 555 |
Self::Snug => "step-snug", |
| 556 |
Self::Base => "step-base", |
| 557 |
Self::Roomy => "step-roomy", |
| 558 |
Self::Wide => "step-wide", |
| 559 |
Self::Loose => "step-loose", |
| 560 |
Self::Broad => "step-broad", |
| 561 |
Self::Vast => "step-vast", |
| 562 |
Self::Colossal => "step-colossal", |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
#[must_use] |
| 568 |
pub const fn all() -> [Self; 10] { |
| 569 |
[ |
| 570 |
Self::Hair, |
| 571 |
Self::Tight, |
| 572 |
Self::Snug, |
| 573 |
Self::Base, |
| 574 |
Self::Roomy, |
| 575 |
Self::Wide, |
| 576 |
Self::Loose, |
| 577 |
Self::Broad, |
| 578 |
Self::Vast, |
| 579 |
Self::Colossal, |
| 580 |
] |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
impl Gap { |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
#[must_use] |
| 596 |
pub const fn step_at(self, density: Density) -> Step { |
| 597 |
self.step_at_size(density, SizeClass::Medium) |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
#[must_use] |
| 622 |
pub const fn step_at_size(self, density: Density, size: SizeClass) -> Step { |
| 623 |
match self { |
| 624 |
|
| 625 |
|
| 626 |
Self::Bound => Step::Tight, |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
Self::Peer => match density { |
| 632 |
Density::Pointer => Step::Snug, |
| 633 |
Density::Touch => Step::Roomy, |
| 634 |
}, |
| 635 |
Self::Group => match density { |
| 636 |
Density::Pointer => Step::Roomy, |
| 637 |
Density::Touch => Step::Wide, |
| 638 |
}, |
| 639 |
Self::Section => match density { |
| 640 |
Density::Pointer => Step::Wide, |
| 641 |
Density::Touch => Step::Loose, |
| 642 |
}, |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
Self::Pane => match size { |
| 647 |
SizeClass::Compact => Step::Loose, |
| 648 |
SizeClass::Medium | SizeClass::Expanded => Step::Broad, |
| 649 |
}, |
| 650 |
Self::Page => match size { |
| 651 |
SizeClass::Compact => Step::Broad, |
| 652 |
SizeClass::Medium | SizeClass::Expanded => Step::Vast, |
| 653 |
}, |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
#[must_use] |
| 659 |
pub const fn step(self) -> Step { |
| 660 |
self.step_at(Density::Pointer) |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
#[must_use] |
| 665 |
pub const fn px_at(self, density: Density) -> u16 { |
| 666 |
self.step_at(density).px() |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
#[must_use] |
| 672 |
pub const fn px_at_size(self, density: Density, size: SizeClass) -> u16 { |
| 673 |
self.step_at_size(density, size).px() |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
#[must_use] |
| 678 |
pub const fn px(self) -> u16 { |
| 679 |
self.step().px() |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
#[must_use] |
| 684 |
pub const fn token(self) -> &'static str { |
| 685 |
match self { |
| 686 |
Self::Bound => "gap-bound", |
| 687 |
Self::Peer => "gap-peer", |
| 688 |
Self::Group => "gap-group", |
| 689 |
Self::Section => "gap-section", |
| 690 |
Self::Pane => "gap-pane", |
| 691 |
Self::Page => "gap-page", |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
#[must_use] |
| 697 |
pub const fn all() -> [Self; 6] { |
| 698 |
[ |
| 699 |
Self::Bound, |
| 700 |
Self::Peer, |
| 701 |
Self::Group, |
| 702 |
Self::Section, |
| 703 |
Self::Pane, |
| 704 |
Self::Page, |
| 705 |
] |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 744 |
pub enum Text { |
| 745 |
|
| 746 |
Fine, |
| 747 |
|
| 748 |
|
| 749 |
Note, |
| 750 |
|
| 751 |
|
| 752 |
Body, |
| 753 |
|
| 754 |
Lead, |
| 755 |
|
| 756 |
Subhead, |
| 757 |
|
| 758 |
Head, |
| 759 |
|
| 760 |
Title, |
| 761 |
|
| 762 |
|
| 763 |
Display, |
| 764 |
|
| 765 |
Hero, |
| 766 |
} |
| 767 |
|
| 768 |
impl Text { |
| 769 |
|
| 770 |
#[must_use] |
| 771 |
pub const fn ratio(self) -> Ratio { |
| 772 |
let (numerator, denominator) = match self { |
| 773 |
Self::Fine => (3, 4), |
| 774 |
Self::Note => (7, 8), |
| 775 |
Self::Body => (1, 1), |
| 776 |
Self::Lead => (9, 8), |
| 777 |
Self::Subhead => (5, 4), |
| 778 |
Self::Head => (3, 2), |
| 779 |
Self::Title => (2, 1), |
| 780 |
Self::Display => (5, 2), |
| 781 |
Self::Hero => (3, 1), |
| 782 |
}; |
| 783 |
Ratio { |
| 784 |
numerator, |
| 785 |
denominator, |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
#[must_use] |
| 791 |
pub const fn px(self) -> u16 { |
| 792 |
self.ratio().px_at(DEFAULT_BASE_PX) |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
#[must_use] |
| 797 |
pub const fn token(self) -> &'static str { |
| 798 |
match self { |
| 799 |
Self::Fine => "text-fine", |
| 800 |
Self::Note => "text-note", |
| 801 |
Self::Body => "text-body", |
| 802 |
Self::Lead => "text-lead", |
| 803 |
Self::Subhead => "text-subhead", |
| 804 |
Self::Head => "text-head", |
| 805 |
Self::Title => "text-title", |
| 806 |
Self::Display => "text-display", |
| 807 |
Self::Hero => "text-hero", |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
#[must_use] |
| 813 |
pub const fn all() -> [Self; 9] { |
| 814 |
[ |
| 815 |
Self::Fine, |
| 816 |
Self::Note, |
| 817 |
Self::Body, |
| 818 |
Self::Lead, |
| 819 |
Self::Subhead, |
| 820 |
Self::Head, |
| 821 |
Self::Title, |
| 822 |
Self::Display, |
| 823 |
Self::Hero, |
| 824 |
] |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 844 |
pub enum Radius { |
| 845 |
|
| 846 |
Square, |
| 847 |
|
| 848 |
|
| 849 |
Fine, |
| 850 |
|
| 851 |
Control, |
| 852 |
|
| 853 |
|
| 854 |
Panel, |
| 855 |
|
| 856 |
Round, |
| 857 |
} |
| 858 |
|
| 859 |
impl Radius { |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
#[must_use] |
| 867 |
pub const fn ratio(self) -> Option<Ratio> { |
| 868 |
let (numerator, denominator) = match self { |
| 869 |
Self::Square => (0, 1), |
| 870 |
Self::Fine => (1, 8), |
| 871 |
Self::Control => (1, 4), |
| 872 |
Self::Panel => (1, 2), |
| 873 |
Self::Round => return None, |
| 874 |
}; |
| 875 |
Some(Ratio { |
| 876 |
numerator, |
| 877 |
denominator, |
| 878 |
}) |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
#[must_use] |
| 883 |
pub const fn px(self) -> Option<u16> { |
| 884 |
match self.ratio() { |
| 885 |
Some(r) => Some(r.px_at(DEFAULT_BASE_PX)), |
| 886 |
None => None, |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
#[must_use] |
| 895 |
pub fn css(self) -> String { |
| 896 |
match self { |
| 897 |
Self::Square => "0".to_owned(), |
| 898 |
Self::Round => "50%".to_owned(), |
| 899 |
other => other |
| 900 |
.ratio() |
| 901 |
.expect("every rung but Round has a ratio") |
| 902 |
.css(), |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
#[must_use] |
| 908 |
pub const fn token(self) -> &'static str { |
| 909 |
match self { |
| 910 |
Self::Square => "radius-square", |
| 911 |
Self::Fine => "radius-fine", |
| 912 |
Self::Control => "radius-control", |
| 913 |
Self::Panel => "radius-panel", |
| 914 |
Self::Round => "radius-round", |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
#[must_use] |
| 920 |
pub const fn all() -> [Self; 5] { |
| 921 |
[ |
| 922 |
Self::Square, |
| 923 |
Self::Fine, |
| 924 |
Self::Control, |
| 925 |
Self::Panel, |
| 926 |
Self::Round, |
| 927 |
] |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
#[must_use] |
| 936 |
pub fn scale_css_declarations() -> String { |
| 937 |
let mut out = String::new(); |
| 938 |
let _ = writeln!( |
| 939 |
out, |
| 940 |
" /* Every size below is a ratio of this. Scale it and the whole\n \ |
| 941 |
layout scales with it, including for a user who has asked for\n \ |
| 942 |
larger text. */\n --{BASE_TOKEN}: 1rem;\n" |
| 943 |
); |
| 944 |
out.push_str(" /* Raw scale. Prefer a --gap-* below; reach here only when\n"); |
| 945 |
out.push_str(" no relationship describes the distance. */\n"); |
| 946 |
for step in Step::all() { |
| 947 |
let _ = writeln!(out, " --{}: {};", step.token(), step.ratio().css()); |
| 948 |
} |
| 949 |
out |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
#[must_use] |
| 957 |
pub fn text_css_declarations() -> String { |
| 958 |
let mut out = String::new(); |
| 959 |
out.push_str(" /* Type. Named for what the text is; the size follows.\n"); |
| 960 |
out.push_str(" Ratios of the base, so text tracks the reader's own\n"); |
| 961 |
out.push_str(" root size. Density-invariant: text is not a target. */\n"); |
| 962 |
for text in Text::all() { |
| 963 |
let _ = writeln!(out, " --{}: {};", text.token(), text.ratio().css()); |
| 964 |
} |
| 965 |
out |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
#[must_use] |
| 973 |
pub fn radius_css_declarations() -> String { |
| 974 |
let mut out = String::new(); |
| 975 |
out.push_str(" /* Corners. Rounding says a thing is meant to be pressed,\n"); |
| 976 |
out.push_str(" so the scale is short on purpose and square is a rung\n"); |
| 977 |
out.push_str(" rather than the absence of one. */\n"); |
| 978 |
for radius in Radius::all() { |
| 979 |
let _ = writeln!(out, " --{}: {};", radius.token(), radius.css()); |
| 980 |
} |
| 981 |
out |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
#[must_use] |
| 989 |
pub fn gap_css_declarations(density: Density) -> String { |
| 990 |
let mut out = String::new(); |
| 991 |
for gap in Gap::all() { |
| 992 |
let _ = writeln!( |
| 993 |
out, |
| 994 |
" --{}: var(--{});", |
| 995 |
gap.token(), |
| 996 |
gap.step_at(density).token() |
| 997 |
); |
| 998 |
} |
| 999 |
out |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
pub const CSS_LAYER: &str = "makeover"; |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
#[must_use] |
| 1037 |
pub fn in_css_layer(css: &str) -> String { |
| 1038 |
let mut out = format!("@layer {CSS_LAYER} {{\n"); |
| 1039 |
for line in css.lines() { |
| 1040 |
|
| 1041 |
if line.is_empty() { |
| 1042 |
out.push('\n'); |
| 1043 |
} else { |
| 1044 |
let _ = writeln!(out, " {line}"); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
out.push_str("}\n"); |
| 1048 |
out |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
#[must_use] |
| 1060 |
pub fn geometry_css_vars(density: Density) -> String { |
| 1061 |
format!( |
| 1062 |
":root {{\n{}\n{}\n{}\n{}}}\n", |
| 1063 |
scale_css_declarations(), |
| 1064 |
gap_css_declarations(density), |
| 1065 |
text_css_declarations(), |
| 1066 |
radius_css_declarations() |
| 1067 |
) |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
#[must_use] |
| 1097 |
pub fn density_css(explicit_touch: Option<&str>) -> String { |
| 1098 |
in_css_layer(&density_declarations(explicit_touch)) |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
fn density_declarations(explicit_touch: Option<&str>) -> String { |
| 1103 |
let mut css = geometry_css_vars(Density::Pointer); |
| 1104 |
css.push_str("\n/* Touch: targets separate, shells hold. */\n"); |
| 1105 |
css.push_str("@media "); |
| 1106 |
css.push_str(Density::Touch.media_condition()); |
| 1107 |
css.push_str(" {\n"); |
| 1108 |
for line in gap_css_overrides(":root", Density::Touch).lines() { |
| 1109 |
css.push_str(" "); |
| 1110 |
css.push_str(line); |
| 1111 |
css.push('\n'); |
| 1112 |
} |
| 1113 |
css.push_str("}\n"); |
| 1114 |
if let Some(selector) = explicit_touch { |
| 1115 |
css.push_str("\n/* An explicit user choice, last so it wins over detection. */\n"); |
| 1116 |
css.push_str(&gap_css_overrides(selector, Density::Touch)); |
| 1117 |
} |
| 1118 |
css |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
#[must_use] |
| 1145 |
pub fn size_class_css() -> String { |
| 1146 |
in_css_layer(&size_class_declarations()) |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
fn size_class_declarations() -> String { |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
let ceiling = f32::from(SizeClass::Medium.min_px()) - 0.02; |
| 1156 |
let mut css = String::new(); |
| 1157 |
css.push_str("/* Compact window: shells tighten. Outer margin is screen you\n"); |
| 1158 |
css.push_str(" don't get, which is a claim about the window and not about\n"); |
| 1159 |
css.push_str(" what is pointing at it, so it lives here and not in the\n"); |
| 1160 |
css.push_str(" density presets. Targets are untouched. */\n"); |
| 1161 |
let _ = writeln!(css, "@media (max-width: {ceiling}px) {{"); |
| 1162 |
css.push_str(" :root {\n"); |
| 1163 |
for gap in [Gap::Pane, Gap::Page] { |
| 1164 |
let _ = writeln!( |
| 1165 |
css, |
| 1166 |
" --{}: var(--{});", |
| 1167 |
gap.token(), |
| 1168 |
gap.step_at_size(Density::Pointer, SizeClass::Compact) |
| 1169 |
.token() |
| 1170 |
); |
| 1171 |
} |
| 1172 |
css.push_str(" }\n}\n"); |
| 1173 |
css |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
#[must_use] |
| 1188 |
pub fn gap_css_overrides(selector: &str, density: Density) -> String { |
| 1189 |
format!("{selector} {{\n{}}}\n", gap_css_declarations(density)) |
| 1190 |
} |
| 1191 |
|
| 1192 |
#[cfg(test)] |
| 1193 |
mod tests { |
| 1194 |
use super::*; |
| 1195 |
|
| 1196 |
#[test] |
| 1197 |
fn density_is_selected_by_capability_not_by_width_or_agent() { |
| 1198 |
let css = density_css(None); |
| 1199 |
assert!(css.contains("@media (hover: none), (pointer: coarse)")); |
| 1200 |
|
| 1201 |
assert!(!css.contains("max-width"), "a breakpoint crept in"); |
| 1202 |
assert!(!css.contains("min-width"), "a breakpoint crept in"); |
| 1203 |
assert!(!css.contains("ui-mode"), "a device mode crept in"); |
| 1204 |
} |
| 1205 |
|
| 1206 |
#[test] |
| 1207 |
fn the_spacing_layer_is_emitted_inside_the_family_layer() { |
| 1208 |
|
| 1209 |
|
| 1210 |
let css = density_css(None); |
| 1211 |
assert!(css.starts_with(&format!("@layer {CSS_LAYER} {{\n"))); |
| 1212 |
assert!(css.trim_end().ends_with('}')); |
| 1213 |
|
| 1214 |
assert!(css.contains(" :root {")); |
| 1215 |
assert!(css.contains("--gap-peer")); |
| 1216 |
} |
| 1217 |
|
| 1218 |
#[test] |
| 1219 |
fn the_type_ramp_ascends_and_never_repeats_a_size() { |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
let sizes: Vec<u16> = Text::all().iter().map(|t| t.px()).collect(); |
| 1224 |
assert!(sizes.windows(2).all(|w| w[0] < w[1]), "{sizes:?}"); |
| 1225 |
assert_eq!(sizes, vec![12, 14, 16, 18, 20, 24, 32, 40, 48]); |
| 1226 |
} |
| 1227 |
|
| 1228 |
#[test] |
| 1229 |
fn the_type_ramp_has_a_legibility_floor() { |
| 1230 |
|
| 1231 |
|
| 1232 |
assert_eq!(Text::Fine.px(), 12); |
| 1233 |
assert!(Text::all().iter().all(|t| t.px() >= 12)); |
| 1234 |
} |
| 1235 |
|
| 1236 |
#[test] |
| 1237 |
fn the_corner_scale_is_short_and_ordered() { |
| 1238 |
|
| 1239 |
|
| 1240 |
let px: Vec<Option<u16>> = Radius::all().iter().map(|r| r.px()).collect(); |
| 1241 |
assert_eq!(px, vec![Some(0), Some(2), Some(4), Some(8), None]); |
| 1242 |
} |
| 1243 |
|
| 1244 |
#[test] |
| 1245 |
fn square_and_round_are_spelled_not_calculated() { |
| 1246 |
|
| 1247 |
|
| 1248 |
assert_eq!(Radius::Square.css(), "0"); |
| 1249 |
assert_eq!(Radius::Round.css(), "50%"); |
| 1250 |
assert_eq!(Radius::Round.ratio(), None); |
| 1251 |
assert!(Radius::Control.css().contains(BASE_TOKEN)); |
| 1252 |
} |
| 1253 |
|
| 1254 |
#[test] |
| 1255 |
fn type_does_not_move_with_density() { |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
let css = density_css(Some(".ui-mode-mobile")); |
| 1260 |
let root_end = css.find("@media").expect("a touch block"); |
| 1261 |
assert!(css[..root_end].contains("--text-body")); |
| 1262 |
assert!(!css[root_end..].contains("--text-"), "{}", &css[root_end..]); |
| 1263 |
} |
| 1264 |
|
| 1265 |
#[test] |
| 1266 |
fn every_type_token_scales_from_the_one_base() { |
| 1267 |
|
| 1268 |
|
| 1269 |
for text in Text::all() { |
| 1270 |
let css = text.ratio().css(); |
| 1271 |
assert!(css.contains(BASE_TOKEN), "{}: {css}", text.token()); |
| 1272 |
} |
| 1273 |
} |
| 1274 |
|
| 1275 |
#[test] |
| 1276 |
fn wrapping_leaves_no_trailing_whitespace_on_blank_lines() { |
| 1277 |
|
| 1278 |
let css = in_css_layer("a {\n\nb\n}\n"); |
| 1279 |
assert!(!css.lines().any(|l| l != l.trim_end()), "{css:?}"); |
| 1280 |
} |
| 1281 |
|
| 1282 |
#[test] |
| 1283 |
fn the_two_density_conditions_are_complements_and_not_negations() { |
| 1284 |
let pointer = Density::Pointer.media_condition(); |
| 1285 |
let touch = Density::Touch.media_condition(); |
| 1286 |
|
| 1287 |
|
| 1288 |
assert!(pointer.contains("hover: hover") && touch.contains("hover: none")); |
| 1289 |
assert!(pointer.contains("pointer: fine") && touch.contains("pointer: coarse")); |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
assert!(touch.contains(", "), "touch must be an OR"); |
| 1295 |
assert!(pointer.contains(" and "), "pointer must be an AND"); |
| 1296 |
assert!(!pointer.contains(','), "pointer must not be an OR"); |
| 1297 |
} |
| 1298 |
|
| 1299 |
#[test] |
| 1300 |
fn the_emitted_touch_block_is_the_condition_and_not_a_second_copy_of_it() { |
| 1301 |
|
| 1302 |
let css = density_css(None); |
| 1303 |
assert!(css.contains(&format!("@media {}", Density::Touch.media_condition()))); |
| 1304 |
} |
| 1305 |
|
| 1306 |
#[test] |
| 1307 |
fn an_explicit_choice_is_emitted_after_the_detection() { |
| 1308 |
let css = density_css(Some(".ui-mode-mobile")); |
| 1309 |
let media = css.find("@media").expect("media query"); |
| 1310 |
let explicit = css.find(".ui-mode-mobile").expect("explicit selector"); |
| 1311 |
|
| 1312 |
|
| 1313 |
assert!(explicit > media, "the explicit selector must come last"); |
| 1314 |
} |
| 1315 |
|
| 1316 |
#[test] |
| 1317 |
fn without_an_explicit_selector_there_are_exactly_two_presets() { |
| 1318 |
assert_eq!(density_css(None).matches("--gap-peer").count(), 2); |
| 1319 |
} |
| 1320 |
|
| 1321 |
#[test] |
| 1322 |
fn the_hig_relationships_land_on_the_hig_values() { |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
assert_eq!(Gap::Bound.px(), 4); |
| 1327 |
assert_eq!(Gap::Peer.px(), 6); |
| 1328 |
assert_eq!(Gap::Group.px(), 10); |
| 1329 |
assert_eq!(Gap::Section.px(), 12); |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn every_ratio_divides_the_default_base_exactly() { |
| 1334 |
for step in Step::all() { |
| 1335 |
let r = step.ratio(); |
| 1336 |
assert_eq!( |
| 1337 |
u32::from(DEFAULT_BASE_PX) * u32::from(r.numerator) % u32::from(r.denominator), |
| 1338 |
0, |
| 1339 |
"{step:?} is fractional at the default base" |
| 1340 |
); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
#[test] |
| 1345 |
fn ratios_scale_linearly() { |
| 1346 |
for step in Step::all() { |
| 1347 |
assert_eq!( |
| 1348 |
step.ratio().px_at(DEFAULT_BASE_PX * 2), |
| 1349 |
step.px() * 2, |
| 1350 |
"{step:?} does not double with the base" |
| 1351 |
); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
#[test] |
| 1356 |
fn steps_ascend_and_never_repeat() { |
| 1357 |
let px: Vec<u16> = Step::all().iter().map(|s| s.px()).collect(); |
| 1358 |
let mut sorted = px.clone(); |
| 1359 |
sorted.sort_unstable(); |
| 1360 |
sorted.dedup(); |
| 1361 |
assert_eq!(px, sorted, "steps must be strictly ascending"); |
| 1362 |
} |
| 1363 |
|
| 1364 |
#[test] |
| 1365 |
fn gaps_ascend_with_their_relationships_at_every_density() { |
| 1366 |
for density in [Density::Pointer, Density::Touch] { |
| 1367 |
let px: Vec<u16> = Gap::all().iter().map(|g| g.px_at(density)).collect(); |
| 1368 |
let mut sorted = px.clone(); |
| 1369 |
sorted.sort_unstable(); |
| 1370 |
assert_eq!(px, sorted, "{density:?}: a looser relationship is tighter"); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
#[test] |
| 1375 |
fn touch_separates_targets_and_holds_the_shells() { |
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
for gap in [Gap::Peer, Gap::Group, Gap::Section] { |
| 1381 |
assert!( |
| 1382 |
gap.px_at(Density::Touch) > gap.px_at(Density::Pointer), |
| 1383 |
"{gap:?} separates tap targets and must open on touch" |
| 1384 |
); |
| 1385 |
} |
| 1386 |
for gap in [Gap::Bound, Gap::Pane, Gap::Page] { |
| 1387 |
assert_eq!( |
| 1388 |
gap.px_at(Density::Touch), |
| 1389 |
gap.px_at(Density::Pointer), |
| 1390 |
"{gap:?} is not a tap target and must not move with the input device" |
| 1391 |
); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
#[test] |
| 1396 |
fn touch_never_resolves_tighter_than_pointer() { |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
for gap in Gap::all() { |
| 1407 |
assert!( |
| 1408 |
gap.px_at(Density::Touch) >= gap.px_at(Density::Pointer), |
| 1409 |
"{gap:?}: Touch resolved tighter than Pointer" |
| 1410 |
); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
#[test] |
| 1415 |
fn the_pointer_retune_is_not_blocked_by_touch() { |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
assert!( |
| 1420 |
Gap::Section.px_at(Density::Touch) <= Gap::Pane.px_at(Density::Touch), |
| 1421 |
"Touch inverted internally, which is what set the old floor" |
| 1422 |
); |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
assert!(Gap::Section.px_at(Density::Touch) >= 16); |
| 1432 |
} |
| 1433 |
|
| 1434 |
#[test] |
| 1435 |
fn shells_tighten_on_a_compact_window_and_nothing_else_does() { |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
for density in [Density::Pointer, Density::Touch] { |
| 1441 |
assert_eq!(Gap::Pane.px_at_size(density, SizeClass::Compact), 16); |
| 1442 |
assert_eq!(Gap::Page.px_at_size(density, SizeClass::Compact), 24); |
| 1443 |
|
| 1444 |
for class in [SizeClass::Medium, SizeClass::Expanded] { |
| 1445 |
assert_eq!(Gap::Pane.px_at_size(density, class), 24); |
| 1446 |
assert_eq!(Gap::Page.px_at_size(density, class), 32); |
| 1447 |
} |
| 1448 |
|
| 1449 |
for gap in [Gap::Bound, Gap::Peer, Gap::Group, Gap::Section] { |
| 1450 |
for class in SizeClass::all() { |
| 1451 |
assert_eq!( |
| 1452 |
gap.px_at_size(density, class), |
| 1453 |
gap.px_at(density), |
| 1454 |
"{gap:?} moved on {class:?}, and only shells may" |
| 1455 |
); |
| 1456 |
} |
| 1457 |
} |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
#[test] |
| 1462 |
fn the_widths_never_needed_a_step_at_seven_eighths() { |
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
for gap in [Gap::Pane, Gap::Page] { |
| 1469 |
for density in [Density::Pointer, Density::Touch] { |
| 1470 |
let step = gap.step_at_size(density, SizeClass::Compact); |
| 1471 |
assert!( |
| 1472 |
Step::all().contains(&step), |
| 1473 |
"{gap:?} compact resolved off the scale" |
| 1474 |
); |
| 1475 |
} |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
#[test] |
| 1480 |
fn no_size_class_inverts_the_ordering() { |
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
for density in [Density::Pointer, Density::Touch] { |
| 1486 |
for class in SizeClass::all() { |
| 1487 |
let v: Vec<u16> = Gap::all() |
| 1488 |
.iter() |
| 1489 |
.map(|g| g.px_at_size(density, class)) |
| 1490 |
.collect(); |
| 1491 |
let mut sorted = v.clone(); |
| 1492 |
sorted.sort_unstable(); |
| 1493 |
assert_eq!(v, sorted, "{density:?} {class:?} inverted: {v:?}"); |
| 1494 |
} |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
#[test] |
| 1499 |
fn step_at_is_the_wider_window_answer() { |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
for gap in Gap::all() { |
| 1504 |
for density in [Density::Pointer, Density::Touch] { |
| 1505 |
assert_eq!( |
| 1506 |
gap.step_at(density), |
| 1507 |
gap.step_at_size(density, SizeClass::Medium) |
| 1508 |
); |
| 1509 |
assert_eq!( |
| 1510 |
gap.step_at(density), |
| 1511 |
gap.step_at_size(density, SizeClass::Expanded) |
| 1512 |
); |
| 1513 |
} |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
#[test] |
| 1518 |
fn the_width_query_lives_outside_the_density_file() { |
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
let css = size_class_css(); |
| 1523 |
assert!(css.contains("max-width")); |
| 1524 |
assert!(css.contains("--gap-pane")); |
| 1525 |
assert!(css.contains("--gap-page")); |
| 1526 |
|
| 1527 |
for token in ["--gap-bound", "--gap-peer", "--gap-group", "--gap-section"] { |
| 1528 |
assert!(!css.contains(token), "{token} crept into a width query"); |
| 1529 |
} |
| 1530 |
|
| 1531 |
assert!(!density_css(None).contains("max-width")); |
| 1532 |
} |
| 1533 |
|
| 1534 |
#[test] |
| 1535 |
fn size_classes_partition_every_width_exactly_once() { |
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
for px in 0..=4000u16 { |
| 1540 |
let hits: Vec<SizeClass> = SizeClass::all() |
| 1541 |
.into_iter() |
| 1542 |
.filter(|c| { |
| 1543 |
let lo = c.min_px(); |
| 1544 |
let hi = match c { |
| 1545 |
SizeClass::Compact => SizeClass::Medium.min_px() - 1, |
| 1546 |
SizeClass::Medium => SizeClass::Expanded.min_px() - 1, |
| 1547 |
SizeClass::Expanded => u16::MAX, |
| 1548 |
}; |
| 1549 |
px >= lo && px <= hi |
| 1550 |
}) |
| 1551 |
.collect(); |
| 1552 |
assert_eq!(hits.len(), 1, "{px}px matched {hits:?}"); |
| 1553 |
assert_eq!(hits[0], SizeClass::at_width(px), "{px}px disagrees"); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
#[test] |
| 1558 |
fn the_quoted_boundaries_are_the_ones_material_publishes() { |
| 1559 |
|
| 1560 |
|
| 1561 |
assert_eq!(SizeClass::Compact.min_px(), 0); |
| 1562 |
assert_eq!(SizeClass::Medium.min_px(), 600); |
| 1563 |
assert_eq!(SizeClass::Expanded.min_px(), 840); |
| 1564 |
} |
| 1565 |
|
| 1566 |
#[test] |
| 1567 |
fn the_media_conditions_do_not_overlap_at_the_boundary() { |
| 1568 |
|
| 1569 |
|
| 1570 |
assert_eq!( |
| 1571 |
SizeClass::Compact.media_condition(), |
| 1572 |
"(max-width: 599px)", |
| 1573 |
"Compact must stop one pixel below Medium" |
| 1574 |
); |
| 1575 |
assert_eq!( |
| 1576 |
SizeClass::Medium.media_condition(), |
| 1577 |
"(min-width: 600px) and (max-width: 839px)" |
| 1578 |
); |
| 1579 |
assert_eq!(SizeClass::Expanded.media_condition(), "(min-width: 840px)"); |
| 1580 |
} |
| 1581 |
|
| 1582 |
#[test] |
| 1583 |
fn size_class_does_not_reach_the_gap_scale() { |
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
let _ = geometry_css_vars(Density::Pointer); |
| 1591 |
let _ = Gap::Page.px_at(Density::Touch); |
| 1592 |
assert_eq!(SizeClass::all().len(), 3); |
| 1593 |
} |
| 1594 |
|
| 1595 |
#[test] |
| 1596 |
fn a_terminal_resolves_the_vocabulary_to_whole_cells() { |
| 1597 |
let t = Surface::terminal(); |
| 1598 |
let cells: Vec<u32> = Gap::all() |
| 1599 |
.iter() |
| 1600 |
.map(|g| t.gap(*g, Density::Pointer)) |
| 1601 |
.collect(); |
| 1602 |
|
| 1603 |
assert_eq!(cells, vec![0, 0, 1, 1, 2, 2]); |
| 1604 |
} |
| 1605 |
|
| 1606 |
#[test] |
| 1607 |
fn collapsing_is_allowed_but_inverting_is_not() { |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
for quantum in [0.5_f32, 1.0, 2.0, 3.0, 7.0] { |
| 1612 |
for density in [Density::Pointer, Density::Touch] { |
| 1613 |
let s = Surface { |
| 1614 |
base: 16.0, |
| 1615 |
quantum, |
| 1616 |
}; |
| 1617 |
let v: Vec<u32> = Gap::all().iter().map(|g| s.gap(*g, density)).collect(); |
| 1618 |
let mut sorted = v.clone(); |
| 1619 |
sorted.sort_unstable(); |
| 1620 |
assert_eq!(v, sorted, "quantum {quantum} {density:?} inverted: {v:?}"); |
| 1621 |
} |
| 1622 |
} |
| 1623 |
} |
| 1624 |
|
| 1625 |
#[test] |
| 1626 |
fn the_web_surface_agrees_with_the_pixel_helper() { |
| 1627 |
let w = Surface::web(); |
| 1628 |
for step in Step::all() { |
| 1629 |
assert_eq!( |
| 1630 |
w.resolve(step.ratio()) as u16, |
| 1631 |
step.px(), |
| 1632 |
"{step:?} disagrees between surface and px_at" |
| 1633 |
); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
#[test] |
| 1638 |
fn quantising_is_monotonic_in_the_ratio() { |
| 1639 |
let (base, quantum) = (16.0, 1.0); |
| 1640 |
let mut previous = 0; |
| 1641 |
for step in Step::all() { |
| 1642 |
let q = step.ratio().quanta(base, quantum); |
| 1643 |
assert!(q >= previous, "{step:?} went backwards"); |
| 1644 |
previous = q; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
#[test] |
| 1649 |
fn a_degenerate_quantum_yields_nothing_rather_than_panicking() { |
| 1650 |
let r = Step::Loose.ratio(); |
| 1651 |
for bad in [0.0_f32, -1.0, f32::NAN] { |
| 1652 |
assert_eq!(r.quanta(16.0, bad), 0); |
| 1653 |
assert!(r.quantize(16.0, bad).abs() < f32::EPSILON); |
| 1654 |
} |
| 1655 |
assert_eq!(r.quanta(f32::INFINITY, 1.0), 0); |
| 1656 |
} |
| 1657 |
|
| 1658 |
#[test] |
| 1659 |
fn px_at_rounds_rather_than_truncating() { |
| 1660 |
|
| 1661 |
|
| 1662 |
assert_eq!(Step::Snug.ratio().px_at(15), 6); |
| 1663 |
assert_eq!(Step::Snug.ratio().px_at(DEFAULT_BASE_PX), 6); |
| 1664 |
} |
| 1665 |
|
| 1666 |
#[test] |
| 1667 |
fn tokens_are_unique() { |
| 1668 |
let mut names: Vec<&str> = Step::all().iter().map(|s| s.token()).collect(); |
| 1669 |
names.extend(Gap::all().iter().map(|g| g.token())); |
| 1670 |
names.extend(Text::all().iter().map(|t| t.token())); |
| 1671 |
names.extend(Radius::all().iter().map(|r| r.token())); |
| 1672 |
let count = names.len(); |
| 1673 |
names.sort_unstable(); |
| 1674 |
names.dedup(); |
| 1675 |
assert_eq!(names.len(), count, "token names collide"); |
| 1676 |
} |
| 1677 |
|
| 1678 |
#[test] |
| 1679 |
fn css_is_expressed_over_the_base_never_in_pixels() { |
| 1680 |
let css = geometry_css_vars(Density::Pointer); |
| 1681 |
assert!(css.starts_with(":root {\n")); |
| 1682 |
assert!(css.trim_end().ends_with('}')); |
| 1683 |
assert!(css.contains("--geometry-base: 1rem;")); |
| 1684 |
for step in Step::all() { |
| 1685 |
let line = format!("--{}: {}", step.token(), step.ratio().css()); |
| 1686 |
assert!(css.contains(&line), "missing or wrong: {line}"); |
| 1687 |
} |
| 1688 |
|
| 1689 |
let scale = scale_css_declarations(); |
| 1690 |
assert!( |
| 1691 |
!scale.contains("px;"), |
| 1692 |
"the scale must not emit pixel literals:\n{scale}" |
| 1693 |
); |
| 1694 |
} |
| 1695 |
|
| 1696 |
#[test] |
| 1697 |
fn ratio_css_drops_redundant_arithmetic() { |
| 1698 |
assert_eq!(Step::Loose.ratio().css(), "var(--geometry-base)"); |
| 1699 |
assert_eq!(Step::Vast.ratio().css(), "calc(var(--geometry-base) * 2)"); |
| 1700 |
assert_eq!( |
| 1701 |
Step::Snug.ratio().css(), |
| 1702 |
"calc(var(--geometry-base) * 3 / 8)" |
| 1703 |
); |
| 1704 |
} |
| 1705 |
|
| 1706 |
#[test] |
| 1707 |
fn gaps_reference_steps_rather_than_repeating_values() { |
| 1708 |
let css = geometry_css_vars(Density::Pointer); |
| 1709 |
assert!(css.contains("--gap-peer: var(--step-snug);")); |
| 1710 |
assert!(!css.contains("--gap-peer: calc")); |
| 1711 |
} |
| 1712 |
|
| 1713 |
#[test] |
| 1714 |
fn a_density_override_emits_only_the_relational_layer() { |
| 1715 |
let css = gap_css_overrides(".ui-mode-mobile", Density::Touch); |
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
assert!(css.contains("--gap-peer: var(--step-")); |
| 1720 |
|
| 1721 |
|
| 1722 |
let declared: Vec<&str> = css |
| 1723 |
.lines() |
| 1724 |
.filter_map(|l| l.trim().strip_prefix("--")) |
| 1725 |
.filter_map(|l| l.split(':').next()) |
| 1726 |
.collect(); |
| 1727 |
assert!( |
| 1728 |
declared.iter().all(|t| t.starts_with("gap-")), |
| 1729 |
"only the relational layer may be overridden, got {declared:?}" |
| 1730 |
); |
| 1731 |
} |
| 1732 |
} |
| 1733 |
|