| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use anyhow::{Context as _, anyhow}; |
| 6 | use core::fmt::Debug; |
| 7 | use derive_more::{Add, AddAssign, Div, DivAssign, Mul, Neg, Sub, SubAssign}; |
| 8 | use refineable::Refineable; |
| 9 | use schemars::{JsonSchema, json_schema}; |
| 10 | use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; |
| 11 | use std::borrow::Cow; |
| 12 | use std::ops::Range; |
| 13 | use std::{ |
| 14 | cmp::{self, PartialOrd}, |
| 15 | fmt::{self, Display}, |
| 16 | hash::Hash, |
| 17 | ops::{Add, Div, Mul, MulAssign, Neg, Sub}, |
| 18 | }; |
| 19 | use taffy::prelude::{TaffyGridLine, TaffyGridSpan}; |
| 20 | |
| 21 | use crate::{App, DisplayId}; |
| 22 | |
| 23 | |
| 24 | #[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] |
| 25 | pub enum Axis { |
| 26 | |
| 27 | Vertical, |
| 28 | |
| 29 | Horizontal, |
| 30 | } |
| 31 | |
| 32 | impl Axis { |
| 33 | |
| 34 | pub fn invert(self) -> Self { |
| 35 | match self { |
| 36 | Axis::Vertical => Axis::Horizontal, |
| 37 | Axis::Horizontal => Axis::Vertical, |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | pub trait Along { |
| 44 | |
| 45 | type Unit; |
| 46 | |
| 47 | |
| 48 | fn along(&self, axis: Axis) -> Self::Unit; |
| 49 | |
| 50 | |
| 51 | fn apply_along(&self, axis: Axis, f: impl FnOnce(Self::Unit) -> Self::Unit) -> Self; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | #[derive( |
| 67 | Refineable, |
| 68 | Default, |
| 69 | Add, |
| 70 | AddAssign, |
| 71 | Sub, |
| 72 | SubAssign, |
| 73 | Copy, |
| 74 | Debug, |
| 75 | PartialEq, |
| 76 | Eq, |
| 77 | Serialize, |
| 78 | Deserialize, |
| 79 | JsonSchema, |
| 80 | Hash, |
| 81 | )] |
| 82 | #[refineable(Debug, PartialEq, Serialize, Deserialize, JsonSchema)] |
| 83 | #[repr(C)] |
| 84 | pub struct Point<T: Clone + Debug + Default + PartialEq> { |
| 85 | |
| 86 | pub x: T, |
| 87 | |
| 88 | pub y: T, |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | pub const fn point<T: Clone + Debug + Default + PartialEq>(x: T, y: T) -> Point<T> { |
| 111 | Point { x, y } |
| 112 | } |
| 113 | |
| 114 | impl<T: Clone + Debug + Default + PartialEq> Point<T> { |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | pub const fn new(x: T, y: T) -> Self { |
| 131 | Self { x, y } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | #[must_use] |
| 153 | pub fn map<U: Clone + Debug + Default + PartialEq>(&self, f: impl Fn(T) -> U) -> Point<U> { |
| 154 | Point { |
| 155 | x: f(self.x.clone()), |
| 156 | y: f(self.y.clone()), |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl<T: Clone + Debug + Default + PartialEq> Along for Point<T> { |
| 162 | type Unit = T; |
| 163 | |
| 164 | fn along(&self, axis: Axis) -> T { |
| 165 | match axis { |
| 166 | Axis::Horizontal => self.x.clone(), |
| 167 | Axis::Vertical => self.y.clone(), |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Point<T> { |
| 172 | match axis { |
| 173 | Axis::Horizontal => Point { |
| 174 | x: f(self.x.clone()), |
| 175 | y: self.y.clone(), |
| 176 | }, |
| 177 | Axis::Vertical => Point { |
| 178 | x: self.x.clone(), |
| 179 | y: f(self.y.clone()), |
| 180 | }, |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | impl<T: Clone + Debug + Default + PartialEq + Negate> Negate for Point<T> { |
| 186 | fn negate(self) -> Self { |
| 187 | self.map(Negate::negate) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl Point<Pixels> { |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | pub fn scale(&self, factor: f32) -> Point<ScaledPixels> { |
| 208 | Point { |
| 209 | x: self.x.scale(factor), |
| 210 | y: self.y.scale(factor), |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | pub fn magnitude(&self) -> f64 { |
| 224 | ((self.x.0.powi(2) + self.y.0.powi(2)) as f64).sqrt() |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | impl<T> Point<T> |
| 229 | where |
| 230 | T: Sub<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 231 | { |
| 232 | |
| 233 | pub fn relative_to(&self, origin: &Point<T>) -> Point<T> { |
| 234 | point( |
| 235 | self.x.clone() - origin.x.clone(), |
| 236 | self.y.clone() - origin.y.clone(), |
| 237 | ) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | impl<T, Rhs> Mul<Rhs> for Point<T> |
| 242 | where |
| 243 | T: Mul<Rhs, Output = T> + Clone + Debug + Default + PartialEq, |
| 244 | Rhs: Clone + Debug, |
| 245 | { |
| 246 | type Output = Point<T>; |
| 247 | |
| 248 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 249 | Point { |
| 250 | x: self.x * rhs.clone(), |
| 251 | y: self.y * rhs, |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | impl<T, S> MulAssign<S> for Point<T> |
| 257 | where |
| 258 | T: Mul<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 259 | S: Clone, |
| 260 | { |
| 261 | fn mul_assign(&mut self, rhs: S) { |
| 262 | self.x = self.x.clone() * rhs.clone(); |
| 263 | self.y = self.y.clone() * rhs; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | impl<T, S> Div<S> for Point<T> |
| 268 | where |
| 269 | T: Div<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 270 | S: Clone, |
| 271 | { |
| 272 | type Output = Self; |
| 273 | |
| 274 | fn div(self, rhs: S) -> Self::Output { |
| 275 | Self { |
| 276 | x: self.x / rhs.clone(), |
| 277 | y: self.y / rhs, |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | impl<T> Point<T> |
| 283 | where |
| 284 | T: PartialOrd + Clone + Debug + Default + PartialEq, |
| 285 | { |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | pub fn max(&self, other: &Self) -> Self { |
| 302 | Point { |
| 303 | x: if self.x > other.x { |
| 304 | self.x.clone() |
| 305 | } else { |
| 306 | other.x.clone() |
| 307 | }, |
| 308 | y: if self.y > other.y { |
| 309 | self.y.clone() |
| 310 | } else { |
| 311 | other.y.clone() |
| 312 | }, |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | pub fn min(&self, other: &Self) -> Self { |
| 332 | Point { |
| 333 | x: if self.x <= other.x { |
| 334 | self.x.clone() |
| 335 | } else { |
| 336 | other.x.clone() |
| 337 | }, |
| 338 | y: if self.y <= other.y { |
| 339 | self.y.clone() |
| 340 | } else { |
| 341 | other.y.clone() |
| 342 | }, |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | pub fn clamp(&self, min: &Self, max: &Self) -> Self { |
| 373 | self.max(min).min(max) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | impl<T: Clone + Debug + Default + PartialEq> Clone for Point<T> { |
| 378 | fn clone(&self) -> Self { |
| 379 | Self { |
| 380 | x: self.x.clone(), |
| 381 | y: self.y.clone(), |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | impl<T: Clone + Debug + Default + PartialEq + Display> Display for Point<T> { |
| 387 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 388 | write!(f, "({}, {})", self.x, self.y) |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | #[derive(Refineable, Default, Clone, Copy, PartialEq, Div, Hash, Serialize, Deserialize)] |
| 397 | #[refineable(Debug, PartialEq, Serialize, Deserialize, JsonSchema)] |
| 398 | #[repr(C)] |
| 399 | pub struct Size<T: Clone + Debug + Default + PartialEq> { |
| 400 | |
| 401 | pub width: T, |
| 402 | |
| 403 | pub height: T, |
| 404 | } |
| 405 | |
| 406 | impl<T: Clone + Debug + Default + PartialEq> Size<T> { |
| 407 | |
| 408 | pub fn new(width: T, height: T) -> Self { |
| 409 | size(width, height) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | pub const fn size<T>(width: T, height: T) -> Size<T> |
| 429 | where |
| 430 | T: Clone + Debug + Default + PartialEq, |
| 431 | { |
| 432 | Size { width, height } |
| 433 | } |
| 434 | |
| 435 | impl<T> Size<T> |
| 436 | where |
| 437 | T: Clone + Debug + Default + PartialEq, |
| 438 | { |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | pub fn map<U>(&self, f: impl Fn(T) -> U) -> Size<U> |
| 458 | where |
| 459 | U: Clone + Debug + Default + PartialEq, |
| 460 | { |
| 461 | Size { |
| 462 | width: f(self.width.clone()), |
| 463 | height: f(self.height.clone()), |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | impl<T> Size<T> |
| 469 | where |
| 470 | T: Clone + Debug + Default + PartialEq + Half, |
| 471 | { |
| 472 | |
| 473 | pub fn center(&self) -> Point<T> { |
| 474 | Point { |
| 475 | x: self.width.half(), |
| 476 | y: self.height.half(), |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | impl Size<Pixels> { |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | pub fn scale(&self, factor: f32) -> Size<ScaledPixels> { |
| 501 | Size { |
| 502 | width: self.width.scale(factor), |
| 503 | height: self.height.scale(factor), |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | impl<T> Along for Size<T> |
| 509 | where |
| 510 | T: Clone + Debug + Default + PartialEq, |
| 511 | { |
| 512 | type Unit = T; |
| 513 | |
| 514 | fn along(&self, axis: Axis) -> T { |
| 515 | match axis { |
| 516 | Axis::Horizontal => self.width.clone(), |
| 517 | Axis::Vertical => self.height.clone(), |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Self { |
| 523 | match axis { |
| 524 | Axis::Horizontal => Size { |
| 525 | width: f(self.width.clone()), |
| 526 | height: self.height.clone(), |
| 527 | }, |
| 528 | Axis::Vertical => Size { |
| 529 | width: self.width.clone(), |
| 530 | height: f(self.height.clone()), |
| 531 | }, |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | impl<T> Size<T> |
| 537 | where |
| 538 | T: PartialOrd + Clone + Debug + Default + PartialEq, |
| 539 | { |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | pub fn max(&self, other: &Self) -> Self { |
| 556 | Size { |
| 557 | width: if self.width >= other.width { |
| 558 | self.width.clone() |
| 559 | } else { |
| 560 | other.width.clone() |
| 561 | }, |
| 562 | height: if self.height >= other.height { |
| 563 | self.height.clone() |
| 564 | } else { |
| 565 | other.height.clone() |
| 566 | }, |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | pub fn min(&self, other: &Self) -> Self { |
| 586 | Size { |
| 587 | width: if self.width >= other.width { |
| 588 | other.width.clone() |
| 589 | } else { |
| 590 | self.width.clone() |
| 591 | }, |
| 592 | height: if self.height >= other.height { |
| 593 | other.height.clone() |
| 594 | } else { |
| 595 | self.height.clone() |
| 596 | }, |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | impl<T> Sub for Size<T> |
| 602 | where |
| 603 | T: Sub<Output = T> + Clone + Debug + Default + PartialEq, |
| 604 | { |
| 605 | type Output = Size<T>; |
| 606 | |
| 607 | fn sub(self, rhs: Self) -> Self::Output { |
| 608 | Size { |
| 609 | width: self.width - rhs.width, |
| 610 | height: self.height - rhs.height, |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | impl<T> Add for Size<T> |
| 616 | where |
| 617 | T: Add<Output = T> + Clone + Debug + Default + PartialEq, |
| 618 | { |
| 619 | type Output = Size<T>; |
| 620 | |
| 621 | fn add(self, rhs: Self) -> Self::Output { |
| 622 | Size { |
| 623 | width: self.width + rhs.width, |
| 624 | height: self.height + rhs.height, |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | impl<T, Rhs> Mul<Rhs> for Size<T> |
| 630 | where |
| 631 | T: Mul<Rhs, Output = Rhs> + Clone + Debug + Default + PartialEq, |
| 632 | Rhs: Clone + Debug + Default + PartialEq, |
| 633 | { |
| 634 | type Output = Size<Rhs>; |
| 635 | |
| 636 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 637 | Size { |
| 638 | width: self.width * rhs.clone(), |
| 639 | height: self.height * rhs, |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | impl<T, S> MulAssign<S> for Size<T> |
| 645 | where |
| 646 | T: Mul<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 647 | S: Clone, |
| 648 | { |
| 649 | fn mul_assign(&mut self, rhs: S) { |
| 650 | self.width = self.width.clone() * rhs.clone(); |
| 651 | self.height = self.height.clone() * rhs; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | impl<T> Eq for Size<T> where T: Eq + Clone + Debug + Default + PartialEq {} |
| 656 | |
| 657 | impl<T> Debug for Size<T> |
| 658 | where |
| 659 | T: Clone + Debug + Default + PartialEq, |
| 660 | { |
| 661 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 662 | write!(f, "Size {{ {:?} × {:?} }}", self.width, self.height) |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | impl<T: Clone + Debug + Default + PartialEq + Display> Display for Size<T> { |
| 667 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 668 | write!(f, "{} × {}", self.width, self.height) |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | impl<T: Clone + Debug + Default + PartialEq> From<Point<T>> for Size<T> { |
| 673 | fn from(point: Point<T>) -> Self { |
| 674 | Self { |
| 675 | width: point.x, |
| 676 | height: point.y, |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | impl From<Size<Pixels>> for Size<DefiniteLength> { |
| 682 | fn from(size: Size<Pixels>) -> Self { |
| 683 | Size { |
| 684 | width: size.width.into(), |
| 685 | height: size.height.into(), |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | impl From<Size<Pixels>> for Size<AbsoluteLength> { |
| 691 | fn from(size: Size<Pixels>) -> Self { |
| 692 | Size { |
| 693 | width: size.width.into(), |
| 694 | height: size.height.into(), |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | impl Size<Length> { |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | pub fn full() -> Self { |
| 709 | Self { |
| 710 | width: relative(1.).into(), |
| 711 | height: relative(1.).into(), |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | impl Size<Length> { |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | pub fn auto() -> Self { |
| 727 | Self { |
| 728 | width: Length::Auto, |
| 729 | height: Length::Auto, |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | #[derive(Refineable, Clone, Default, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)] |
| 752 | #[refineable(Debug)] |
| 753 | #[repr(C)] |
| 754 | pub struct Bounds<T: Clone + Debug + Default + PartialEq> { |
| 755 | |
| 756 | pub origin: Point<T>, |
| 757 | |
| 758 | pub size: Size<T>, |
| 759 | } |
| 760 | |
| 761 | |
| 762 | pub fn bounds<T: Clone + Debug + Default + PartialEq>( |
| 763 | origin: Point<T>, |
| 764 | size: Size<T>, |
| 765 | ) -> Bounds<T> { |
| 766 | Bounds { origin, size } |
| 767 | } |
| 768 | |
| 769 | impl Bounds<Pixels> { |
| 770 | |
| 771 | pub fn centered(display_id: Option<DisplayId>, size: Size<Pixels>, cx: &App) -> Self { |
| 772 | let display = display_id |
| 773 | .and_then(|id| cx.find_display(id)) |
| 774 | .or_else(|| cx.primary_display()); |
| 775 | |
| 776 | display |
| 777 | .map(|display| Bounds::centered_at(display.bounds().center(), size)) |
| 778 | .unwrap_or_else(|| Bounds { |
| 779 | origin: point(px(0.), px(0.)), |
| 780 | size, |
| 781 | }) |
| 782 | } |
| 783 | |
| 784 | |
| 785 | pub fn maximized(display_id: Option<DisplayId>, cx: &App) -> Self { |
| 786 | let display = display_id |
| 787 | .and_then(|id| cx.find_display(id)) |
| 788 | .or_else(|| cx.primary_display()); |
| 789 | |
| 790 | display |
| 791 | .map(|display| display.bounds()) |
| 792 | .unwrap_or_else(|| Bounds { |
| 793 | origin: point(px(0.), px(0.)), |
| 794 | size: size(px(1024.), px(768.)), |
| 795 | }) |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | impl<T> Bounds<T> |
| 800 | where |
| 801 | T: Clone + Debug + Default + PartialEq, |
| 802 | { |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | pub fn new(origin: Point<T>, size: Size<T>) -> Self { |
| 814 | Bounds { origin, size } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | impl<T> Bounds<T> |
| 819 | where |
| 820 | T: Sub<Output = T> + Clone + Debug + Default + PartialEq, |
| 821 | { |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | pub fn from_corners(top_left: Point<T>, bottom_right: Point<T>) -> Self { |
| 850 | let origin = Point { |
| 851 | x: top_left.x.clone(), |
| 852 | y: top_left.y.clone(), |
| 853 | }; |
| 854 | let size = Size { |
| 855 | width: bottom_right.x - top_left.x, |
| 856 | height: bottom_right.y - top_left.y, |
| 857 | }; |
| 858 | Bounds { origin, size } |
| 859 | } |
| 860 | |
| 861 | |
| 862 | |
| 863 | pub fn from_corner_and_size(corner: Corner, origin: Point<T>, size: Size<T>) -> Bounds<T> { |
| 864 | let origin = match corner { |
| 865 | Corner::TopLeft => origin, |
| 866 | Corner::TopRight => Point { |
| 867 | x: origin.x - size.width.clone(), |
| 868 | y: origin.y, |
| 869 | }, |
| 870 | Corner::BottomLeft => Point { |
| 871 | x: origin.x, |
| 872 | y: origin.y - size.height.clone(), |
| 873 | }, |
| 874 | Corner::BottomRight => Point { |
| 875 | x: origin.x - size.width.clone(), |
| 876 | y: origin.y - size.height.clone(), |
| 877 | }, |
| 878 | }; |
| 879 | |
| 880 | Bounds { origin, size } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | impl<T> Bounds<T> |
| 885 | where |
| 886 | T: Sub<T, Output = T> + Half + Clone + Debug + Default + PartialEq, |
| 887 | { |
| 888 | |
| 889 | pub fn centered_at(center: Point<T>, size: Size<T>) -> Self { |
| 890 | let origin = Point { |
| 891 | x: center.x - size.width.half(), |
| 892 | y: center.y - size.height.half(), |
| 893 | }; |
| 894 | Self::new(origin, size) |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | impl<T> Bounds<T> |
| 899 | where |
| 900 | T: PartialOrd + Add<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 901 | { |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | pub fn intersects(&self, other: &Bounds<T>) -> bool { |
| 936 | let my_lower_right = self.bottom_right(); |
| 937 | let their_lower_right = other.bottom_right(); |
| 938 | |
| 939 | self.origin.x < their_lower_right.x |
| 940 | && my_lower_right.x > other.origin.x |
| 941 | && self.origin.y < their_lower_right.y |
| 942 | && my_lower_right.y > other.origin.y |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | impl<T> Bounds<T> |
| 947 | where |
| 948 | T: Add<T, Output = T> + Half + Clone + Debug + Default + PartialEq, |
| 949 | { |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | pub fn center(&self) -> Point<T> { |
| 972 | Point { |
| 973 | x: self.origin.x.clone() + self.size.width.clone().half(), |
| 974 | y: self.origin.y.clone() + self.size.height.clone().half(), |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | impl<T> Bounds<T> |
| 980 | where |
| 981 | T: Add<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 982 | { |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | pub fn half_perimeter(&self) -> T { |
| 1002 | self.size.width.clone() + self.size.height.clone() |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | impl<T> Bounds<T> |
| 1007 | where |
| 1008 | T: Add<T, Output = T> + Sub<Output = T> + Clone + Debug + Default + PartialEq, |
| 1009 | { |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | #[must_use] |
| 1037 | pub fn dilate(&self, amount: T) -> Bounds<T> { |
| 1038 | let double_amount = amount.clone() + amount.clone(); |
| 1039 | Bounds { |
| 1040 | origin: self.origin.clone() - point(amount.clone(), amount), |
| 1041 | size: self.size.clone() + size(double_amount.clone(), double_amount), |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | #[must_use] |
| 1047 | pub fn extend(&self, amount: Edges<T>) -> Bounds<T> { |
| 1048 | Bounds { |
| 1049 | origin: self.origin.clone() - point(amount.left.clone(), amount.top.clone()), |
| 1050 | size: self.size.clone() |
| 1051 | + size( |
| 1052 | amount.left.clone() + amount.right.clone(), |
| 1053 | amount.top.clone() + amount.bottom, |
| 1054 | ), |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | impl<T> Bounds<T> |
| 1060 | where |
| 1061 | T: Add<T, Output = T> |
| 1062 | + Sub<T, Output = T> |
| 1063 | + Neg<Output = T> |
| 1064 | + Clone |
| 1065 | + Debug |
| 1066 | + Default |
| 1067 | + PartialEq, |
| 1068 | { |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | pub fn inset(&self, amount: T) -> Self { |
| 1073 | self.dilate(-amount) |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | impl<T: PartialOrd + Add<T, Output = T> + Sub<Output = T> + Clone + Debug + Default + PartialEq> |
| 1078 | Bounds<T> |
| 1079 | { |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | pub fn intersect(&self, other: &Self) -> Self { |
| 1114 | let upper_left = self.origin.max(&other.origin); |
| 1115 | let bottom_right = self.bottom_right().min(&other.bottom_right()); |
| 1116 | Self::from_corners(upper_left, bottom_right) |
| 1117 | } |
| 1118 | |
| 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 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | pub fn union(&self, other: &Self) -> Self { |
| 1153 | let top_left = self.origin.min(&other.origin); |
| 1154 | let bottom_right = self.bottom_right().max(&other.bottom_right()); |
| 1155 | Bounds::from_corners(top_left, bottom_right) |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | impl<T> Bounds<T> |
| 1160 | where |
| 1161 | T: Add<T, Output = T> + Sub<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 1162 | { |
| 1163 | |
| 1164 | pub fn space_within(&self, outer: &Self) -> Edges<T> { |
| 1165 | Edges { |
| 1166 | top: self.top() - outer.top(), |
| 1167 | right: outer.right() - self.right(), |
| 1168 | bottom: outer.bottom() - self.bottom(), |
| 1169 | left: self.left() - outer.left(), |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | impl<T, Rhs> Mul<Rhs> for Bounds<T> |
| 1175 | where |
| 1176 | T: Mul<Rhs, Output = Rhs> + Clone + Debug + Default + PartialEq, |
| 1177 | Point<T>: Mul<Rhs, Output = Point<Rhs>>, |
| 1178 | Rhs: Clone + Debug + Default + PartialEq, |
| 1179 | { |
| 1180 | type Output = Bounds<Rhs>; |
| 1181 | |
| 1182 | fn mul(self, rhs: Rhs) -> Self::Output { |
| 1183 | Bounds { |
| 1184 | origin: self.origin * rhs.clone(), |
| 1185 | size: self.size * rhs, |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | impl<T, S> MulAssign<S> for Bounds<T> |
| 1191 | where |
| 1192 | T: Mul<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 1193 | S: Clone, |
| 1194 | { |
| 1195 | fn mul_assign(&mut self, rhs: S) { |
| 1196 | self.origin *= rhs.clone(); |
| 1197 | self.size *= rhs; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | impl<T, S> Div<S> for Bounds<T> |
| 1202 | where |
| 1203 | Size<T>: Div<S, Output = Size<T>>, |
| 1204 | T: Div<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 1205 | S: Clone, |
| 1206 | { |
| 1207 | type Output = Self; |
| 1208 | |
| 1209 | fn div(self, rhs: S) -> Self { |
| 1210 | Self { |
| 1211 | origin: self.origin / rhs.clone(), |
| 1212 | size: self.size / rhs, |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | impl<T> Add<Point<T>> for Bounds<T> |
| 1218 | where |
| 1219 | T: Add<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 1220 | { |
| 1221 | type Output = Self; |
| 1222 | |
| 1223 | fn add(self, rhs: Point<T>) -> Self { |
| 1224 | Self { |
| 1225 | origin: self.origin + rhs, |
| 1226 | size: self.size, |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | impl<T> Sub<Point<T>> for Bounds<T> |
| 1232 | where |
| 1233 | T: Sub<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 1234 | { |
| 1235 | type Output = Self; |
| 1236 | |
| 1237 | fn sub(self, rhs: Point<T>) -> Self { |
| 1238 | Self { |
| 1239 | origin: self.origin - rhs, |
| 1240 | size: self.size, |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | impl<T> Bounds<T> |
| 1246 | where |
| 1247 | T: Add<T, Output = T> + Clone + Debug + Default + PartialEq, |
| 1248 | { |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | pub fn top(&self) -> T { |
| 1255 | self.origin.y.clone() |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | pub fn bottom(&self) -> T { |
| 1264 | self.origin.y.clone() + self.size.height.clone() |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | pub fn left(&self) -> T { |
| 1273 | self.origin.x.clone() |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | pub fn right(&self) -> T { |
| 1282 | self.origin.x.clone() + self.size.width.clone() |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | pub fn top_right(&self) -> Point<T> { |
| 1303 | Point { |
| 1304 | x: self.origin.x.clone() + self.size.width.clone(), |
| 1305 | y: self.origin.y.clone(), |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | pub fn bottom_right(&self) -> Point<T> { |
| 1327 | Point { |
| 1328 | x: self.origin.x.clone() + self.size.width.clone(), |
| 1329 | y: self.origin.y.clone() + self.size.height.clone(), |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | pub fn bottom_left(&self) -> Point<T> { |
| 1351 | Point { |
| 1352 | x: self.origin.x.clone(), |
| 1353 | y: self.origin.y.clone() + self.size.height.clone(), |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | |
| 1363 | |
| 1364 | |
| 1365 | |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | pub fn corner(&self, corner: Corner) -> Point<T> { |
| 1375 | match corner { |
| 1376 | Corner::TopLeft => self.origin.clone(), |
| 1377 | Corner::TopRight => self.top_right(), |
| 1378 | Corner::BottomLeft => self.bottom_left(), |
| 1379 | Corner::BottomRight => self.bottom_right(), |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | impl<T> Bounds<T> |
| 1385 | where |
| 1386 | T: Add<T, Output = T> + PartialOrd + Clone + Debug + Default + PartialEq, |
| 1387 | { |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | pub fn contains(&self, point: &Point<T>) -> bool { |
| 1418 | point.x >= self.origin.x |
| 1419 | && point.x <= self.origin.x.clone() + self.size.width.clone() |
| 1420 | && point.y >= self.origin.y |
| 1421 | && point.y <= self.origin.y.clone() + self.size.height.clone() |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | pub fn is_contained_within(&self, other: &Self) -> bool { |
| 1459 | other.contains(&self.origin) && other.contains(&self.bottom_right()) |
| 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U> |
| 1492 | where |
| 1493 | U: Clone + Debug + Default + PartialEq, |
| 1494 | { |
| 1495 | Bounds { |
| 1496 | origin: self.origin.map(&f), |
| 1497 | size: self.size.map(f), |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | |
| 1518 | pub fn map_origin(self, f: impl Fn(T) -> T) -> Bounds<T> { |
| 1519 | Bounds { |
| 1520 | origin: self.origin.map(f), |
| 1521 | size: self.size, |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | pub fn map_size(self, f: impl Fn(T) -> T) -> Bounds<T> { |
| 1543 | Bounds { |
| 1544 | origin: self.origin, |
| 1545 | size: self.size.map(f), |
| 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | impl<T> Bounds<T> |
| 1551 | where |
| 1552 | T: Add<T, Output = T> + Sub<T, Output = T> + PartialOrd + Clone + Debug + Default + PartialEq, |
| 1553 | { |
| 1554 | |
| 1555 | pub fn localize(&self, point: &Point<T>) -> Option<Point<T>> { |
| 1556 | self.contains(point) |
| 1557 | .then(|| point.relative_to(&self.origin)) |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | impl<T: PartialOrd + Clone + Debug + Default + PartialEq> Bounds<T> { |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | #[must_use] |
| 1573 | pub fn is_empty(&self) -> bool { |
| 1574 | self.size.width <= T::default() || self.size.height <= T::default() |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | impl<T: Clone + Debug + Default + PartialEq + Display + Add<T, Output = T>> Display for Bounds<T> { |
| 1579 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1580 | write!( |
| 1581 | f, |
| 1582 | "{} - {} (size {})", |
| 1583 | self.origin, |
| 1584 | self.bottom_right(), |
| 1585 | self.size |
| 1586 | ) |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | impl Size<DevicePixels> { |
| 1591 | |
| 1592 | pub(crate) fn to_pixels(self, scale_factor: f32) -> Size<Pixels> { |
| 1593 | size( |
| 1594 | px(self.width.0 as f32 / scale_factor), |
| 1595 | px(self.height.0 as f32 / scale_factor), |
| 1596 | ) |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | impl Size<Pixels> { |
| 1601 | |
| 1602 | pub(crate) fn to_device_pixels(self, scale_factor: f32) -> Size<DevicePixels> { |
| 1603 | size( |
| 1604 | DevicePixels((self.width.0 * scale_factor).round() as i32), |
| 1605 | DevicePixels((self.height.0 * scale_factor).round() as i32), |
| 1606 | ) |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | impl Bounds<Pixels> { |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | |
| 1624 | |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | |
| 1644 | |
| 1645 | |
| 1646 | |
| 1647 | pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> { |
| 1648 | Bounds { |
| 1649 | origin: self.origin.scale(factor), |
| 1650 | size: self.size.scale(factor), |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | pub fn to_device_pixels(self, factor: f32) -> Bounds<DevicePixels> { |
| 1656 | Bounds { |
| 1657 | origin: point( |
| 1658 | DevicePixels((self.origin.x.0 * factor).round() as i32), |
| 1659 | DevicePixels((self.origin.y.0 * factor).round() as i32), |
| 1660 | ), |
| 1661 | size: self.size.to_device_pixels(factor), |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | impl Bounds<DevicePixels> { |
| 1667 | |
| 1668 | pub fn to_pixels(self, scale_factor: f32) -> Bounds<Pixels> { |
| 1669 | Bounds { |
| 1670 | origin: point( |
| 1671 | px(self.origin.x.0 as f32 / scale_factor), |
| 1672 | px(self.origin.y.0 as f32 / scale_factor), |
| 1673 | ), |
| 1674 | size: self.size.to_pixels(scale_factor), |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | impl<T: Copy + Clone + Debug + Default + PartialEq> Copy for Bounds<T> {} |
| 1680 | |
| 1681 | |
| 1682 | |
| 1683 | |
| 1684 | |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | |
| 1690 | |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | |
| 1697 | |
| 1698 | |
| 1699 | |
| 1700 | |
| 1701 | #[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)] |
| 1702 | #[refineable(Debug, PartialEq, Serialize, Deserialize, JsonSchema)] |
| 1703 | #[repr(C)] |
| 1704 | pub struct Edges<T: Clone + Debug + Default + PartialEq> { |
| 1705 | |
| 1706 | pub top: T, |
| 1707 | |
| 1708 | pub right: T, |
| 1709 | |
| 1710 | pub bottom: T, |
| 1711 | |
| 1712 | pub left: T, |
| 1713 | } |
| 1714 | |
| 1715 | impl<T> Mul for Edges<T> |
| 1716 | where |
| 1717 | T: Mul<Output = T> + Clone + Debug + Default + PartialEq, |
| 1718 | { |
| 1719 | type Output = Self; |
| 1720 | |
| 1721 | fn mul(self, rhs: Self) -> Self::Output { |
| 1722 | Self { |
| 1723 | top: self.top.clone() * rhs.top, |
| 1724 | right: self.right.clone() * rhs.right, |
| 1725 | bottom: self.bottom.clone() * rhs.bottom, |
| 1726 | left: self.left * rhs.left, |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | impl<T, S> MulAssign<S> for Edges<T> |
| 1732 | where |
| 1733 | T: Mul<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 1734 | S: Clone, |
| 1735 | { |
| 1736 | fn mul_assign(&mut self, rhs: S) { |
| 1737 | self.top = self.top.clone() * rhs.clone(); |
| 1738 | self.right = self.right.clone() * rhs.clone(); |
| 1739 | self.bottom = self.bottom.clone() * rhs.clone(); |
| 1740 | self.left = self.left.clone() * rhs; |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | impl<T: Clone + Debug + Default + PartialEq + Copy> Copy for Edges<T> {} |
| 1745 | |
| 1746 | impl<T: Clone + Debug + Default + PartialEq> Edges<T> { |
| 1747 | |
| 1748 | |
| 1749 | |
| 1750 | |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | |
| 1756 | |
| 1757 | |
| 1758 | |
| 1759 | |
| 1760 | |
| 1761 | |
| 1762 | |
| 1763 | |
| 1764 | |
| 1765 | |
| 1766 | |
| 1767 | |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | pub fn all(value: T) -> Self { |
| 1772 | Self { |
| 1773 | top: value.clone(), |
| 1774 | right: value.clone(), |
| 1775 | bottom: value.clone(), |
| 1776 | left: value, |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | |
| 1787 | |
| 1788 | |
| 1789 | |
| 1790 | |
| 1791 | |
| 1792 | |
| 1793 | |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | |
| 1800 | |
| 1801 | |
| 1802 | pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Edges<U> |
| 1803 | where |
| 1804 | U: Clone + Debug + Default + PartialEq, |
| 1805 | { |
| 1806 | Edges { |
| 1807 | top: f(&self.top), |
| 1808 | right: f(&self.right), |
| 1809 | bottom: f(&self.bottom), |
| 1810 | left: f(&self.left), |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | |
| 1828 | |
| 1829 | |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | pub fn any<F: Fn(&T) -> bool>(&self, predicate: F) -> bool { |
| 1842 | predicate(&self.top) |
| 1843 | || predicate(&self.right) |
| 1844 | || predicate(&self.bottom) |
| 1845 | || predicate(&self.left) |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | impl Edges<Length> { |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | |
| 1857 | |
| 1858 | |
| 1859 | |
| 1860 | |
| 1861 | |
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | |
| 1868 | pub fn auto() -> Self { |
| 1869 | Self { |
| 1870 | top: Length::Auto, |
| 1871 | right: Length::Auto, |
| 1872 | bottom: Length::Auto, |
| 1873 | left: Length::Auto, |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | |
| 1878 | |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | pub fn zero() -> Self { |
| 1897 | Self { |
| 1898 | top: px(0.).into(), |
| 1899 | right: px(0.).into(), |
| 1900 | bottom: px(0.).into(), |
| 1901 | left: px(0.).into(), |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | impl Edges<DefiniteLength> { |
| 1907 | |
| 1908 | |
| 1909 | |
| 1910 | |
| 1911 | |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | |
| 1916 | |
| 1917 | |
| 1918 | |
| 1919 | |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | |
| 1924 | |
| 1925 | |
| 1926 | pub fn zero() -> Self { |
| 1927 | Self { |
| 1928 | top: px(0.).into(), |
| 1929 | right: px(0.).into(), |
| 1930 | bottom: px(0.).into(), |
| 1931 | left: px(0.).into(), |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | |
| 1936 | |
| 1937 | |
| 1938 | |
| 1939 | |
| 1940 | |
| 1941 | |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | |
| 1966 | |
| 1967 | |
| 1968 | |
| 1969 | |
| 1970 | |
| 1971 | pub fn to_pixels(self, parent_size: Size<AbsoluteLength>, rem_size: Pixels) -> Edges<Pixels> { |
| 1972 | Edges { |
| 1973 | top: self.top.to_pixels(parent_size.height, rem_size), |
| 1974 | right: self.right.to_pixels(parent_size.width, rem_size), |
| 1975 | bottom: self.bottom.to_pixels(parent_size.height, rem_size), |
| 1976 | left: self.left.to_pixels(parent_size.width, rem_size), |
| 1977 | } |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | impl Edges<AbsoluteLength> { |
| 1982 | |
| 1983 | |
| 1984 | |
| 1985 | |
| 1986 | |
| 1987 | |
| 1988 | |
| 1989 | |
| 1990 | |
| 1991 | |
| 1992 | |
| 1993 | |
| 1994 | |
| 1995 | |
| 1996 | |
| 1997 | |
| 1998 | |
| 1999 | |
| 2000 | |
| 2001 | pub fn zero() -> Self { |
| 2002 | Self { |
| 2003 | top: px(0.).into(), |
| 2004 | right: px(0.).into(), |
| 2005 | bottom: px(0.).into(), |
| 2006 | left: px(0.).into(), |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | |
| 2011 | |
| 2012 | |
| 2013 | |
| 2014 | |
| 2015 | |
| 2016 | |
| 2017 | |
| 2018 | |
| 2019 | |
| 2020 | |
| 2021 | |
| 2022 | |
| 2023 | |
| 2024 | |
| 2025 | |
| 2026 | |
| 2027 | |
| 2028 | |
| 2029 | |
| 2030 | |
| 2031 | |
| 2032 | |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | |
| 2040 | |
| 2041 | pub fn to_pixels(self, rem_size: Pixels) -> Edges<Pixels> { |
| 2042 | Edges { |
| 2043 | top: self.top.to_pixels(rem_size), |
| 2044 | right: self.right.to_pixels(rem_size), |
| 2045 | bottom: self.bottom.to_pixels(rem_size), |
| 2046 | left: self.left.to_pixels(rem_size), |
| 2047 | } |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | impl Edges<Pixels> { |
| 2052 | |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | |
| 2063 | |
| 2064 | |
| 2065 | |
| 2066 | |
| 2067 | |
| 2068 | |
| 2069 | |
| 2070 | |
| 2071 | |
| 2072 | |
| 2073 | |
| 2074 | |
| 2075 | |
| 2076 | |
| 2077 | |
| 2078 | |
| 2079 | |
| 2080 | pub fn scale(&self, factor: f32) -> Edges<ScaledPixels> { |
| 2081 | Edges { |
| 2082 | top: self.top.scale(factor), |
| 2083 | right: self.right.scale(factor), |
| 2084 | bottom: self.bottom.scale(factor), |
| 2085 | left: self.left.scale(factor), |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | |
| 2090 | |
| 2091 | |
| 2092 | |
| 2093 | |
| 2094 | pub fn max(&self) -> Pixels { |
| 2095 | self.top.max(self.right).max(self.bottom).max(self.left) |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | impl From<f32> for Edges<Pixels> { |
| 2100 | fn from(val: f32) -> Self { |
| 2101 | let val: Pixels = val.into(); |
| 2102 | val.into() |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | impl From<Pixels> for Edges<Pixels> { |
| 2107 | fn from(val: Pixels) -> Self { |
| 2108 | Edges { |
| 2109 | top: val, |
| 2110 | right: val, |
| 2111 | bottom: val, |
| 2112 | left: val, |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | |
| 2118 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 2119 | pub enum Corner { |
| 2120 | |
| 2121 | TopLeft, |
| 2122 | |
| 2123 | TopRight, |
| 2124 | |
| 2125 | BottomLeft, |
| 2126 | |
| 2127 | BottomRight, |
| 2128 | } |
| 2129 | |
| 2130 | impl Corner { |
| 2131 | |
| 2132 | |
| 2133 | |
| 2134 | |
| 2135 | |
| 2136 | |
| 2137 | |
| 2138 | |
| 2139 | #[must_use] |
| 2140 | pub fn opposite_corner(self) -> Self { |
| 2141 | match self { |
| 2142 | Corner::TopLeft => Corner::BottomRight, |
| 2143 | Corner::TopRight => Corner::BottomLeft, |
| 2144 | Corner::BottomLeft => Corner::TopRight, |
| 2145 | Corner::BottomRight => Corner::TopLeft, |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | |
| 2151 | |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | |
| 2158 | #[must_use] |
| 2159 | pub fn other_side_corner_along(self, axis: Axis) -> Self { |
| 2160 | match axis { |
| 2161 | Axis::Vertical => match self { |
| 2162 | Corner::TopLeft => Corner::BottomLeft, |
| 2163 | Corner::TopRight => Corner::BottomRight, |
| 2164 | Corner::BottomLeft => Corner::TopLeft, |
| 2165 | Corner::BottomRight => Corner::TopRight, |
| 2166 | }, |
| 2167 | Axis::Horizontal => match self { |
| 2168 | Corner::TopLeft => Corner::TopRight, |
| 2169 | Corner::TopRight => Corner::TopLeft, |
| 2170 | Corner::BottomLeft => Corner::BottomRight, |
| 2171 | Corner::BottomRight => Corner::BottomLeft, |
| 2172 | }, |
| 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | #[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)] |
| 2181 | #[refineable(Debug, PartialEq, Serialize, Deserialize, JsonSchema)] |
| 2182 | #[repr(C)] |
| 2183 | pub struct Corners<T: Clone + Debug + Default + PartialEq> { |
| 2184 | |
| 2185 | pub top_left: T, |
| 2186 | |
| 2187 | pub top_right: T, |
| 2188 | |
| 2189 | pub bottom_right: T, |
| 2190 | |
| 2191 | pub bottom_left: T, |
| 2192 | } |
| 2193 | |
| 2194 | impl<T> Corners<T> |
| 2195 | where |
| 2196 | T: Clone + Debug + Default + PartialEq, |
| 2197 | { |
| 2198 | |
| 2199 | |
| 2200 | |
| 2201 | |
| 2202 | |
| 2203 | |
| 2204 | |
| 2205 | |
| 2206 | |
| 2207 | |
| 2208 | |
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | |
| 2220 | |
| 2221 | |
| 2222 | pub fn all(value: T) -> Self { |
| 2223 | Self { |
| 2224 | top_left: value.clone(), |
| 2225 | top_right: value.clone(), |
| 2226 | bottom_right: value.clone(), |
| 2227 | bottom_left: value, |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | |
| 2247 | |
| 2248 | |
| 2249 | #[must_use] |
| 2250 | pub fn corner(&self, corner: Corner) -> T { |
| 2251 | match corner { |
| 2252 | Corner::TopLeft => self.top_left.clone(), |
| 2253 | Corner::TopRight => self.top_right.clone(), |
| 2254 | Corner::BottomLeft => self.bottom_left.clone(), |
| 2255 | Corner::BottomRight => self.bottom_right.clone(), |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | impl Corners<AbsoluteLength> { |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | |
| 2268 | |
| 2269 | |
| 2270 | |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | |
| 2277 | |
| 2278 | |
| 2279 | |
| 2280 | |
| 2281 | |
| 2282 | |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | pub fn to_pixels(self, rem_size: Pixels) -> Corners<Pixels> { |
| 2290 | Corners { |
| 2291 | top_left: self.top_left.to_pixels(rem_size), |
| 2292 | top_right: self.top_right.to_pixels(rem_size), |
| 2293 | bottom_right: self.bottom_right.to_pixels(rem_size), |
| 2294 | bottom_left: self.bottom_left.to_pixels(rem_size), |
| 2295 | } |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | impl Corners<Pixels> { |
| 2300 | |
| 2301 | |
| 2302 | |
| 2303 | |
| 2304 | |
| 2305 | |
| 2306 | |
| 2307 | |
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | |
| 2316 | |
| 2317 | |
| 2318 | |
| 2319 | |
| 2320 | |
| 2321 | |
| 2322 | |
| 2323 | |
| 2324 | |
| 2325 | |
| 2326 | |
| 2327 | |
| 2328 | #[must_use] |
| 2329 | pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> { |
| 2330 | Corners { |
| 2331 | top_left: self.top_left.scale(factor), |
| 2332 | top_right: self.top_right.scale(factor), |
| 2333 | bottom_right: self.bottom_right.scale(factor), |
| 2334 | bottom_left: self.bottom_left.scale(factor), |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | |
| 2340 | |
| 2341 | |
| 2342 | |
| 2343 | #[must_use] |
| 2344 | pub fn max(&self) -> Pixels { |
| 2345 | self.top_left |
| 2346 | .max(self.top_right) |
| 2347 | .max(self.bottom_right) |
| 2348 | .max(self.bottom_left) |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | impl<T: Div<f32, Output = T> + Ord + Clone + Debug + Default + PartialEq> Corners<T> { |
| 2353 | |
| 2354 | |
| 2355 | |
| 2356 | |
| 2357 | |
| 2358 | |
| 2359 | |
| 2360 | |
| 2361 | |
| 2362 | #[must_use] |
| 2363 | pub fn clamp_radii_for_quad_size(self, size: Size<T>) -> Corners<T> { |
| 2364 | let max = cmp::min(size.width, size.height) / 2.; |
| 2365 | Corners { |
| 2366 | top_left: cmp::min(self.top_left, max.clone()), |
| 2367 | top_right: cmp::min(self.top_right, max.clone()), |
| 2368 | bottom_right: cmp::min(self.bottom_right, max.clone()), |
| 2369 | bottom_left: cmp::min(self.bottom_left, max), |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | impl<T: Clone + Debug + Default + PartialEq> Corners<T> { |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | |
| 2383 | |
| 2384 | |
| 2385 | |
| 2386 | |
| 2387 | |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
| 2395 | |
| 2396 | |
| 2397 | |
| 2398 | |
| 2399 | |
| 2400 | |
| 2401 | |
| 2402 | |
| 2403 | |
| 2404 | |
| 2405 | |
| 2406 | |
| 2407 | #[must_use] |
| 2408 | pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Corners<U> |
| 2409 | where |
| 2410 | U: Clone + Debug + Default + PartialEq, |
| 2411 | { |
| 2412 | Corners { |
| 2413 | top_left: f(&self.top_left), |
| 2414 | top_right: f(&self.top_right), |
| 2415 | bottom_right: f(&self.bottom_right), |
| 2416 | bottom_left: f(&self.bottom_left), |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | impl<T> Mul for Corners<T> |
| 2422 | where |
| 2423 | T: Mul<Output = T> + Clone + Debug + Default + PartialEq, |
| 2424 | { |
| 2425 | type Output = Self; |
| 2426 | |
| 2427 | fn mul(self, rhs: Self) -> Self::Output { |
| 2428 | Self { |
| 2429 | top_left: self.top_left.clone() * rhs.top_left, |
| 2430 | top_right: self.top_right.clone() * rhs.top_right, |
| 2431 | bottom_right: self.bottom_right.clone() * rhs.bottom_right, |
| 2432 | bottom_left: self.bottom_left * rhs.bottom_left, |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | impl<T, S> MulAssign<S> for Corners<T> |
| 2438 | where |
| 2439 | T: Mul<S, Output = T> + Clone + Debug + Default + PartialEq, |
| 2440 | S: Clone, |
| 2441 | { |
| 2442 | fn mul_assign(&mut self, rhs: S) { |
| 2443 | self.top_left = self.top_left.clone() * rhs.clone(); |
| 2444 | self.top_right = self.top_right.clone() * rhs.clone(); |
| 2445 | self.bottom_right = self.bottom_right.clone() * rhs.clone(); |
| 2446 | self.bottom_left = self.bottom_left.clone() * rhs; |
| 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | impl<T> Copy for Corners<T> where T: Copy + Clone + Debug + Default + PartialEq {} |
| 2451 | |
| 2452 | impl From<f32> for Corners<Pixels> { |
| 2453 | fn from(val: f32) -> Self { |
| 2454 | Corners { |
| 2455 | top_left: val.into(), |
| 2456 | top_right: val.into(), |
| 2457 | bottom_right: val.into(), |
| 2458 | bottom_left: val.into(), |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | impl From<Pixels> for Corners<Pixels> { |
| 2464 | fn from(val: Pixels) -> Self { |
| 2465 | Corners { |
| 2466 | top_left: val, |
| 2467 | top_right: val, |
| 2468 | bottom_right: val, |
| 2469 | bottom_left: val, |
| 2470 | } |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | |
| 2475 | #[derive( |
| 2476 | Clone, |
| 2477 | Copy, |
| 2478 | Default, |
| 2479 | Add, |
| 2480 | AddAssign, |
| 2481 | Sub, |
| 2482 | SubAssign, |
| 2483 | Neg, |
| 2484 | Div, |
| 2485 | DivAssign, |
| 2486 | PartialEq, |
| 2487 | Serialize, |
| 2488 | Deserialize, |
| 2489 | Debug, |
| 2490 | )] |
| 2491 | #[repr(transparent)] |
| 2492 | pub struct Radians(pub f32); |
| 2493 | |
| 2494 | |
| 2495 | pub fn radians(value: f32) -> Radians { |
| 2496 | Radians(value) |
| 2497 | } |
| 2498 | |
| 2499 | |
| 2500 | #[derive( |
| 2501 | Clone, |
| 2502 | Copy, |
| 2503 | Default, |
| 2504 | Add, |
| 2505 | AddAssign, |
| 2506 | Sub, |
| 2507 | SubAssign, |
| 2508 | Neg, |
| 2509 | Div, |
| 2510 | DivAssign, |
| 2511 | PartialEq, |
| 2512 | Serialize, |
| 2513 | Deserialize, |
| 2514 | Debug, |
| 2515 | )] |
| 2516 | #[repr(transparent)] |
| 2517 | pub struct Percentage(pub f32); |
| 2518 | |
| 2519 | |
| 2520 | pub fn percentage(value: f32) -> Percentage { |
| 2521 | debug_assert!( |
| 2522 | (0.0..=1.0).contains(&value), |
| 2523 | "Percentage must be between 0 and 1" |
| 2524 | ); |
| 2525 | Percentage(value) |
| 2526 | } |
| 2527 | |
| 2528 | impl From<Percentage> for Radians { |
| 2529 | fn from(value: Percentage) -> Self { |
| 2530 | radians(value.0 * std::f32::consts::PI * 2.0) |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | |
| 2535 | |
| 2536 | |
| 2537 | |
| 2538 | |
| 2539 | |
| 2540 | |
| 2541 | |
| 2542 | |
| 2543 | |
| 2544 | |
| 2545 | |
| 2546 | |
| 2547 | |
| 2548 | |
| 2549 | |
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | #[derive( |
| 2557 | Clone, |
| 2558 | Copy, |
| 2559 | Default, |
| 2560 | Add, |
| 2561 | AddAssign, |
| 2562 | Sub, |
| 2563 | SubAssign, |
| 2564 | Neg, |
| 2565 | Div, |
| 2566 | DivAssign, |
| 2567 | PartialEq, |
| 2568 | Serialize, |
| 2569 | Deserialize, |
| 2570 | JsonSchema, |
| 2571 | )] |
| 2572 | #[repr(transparent)] |
| 2573 | pub struct Pixels(pub(crate) f32); |
| 2574 | |
| 2575 | impl Div for Pixels { |
| 2576 | type Output = f32; |
| 2577 | |
| 2578 | fn div(self, rhs: Self) -> Self::Output { |
| 2579 | self.0 / rhs.0 |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | impl std::ops::DivAssign for Pixels { |
| 2584 | fn div_assign(&mut self, rhs: Self) { |
| 2585 | *self = Self(self.0 / rhs.0); |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | impl std::ops::RemAssign for Pixels { |
| 2590 | fn rem_assign(&mut self, rhs: Self) { |
| 2591 | self.0 %= rhs.0; |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | impl std::ops::Rem for Pixels { |
| 2596 | type Output = Self; |
| 2597 | |
| 2598 | fn rem(self, rhs: Self) -> Self { |
| 2599 | Self(self.0 % rhs.0) |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | impl Mul<f32> for Pixels { |
| 2604 | type Output = Self; |
| 2605 | |
| 2606 | fn mul(self, rhs: f32) -> Self { |
| 2607 | Self(self.0 * rhs) |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | impl Mul<Pixels> for f32 { |
| 2612 | type Output = Pixels; |
| 2613 | |
| 2614 | fn mul(self, rhs: Pixels) -> Self::Output { |
| 2615 | rhs * self |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | impl Mul<usize> for Pixels { |
| 2620 | type Output = Self; |
| 2621 | |
| 2622 | fn mul(self, rhs: usize) -> Self { |
| 2623 | self * (rhs as f32) |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | impl Mul<Pixels> for usize { |
| 2628 | type Output = Pixels; |
| 2629 | |
| 2630 | fn mul(self, rhs: Pixels) -> Pixels { |
| 2631 | rhs * self |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | impl MulAssign<f32> for Pixels { |
| 2636 | fn mul_assign(&mut self, rhs: f32) { |
| 2637 | self.0 *= rhs; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | impl Display for Pixels { |
| 2642 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2643 | write!(f, "{}px", self.0) |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | impl Debug for Pixels { |
| 2648 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2649 | Display::fmt(self, f) |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | impl TryFrom<&'_ str> for Pixels { |
| 2654 | type Error = anyhow::Error; |
| 2655 | |
| 2656 | fn try_from(value: &'_ str) -> Result<Self, Self::Error> { |
| 2657 | value |
| 2658 | .strip_suffix("px") |
| 2659 | .context("expected 'px' suffix") |
| 2660 | .and_then(|number| Ok(number.parse()?)) |
| 2661 | .map(Self) |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | impl Pixels { |
| 2666 | |
| 2667 | pub const ZERO: Pixels = Pixels(0.0); |
| 2668 | |
| 2669 | pub const MAX: Pixels = Pixels(f32::MAX); |
| 2670 | |
| 2671 | pub const MIN: Pixels = Pixels(f32::MIN); |
| 2672 | |
| 2673 | |
| 2674 | |
| 2675 | |
| 2676 | |
| 2677 | |
| 2678 | pub fn floor(&self) -> Self { |
| 2679 | Self(self.0.floor()) |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | |
| 2684 | |
| 2685 | |
| 2686 | |
| 2687 | pub fn round(&self) -> Self { |
| 2688 | Self(self.0.round()) |
| 2689 | } |
| 2690 | |
| 2691 | |
| 2692 | |
| 2693 | |
| 2694 | |
| 2695 | |
| 2696 | pub fn ceil(&self) -> Self { |
| 2697 | Self(self.0.ceil()) |
| 2698 | } |
| 2699 | |
| 2700 | |
| 2701 | |
| 2702 | |
| 2703 | |
| 2704 | |
| 2705 | |
| 2706 | |
| 2707 | |
| 2708 | #[must_use] |
| 2709 | pub fn scale(&self, factor: f32) -> ScaledPixels { |
| 2710 | ScaledPixels(self.0 * factor) |
| 2711 | } |
| 2712 | |
| 2713 | |
| 2714 | |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | |
| 2719 | |
| 2720 | |
| 2721 | |
| 2722 | pub fn pow(&self, exponent: f32) -> Self { |
| 2723 | Self(self.0.powf(exponent)) |
| 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | |
| 2728 | |
| 2729 | |
| 2730 | |
| 2731 | pub fn abs(&self) -> Self { |
| 2732 | Self(self.0.abs()) |
| 2733 | } |
| 2734 | |
| 2735 | |
| 2736 | |
| 2737 | |
| 2738 | |
| 2739 | |
| 2740 | |
| 2741 | |
| 2742 | pub fn signum(&self) -> f32 { |
| 2743 | self.0.signum() |
| 2744 | } |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | |
| 2749 | |
| 2750 | |
| 2751 | pub fn to_f64(self) -> f64 { |
| 2752 | self.0 as f64 |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | impl Eq for Pixels {} |
| 2757 | |
| 2758 | impl PartialOrd for Pixels { |
| 2759 | fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { |
| 2760 | Some(self.cmp(other)) |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | impl Ord for Pixels { |
| 2765 | fn cmp(&self, other: &Self) -> cmp::Ordering { |
| 2766 | self.0.total_cmp(&other.0) |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | impl std::hash::Hash for Pixels { |
| 2771 | fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 2772 | self.0.to_bits().hash(state); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | impl From<f64> for Pixels { |
| 2777 | fn from(pixels: f64) -> Self { |
| 2778 | Pixels(pixels as f32) |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | impl From<f32> for Pixels { |
| 2783 | fn from(pixels: f32) -> Self { |
| 2784 | Pixels(pixels) |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | impl From<Pixels> for f32 { |
| 2789 | fn from(pixels: Pixels) -> Self { |
| 2790 | pixels.0 |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | impl From<&Pixels> for f32 { |
| 2795 | fn from(pixels: &Pixels) -> Self { |
| 2796 | pixels.0 |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | impl From<Pixels> for f64 { |
| 2801 | fn from(pixels: Pixels) -> Self { |
| 2802 | pixels.0 as f64 |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | impl From<Pixels> for u32 { |
| 2807 | fn from(pixels: Pixels) -> Self { |
| 2808 | pixels.0 as u32 |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | impl From<&Pixels> for u32 { |
| 2813 | fn from(pixels: &Pixels) -> Self { |
| 2814 | pixels.0 as u32 |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | impl From<u32> for Pixels { |
| 2819 | fn from(pixels: u32) -> Self { |
| 2820 | Pixels(pixels as f32) |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | impl From<Pixels> for usize { |
| 2825 | fn from(pixels: Pixels) -> Self { |
| 2826 | pixels.0 as usize |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | impl From<usize> for Pixels { |
| 2831 | fn from(pixels: usize) -> Self { |
| 2832 | Pixels(pixels as f32) |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | |
| 2837 | |
| 2838 | |
| 2839 | |
| 2840 | |
| 2841 | |
| 2842 | |
| 2843 | #[derive( |
| 2844 | Add, |
| 2845 | AddAssign, |
| 2846 | Clone, |
| 2847 | Copy, |
| 2848 | Default, |
| 2849 | Div, |
| 2850 | Eq, |
| 2851 | Hash, |
| 2852 | Ord, |
| 2853 | PartialEq, |
| 2854 | PartialOrd, |
| 2855 | Sub, |
| 2856 | SubAssign, |
| 2857 | Serialize, |
| 2858 | Deserialize, |
| 2859 | )] |
| 2860 | #[repr(transparent)] |
| 2861 | pub struct DevicePixels(pub i32); |
| 2862 | |
| 2863 | impl DevicePixels { |
| 2864 | |
| 2865 | |
| 2866 | |
| 2867 | |
| 2868 | |
| 2869 | |
| 2870 | |
| 2871 | |
| 2872 | |
| 2873 | |
| 2874 | |
| 2875 | |
| 2876 | |
| 2877 | |
| 2878 | |
| 2879 | |
| 2880 | |
| 2881 | |
| 2882 | |
| 2883 | |
| 2884 | |
| 2885 | |
| 2886 | pub fn to_bytes(self, bytes_per_pixel: u8) -> u32 { |
| 2887 | self.0 as u32 * bytes_per_pixel as u32 |
| 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | impl fmt::Debug for DevicePixels { |
| 2892 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2893 | write!(f, "{} px (device)", self.0) |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | impl From<DevicePixels> for i32 { |
| 2898 | fn from(device_pixels: DevicePixels) -> Self { |
| 2899 | device_pixels.0 |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | impl From<i32> for DevicePixels { |
| 2904 | fn from(device_pixels: i32) -> Self { |
| 2905 | DevicePixels(device_pixels) |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | impl From<u32> for DevicePixels { |
| 2910 | fn from(device_pixels: u32) -> Self { |
| 2911 | DevicePixels(device_pixels as i32) |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | impl From<DevicePixels> for u32 { |
| 2916 | fn from(device_pixels: DevicePixels) -> Self { |
| 2917 | device_pixels.0 as u32 |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | impl From<DevicePixels> for u64 { |
| 2922 | fn from(device_pixels: DevicePixels) -> Self { |
| 2923 | device_pixels.0 as u64 |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | impl From<u64> for DevicePixels { |
| 2928 | fn from(device_pixels: u64) -> Self { |
| 2929 | DevicePixels(device_pixels as i32) |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | impl From<DevicePixels> for usize { |
| 2934 | fn from(device_pixels: DevicePixels) -> Self { |
| 2935 | device_pixels.0 as usize |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | impl From<usize> for DevicePixels { |
| 2940 | fn from(device_pixels: usize) -> Self { |
| 2941 | DevicePixels(device_pixels as i32) |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | |
| 2946 | |
| 2947 | |
| 2948 | |
| 2949 | |
| 2950 | |
| 2951 | |
| 2952 | #[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, DivAssign, PartialEq)] |
| 2953 | #[repr(transparent)] |
| 2954 | pub struct ScaledPixels(pub(crate) f32); |
| 2955 | |
| 2956 | impl ScaledPixels { |
| 2957 | |
| 2958 | |
| 2959 | |
| 2960 | |
| 2961 | |
| 2962 | pub fn floor(&self) -> Self { |
| 2963 | Self(self.0.floor()) |
| 2964 | } |
| 2965 | |
| 2966 | |
| 2967 | |
| 2968 | |
| 2969 | |
| 2970 | |
| 2971 | pub fn round(&self) -> Self { |
| 2972 | Self(self.0.round()) |
| 2973 | } |
| 2974 | |
| 2975 | |
| 2976 | |
| 2977 | |
| 2978 | |
| 2979 | |
| 2980 | pub fn ceil(&self) -> Self { |
| 2981 | Self(self.0.ceil()) |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | impl Eq for ScaledPixels {} |
| 2986 | |
| 2987 | impl PartialOrd for ScaledPixels { |
| 2988 | fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { |
| 2989 | Some(self.cmp(other)) |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | impl Ord for ScaledPixels { |
| 2994 | fn cmp(&self, other: &Self) -> cmp::Ordering { |
| 2995 | self.0.total_cmp(&other.0) |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | impl Debug for ScaledPixels { |
| 3000 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3001 | write!(f, "{}px (scaled)", self.0) |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | impl From<ScaledPixels> for DevicePixels { |
| 3006 | fn from(scaled: ScaledPixels) -> Self { |
| 3007 | DevicePixels(scaled.0.ceil() as i32) |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | impl From<DevicePixels> for ScaledPixels { |
| 3012 | fn from(device: DevicePixels) -> Self { |
| 3013 | ScaledPixels(device.0 as f32) |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | impl From<ScaledPixels> for f64 { |
| 3018 | fn from(scaled_pixels: ScaledPixels) -> Self { |
| 3019 | scaled_pixels.0 as f64 |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | impl From<ScaledPixels> for u32 { |
| 3024 | fn from(pixels: ScaledPixels) -> Self { |
| 3025 | pixels.0 as u32 |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | impl From<f32> for ScaledPixels { |
| 3030 | fn from(pixels: f32) -> Self { |
| 3031 | Self(pixels) |
| 3032 | } |
| 3033 | } |
| 3034 | |
| 3035 | impl Div for ScaledPixels { |
| 3036 | type Output = f32; |
| 3037 | |
| 3038 | fn div(self, rhs: Self) -> Self::Output { |
| 3039 | self.0 / rhs.0 |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | impl std::ops::DivAssign for ScaledPixels { |
| 3044 | fn div_assign(&mut self, rhs: Self) { |
| 3045 | *self = Self(self.0 / rhs.0); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | impl std::ops::RemAssign for ScaledPixels { |
| 3050 | fn rem_assign(&mut self, rhs: Self) { |
| 3051 | self.0 %= rhs.0; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | impl std::ops::Rem for ScaledPixels { |
| 3056 | type Output = Self; |
| 3057 | |
| 3058 | fn rem(self, rhs: Self) -> Self { |
| 3059 | Self(self.0 % rhs.0) |
| 3060 | } |
| 3061 | } |
| 3062 | |
| 3063 | impl Mul<f32> for ScaledPixels { |
| 3064 | type Output = Self; |
| 3065 | |
| 3066 | fn mul(self, rhs: f32) -> Self { |
| 3067 | Self(self.0 * rhs) |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | impl Mul<ScaledPixels> for f32 { |
| 3072 | type Output = ScaledPixels; |
| 3073 | |
| 3074 | fn mul(self, rhs: ScaledPixels) -> Self::Output { |
| 3075 | rhs * self |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | impl Mul<usize> for ScaledPixels { |
| 3080 | type Output = Self; |
| 3081 | |
| 3082 | fn mul(self, rhs: usize) -> Self { |
| 3083 | self * (rhs as f32) |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | impl Mul<ScaledPixels> for usize { |
| 3088 | type Output = ScaledPixels; |
| 3089 | |
| 3090 | fn mul(self, rhs: ScaledPixels) -> ScaledPixels { |
| 3091 | rhs * self |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | impl MulAssign<f32> for ScaledPixels { |
| 3096 | fn mul_assign(&mut self, rhs: f32) { |
| 3097 | self.0 *= rhs; |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | |
| 3102 | |
| 3103 | |
| 3104 | |
| 3105 | |
| 3106 | |
| 3107 | |
| 3108 | |
| 3109 | |
| 3110 | |
| 3111 | #[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg, PartialEq)] |
| 3112 | pub struct Rems(pub f32); |
| 3113 | |
| 3114 | impl Rems { |
| 3115 | |
| 3116 | pub fn to_pixels(self, rem_size: Pixels) -> Pixels { |
| 3117 | self * rem_size |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | impl Mul<Pixels> for Rems { |
| 3122 | type Output = Pixels; |
| 3123 | |
| 3124 | fn mul(self, other: Pixels) -> Pixels { |
| 3125 | Pixels(self.0 * other.0) |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | impl Display for Rems { |
| 3130 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3131 | write!(f, "{}rem", self.0) |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | impl Debug for Rems { |
| 3136 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3137 | Display::fmt(self, f) |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | impl TryFrom<&'_ str> for Rems { |
| 3142 | type Error = anyhow::Error; |
| 3143 | |
| 3144 | fn try_from(value: &'_ str) -> Result<Self, Self::Error> { |
| 3145 | value |
| 3146 | .strip_suffix("rem") |
| 3147 | .context("expected 'rem' suffix") |
| 3148 | .and_then(|number| Ok(number.parse()?)) |
| 3149 | .map(Self) |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | |
| 3154 | |
| 3155 | |
| 3156 | |
| 3157 | |
| 3158 | |
| 3159 | #[derive(Clone, Copy, Neg, PartialEq)] |
| 3160 | pub enum AbsoluteLength { |
| 3161 | |
| 3162 | Pixels(Pixels), |
| 3163 | |
| 3164 | Rems(Rems), |
| 3165 | } |
| 3166 | |
| 3167 | impl AbsoluteLength { |
| 3168 | |
| 3169 | pub fn is_zero(&self) -> bool { |
| 3170 | match self { |
| 3171 | AbsoluteLength::Pixels(px) => px.0 == 0.0, |
| 3172 | AbsoluteLength::Rems(rems) => rems.0 == 0.0, |
| 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | impl From<Pixels> for AbsoluteLength { |
| 3178 | fn from(pixels: Pixels) -> Self { |
| 3179 | AbsoluteLength::Pixels(pixels) |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | impl From<Rems> for AbsoluteLength { |
| 3184 | fn from(rems: Rems) -> Self { |
| 3185 | AbsoluteLength::Rems(rems) |
| 3186 | } |
| 3187 | } |
| 3188 | |
| 3189 | impl AbsoluteLength { |
| 3190 | |
| 3191 | |
| 3192 | |
| 3193 | |
| 3194 | |
| 3195 | |
| 3196 | |
| 3197 | |
| 3198 | |
| 3199 | |
| 3200 | |
| 3201 | |
| 3202 | |
| 3203 | |
| 3204 | |
| 3205 | |
| 3206 | |
| 3207 | |
| 3208 | |
| 3209 | |
| 3210 | |
| 3211 | pub fn to_pixels(self, rem_size: Pixels) -> Pixels { |
| 3212 | match self { |
| 3213 | AbsoluteLength::Pixels(pixels) => pixels, |
| 3214 | AbsoluteLength::Rems(rems) => rems.to_pixels(rem_size), |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | |
| 3219 | |
| 3220 | |
| 3221 | |
| 3222 | |
| 3223 | |
| 3224 | |
| 3225 | |
| 3226 | |
| 3227 | pub fn to_rems(self, rem_size: Pixels) -> Rems { |
| 3228 | match self { |
| 3229 | AbsoluteLength::Pixels(pixels) => Rems(pixels.0 / rem_size.0), |
| 3230 | AbsoluteLength::Rems(rems) => rems, |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | impl Default for AbsoluteLength { |
| 3236 | fn default() -> Self { |
| 3237 | px(0.).into() |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | impl Display for AbsoluteLength { |
| 3242 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3243 | match self { |
| 3244 | Self::Pixels(pixels) => write!(f, "{pixels}"), |
| 3245 | Self::Rems(rems) => write!(f, "{rems}"), |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | impl Debug for AbsoluteLength { |
| 3251 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3252 | Display::fmt(self, f) |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | const EXPECTED_ABSOLUTE_LENGTH: &str = "number with 'px' or 'rem' suffix"; |
| 3257 | |
| 3258 | impl TryFrom<&'_ str> for AbsoluteLength { |
| 3259 | type Error = anyhow::Error; |
| 3260 | |
| 3261 | fn try_from(value: &'_ str) -> Result<Self, Self::Error> { |
| 3262 | if let Ok(pixels) = value.try_into() { |
| 3263 | Ok(Self::Pixels(pixels)) |
| 3264 | } else if let Ok(rems) = value.try_into() { |
| 3265 | Ok(Self::Rems(rems)) |
| 3266 | } else { |
| 3267 | Err(anyhow!( |
| 3268 | "invalid AbsoluteLength '{value}', expected {EXPECTED_ABSOLUTE_LENGTH}" |
| 3269 | )) |
| 3270 | } |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | impl JsonSchema for AbsoluteLength { |
| 3275 | fn schema_name() -> Cow<'static, str> { |
| 3276 | "AbsoluteLength".into() |
| 3277 | } |
| 3278 | |
| 3279 | fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 3280 | json_schema!({ |
| 3281 | "type": "string", |
| 3282 | "pattern": r"^-?\d+(\.\d+)?(px|rem)$" |
| 3283 | }) |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | impl<'de> Deserialize<'de> for AbsoluteLength { |
| 3288 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 3289 | struct StringVisitor; |
| 3290 | |
| 3291 | impl de::Visitor<'_> for StringVisitor { |
| 3292 | type Value = AbsoluteLength; |
| 3293 | |
| 3294 | fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 3295 | write!(f, "{EXPECTED_ABSOLUTE_LENGTH}") |
| 3296 | } |
| 3297 | |
| 3298 | fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> { |
| 3299 | AbsoluteLength::try_from(value).map_err(E::custom) |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | deserializer.deserialize_str(StringVisitor) |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | impl Serialize for AbsoluteLength { |
| 3308 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 3309 | where |
| 3310 | S: Serializer, |
| 3311 | { |
| 3312 | serializer.serialize_str(&format!("{self}")) |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | |
| 3317 | |
| 3318 | |
| 3319 | |
| 3320 | |
| 3321 | #[derive(Clone, Copy, Neg, PartialEq)] |
| 3322 | pub enum DefiniteLength { |
| 3323 | |
| 3324 | Absolute(AbsoluteLength), |
| 3325 | |
| 3326 | Fraction(f32), |
| 3327 | } |
| 3328 | |
| 3329 | impl DefiniteLength { |
| 3330 | |
| 3331 | |
| 3332 | |
| 3333 | |
| 3334 | |
| 3335 | |
| 3336 | |
| 3337 | |
| 3338 | |
| 3339 | |
| 3340 | |
| 3341 | |
| 3342 | |
| 3343 | |
| 3344 | |
| 3345 | |
| 3346 | |
| 3347 | |
| 3348 | |
| 3349 | |
| 3350 | |
| 3351 | |
| 3352 | |
| 3353 | |
| 3354 | |
| 3355 | |
| 3356 | |
| 3357 | |
| 3358 | pub fn to_pixels(self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels { |
| 3359 | match self { |
| 3360 | DefiniteLength::Absolute(size) => size.to_pixels(rem_size), |
| 3361 | DefiniteLength::Fraction(fraction) => match base_size { |
| 3362 | AbsoluteLength::Pixels(px) => px * fraction, |
| 3363 | AbsoluteLength::Rems(rems) => rems * rem_size * fraction, |
| 3364 | }, |
| 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | impl Debug for DefiniteLength { |
| 3370 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3371 | Display::fmt(self, f) |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | impl Display for DefiniteLength { |
| 3376 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3377 | match self { |
| 3378 | DefiniteLength::Absolute(length) => write!(f, "{length}"), |
| 3379 | DefiniteLength::Fraction(fraction) => write!(f, "{}%", (fraction * 100.0) as i32), |
| 3380 | } |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | const EXPECTED_DEFINITE_LENGTH: &str = "expected number with 'px', 'rem', or '%' suffix"; |
| 3385 | |
| 3386 | impl TryFrom<&'_ str> for DefiniteLength { |
| 3387 | type Error = anyhow::Error; |
| 3388 | |
| 3389 | fn try_from(value: &'_ str) -> Result<Self, Self::Error> { |
| 3390 | if let Some(percentage) = value.strip_suffix('%') { |
| 3391 | let fraction: f32 = percentage.parse::<f32>().with_context(|| { |
| 3392 | format!("invalid DefiniteLength '{value}', expected {EXPECTED_DEFINITE_LENGTH}") |
| 3393 | })?; |
| 3394 | Ok(DefiniteLength::Fraction(fraction / 100.0)) |
| 3395 | } else if let Ok(absolute_length) = value.try_into() { |
| 3396 | Ok(DefiniteLength::Absolute(absolute_length)) |
| 3397 | } else { |
| 3398 | Err(anyhow!( |
| 3399 | "invalid DefiniteLength '{value}', expected {EXPECTED_DEFINITE_LENGTH}" |
| 3400 | )) |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | impl JsonSchema for DefiniteLength { |
| 3406 | fn schema_name() -> Cow<'static, str> { |
| 3407 | "DefiniteLength".into() |
| 3408 | } |
| 3409 | |
| 3410 | fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 3411 | json_schema!({ |
| 3412 | "type": "string", |
| 3413 | "pattern": r"^-?\d+(\.\d+)?(px|rem|%)$" |
| 3414 | }) |
| 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | impl<'de> Deserialize<'de> for DefiniteLength { |
| 3419 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 3420 | struct StringVisitor; |
| 3421 | |
| 3422 | impl de::Visitor<'_> for StringVisitor { |
| 3423 | type Value = DefiniteLength; |
| 3424 | |
| 3425 | fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 3426 | write!(f, "{EXPECTED_DEFINITE_LENGTH}") |
| 3427 | } |
| 3428 | |
| 3429 | fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> { |
| 3430 | DefiniteLength::try_from(value).map_err(E::custom) |
| 3431 | } |
| 3432 | } |
| 3433 | |
| 3434 | deserializer.deserialize_str(StringVisitor) |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | impl Serialize for DefiniteLength { |
| 3439 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 3440 | where |
| 3441 | S: Serializer, |
| 3442 | { |
| 3443 | serializer.serialize_str(&format!("{self}")) |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | impl From<Pixels> for DefiniteLength { |
| 3448 | fn from(pixels: Pixels) -> Self { |
| 3449 | Self::Absolute(pixels.into()) |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | impl From<Rems> for DefiniteLength { |
| 3454 | fn from(rems: Rems) -> Self { |
| 3455 | Self::Absolute(rems.into()) |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | impl From<AbsoluteLength> for DefiniteLength { |
| 3460 | fn from(length: AbsoluteLength) -> Self { |
| 3461 | Self::Absolute(length) |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | impl Default for DefiniteLength { |
| 3466 | fn default() -> Self { |
| 3467 | Self::Absolute(AbsoluteLength::default()) |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | |
| 3472 | #[derive(Clone, Copy, PartialEq)] |
| 3473 | pub enum Length { |
| 3474 | |
| 3475 | Definite(DefiniteLength), |
| 3476 | |
| 3477 | Auto, |
| 3478 | } |
| 3479 | |
| 3480 | impl Debug for Length { |
| 3481 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3482 | Display::fmt(self, f) |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | impl Display for Length { |
| 3487 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 3488 | match self { |
| 3489 | Length::Definite(definite_length) => write!(f, "{}", definite_length), |
| 3490 | Length::Auto => write!(f, "auto"), |
| 3491 | } |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | const EXPECTED_LENGTH: &str = "expected 'auto' or number with 'px', 'rem', or '%' suffix"; |
| 3496 | |
| 3497 | impl TryFrom<&'_ str> for Length { |
| 3498 | type Error = anyhow::Error; |
| 3499 | |
| 3500 | fn try_from(value: &'_ str) -> Result<Self, Self::Error> { |
| 3501 | if value == "auto" { |
| 3502 | Ok(Length::Auto) |
| 3503 | } else if let Ok(definite_length) = value.try_into() { |
| 3504 | Ok(Length::Definite(definite_length)) |
| 3505 | } else { |
| 3506 | Err(anyhow!( |
| 3507 | "invalid Length '{value}', expected {EXPECTED_LENGTH}" |
| 3508 | )) |
| 3509 | } |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | impl JsonSchema for Length { |
| 3514 | fn schema_name() -> Cow<'static, str> { |
| 3515 | "Length".into() |
| 3516 | } |
| 3517 | |
| 3518 | fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 3519 | json_schema!({ |
| 3520 | "type": "string", |
| 3521 | "pattern": r"^(auto|-?\d+(\.\d+)?(px|rem|%))$" |
| 3522 | }) |
| 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | impl<'de> Deserialize<'de> for Length { |
| 3527 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 3528 | struct StringVisitor; |
| 3529 | |
| 3530 | impl de::Visitor<'_> for StringVisitor { |
| 3531 | type Value = Length; |
| 3532 | |
| 3533 | fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 3534 | write!(f, "{EXPECTED_LENGTH}") |
| 3535 | } |
| 3536 | |
| 3537 | fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> { |
| 3538 | Length::try_from(value).map_err(E::custom) |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | deserializer.deserialize_str(StringVisitor) |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | impl Serialize for Length { |
| 3547 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 3548 | where |
| 3549 | S: Serializer, |
| 3550 | { |
| 3551 | serializer.serialize_str(&format!("{self}")) |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | |
| 3556 | |
| 3557 | |
| 3558 | |
| 3559 | |
| 3560 | |
| 3561 | |
| 3562 | |
| 3563 | |
| 3564 | |
| 3565 | |
| 3566 | |
| 3567 | pub const fn relative(fraction: f32) -> DefiniteLength { |
| 3568 | DefiniteLength::Fraction(fraction) |
| 3569 | } |
| 3570 | |
| 3571 | |
| 3572 | pub fn phi() -> DefiniteLength { |
| 3573 | relative(1.618_034) |
| 3574 | } |
| 3575 | |
| 3576 | |
| 3577 | |
| 3578 | |
| 3579 | |
| 3580 | |
| 3581 | |
| 3582 | |
| 3583 | |
| 3584 | |
| 3585 | pub fn rems(rems: f32) -> Rems { |
| 3586 | Rems(rems) |
| 3587 | } |
| 3588 | |
| 3589 | |
| 3590 | |
| 3591 | |
| 3592 | |
| 3593 | |
| 3594 | |
| 3595 | |
| 3596 | |
| 3597 | |
| 3598 | pub const fn px(pixels: f32) -> Pixels { |
| 3599 | Pixels(pixels) |
| 3600 | } |
| 3601 | |
| 3602 | |
| 3603 | |
| 3604 | |
| 3605 | |
| 3606 | |
| 3607 | |
| 3608 | |
| 3609 | |
| 3610 | |
| 3611 | |
| 3612 | |
| 3613 | pub fn auto() -> Length { |
| 3614 | Length::Auto |
| 3615 | } |
| 3616 | |
| 3617 | impl From<Pixels> for Length { |
| 3618 | fn from(pixels: Pixels) -> Self { |
| 3619 | Self::Definite(pixels.into()) |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | impl From<Rems> for Length { |
| 3624 | fn from(rems: Rems) -> Self { |
| 3625 | Self::Definite(rems.into()) |
| 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | impl From<DefiniteLength> for Length { |
| 3630 | fn from(length: DefiniteLength) -> Self { |
| 3631 | Self::Definite(length) |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | impl From<AbsoluteLength> for Length { |
| 3636 | fn from(length: AbsoluteLength) -> Self { |
| 3637 | Self::Definite(length.into()) |
| 3638 | } |
| 3639 | } |
| 3640 | |
| 3641 | impl Default for Length { |
| 3642 | fn default() -> Self { |
| 3643 | Self::Definite(DefiniteLength::default()) |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | impl From<()> for Length { |
| 3648 | fn from(_: ()) -> Self { |
| 3649 | Self::Definite(DefiniteLength::default()) |
| 3650 | } |
| 3651 | } |
| 3652 | |
| 3653 | |
| 3654 | #[derive(Clone, PartialEq, Debug, Serialize, Deserialize, JsonSchema, Default)] |
| 3655 | pub struct GridLocation { |
| 3656 | |
| 3657 | pub row: Range<GridPlacement>, |
| 3658 | |
| 3659 | pub column: Range<GridPlacement>, |
| 3660 | } |
| 3661 | |
| 3662 | |
| 3663 | #[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize, JsonSchema, Default)] |
| 3664 | pub enum GridPlacement { |
| 3665 | |
| 3666 | Line(i16), |
| 3667 | |
| 3668 | Span(u16), |
| 3669 | |
| 3670 | #[default] |
| 3671 | Auto, |
| 3672 | } |
| 3673 | |
| 3674 | impl From<GridPlacement> for taffy::GridPlacement { |
| 3675 | fn from(placement: GridPlacement) -> Self { |
| 3676 | match placement { |
| 3677 | GridPlacement::Line(index) => taffy::GridPlacement::from_line_index(index), |
| 3678 | GridPlacement::Span(span) => taffy::GridPlacement::from_span(span), |
| 3679 | GridPlacement::Auto => taffy::GridPlacement::Auto, |
| 3680 | } |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | |
| 3685 | |
| 3686 | |
| 3687 | |
| 3688 | |
| 3689 | pub trait Half { |
| 3690 | |
| 3691 | |
| 3692 | |
| 3693 | |
| 3694 | |
| 3695 | fn half(&self) -> Self; |
| 3696 | } |
| 3697 | |
| 3698 | impl Half for i32 { |
| 3699 | fn half(&self) -> Self { |
| 3700 | self / 2 |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | impl Half for f32 { |
| 3705 | fn half(&self) -> Self { |
| 3706 | self / 2. |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | impl Half for DevicePixels { |
| 3711 | fn half(&self) -> Self { |
| 3712 | Self(self.0 / 2) |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | impl Half for ScaledPixels { |
| 3717 | fn half(&self) -> Self { |
| 3718 | Self(self.0 / 2.) |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | impl Half for Pixels { |
| 3723 | fn half(&self) -> Self { |
| 3724 | Self(self.0 / 2.) |
| 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | impl Half for Rems { |
| 3729 | fn half(&self) -> Self { |
| 3730 | Self(self.0 / 2.) |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | |
| 3735 | pub trait Negate { |
| 3736 | |
| 3737 | fn negate(self) -> Self; |
| 3738 | } |
| 3739 | |
| 3740 | impl Negate for i32 { |
| 3741 | fn negate(self) -> Self { |
| 3742 | -self |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | impl Negate for f32 { |
| 3747 | fn negate(self) -> Self { |
| 3748 | -self |
| 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | impl Negate for DevicePixels { |
| 3753 | fn negate(self) -> Self { |
| 3754 | Self(-self.0) |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | impl Negate for ScaledPixels { |
| 3759 | fn negate(self) -> Self { |
| 3760 | Self(-self.0) |
| 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | impl Negate for Pixels { |
| 3765 | fn negate(self) -> Self { |
| 3766 | Self(-self.0) |
| 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | impl Negate for Rems { |
| 3771 | fn negate(self) -> Self { |
| 3772 | Self(-self.0) |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | |
| 3778 | |
| 3779 | |
| 3780 | |
| 3781 | |
| 3782 | pub trait IsZero { |
| 3783 | |
| 3784 | |
| 3785 | |
| 3786 | |
| 3787 | |
| 3788 | fn is_zero(&self) -> bool; |
| 3789 | } |
| 3790 | |
| 3791 | impl IsZero for DevicePixels { |
| 3792 | fn is_zero(&self) -> bool { |
| 3793 | self.0 == 0 |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | impl IsZero for ScaledPixels { |
| 3798 | fn is_zero(&self) -> bool { |
| 3799 | self.0 == 0. |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | impl IsZero for Pixels { |
| 3804 | fn is_zero(&self) -> bool { |
| 3805 | self.0 == 0. |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | impl IsZero for Rems { |
| 3810 | fn is_zero(&self) -> bool { |
| 3811 | self.0 == 0. |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | impl IsZero for AbsoluteLength { |
| 3816 | fn is_zero(&self) -> bool { |
| 3817 | match self { |
| 3818 | AbsoluteLength::Pixels(pixels) => pixels.is_zero(), |
| 3819 | AbsoluteLength::Rems(rems) => rems.is_zero(), |
| 3820 | } |
| 3821 | } |
| 3822 | } |
| 3823 | |
| 3824 | impl IsZero for DefiniteLength { |
| 3825 | fn is_zero(&self) -> bool { |
| 3826 | match self { |
| 3827 | DefiniteLength::Absolute(length) => length.is_zero(), |
| 3828 | DefiniteLength::Fraction(fraction) => *fraction == 0., |
| 3829 | } |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | impl IsZero for Length { |
| 3834 | fn is_zero(&self) -> bool { |
| 3835 | match self { |
| 3836 | Length::Definite(length) => length.is_zero(), |
| 3837 | Length::Auto => false, |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | impl<T: IsZero + Clone + Debug + Default + PartialEq> IsZero for Point<T> { |
| 3843 | fn is_zero(&self) -> bool { |
| 3844 | self.x.is_zero() && self.y.is_zero() |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | impl<T> IsZero for Size<T> |
| 3849 | where |
| 3850 | T: IsZero + Clone + Debug + Default + PartialEq, |
| 3851 | { |
| 3852 | fn is_zero(&self) -> bool { |
| 3853 | self.width.is_zero() || self.height.is_zero() |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | impl<T: IsZero + Clone + Debug + Default + PartialEq> IsZero for Bounds<T> { |
| 3858 | fn is_zero(&self) -> bool { |
| 3859 | self.size.is_zero() |
| 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | impl<T> IsZero for Corners<T> |
| 3864 | where |
| 3865 | T: IsZero + Clone + Debug + Default + PartialEq, |
| 3866 | { |
| 3867 | fn is_zero(&self) -> bool { |
| 3868 | self.top_left.is_zero() |
| 3869 | && self.top_right.is_zero() |
| 3870 | && self.bottom_right.is_zero() |
| 3871 | && self.bottom_left.is_zero() |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | #[cfg(test)] |
| 3876 | mod tests { |
| 3877 | use super::*; |
| 3878 | |
| 3879 | #[test] |
| 3880 | fn test_bounds_intersects() { |
| 3881 | let bounds1 = Bounds { |
| 3882 | origin: Point { x: 0.0, y: 0.0 }, |
| 3883 | size: Size { |
| 3884 | width: 5.0, |
| 3885 | height: 5.0, |
| 3886 | }, |
| 3887 | }; |
| 3888 | let bounds2 = Bounds { |
| 3889 | origin: Point { x: 4.0, y: 4.0 }, |
| 3890 | size: Size { |
| 3891 | width: 5.0, |
| 3892 | height: 5.0, |
| 3893 | }, |
| 3894 | }; |
| 3895 | let bounds3 = Bounds { |
| 3896 | origin: Point { x: 10.0, y: 10.0 }, |
| 3897 | size: Size { |
| 3898 | width: 5.0, |
| 3899 | height: 5.0, |
| 3900 | }, |
| 3901 | }; |
| 3902 | |
| 3903 | |
| 3904 | assert!(bounds1.intersects(&bounds2)); |
| 3905 | |
| 3906 | |
| 3907 | assert!(!bounds1.intersects(&bounds3)); |
| 3908 | |
| 3909 | |
| 3910 | assert!(bounds1.intersects(&bounds1)); |
| 3911 | } |
| 3912 | } |