1
0

ProgressView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import Foundation
  2. /// A progress indicator; either a bar or a spinner.
  3. public struct ProgressView<Label: View>: View {
  4. /// The label for this progress view.
  5. private var label: Label
  6. /// The current progress, if this is a progress bar.
  7. private var progress: Double?
  8. private var kind: Kind
  9. private var isSpinnerResizable: Bool = false
  10. private enum Kind {
  11. case spinner
  12. case bar
  13. }
  14. public var body: some View {
  15. if label as? EmptyView == nil {
  16. progressIndicator
  17. label
  18. } else {
  19. progressIndicator
  20. }
  21. }
  22. @ViewBuilder
  23. private var progressIndicator: some View {
  24. switch kind {
  25. case .spinner:
  26. ProgressSpinnerView(isResizable: isSpinnerResizable)
  27. case .bar:
  28. ProgressBarView(value: progress)
  29. }
  30. }
  31. /// Creates an indeterminate progress view (a spinner).
  32. ///
  33. /// - Parameter label: The label for this progress view.
  34. public init(_ label: Label) {
  35. self.label = label
  36. self.kind = .spinner
  37. }
  38. /// Creates a progress bar.
  39. ///
  40. /// - Parameters:
  41. /// - label: The label for this progress view.
  42. /// - progress: The current progress.
  43. public init(_ label: Label, _ progress: Progress) {
  44. self.label = label
  45. self.kind = .bar
  46. if !progress.isIndeterminate {
  47. self.progress = progress.fractionCompleted
  48. }
  49. }
  50. /// Creates a progress bar.
  51. ///
  52. /// - Parameters:
  53. /// - label: The label for this progress view.
  54. /// - value: The current progress. If `nil`, an indeterminate progress bar
  55. /// will be shown.
  56. public init<Value: BinaryFloatingPoint>(_ label: Label, value: Value?) {
  57. self.label = label
  58. self.kind = .bar
  59. self.progress = value.map { Double($0) }
  60. }
  61. /// Makes the `ProgressView` resize to fit the available space.
  62. ///
  63. /// This only affects spinners.
  64. public func resizable(_ isResizable: Bool = true) -> Self {
  65. var progressView = self
  66. progressView.isSpinnerResizable = isResizable
  67. return progressView
  68. }
  69. }
  70. extension ProgressView where Label == EmptyView {
  71. /// Creates an indeterminate progress view (a spinner).
  72. public init() {
  73. self.label = EmptyView()
  74. self.kind = .spinner
  75. }
  76. /// Creates a progress bar.
  77. ///
  78. /// - Parameters:
  79. /// - progress: The current progress.
  80. public init(_ progress: Progress) {
  81. self.label = EmptyView()
  82. self.kind = .bar
  83. if !progress.isIndeterminate {
  84. self.progress = progress.fractionCompleted
  85. }
  86. }
  87. /// Creates a progress bar.
  88. ///
  89. /// - Parameters:
  90. /// - value: The current progress. If `nil`, an indeterminate progress bar
  91. /// will be shown.
  92. public init<Value: BinaryFloatingPoint>(value: Value?) {
  93. self.label = EmptyView()
  94. self.kind = .bar
  95. self.progress = value.map { Double($0) }
  96. }
  97. }
  98. extension ProgressView where Label == Text {
  99. /// Creates an indeterminate progress view (a spinner).
  100. ///
  101. /// - Parameter label: The label for this progress view.
  102. public init(_ label: String) {
  103. self.label = Text(label)
  104. self.kind = .spinner
  105. }
  106. /// Creates a progress bar.
  107. ///
  108. /// - Parameters:
  109. /// - label: The label for this progress view.
  110. /// - progress: The current progress.
  111. public init(_ label: String, _ progress: Progress) {
  112. self.label = Text(label)
  113. self.kind = .bar
  114. if !progress.isIndeterminate {
  115. self.progress = progress.fractionCompleted
  116. }
  117. }
  118. /// Creates a progress bar.
  119. ///
  120. /// - Parameters:
  121. /// - label: The label for this progress view.
  122. /// - value: The current progress. If `nil`, an indeterminate progress bar
  123. /// will be shown.
  124. public init<Value: BinaryFloatingPoint>(_ label: String, value: Value?) {
  125. self.label = Text(label)
  126. self.kind = .bar
  127. self.progress = value.map { Double($0) }
  128. }
  129. }
  130. struct ProgressSpinnerView: ElementaryView {
  131. let isResizable: Bool
  132. init(isResizable: Bool = false) {
  133. self.isResizable = isResizable
  134. }
  135. func asWidget<Backend: BaseAppBackend>(backend: Backend) -> Backend.Widget {
  136. backend.createProgressSpinner()
  137. }
  138. func computeLayout<Backend: BaseAppBackend>(
  139. _ widget: Backend.Widget,
  140. proposedSize: ProposedViewSize,
  141. environment: EnvironmentValues,
  142. backend: Backend
  143. ) -> ViewLayoutResult {
  144. let naturalSize = backend.naturalSize(of: widget)
  145. guard isResizable else {
  146. return ViewLayoutResult.leafView(size: ViewSize(naturalSize))
  147. }
  148. let dimension: Double
  149. if let proposedWidth = proposedSize.width, let proposedHeight = proposedSize.height {
  150. dimension = min(proposedWidth, proposedHeight)
  151. } else if let proposedWidth = proposedSize.width {
  152. dimension = proposedWidth
  153. } else if let proposedHeight = proposedSize.height {
  154. dimension = proposedHeight
  155. } else {
  156. dimension = Double(min(naturalSize.x, naturalSize.y))
  157. }
  158. return ViewLayoutResult.leafView(
  159. size: ViewSize(dimension, dimension)
  160. )
  161. }
  162. func commit<Backend: BaseAppBackend>(
  163. _ widget: Backend.Widget,
  164. layout: ViewLayoutResult,
  165. environment: EnvironmentValues,
  166. backend: Backend
  167. ) {
  168. // Doesn't change the rendered size of ProgressSpinner
  169. // on UIKitBackend, but still sets container size to
  170. // (width: n, height: n) n = min(proposedSize.x, proposedSize.y)
  171. backend.setSize(ofProgressSpinner: widget, to: layout.size.vector)
  172. }
  173. }
  174. struct ProgressBarView: ElementaryView {
  175. /// The ideal width of a ProgressBarView.
  176. static let idealWidth: Double = 100
  177. var value: Double?
  178. init(value: Double?) {
  179. self.value = value
  180. }
  181. func asWidget<Backend: BaseAppBackend>(backend: Backend) -> Backend.Widget {
  182. backend.createProgressBar()
  183. }
  184. func computeLayout<Backend: BaseAppBackend>(
  185. _ widget: Backend.Widget,
  186. proposedSize: ProposedViewSize,
  187. environment: EnvironmentValues,
  188. backend: Backend
  189. ) -> ViewLayoutResult {
  190. let height = backend.naturalSize(of: widget).y
  191. let size = ViewSize(
  192. proposedSize.width ?? Self.idealWidth,
  193. Double(height)
  194. )
  195. return ViewLayoutResult.leafView(size: size)
  196. }
  197. func commit<Backend: BaseAppBackend>(
  198. _ widget: Backend.Widget,
  199. layout: ViewLayoutResult,
  200. environment: EnvironmentValues,
  201. backend: Backend
  202. ) {
  203. backend.updateProgressBar(widget, progressFraction: value, environment: environment)
  204. backend.setSize(of: widget, to: layout.size.vector)
  205. }
  206. }