| 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 |
#![forbid(unsafe_code)] |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub trait Intent { |
| 70 |
|
| 71 |
fn token(self) -> &'static str; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 81 |
pub enum Bevel { |
| 82 |
|
| 83 |
Raised, |
| 84 |
|
| 85 |
|
| 86 |
Inset, |
| 87 |
} |
| 88 |
|
| 89 |
impl Bevel { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#[must_use] |
| 95 |
pub const fn edges(self) -> (Edge, Edge) { |
| 96 |
match self { |
| 97 |
Self::Raised => (Edge::Light, Edge::Dark), |
| 98 |
Self::Inset => (Edge::Dark, Edge::Light), |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
#[must_use] |
| 108 |
pub const fn pressed(self) -> Self { |
| 109 |
match self { |
| 110 |
Self::Raised => Self::Inset, |
| 111 |
Self::Inset => Self::Raised, |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 118 |
pub enum Edge { |
| 119 |
|
| 120 |
Light, |
| 121 |
|
| 122 |
Dark, |
| 123 |
} |
| 124 |
|
| 125 |
impl Intent for Edge { |
| 126 |
fn token(self) -> &'static str { |
| 127 |
match self { |
| 128 |
Self::Light => "bevel-light", |
| 129 |
Self::Dark => "bevel-dark", |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 136 |
pub enum Fill { |
| 137 |
|
| 138 |
Page, |
| 139 |
|
| 140 |
Raised, |
| 141 |
|
| 142 |
Overlay, |
| 143 |
|
| 144 |
Well, |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
impl Intent for Fill { |
| 158 |
fn token(self) -> &'static str { |
| 159 |
match self { |
| 160 |
Self::Page => "surface-page", |
| 161 |
Self::Raised => "surface-raised", |
| 162 |
Self::Overlay => "surface-overlay", |
| 163 |
Self::Well => "surface-well", |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 177 |
pub enum Depth { |
| 178 |
|
| 179 |
Flat, |
| 180 |
|
| 181 |
Raised, |
| 182 |
|
| 183 |
|
| 184 |
Well, |
| 185 |
} |
| 186 |
|
| 187 |
impl Depth { |
| 188 |
|
| 189 |
#[must_use] |
| 190 |
pub const fn bevel(self) -> Option<Bevel> { |
| 191 |
match self { |
| 192 |
Self::Flat => None, |
| 193 |
Self::Raised => Some(Bevel::Raised), |
| 194 |
Self::Well => Some(Bevel::Inset), |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[must_use] |
| 203 |
pub const fn fill(self) -> Option<Fill> { |
| 204 |
match self { |
| 205 |
Self::Flat => None, |
| 206 |
Self::Raised => Some(Fill::Raised), |
| 207 |
Self::Well => Some(Fill::Well), |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
#[must_use] |
| 213 |
pub const fn pressed(self) -> Self { |
| 214 |
match self { |
| 215 |
Self::Raised => Self::Well, |
| 216 |
other => other, |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 234 |
pub enum Tone { |
| 235 |
|
| 236 |
Neutral, |
| 237 |
|
| 238 |
Info, |
| 239 |
|
| 240 |
Success, |
| 241 |
|
| 242 |
Warning, |
| 243 |
|
| 244 |
Danger, |
| 245 |
} |
| 246 |
|
| 247 |
impl Intent for Tone { |
| 248 |
fn token(self) -> &'static str { |
| 249 |
match self { |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
Self::Neutral => "content-muted", |
| 254 |
Self::Info => "info", |
| 255 |
Self::Success => "success", |
| 256 |
Self::Warning => "warning", |
| 257 |
Self::Danger => "danger", |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 277 |
pub enum Token { |
| 278 |
|
| 279 |
Badge, |
| 280 |
|
| 281 |
|
| 282 |
Chip { |
| 283 |
|
| 284 |
removable: bool, |
| 285 |
}, |
| 286 |
} |
| 287 |
|
| 288 |
impl Token { |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
#[must_use] |
| 294 |
pub const fn interactive(self) -> bool { |
| 295 |
matches!(self, Self::Chip { .. }) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
#[must_use] |
| 303 |
pub const fn depth(self, latched: bool) -> Depth { |
| 304 |
match self { |
| 305 |
Self::Badge => Depth::Flat, |
| 306 |
Self::Chip { .. } if latched => Depth::Well, |
| 307 |
Self::Chip { .. } => Depth::Raised, |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 328 |
pub enum Notice { |
| 329 |
|
| 330 |
Toast, |
| 331 |
|
| 332 |
Banner, |
| 333 |
} |
| 334 |
|
| 335 |
impl Notice { |
| 336 |
|
| 337 |
#[must_use] |
| 338 |
pub const fn transient(self) -> bool { |
| 339 |
matches!(self, Self::Toast) |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
#[must_use] |
| 348 |
pub const fn fill(self) -> Fill { |
| 349 |
match self { |
| 350 |
Self::Toast => Fill::Overlay, |
| 351 |
Self::Banner => Fill::Raised, |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 364 |
pub enum RowPart { |
| 365 |
|
| 366 |
Primary, |
| 367 |
|
| 368 |
Secondary, |
| 369 |
|
| 370 |
Meta, |
| 371 |
|
| 372 |
Actions, |
| 373 |
} |
| 374 |
|
| 375 |
impl RowPart { |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
#[must_use] |
| 385 |
pub const fn revealed_on_hover(self) -> bool { |
| 386 |
matches!(self, Self::Actions) |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
#[must_use] |
| 391 |
pub const fn intent(self) -> &'static str { |
| 392 |
match self { |
| 393 |
Self::Primary => "content", |
| 394 |
Self::Secondary => "content-secondary", |
| 395 |
Self::Meta => "content-muted", |
| 396 |
|
| 397 |
Self::Actions => "content", |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 409 |
pub enum Heading { |
| 410 |
|
| 411 |
Page, |
| 412 |
|
| 413 |
Section, |
| 414 |
|
| 415 |
Subsection, |
| 416 |
} |
| 417 |
|
| 418 |
impl Heading { |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
#[must_use] |
| 425 |
pub const fn separated(self) -> bool { |
| 426 |
matches!(self, Self::Section) |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 436 |
pub enum Selector { |
| 437 |
|
| 438 |
Segmented, |
| 439 |
|
| 440 |
Toggle, |
| 441 |
|
| 442 |
Tabs, |
| 443 |
} |
| 444 |
|
| 445 |
impl Selector { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
#[must_use] |
| 453 |
pub const fn chosen(self) -> Depth { |
| 454 |
match self { |
| 455 |
Self::Segmented | Self::Toggle => Depth::Well, |
| 456 |
Self::Tabs => Depth::Raised, |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
#[must_use] |
| 467 |
pub const fn abutting(self) -> bool { |
| 468 |
matches!(self, Self::Segmented | Self::Tabs) |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 481 |
pub enum Readiness { |
| 482 |
|
| 483 |
Ready, |
| 484 |
|
| 485 |
Pending, |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 498 |
pub enum Region<'a> { |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
Band, |
| 504 |
|
| 505 |
Sidebar, |
| 506 |
|
| 507 |
Pane, |
| 508 |
|
| 509 |
Split, |
| 510 |
|
| 511 |
TabGroup, |
| 512 |
|
| 513 |
Modal, |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
Bespoke { |
| 533 |
|
| 534 |
name: &'a str, |
| 535 |
}, |
| 536 |
} |
| 537 |
|
| 538 |
impl Region<'_> { |
| 539 |
|
| 540 |
#[must_use] |
| 541 |
pub const fn depth(self) -> Depth { |
| 542 |
match self { |
| 543 |
Self::Band | Self::Sidebar | Self::Split | Self::TabGroup => Depth::Flat, |
| 544 |
|
| 545 |
Self::Pane => Depth::Well, |
| 546 |
Self::Modal => Depth::Raised, |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
Self::Bespoke { .. } => Depth::Flat, |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
#[must_use] |
| 561 |
pub const fn described(self) -> bool { |
| 562 |
!matches!(self, Self::Bespoke { .. }) |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 577 |
pub enum Arrangement { |
| 578 |
|
| 579 |
ListDetail { |
| 580 |
|
| 581 |
tabbed: bool, |
| 582 |
}, |
| 583 |
|
| 584 |
SidebarContent, |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 601 |
pub enum FieldKind { |
| 602 |
|
| 603 |
Text, |
| 604 |
|
| 605 |
|
| 606 |
Secret, |
| 607 |
|
| 608 |
Number, |
| 609 |
|
| 610 |
Textarea, |
| 611 |
|
| 612 |
Select, |
| 613 |
|
| 614 |
Checkbox, |
| 615 |
|
| 616 |
Hidden, |
| 617 |
} |
| 618 |
|
| 619 |
impl FieldKind { |
| 620 |
|
| 621 |
#[must_use] |
| 622 |
pub const fn visible(self) -> bool { |
| 623 |
!matches!(self, Self::Hidden) |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
#[must_use] |
| 628 |
pub const fn confidential(self) -> bool { |
| 629 |
matches!(self, Self::Secret) |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
#[must_use] |
| 638 |
pub const fn labels_itself(self) -> bool { |
| 639 |
matches!(self, Self::Checkbox) |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 648 |
pub struct Field<'a> { |
| 649 |
|
| 650 |
pub kind: FieldKind, |
| 651 |
|
| 652 |
pub name: &'a str, |
| 653 |
|
| 654 |
pub label: &'a str, |
| 655 |
|
| 656 |
pub hint: Option<&'a str>, |
| 657 |
|
| 658 |
pub error: Option<&'a str>, |
| 659 |
|
| 660 |
pub required: bool, |
| 661 |
|
| 662 |
pub extended: bool, |
| 663 |
} |
| 664 |
|
| 665 |
impl<'a> Field<'a> { |
| 666 |
|
| 667 |
#[must_use] |
| 668 |
pub const fn new(kind: FieldKind, name: &'a str, label: &'a str) -> Self { |
| 669 |
Self { |
| 670 |
kind, |
| 671 |
name, |
| 672 |
label, |
| 673 |
hint: None, |
| 674 |
error: None, |
| 675 |
required: false, |
| 676 |
extended: false, |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
#[must_use] |
| 689 |
pub const fn invalid(&self) -> bool { |
| 690 |
self.error.is_some() |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 700 |
pub enum Width { |
| 701 |
|
| 702 |
Content, |
| 703 |
|
| 704 |
Fixed, |
| 705 |
|
| 706 |
Fill, |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 717 |
pub enum Priority { |
| 718 |
|
| 719 |
Optional, |
| 720 |
|
| 721 |
Secondary, |
| 722 |
|
| 723 |
Essential, |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 732 |
pub struct Column<'a> { |
| 733 |
|
| 734 |
pub name: &'a str, |
| 735 |
|
| 736 |
pub width: Width, |
| 737 |
|
| 738 |
pub priority: Priority, |
| 739 |
} |
| 740 |
|
| 741 |
impl<'a> Column<'a> { |
| 742 |
|
| 743 |
#[must_use] |
| 744 |
pub const fn new(name: &'a str) -> Self { |
| 745 |
Self { |
| 746 |
name, |
| 747 |
width: Width::Fill, |
| 748 |
priority: Priority::Secondary, |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
#[must_use] |
| 757 |
pub const fn kept_at(&self, cutoff: Priority) -> bool { |
| 758 |
(self.priority as u8) >= (cutoff as u8) |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
#[cfg(test)] |
| 763 |
mod tests { |
| 764 |
use super::*; |
| 765 |
|
| 766 |
#[test] |
| 767 |
fn inset_is_raised_with_the_light_moved() { |
| 768 |
let (rl, rd) = Bevel::Raised.edges(); |
| 769 |
let (il, id) = Bevel::Inset.edges(); |
| 770 |
assert_eq!((rl, rd), (Edge::Light, Edge::Dark)); |
| 771 |
assert_eq!((il, id), (rd, rl)); |
| 772 |
} |
| 773 |
|
| 774 |
#[test] |
| 775 |
fn pressing_twice_is_a_no_op() { |
| 776 |
for b in [Bevel::Raised, Bevel::Inset] { |
| 777 |
assert_eq!(b.pressed().pressed(), b); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
#[test] |
| 782 |
fn a_raised_region_is_never_filled_with_a_recessed_surface() { |
| 783 |
|
| 784 |
assert_eq!(Depth::Raised.fill(), Some(Fill::Raised)); |
| 785 |
assert_eq!(Depth::Raised.bevel(), Some(Bevel::Raised)); |
| 786 |
assert_eq!(Depth::Well.bevel(), Some(Bevel::Inset)); |
| 787 |
assert_ne!(Depth::Well.fill(), Depth::Raised.fill()); |
| 788 |
} |
| 789 |
|
| 790 |
#[test] |
| 791 |
fn flat_has_neither_edge_nor_fill() { |
| 792 |
assert_eq!(Depth::Flat.bevel(), None); |
| 793 |
assert_eq!(Depth::Flat.fill(), None); |
| 794 |
} |
| 795 |
|
| 796 |
#[test] |
| 797 |
fn pressing_a_card_makes_a_well() { |
| 798 |
assert_eq!(Depth::Raised.pressed(), Depth::Well); |
| 799 |
assert_eq!( |
| 800 |
Depth::Raised.pressed().bevel(), |
| 801 |
Depth::Raised.bevel().map(Bevel::pressed) |
| 802 |
); |
| 803 |
|
| 804 |
assert_eq!(Depth::Flat.pressed(), Depth::Flat); |
| 805 |
assert_eq!(Depth::Well.pressed(), Depth::Well); |
| 806 |
} |
| 807 |
|
| 808 |
#[test] |
| 809 |
fn intents_name_makeover_tokens_and_nothing_else() { |
| 810 |
assert_eq!(Edge::Light.token(), "bevel-light"); |
| 811 |
assert_eq!(Edge::Dark.token(), "bevel-dark"); |
| 812 |
assert_eq!(Fill::Raised.token(), "surface-raised"); |
| 813 |
assert_eq!(Fill::Well.token(), "surface-well"); |
| 814 |
|
| 815 |
for t in [ |
| 816 |
Edge::Light.token(), |
| 817 |
Edge::Dark.token(), |
| 818 |
Tone::Danger.token(), |
| 819 |
Tone::Neutral.token(), |
| 820 |
] { |
| 821 |
assert!(!t.starts_with('#'), "{t} looks like a value"); |
| 822 |
assert!( |
| 823 |
!t.chars().next().unwrap().is_ascii_digit(), |
| 824 |
"{t} is a value" |
| 825 |
); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
#[test] |
| 830 |
fn a_badge_cannot_be_pressed_and_a_chip_latches() { |
| 831 |
|
| 832 |
assert!(!Token::Badge.interactive()); |
| 833 |
assert!(Token::Chip { removable: false }.interactive()); |
| 834 |
assert!(Token::Chip { removable: true }.interactive()); |
| 835 |
|
| 836 |
|
| 837 |
assert_eq!(Token::Badge.depth(false), Depth::Flat); |
| 838 |
assert_eq!(Token::Badge.depth(true), Depth::Flat); |
| 839 |
|
| 840 |
|
| 841 |
let chip = Token::Chip { removable: false }; |
| 842 |
assert_eq!(chip.depth(false), Depth::Raised); |
| 843 |
assert_eq!(chip.depth(true), Depth::Raised.pressed()); |
| 844 |
} |
| 845 |
|
| 846 |
#[test] |
| 847 |
fn a_toast_and_a_banner_differ_in_more_than_placement() { |
| 848 |
assert!(Notice::Toast.transient()); |
| 849 |
assert!(!Notice::Banner.transient()); |
| 850 |
|
| 851 |
assert_eq!(Notice::Toast.fill(), Fill::Overlay); |
| 852 |
assert_eq!(Notice::Banner.fill(), Fill::Raised); |
| 853 |
} |
| 854 |
|
| 855 |
#[test] |
| 856 |
fn only_the_actions_part_hides_until_hovered() { |
| 857 |
for p in [RowPart::Primary, RowPart::Secondary, RowPart::Meta] { |
| 858 |
assert!(!p.revealed_on_hover(), "{p:?} should always be visible"); |
| 859 |
} |
| 860 |
assert!(RowPart::Actions.revealed_on_hover()); |
| 861 |
|
| 862 |
assert_eq!(RowPart::Primary.intent(), "content"); |
| 863 |
assert_eq!(RowPart::Secondary.intent(), "content-secondary"); |
| 864 |
assert_eq!(RowPart::Meta.intent(), "content-muted"); |
| 865 |
} |
| 866 |
|
| 867 |
#[test] |
| 868 |
fn a_separator_is_what_tells_a_section_from_a_subsection() { |
| 869 |
assert!(Heading::Section.separated()); |
| 870 |
assert!(!Heading::Subsection.separated()); |
| 871 |
assert!(!Heading::Page.separated()); |
| 872 |
} |
| 873 |
|
| 874 |
#[test] |
| 875 |
fn a_chosen_segment_is_held_in_and_a_chosen_tab_comes_forward() { |
| 876 |
assert_eq!(Selector::Segmented.chosen(), Depth::Well); |
| 877 |
assert_eq!(Selector::Toggle.chosen(), Depth::Well); |
| 878 |
|
| 879 |
|
| 880 |
assert_eq!(Selector::Tabs.chosen(), Depth::Raised); |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
assert_eq!(Selector::Segmented.chosen(), Depth::Raised.pressed()); |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
assert!(Selector::Segmented.abutting()); |
| 889 |
assert!(Selector::Tabs.abutting()); |
| 890 |
assert!(!Selector::Toggle.abutting()); |
| 891 |
} |
| 892 |
|
| 893 |
#[test] |
| 894 |
fn a_pane_is_looked_into_and_a_band_is_not() { |
| 895 |
assert_eq!(Region::Pane.depth(), Depth::Well); |
| 896 |
assert_eq!(Region::Modal.depth(), Depth::Raised); |
| 897 |
for r in [ |
| 898 |
Region::Band, |
| 899 |
Region::Sidebar, |
| 900 |
Region::Split, |
| 901 |
Region::TabGroup, |
| 902 |
] { |
| 903 |
assert_eq!(r.depth(), Depth::Flat, "{r:?} should carry no edge"); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
#[test] |
| 908 |
fn exactly_one_region_is_opaque() { |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
for r in [ |
| 913 |
Region::Band, |
| 914 |
Region::Sidebar, |
| 915 |
Region::Pane, |
| 916 |
Region::Split, |
| 917 |
Region::TabGroup, |
| 918 |
Region::Modal, |
| 919 |
] { |
| 920 |
assert!(r.described(), "{r:?} should be describable"); |
| 921 |
} |
| 922 |
assert!(!Region::Bespoke { name: "day-plan" }.described()); |
| 923 |
} |
| 924 |
|
| 925 |
#[test] |
| 926 |
fn a_bespoke_region_inherits_its_depth_rather_than_choosing_one() { |
| 927 |
|
| 928 |
|
| 929 |
assert_eq!(Region::Bespoke { name: "day-plan" }.depth(), Depth::Flat); |
| 930 |
assert_eq!(Region::Bespoke { name: "kanban" }.depth(), Depth::Flat); |
| 931 |
} |
| 932 |
|
| 933 |
#[test] |
| 934 |
fn a_screen_with_a_bespoke_region_is_still_a_whole_screen() { |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
let day_plan = [ |
| 939 |
Region::Band, |
| 940 |
Region::Bespoke { name: "day-plan" }, |
| 941 |
Region::Sidebar, |
| 942 |
]; |
| 943 |
assert_eq!(day_plan.iter().filter(|r| r.described()).count(), 2); |
| 944 |
assert_eq!(day_plan.iter().filter(|r| !r.described()).count(), 1); |
| 945 |
} |
| 946 |
|
| 947 |
#[test] |
| 948 |
fn a_secret_field_is_marked_as_one_and_a_hidden_field_is_not_drawn() { |
| 949 |
let secret = Field::new(FieldKind::Secret, "password", "Password"); |
| 950 |
assert!(secret.kind.confidential()); |
| 951 |
assert!(secret.kind.visible()); |
| 952 |
|
| 953 |
assert!(!FieldKind::Hidden.visible()); |
| 954 |
|
| 955 |
for k in [ |
| 956 |
FieldKind::Text, |
| 957 |
FieldKind::Number, |
| 958 |
FieldKind::Textarea, |
| 959 |
FieldKind::Select, |
| 960 |
FieldKind::Checkbox, |
| 961 |
FieldKind::Hidden, |
| 962 |
] { |
| 963 |
assert!(!k.confidential(), "{k:?} should not be confidential"); |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
assert!(FieldKind::Checkbox.labels_itself()); |
| 968 |
assert!(!FieldKind::Text.labels_itself()); |
| 969 |
} |
| 970 |
|
| 971 |
#[test] |
| 972 |
fn a_field_reports_its_own_error_state() { |
| 973 |
let mut f = Field::new(FieldKind::Text, "title", "Title"); |
| 974 |
assert!(!f.invalid()); |
| 975 |
f.error = Some("Required"); |
| 976 |
assert!(f.invalid()); |
| 977 |
} |
| 978 |
|
| 979 |
#[test] |
| 980 |
fn columns_drop_by_priority_and_never_by_position() { |
| 981 |
let cols = [ |
| 982 |
Column { |
| 983 |
name: "Title", |
| 984 |
width: Width::Fill, |
| 985 |
priority: Priority::Essential, |
| 986 |
}, |
| 987 |
Column { |
| 988 |
name: "Due", |
| 989 |
width: Width::Fixed, |
| 990 |
priority: Priority::Secondary, |
| 991 |
}, |
| 992 |
Column { |
| 993 |
name: "Estimate", |
| 994 |
width: Width::Fixed, |
| 995 |
priority: Priority::Optional, |
| 996 |
}, |
| 997 |
]; |
| 998 |
|
| 999 |
|
| 1000 |
assert_eq!( |
| 1001 |
cols.iter() |
| 1002 |
.filter(|c| c.kept_at(Priority::Optional)) |
| 1003 |
.count(), |
| 1004 |
3 |
| 1005 |
); |
| 1006 |
|
| 1007 |
let kept: Vec<_> = cols |
| 1008 |
.iter() |
| 1009 |
.filter(|c| c.kept_at(Priority::Secondary)) |
| 1010 |
.map(|c| c.name) |
| 1011 |
.collect(); |
| 1012 |
assert_eq!(kept, ["Title", "Due"]); |
| 1013 |
|
| 1014 |
let kept: Vec<_> = cols |
| 1015 |
.iter() |
| 1016 |
.filter(|c| c.kept_at(Priority::Essential)) |
| 1017 |
.map(|c| c.name) |
| 1018 |
.collect(); |
| 1019 |
assert_eq!(kept, ["Title"]); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#[test] |
| 1023 |
fn inserting_a_column_does_not_move_what_gets_dropped() { |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
let before = [ |
| 1028 |
Column::new("Title"), |
| 1029 |
Column { |
| 1030 |
name: "Estimate", |
| 1031 |
width: Width::Fixed, |
| 1032 |
priority: Priority::Optional, |
| 1033 |
}, |
| 1034 |
]; |
| 1035 |
let after = [ |
| 1036 |
Column::new("Title"), |
| 1037 |
Column::new("Project"), |
| 1038 |
Column { |
| 1039 |
name: "Estimate", |
| 1040 |
width: Width::Fixed, |
| 1041 |
priority: Priority::Optional, |
| 1042 |
}, |
| 1043 |
]; |
| 1044 |
|
| 1045 |
fn dropped<'a>(cols: &[Column<'a>]) -> Vec<&'a str> { |
| 1046 |
cols.iter() |
| 1047 |
.filter(|c| !c.kept_at(Priority::Secondary)) |
| 1048 |
.map(|c| c.name) |
| 1049 |
.collect() |
| 1050 |
} |
| 1051 |
assert_eq!(dropped(&before), ["Estimate"]); |
| 1052 |
assert_eq!(dropped(&after), ["Estimate"]); |
| 1053 |
} |
| 1054 |
|
| 1055 |
#[test] |
| 1056 |
fn an_arrangement_carries_the_tab_group_as_a_modifier() { |
| 1057 |
|
| 1058 |
|
| 1059 |
let go = Arrangement::ListDetail { tabbed: true }; |
| 1060 |
let plain = Arrangement::ListDetail { tabbed: false }; |
| 1061 |
assert_ne!(go, plain); |
| 1062 |
assert_ne!(go, Arrangement::SidebarContent); |
| 1063 |
} |
| 1064 |
|
| 1065 |
#[test] |
| 1066 |
fn readiness_names_the_state_and_not_the_shimmer() { |
| 1067 |
|
| 1068 |
|
| 1069 |
assert_ne!(Readiness::Ready, Readiness::Pending); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|