| 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 |
#![forbid(unsafe_code)] |
| 150 |
|
| 151 |
use std::fmt::Write as _; |
| 152 |
|
| 153 |
|
| 154 |
pub const DEFAULT_BASE_PX: u16 = 16; |
| 155 |
|
| 156 |
|
| 157 |
pub const BASE_TOKEN: &str = "geometry-base"; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 164 |
pub struct Ratio { |
| 165 |
|
| 166 |
pub numerator: u16, |
| 167 |
|
| 168 |
pub denominator: u16, |
| 169 |
} |
| 170 |
|
| 171 |
impl Ratio { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[must_use] |
| 179 |
pub const fn px_at(self, base_px: u16) -> u16 { |
| 180 |
let (n, d) = (self.numerator as u32, self.denominator as u32); |
| 181 |
let scaled = base_px as u32 * n; |
| 182 |
|
| 183 |
((scaled * 2 + d) / (d * 2)) as u16 |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#[must_use] |
| 191 |
pub fn scale(self, base: f32) -> f32 { |
| 192 |
base * f32::from(self.numerator) / f32::from(self.denominator) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
#[must_use] |
| 208 |
pub fn quanta(self, base: f32, quantum: f32) -> u32 { |
| 209 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
let exact = self.scale(base) / quantum; |
| 213 |
if exact <= 0.0 { |
| 214 |
0 |
| 215 |
} else { |
| 216 |
|
| 217 |
exact.round() as u32 |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#[must_use] |
| 228 |
pub fn quantize(self, base: f32, quantum: f32) -> f32 { |
| 229 |
if !quantum.is_finite() || quantum <= 0.0 || !base.is_finite() { |
| 230 |
return 0.0; |
| 231 |
} |
| 232 |
self.quanta(base, quantum) as f32 * quantum |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#[must_use] |
| 240 |
pub fn css(self) -> String { |
| 241 |
match (self.numerator, self.denominator) { |
| 242 |
(n, d) if n == d => format!("var(--{BASE_TOKEN})"), |
| 243 |
(n, 1) => format!("calc(var(--{BASE_TOKEN}) * {n})"), |
| 244 |
(n, d) => format!("calc(var(--{BASE_TOKEN}) * {n} / {d})"), |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 264 |
pub struct Surface { |
| 265 |
|
| 266 |
pub base: f32, |
| 267 |
|
| 268 |
pub quantum: f32, |
| 269 |
} |
| 270 |
|
| 271 |
impl Surface { |
| 272 |
|
| 273 |
#[must_use] |
| 274 |
pub fn web() -> Self { |
| 275 |
Self { |
| 276 |
base: f32::from(DEFAULT_BASE_PX), |
| 277 |
quantum: 1.0, |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#[must_use] |
| 288 |
pub fn terminal() -> Self { |
| 289 |
Self { |
| 290 |
base: 1.0, |
| 291 |
quantum: 1.0, |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
#[must_use] |
| 297 |
pub fn resolve(self, ratio: Ratio) -> f32 { |
| 298 |
ratio.quantize(self.base, self.quantum) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
#[must_use] |
| 305 |
pub fn quanta(self, ratio: Ratio) -> u32 { |
| 306 |
ratio.quanta(self.base, self.quantum) |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
#[must_use] |
| 314 |
pub fn gap(self, gap: Gap, density: Density) -> u32 { |
| 315 |
self.quanta(gap.step_at(density).ratio()) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] |
| 326 |
pub enum Density { |
| 327 |
|
| 328 |
#[default] |
| 329 |
Pointer, |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
Touch, |
| 335 |
} |
| 336 |
|
| 337 |
impl Density { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
#[must_use] |
| 356 |
pub const fn media_condition(self) -> &'static str { |
| 357 |
match self { |
| 358 |
Self::Pointer => "(hover: hover) and (pointer: fine)", |
| 359 |
Self::Touch => "(hover: none), (pointer: coarse)", |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)] |
| 387 |
pub enum SizeClass { |
| 388 |
|
| 389 |
|
| 390 |
Compact, |
| 391 |
|
| 392 |
#[default] |
| 393 |
Medium, |
| 394 |
|
| 395 |
Expanded, |
| 396 |
} |
| 397 |
|
| 398 |
impl SizeClass { |
| 399 |
|
| 400 |
#[must_use] |
| 401 |
pub const fn min_px(self) -> u16 { |
| 402 |
match self { |
| 403 |
Self::Compact => 0, |
| 404 |
Self::Medium => 600, |
| 405 |
Self::Expanded => 840, |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
#[must_use] |
| 415 |
pub fn media_condition(self) -> String { |
| 416 |
match self { |
| 417 |
Self::Compact => format!("(max-width: {}px)", Self::Medium.min_px() - 1), |
| 418 |
Self::Medium => format!( |
| 419 |
"(min-width: {}px) and (max-width: {}px)", |
| 420 |
Self::Medium.min_px(), |
| 421 |
Self::Expanded.min_px() - 1 |
| 422 |
), |
| 423 |
Self::Expanded => format!("(min-width: {}px)", Self::Expanded.min_px()), |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
#[must_use] |
| 429 |
pub const fn token(self) -> &'static str { |
| 430 |
match self { |
| 431 |
Self::Compact => "size-compact", |
| 432 |
Self::Medium => "size-medium", |
| 433 |
Self::Expanded => "size-expanded", |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
#[must_use] |
| 439 |
pub const fn all() -> [Self; 3] { |
| 440 |
[Self::Compact, Self::Medium, Self::Expanded] |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
#[must_use] |
| 445 |
pub const fn at_width(px: u16) -> Self { |
| 446 |
if px >= Self::Expanded.min_px() { |
| 447 |
Self::Expanded |
| 448 |
} else if px >= Self::Medium.min_px() { |
| 449 |
Self::Medium |
| 450 |
} else { |
| 451 |
Self::Compact |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 461 |
pub enum Gap { |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
Bound, |
| 466 |
|
| 467 |
|
| 468 |
Peer, |
| 469 |
|
| 470 |
|
| 471 |
Group, |
| 472 |
|
| 473 |
|
| 474 |
Section, |
| 475 |
|
| 476 |
Pane, |
| 477 |
|
| 478 |
Page, |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 487 |
pub enum Step { |
| 488 |
|
| 489 |
Hair, |
| 490 |
|
| 491 |
Tight, |
| 492 |
|
| 493 |
Snug, |
| 494 |
|
| 495 |
Base, |
| 496 |
|
| 497 |
Roomy, |
| 498 |
|
| 499 |
Wide, |
| 500 |
|
| 501 |
Loose, |
| 502 |
|
| 503 |
Broad, |
| 504 |
|
| 505 |
Vast, |
| 506 |
|
| 507 |
Colossal, |
| 508 |
} |
| 509 |
|
| 510 |
impl Step { |
| 511 |
|
| 512 |
#[must_use] |
| 513 |
pub const fn ratio(self) -> Ratio { |
| 514 |
let (numerator, denominator) = match self { |
| 515 |
Self::Hair => (1, 8), |
| 516 |
Self::Tight => (1, 4), |
| 517 |
Self::Snug => (3, 8), |
| 518 |
Self::Base => (1, 2), |
| 519 |
Self::Roomy => (5, 8), |
| 520 |
Self::Wide => (3, 4), |
| 521 |
Self::Loose => (1, 1), |
| 522 |
Self::Broad => (3, 2), |
| 523 |
Self::Vast => (2, 1), |
| 524 |
Self::Colossal => (3, 1), |
| 525 |
}; |
| 526 |
Ratio { |
| 527 |
numerator, |
| 528 |
denominator, |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
#[must_use] |
| 534 |
pub const fn px(self) -> u16 { |
| 535 |
self.ratio().px_at(DEFAULT_BASE_PX) |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
#[must_use] |
| 540 |
pub const fn token(self) -> &'static str { |
| 541 |
match self { |
| 542 |
Self::Hair => "step-hair", |
| 543 |
Self::Tight => "step-tight", |
| 544 |
Self::Snug => "step-snug", |
| 545 |
Self::Base => "step-base", |
| 546 |
Self::Roomy => "step-roomy", |
| 547 |
Self::Wide => "step-wide", |
| 548 |
Self::Loose => "step-loose", |
| 549 |
Self::Broad => "step-broad", |
| 550 |
Self::Vast => "step-vast", |
| 551 |
Self::Colossal => "step-colossal", |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
#[must_use] |
| 557 |
pub const fn all() -> [Self; 10] { |
| 558 |
[ |
| 559 |
Self::Hair, |
| 560 |
Self::Tight, |
| 561 |
Self::Snug, |
| 562 |
Self::Base, |
| 563 |
Self::Roomy, |
| 564 |
Self::Wide, |
| 565 |
Self::Loose, |
| 566 |
Self::Broad, |
| 567 |
Self::Vast, |
| 568 |
Self::Colossal, |
| 569 |
] |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
impl Gap { |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
#[must_use] |
| 580 |
pub const fn step_at(self, density: Density) -> Step { |
| 581 |
match self { |
| 582 |
|
| 583 |
|
| 584 |
Self::Bound => Step::Tight, |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
Self::Peer => match density { |
| 590 |
Density::Pointer => Step::Snug, |
| 591 |
Density::Touch => Step::Roomy, |
| 592 |
}, |
| 593 |
Self::Group => match density { |
| 594 |
Density::Pointer => Step::Roomy, |
| 595 |
Density::Touch => Step::Wide, |
| 596 |
}, |
| 597 |
Self::Section => match density { |
| 598 |
Density::Pointer => Step::Wide, |
| 599 |
Density::Touch => Step::Loose, |
| 600 |
}, |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
Self::Pane => Step::Broad, |
| 605 |
Self::Page => Step::Vast, |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
#[must_use] |
| 611 |
pub const fn step(self) -> Step { |
| 612 |
self.step_at(Density::Pointer) |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
#[must_use] |
| 617 |
pub const fn px_at(self, density: Density) -> u16 { |
| 618 |
self.step_at(density).px() |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
#[must_use] |
| 623 |
pub const fn px(self) -> u16 { |
| 624 |
self.step().px() |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
#[must_use] |
| 629 |
pub const fn token(self) -> &'static str { |
| 630 |
match self { |
| 631 |
Self::Bound => "gap-bound", |
| 632 |
Self::Peer => "gap-peer", |
| 633 |
Self::Group => "gap-group", |
| 634 |
Self::Section => "gap-section", |
| 635 |
Self::Pane => "gap-pane", |
| 636 |
Self::Page => "gap-page", |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
#[must_use] |
| 642 |
pub const fn all() -> [Self; 6] { |
| 643 |
[ |
| 644 |
Self::Bound, |
| 645 |
Self::Peer, |
| 646 |
Self::Group, |
| 647 |
Self::Section, |
| 648 |
Self::Pane, |
| 649 |
Self::Page, |
| 650 |
] |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 689 |
pub enum Text { |
| 690 |
|
| 691 |
Fine, |
| 692 |
|
| 693 |
|
| 694 |
Note, |
| 695 |
|
| 696 |
|
| 697 |
Body, |
| 698 |
|
| 699 |
Lead, |
| 700 |
|
| 701 |
Subhead, |
| 702 |
|
| 703 |
Head, |
| 704 |
|
| 705 |
Title, |
| 706 |
|
| 707 |
|
| 708 |
Display, |
| 709 |
|
| 710 |
Hero, |
| 711 |
} |
| 712 |
|
| 713 |
impl Text { |
| 714 |
|
| 715 |
#[must_use] |
| 716 |
pub const fn ratio(self) -> Ratio { |
| 717 |
let (numerator, denominator) = match self { |
| 718 |
Self::Fine => (3, 4), |
| 719 |
Self::Note => (7, 8), |
| 720 |
Self::Body => (1, 1), |
| 721 |
Self::Lead => (9, 8), |
| 722 |
Self::Subhead => (5, 4), |
| 723 |
Self::Head => (3, 2), |
| 724 |
Self::Title => (2, 1), |
| 725 |
Self::Display => (5, 2), |
| 726 |
Self::Hero => (3, 1), |
| 727 |
}; |
| 728 |
Ratio { |
| 729 |
numerator, |
| 730 |
denominator, |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
#[must_use] |
| 736 |
pub const fn px(self) -> u16 { |
| 737 |
self.ratio().px_at(DEFAULT_BASE_PX) |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
#[must_use] |
| 742 |
pub const fn token(self) -> &'static str { |
| 743 |
match self { |
| 744 |
Self::Fine => "text-fine", |
| 745 |
Self::Note => "text-note", |
| 746 |
Self::Body => "text-body", |
| 747 |
Self::Lead => "text-lead", |
| 748 |
Self::Subhead => "text-subhead", |
| 749 |
Self::Head => "text-head", |
| 750 |
Self::Title => "text-title", |
| 751 |
Self::Display => "text-display", |
| 752 |
Self::Hero => "text-hero", |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
#[must_use] |
| 758 |
pub const fn all() -> [Self; 9] { |
| 759 |
[ |
| 760 |
Self::Fine, |
| 761 |
Self::Note, |
| 762 |
Self::Body, |
| 763 |
Self::Lead, |
| 764 |
Self::Subhead, |
| 765 |
Self::Head, |
| 766 |
Self::Title, |
| 767 |
Self::Display, |
| 768 |
Self::Hero, |
| 769 |
] |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
#[must_use] |
| 778 |
pub fn scale_css_declarations() -> String { |
| 779 |
let mut out = String::new(); |
| 780 |
let _ = writeln!( |
| 781 |
out, |
| 782 |
" /* Every size below is a ratio of this. Scale it and the whole\n \ |
| 783 |
layout scales with it, including for a user who has asked for\n \ |
| 784 |
larger text. */\n --{BASE_TOKEN}: 1rem;\n" |
| 785 |
); |
| 786 |
out.push_str(" /* Raw scale. Prefer a --gap-* below; reach here only when\n"); |
| 787 |
out.push_str(" no relationship describes the distance. */\n"); |
| 788 |
for step in Step::all() { |
| 789 |
let _ = writeln!(out, " --{}: {};", step.token(), step.ratio().css()); |
| 790 |
} |
| 791 |
out |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
#[must_use] |
| 799 |
pub fn text_css_declarations() -> String { |
| 800 |
let mut out = String::new(); |
| 801 |
out.push_str(" /* Type. Named for what the text is; the size follows.\n"); |
| 802 |
out.push_str(" Ratios of the base, so text tracks the reader's own\n"); |
| 803 |
out.push_str(" root size. Density-invariant: text is not a target. */\n"); |
| 804 |
for text in Text::all() { |
| 805 |
let _ = writeln!(out, " --{}: {};", text.token(), text.ratio().css()); |
| 806 |
} |
| 807 |
out |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
#[must_use] |
| 815 |
pub fn gap_css_declarations(density: Density) -> String { |
| 816 |
let mut out = String::new(); |
| 817 |
for gap in Gap::all() { |
| 818 |
let _ = writeln!( |
| 819 |
out, |
| 820 |
" --{}: var(--{});", |
| 821 |
gap.token(), |
| 822 |
gap.step_at(density).token() |
| 823 |
); |
| 824 |
} |
| 825 |
out |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
pub const CSS_LAYER: &str = "makeover"; |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
#[must_use] |
| 863 |
pub fn in_css_layer(css: &str) -> String { |
| 864 |
let mut out = format!("@layer {CSS_LAYER} {{\n"); |
| 865 |
for line in css.lines() { |
| 866 |
|
| 867 |
if line.is_empty() { |
| 868 |
out.push('\n'); |
| 869 |
} else { |
| 870 |
let _ = writeln!(out, " {line}"); |
| 871 |
} |
| 872 |
} |
| 873 |
out.push_str("}\n"); |
| 874 |
out |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
#[must_use] |
| 886 |
pub fn geometry_css_vars(density: Density) -> String { |
| 887 |
format!( |
| 888 |
":root {{\n{}\n{}\n{}}}\n", |
| 889 |
scale_css_declarations(), |
| 890 |
gap_css_declarations(density), |
| 891 |
text_css_declarations() |
| 892 |
) |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
#[must_use] |
| 922 |
pub fn density_css(explicit_touch: Option<&str>) -> String { |
| 923 |
in_css_layer(&density_declarations(explicit_touch)) |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
fn density_declarations(explicit_touch: Option<&str>) -> String { |
| 928 |
let mut css = geometry_css_vars(Density::Pointer); |
| 929 |
css.push_str("\n/* Touch: targets separate, shells hold. */\n"); |
| 930 |
css.push_str("@media "); |
| 931 |
css.push_str(Density::Touch.media_condition()); |
| 932 |
css.push_str(" {\n"); |
| 933 |
for line in gap_css_overrides(":root", Density::Touch).lines() { |
| 934 |
css.push_str(" "); |
| 935 |
css.push_str(line); |
| 936 |
css.push('\n'); |
| 937 |
} |
| 938 |
css.push_str("}\n"); |
| 939 |
if let Some(selector) = explicit_touch { |
| 940 |
css.push_str("\n/* An explicit user choice, last so it wins over detection. */\n"); |
| 941 |
css.push_str(&gap_css_overrides(selector, Density::Touch)); |
| 942 |
} |
| 943 |
css |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
#[must_use] |
| 958 |
pub fn gap_css_overrides(selector: &str, density: Density) -> String { |
| 959 |
format!("{selector} {{\n{}}}\n", gap_css_declarations(density)) |
| 960 |
} |
| 961 |
|
| 962 |
#[cfg(test)] |
| 963 |
mod tests { |
| 964 |
use super::*; |
| 965 |
|
| 966 |
#[test] |
| 967 |
fn density_is_selected_by_capability_not_by_width_or_agent() { |
| 968 |
let css = density_css(None); |
| 969 |
assert!(css.contains("@media (hover: none), (pointer: coarse)")); |
| 970 |
|
| 971 |
assert!(!css.contains("max-width"), "a breakpoint crept in"); |
| 972 |
assert!(!css.contains("min-width"), "a breakpoint crept in"); |
| 973 |
assert!(!css.contains("ui-mode"), "a device mode crept in"); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn the_spacing_layer_is_emitted_inside_the_family_layer() { |
| 978 |
|
| 979 |
|
| 980 |
let css = density_css(None); |
| 981 |
assert!(css.starts_with(&format!("@layer {CSS_LAYER} {{\n"))); |
| 982 |
assert!(css.trim_end().ends_with('}')); |
| 983 |
|
| 984 |
assert!(css.contains(" :root {")); |
| 985 |
assert!(css.contains("--gap-peer")); |
| 986 |
} |
| 987 |
|
| 988 |
#[test] |
| 989 |
fn the_type_ramp_ascends_and_never_repeats_a_size() { |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
let sizes: Vec<u16> = Text::all().iter().map(|t| t.px()).collect(); |
| 994 |
assert!(sizes.windows(2).all(|w| w[0] < w[1]), "{sizes:?}"); |
| 995 |
assert_eq!(sizes, vec![12, 14, 16, 18, 20, 24, 32, 40, 48]); |
| 996 |
} |
| 997 |
|
| 998 |
#[test] |
| 999 |
fn the_type_ramp_has_a_legibility_floor() { |
| 1000 |
|
| 1001 |
|
| 1002 |
assert_eq!(Text::Fine.px(), 12); |
| 1003 |
assert!(Text::all().iter().all(|t| t.px() >= 12)); |
| 1004 |
} |
| 1005 |
|
| 1006 |
#[test] |
| 1007 |
fn type_does_not_move_with_density() { |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
let css = density_css(Some(".ui-mode-mobile")); |
| 1012 |
let root_end = css.find("@media").expect("a touch block"); |
| 1013 |
assert!(css[..root_end].contains("--text-body")); |
| 1014 |
assert!(!css[root_end..].contains("--text-"), "{}", &css[root_end..]); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
fn every_type_token_scales_from_the_one_base() { |
| 1019 |
|
| 1020 |
|
| 1021 |
for text in Text::all() { |
| 1022 |
let css = text.ratio().css(); |
| 1023 |
assert!(css.contains(BASE_TOKEN), "{}: {css}", text.token()); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
#[test] |
| 1028 |
fn wrapping_leaves_no_trailing_whitespace_on_blank_lines() { |
| 1029 |
|
| 1030 |
let css = in_css_layer("a {\n\nb\n}\n"); |
| 1031 |
assert!(!css.lines().any(|l| l != l.trim_end()), "{css:?}"); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn the_two_density_conditions_are_complements_and_not_negations() { |
| 1036 |
let pointer = Density::Pointer.media_condition(); |
| 1037 |
let touch = Density::Touch.media_condition(); |
| 1038 |
|
| 1039 |
|
| 1040 |
assert!(pointer.contains("hover: hover") && touch.contains("hover: none")); |
| 1041 |
assert!(pointer.contains("pointer: fine") && touch.contains("pointer: coarse")); |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
assert!(touch.contains(", "), "touch must be an OR"); |
| 1047 |
assert!(pointer.contains(" and "), "pointer must be an AND"); |
| 1048 |
assert!(!pointer.contains(','), "pointer must not be an OR"); |
| 1049 |
} |
| 1050 |
|
| 1051 |
#[test] |
| 1052 |
fn the_emitted_touch_block_is_the_condition_and_not_a_second_copy_of_it() { |
| 1053 |
|
| 1054 |
let css = density_css(None); |
| 1055 |
assert!(css.contains(&format!("@media {}", Density::Touch.media_condition()))); |
| 1056 |
} |
| 1057 |
|
| 1058 |
#[test] |
| 1059 |
fn an_explicit_choice_is_emitted_after_the_detection() { |
| 1060 |
let css = density_css(Some(".ui-mode-mobile")); |
| 1061 |
let media = css.find("@media").expect("media query"); |
| 1062 |
let explicit = css.find(".ui-mode-mobile").expect("explicit selector"); |
| 1063 |
|
| 1064 |
|
| 1065 |
assert!(explicit > media, "the explicit selector must come last"); |
| 1066 |
} |
| 1067 |
|
| 1068 |
#[test] |
| 1069 |
fn without_an_explicit_selector_there_are_exactly_two_presets() { |
| 1070 |
assert_eq!(density_css(None).matches("--gap-peer").count(), 2); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[test] |
| 1074 |
fn the_hig_relationships_land_on_the_hig_values() { |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
assert_eq!(Gap::Bound.px(), 4); |
| 1079 |
assert_eq!(Gap::Peer.px(), 6); |
| 1080 |
assert_eq!(Gap::Group.px(), 10); |
| 1081 |
assert_eq!(Gap::Section.px(), 12); |
| 1082 |
} |
| 1083 |
|
| 1084 |
#[test] |
| 1085 |
fn every_ratio_divides_the_default_base_exactly() { |
| 1086 |
for step in Step::all() { |
| 1087 |
let r = step.ratio(); |
| 1088 |
assert_eq!( |
| 1089 |
u32::from(DEFAULT_BASE_PX) * u32::from(r.numerator) % u32::from(r.denominator), |
| 1090 |
0, |
| 1091 |
"{step:?} is fractional at the default base" |
| 1092 |
); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
#[test] |
| 1097 |
fn ratios_scale_linearly() { |
| 1098 |
for step in Step::all() { |
| 1099 |
assert_eq!( |
| 1100 |
step.ratio().px_at(DEFAULT_BASE_PX * 2), |
| 1101 |
step.px() * 2, |
| 1102 |
"{step:?} does not double with the base" |
| 1103 |
); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
#[test] |
| 1108 |
fn steps_ascend_and_never_repeat() { |
| 1109 |
let px: Vec<u16> = Step::all().iter().map(|s| s.px()).collect(); |
| 1110 |
let mut sorted = px.clone(); |
| 1111 |
sorted.sort_unstable(); |
| 1112 |
sorted.dedup(); |
| 1113 |
assert_eq!(px, sorted, "steps must be strictly ascending"); |
| 1114 |
} |
| 1115 |
|
| 1116 |
#[test] |
| 1117 |
fn gaps_ascend_with_their_relationships_at_every_density() { |
| 1118 |
for density in [Density::Pointer, Density::Touch] { |
| 1119 |
let px: Vec<u16> = Gap::all().iter().map(|g| g.px_at(density)).collect(); |
| 1120 |
let mut sorted = px.clone(); |
| 1121 |
sorted.sort_unstable(); |
| 1122 |
assert_eq!(px, sorted, "{density:?}: a looser relationship is tighter"); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
#[test] |
| 1127 |
fn touch_separates_targets_and_holds_the_shells() { |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
for gap in [Gap::Peer, Gap::Group, Gap::Section] { |
| 1133 |
assert!( |
| 1134 |
gap.px_at(Density::Touch) > gap.px_at(Density::Pointer), |
| 1135 |
"{gap:?} separates tap targets and must open on touch" |
| 1136 |
); |
| 1137 |
} |
| 1138 |
for gap in [Gap::Bound, Gap::Pane, Gap::Page] { |
| 1139 |
assert_eq!( |
| 1140 |
gap.px_at(Density::Touch), |
| 1141 |
gap.px_at(Density::Pointer), |
| 1142 |
"{gap:?} is not a tap target and must not move with the input device" |
| 1143 |
); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
#[test] |
| 1148 |
fn touch_never_resolves_tighter_than_pointer() { |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
for gap in Gap::all() { |
| 1159 |
assert!( |
| 1160 |
gap.px_at(Density::Touch) >= gap.px_at(Density::Pointer), |
| 1161 |
"{gap:?}: Touch resolved tighter than Pointer" |
| 1162 |
); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
#[test] |
| 1167 |
fn the_pointer_retune_is_not_blocked_by_touch() { |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
assert!( |
| 1172 |
Gap::Section.px_at(Density::Touch) <= Gap::Pane.px_at(Density::Touch), |
| 1173 |
"Touch inverted internally, which is what set the old floor" |
| 1174 |
); |
| 1175 |
|
| 1176 |
|
| 1177 |
assert!(Gap::Section.px_at(Density::Touch) >= 16); |
| 1178 |
} |
| 1179 |
|
| 1180 |
#[test] |
| 1181 |
fn size_classes_partition_every_width_exactly_once() { |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
for px in 0..=4000u16 { |
| 1186 |
let hits: Vec<SizeClass> = SizeClass::all() |
| 1187 |
.into_iter() |
| 1188 |
.filter(|c| { |
| 1189 |
let lo = c.min_px(); |
| 1190 |
let hi = match c { |
| 1191 |
SizeClass::Compact => SizeClass::Medium.min_px() - 1, |
| 1192 |
SizeClass::Medium => SizeClass::Expanded.min_px() - 1, |
| 1193 |
SizeClass::Expanded => u16::MAX, |
| 1194 |
}; |
| 1195 |
px >= lo && px <= hi |
| 1196 |
}) |
| 1197 |
.collect(); |
| 1198 |
assert_eq!(hits.len(), 1, "{px}px matched {hits:?}"); |
| 1199 |
assert_eq!(hits[0], SizeClass::at_width(px), "{px}px disagrees"); |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
#[test] |
| 1204 |
fn the_quoted_boundaries_are_the_ones_material_publishes() { |
| 1205 |
|
| 1206 |
|
| 1207 |
assert_eq!(SizeClass::Compact.min_px(), 0); |
| 1208 |
assert_eq!(SizeClass::Medium.min_px(), 600); |
| 1209 |
assert_eq!(SizeClass::Expanded.min_px(), 840); |
| 1210 |
} |
| 1211 |
|
| 1212 |
#[test] |
| 1213 |
fn the_media_conditions_do_not_overlap_at_the_boundary() { |
| 1214 |
|
| 1215 |
|
| 1216 |
assert_eq!( |
| 1217 |
SizeClass::Compact.media_condition(), |
| 1218 |
"(max-width: 599px)", |
| 1219 |
"Compact must stop one pixel below Medium" |
| 1220 |
); |
| 1221 |
assert_eq!( |
| 1222 |
SizeClass::Medium.media_condition(), |
| 1223 |
"(min-width: 600px) and (max-width: 839px)" |
| 1224 |
); |
| 1225 |
assert_eq!(SizeClass::Expanded.media_condition(), "(min-width: 840px)"); |
| 1226 |
} |
| 1227 |
|
| 1228 |
#[test] |
| 1229 |
fn size_class_does_not_reach_the_gap_scale() { |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
let _ = geometry_css_vars(Density::Pointer); |
| 1237 |
let _ = Gap::Page.px_at(Density::Touch); |
| 1238 |
assert_eq!(SizeClass::all().len(), 3); |
| 1239 |
} |
| 1240 |
|
| 1241 |
#[test] |
| 1242 |
fn a_terminal_resolves_the_vocabulary_to_whole_cells() { |
| 1243 |
let t = Surface::terminal(); |
| 1244 |
let cells: Vec<u32> = Gap::all() |
| 1245 |
.iter() |
| 1246 |
.map(|g| t.gap(*g, Density::Pointer)) |
| 1247 |
.collect(); |
| 1248 |
|
| 1249 |
assert_eq!(cells, vec![0, 0, 1, 1, 2, 2]); |
| 1250 |
} |
| 1251 |
|
| 1252 |
#[test] |
| 1253 |
fn collapsing_is_allowed_but_inverting_is_not() { |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
for quantum in [0.5_f32, 1.0, 2.0, 3.0, 7.0] { |
| 1258 |
for density in [Density::Pointer, Density::Touch] { |
| 1259 |
let s = Surface { |
| 1260 |
base: 16.0, |
| 1261 |
quantum, |
| 1262 |
}; |
| 1263 |
let v: Vec<u32> = Gap::all().iter().map(|g| s.gap(*g, density)).collect(); |
| 1264 |
let mut sorted = v.clone(); |
| 1265 |
sorted.sort_unstable(); |
| 1266 |
assert_eq!(v, sorted, "quantum {quantum} {density:?} inverted: {v:?}"); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
#[test] |
| 1272 |
fn the_web_surface_agrees_with_the_pixel_helper() { |
| 1273 |
let w = Surface::web(); |
| 1274 |
for step in Step::all() { |
| 1275 |
assert_eq!( |
| 1276 |
w.resolve(step.ratio()) as u16, |
| 1277 |
step.px(), |
| 1278 |
"{step:?} disagrees between surface and px_at" |
| 1279 |
); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
#[test] |
| 1284 |
fn quantising_is_monotonic_in_the_ratio() { |
| 1285 |
let (base, quantum) = (16.0, 1.0); |
| 1286 |
let mut previous = 0; |
| 1287 |
for step in Step::all() { |
| 1288 |
let q = step.ratio().quanta(base, quantum); |
| 1289 |
assert!(q >= previous, "{step:?} went backwards"); |
| 1290 |
previous = q; |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
#[test] |
| 1295 |
fn a_degenerate_quantum_yields_nothing_rather_than_panicking() { |
| 1296 |
let r = Step::Loose.ratio(); |
| 1297 |
for bad in [0.0_f32, -1.0, f32::NAN] { |
| 1298 |
assert_eq!(r.quanta(16.0, bad), 0); |
| 1299 |
assert!(r.quantize(16.0, bad).abs() < f32::EPSILON); |
| 1300 |
} |
| 1301 |
assert_eq!(r.quanta(f32::INFINITY, 1.0), 0); |
| 1302 |
} |
| 1303 |
|
| 1304 |
#[test] |
| 1305 |
fn px_at_rounds_rather_than_truncating() { |
| 1306 |
|
| 1307 |
|
| 1308 |
assert_eq!(Step::Snug.ratio().px_at(15), 6); |
| 1309 |
assert_eq!(Step::Snug.ratio().px_at(DEFAULT_BASE_PX), 6); |
| 1310 |
} |
| 1311 |
|
| 1312 |
#[test] |
| 1313 |
fn tokens_are_unique() { |
| 1314 |
let mut names: Vec<&str> = Step::all().iter().map(|s| s.token()).collect(); |
| 1315 |
names.extend(Gap::all().iter().map(|g| g.token())); |
| 1316 |
names.extend(Text::all().iter().map(|t| t.token())); |
| 1317 |
let count = names.len(); |
| 1318 |
names.sort_unstable(); |
| 1319 |
names.dedup(); |
| 1320 |
assert_eq!(names.len(), count, "token names collide"); |
| 1321 |
} |
| 1322 |
|
| 1323 |
#[test] |
| 1324 |
fn css_is_expressed_over_the_base_never_in_pixels() { |
| 1325 |
let css = geometry_css_vars(Density::Pointer); |
| 1326 |
assert!(css.starts_with(":root {\n")); |
| 1327 |
assert!(css.trim_end().ends_with('}')); |
| 1328 |
assert!(css.contains("--geometry-base: 1rem;")); |
| 1329 |
for step in Step::all() { |
| 1330 |
let line = format!("--{}: {}", step.token(), step.ratio().css()); |
| 1331 |
assert!(css.contains(&line), "missing or wrong: {line}"); |
| 1332 |
} |
| 1333 |
|
| 1334 |
let scale = scale_css_declarations(); |
| 1335 |
assert!( |
| 1336 |
!scale.contains("px;"), |
| 1337 |
"the scale must not emit pixel literals:\n{scale}" |
| 1338 |
); |
| 1339 |
} |
| 1340 |
|
| 1341 |
#[test] |
| 1342 |
fn ratio_css_drops_redundant_arithmetic() { |
| 1343 |
assert_eq!(Step::Loose.ratio().css(), "var(--geometry-base)"); |
| 1344 |
assert_eq!(Step::Vast.ratio().css(), "calc(var(--geometry-base) * 2)"); |
| 1345 |
assert_eq!( |
| 1346 |
Step::Snug.ratio().css(), |
| 1347 |
"calc(var(--geometry-base) * 3 / 8)" |
| 1348 |
); |
| 1349 |
} |
| 1350 |
|
| 1351 |
#[test] |
| 1352 |
fn gaps_reference_steps_rather_than_repeating_values() { |
| 1353 |
let css = geometry_css_vars(Density::Pointer); |
| 1354 |
assert!(css.contains("--gap-peer: var(--step-snug);")); |
| 1355 |
assert!(!css.contains("--gap-peer: calc")); |
| 1356 |
} |
| 1357 |
|
| 1358 |
#[test] |
| 1359 |
fn a_density_override_emits_only_the_relational_layer() { |
| 1360 |
let css = gap_css_overrides(".ui-mode-mobile", Density::Touch); |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
assert!(css.contains("--gap-peer: var(--step-")); |
| 1365 |
|
| 1366 |
|
| 1367 |
let declared: Vec<&str> = css |
| 1368 |
.lines() |
| 1369 |
.filter_map(|l| l.trim().strip_prefix("--")) |
| 1370 |
.filter_map(|l| l.split(':').next()) |
| 1371 |
.collect(); |
| 1372 |
assert!( |
| 1373 |
declared.iter().all(|t| t.starts_with("gap-")), |
| 1374 |
"only the relational layer may be overridden, got {declared:?}" |
| 1375 |
); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|