ViewSize.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /// The size of a view.
  2. public struct ViewSize: Hashable, Sendable {
  3. /// The zero view size.
  4. public static let zero = Self(0, 0)
  5. /// The view's width.
  6. public var width: Double
  7. /// The view's height.
  8. public var height: Double
  9. /// Creates a view size.
  10. ///
  11. /// - Parameters:
  12. /// - width: The view's width.
  13. /// - height: The view's height.
  14. public init(_ width: Double, _ height: Double) {
  15. self.width = width
  16. self.height = height
  17. }
  18. /// Creates a view size from an integer vector.
  19. ///
  20. /// - Parameter vector: The vector to create the view size from. The
  21. /// X component becomes the width, and the Y component becomes the
  22. /// height.
  23. init(_ vector: SIMD2<Int>) {
  24. width = Double(vector.x)
  25. height = Double(vector.y)
  26. }
  27. /// Gets the view size as a vector.
  28. @_spi(Backends) public var vector: SIMD2<Int> {
  29. SIMD2<Int>(
  30. LayoutSystem.roundSize(width),
  31. LayoutSystem.roundSize(height)
  32. )
  33. }
  34. /// The size component associated with the given orientation.
  35. ///
  36. /// - Parameter orientation: The orientation.
  37. /// - Returns: The component corresponding to `orientation`.
  38. public subscript(component orientation: Orientation) -> Double {
  39. get {
  40. switch orientation {
  41. case .horizontal:
  42. width
  43. case .vertical:
  44. height
  45. }
  46. }
  47. set {
  48. switch orientation {
  49. case .horizontal:
  50. width = newValue
  51. case .vertical:
  52. height = newValue
  53. }
  54. }
  55. }
  56. /// The size component associated with the given axis.
  57. ///
  58. /// - Parameter axis: The axis.
  59. /// - Returns: The component corresponding to `axis`.
  60. public subscript(component axis: Axis) -> Double {
  61. get {
  62. self[component: axis.orientation]
  63. }
  64. set {
  65. self[component: axis.orientation] = newValue
  66. }
  67. }
  68. }