Circle.swift 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. /// A circle.
  2. ///
  3. /// Circles have equal widths and heights; the `Circle` shape will take on the
  4. /// minimum of its proposed width and height.
  5. public struct Circle: InsettableShape {
  6. /// The ideal diameter of a ``Circle``.
  7. nonisolated static let idealDiameter = 10.0
  8. /// Creates a ``Circle`` instance.
  9. public nonisolated init() {}
  10. public nonisolated func path(in bounds: Path.Rect) -> Path {
  11. Path()
  12. .addCircle(center: bounds.center, radius: min(bounds.width, bounds.height) / 2.0)
  13. }
  14. public nonisolated func size(fitting proposal: ProposedViewSize) -> ViewSize {
  15. let diameter: Double
  16. if let proposal = proposal.concrete {
  17. diameter = min(proposal.width, proposal.height)
  18. } else {
  19. diameter = proposal.width ?? proposal.height ?? Circle.idealDiameter
  20. }
  21. return ViewSize(diameter, diameter)
  22. }
  23. public nonisolated func inset(by amount: Double) -> some InsettableShape {
  24. InsettableShapeImpl(inset: amount, base: self)
  25. }
  26. }