HorizontalAlignment.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /// Alignment of items layed out along the horizontal axis.
  2. public enum HorizontalAlignment: Sendable {
  3. /// Leading alignment (left alignment in left-to-right locales).
  4. case leading
  5. /// Center alignment.
  6. case center
  7. /// Trailing alignment (right alignment in left-to-right locales).
  8. case trailing
  9. /// Converts this value to a ``StackAlignment``.
  10. var asStackAlignment: StackAlignment {
  11. switch self {
  12. case .leading:
  13. .leading
  14. case .center:
  15. .center
  16. case .trailing:
  17. .trailing
  18. }
  19. }
  20. /// Gets the position of a child of a given width in a frame of a given
  21. /// width using the alignment.
  22. /// - Parameter childWidth: The width of the child.
  23. /// - Parameter frameWidth: The width of the frame.
  24. /// - Returns: The position of the child.
  25. func position(ofChild childWidth: Double, in frameWidth: Double) -> Double {
  26. switch self {
  27. case .leading: 0
  28. case .center: (frameWidth - childWidth) / 2
  29. case .trailing: frameWidth - childWidth
  30. }
  31. }
  32. }