Indicator.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if !os(watchOS)
  27. #if canImport(AppKit) && !targetEnvironment(macCatalyst)
  28. import AppKit
  29. public typealias IndicatorView = NSView
  30. #else
  31. import UIKit
  32. public typealias IndicatorView = UIView
  33. #endif
  34. /// Represents the activity indicator type that should be added to an image view when an image is being downloaded.
  35. public enum IndicatorType {
  36. /// No indicator.
  37. case none
  38. /// Uses the system activity indicator.
  39. case activity
  40. /// Uses an image as an indicator. GIF is supported.
  41. case image(imageData: Data)
  42. /// Uses a custom indicator.
  43. ///
  44. /// The type of the associated value should conform to the ``Indicator`` protocol.
  45. case custom(indicator: any Indicator)
  46. }
  47. /// An indicator type which can be used to show that the download task is in progress.
  48. @MainActor
  49. public protocol Indicator: Sendable {
  50. /// Called when the indicator should start animating.
  51. func startAnimatingView()
  52. /// Called when the indicator should stop animating.
  53. func stopAnimatingView()
  54. /// Center offset of the indicator.
  55. ///
  56. /// Kingfisher will use this value to determine the position of the indicator in the superview.
  57. var centerOffset: CGPoint { get }
  58. /// The indicator view which would be added to the superview.
  59. var view: IndicatorView { get }
  60. /// The size strategy used when adding the indicator to the image view.
  61. /// - Parameter imageView: The superview of the indicator.
  62. /// - Returns: An ``IndicatorSizeStrategy`` that determines how the indicator should be sized.
  63. func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy
  64. }
  65. /// The idicator size strategy used when sizing the indicator in the image view.
  66. public enum IndicatorSizeStrategy {
  67. /// Uses the intrinsic size of the indicator.
  68. case intrinsicSize
  69. /// Match the size of the super view of the indicator.
  70. case full
  71. /// Uses the associated `CGSize` to set the indicator size.
  72. case size(CGSize)
  73. }
  74. extension Indicator {
  75. /// Default implementation of ``Indicator/centerOffset-7jxdw`` of the ``Indicator``.
  76. ///
  77. /// The default value is `.zero`, which means that there is no offset for the indicator view.
  78. public var centerOffset: CGPoint {
  79. .zero
  80. }
  81. /// Default implementation of ``Indicator/sizeStrategy(in:)-5x0b4`` of the ``Indicator``.
  82. ///
  83. /// The default value is ``IndicatorSizeStrategy/full``, means that the indicator will pin to the same height and
  84. /// width as the image view.
  85. /// - Parameter imageView: The image view which holds the indicator.
  86. /// - Returns: The desired ``IndicatorSizeStrategy``
  87. public func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
  88. .full
  89. }
  90. }
  91. // Displays a NSProgressIndicator / UIActivityIndicatorView
  92. @MainActor
  93. final class ActivityIndicator: Indicator {
  94. #if os(macOS)
  95. private let activityIndicatorView: NSProgressIndicator
  96. #else
  97. private let activityIndicatorView: UIActivityIndicatorView
  98. #endif
  99. private var animatingCount = 0
  100. var view: IndicatorView {
  101. return activityIndicatorView
  102. }
  103. func startAnimatingView() {
  104. if animatingCount == 0 {
  105. #if os(macOS)
  106. activityIndicatorView.startAnimation(nil)
  107. #else
  108. activityIndicatorView.startAnimating()
  109. #endif
  110. activityIndicatorView.isHidden = false
  111. }
  112. animatingCount += 1
  113. }
  114. func stopAnimatingView() {
  115. animatingCount = max(animatingCount - 1, 0)
  116. if animatingCount == 0 {
  117. #if os(macOS)
  118. activityIndicatorView.stopAnimation(nil)
  119. #else
  120. activityIndicatorView.stopAnimating()
  121. #endif
  122. activityIndicatorView.isHidden = true
  123. }
  124. }
  125. func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
  126. return .intrinsicSize
  127. }
  128. init() {
  129. #if os(macOS)
  130. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  131. activityIndicatorView.controlSize = .regular
  132. activityIndicatorView.style = .spinning
  133. #else
  134. let indicatorStyle: UIActivityIndicatorView.Style
  135. #if os(tvOS)
  136. if #available(tvOS 13.0, *) {
  137. indicatorStyle = UIActivityIndicatorView.Style.large
  138. } else {
  139. indicatorStyle = UIActivityIndicatorView.Style.white
  140. }
  141. #elseif os(visionOS)
  142. indicatorStyle = UIActivityIndicatorView.Style.medium
  143. #else
  144. if #available(iOS 13.0, * ) {
  145. indicatorStyle = UIActivityIndicatorView.Style.medium
  146. } else {
  147. indicatorStyle = UIActivityIndicatorView.Style.gray
  148. }
  149. #endif
  150. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  151. #endif
  152. }
  153. }
  154. #if canImport(UIKit)
  155. extension UIActivityIndicatorView.Style {
  156. #if compiler(>=5.1)
  157. #else
  158. static let large = UIActivityIndicatorView.Style.white
  159. #if !os(tvOS)
  160. static let medium = UIActivityIndicatorView.Style.gray
  161. #endif
  162. #endif
  163. }
  164. #endif
  165. // MARK: - ImageIndicator
  166. // Displays an ImageView. Supports gif
  167. final class ImageIndicator: Indicator {
  168. private let animatedImageIndicatorView: KFCrossPlatformImageView
  169. var view: IndicatorView {
  170. return animatedImageIndicatorView
  171. }
  172. init?(
  173. imageData data: Data,
  174. processor: any ImageProcessor = DefaultImageProcessor.default,
  175. options: KingfisherParsedOptionsInfo? = nil)
  176. {
  177. var options = options ?? KingfisherParsedOptionsInfo(nil)
  178. // Use normal image view to show animations, so we need to preload all animation data.
  179. if !options.preloadAllAnimationData {
  180. options.preloadAllAnimationData = true
  181. }
  182. guard let image = processor.process(item: .data(data), options: options) else {
  183. return nil
  184. }
  185. animatedImageIndicatorView = KFCrossPlatformImageView()
  186. animatedImageIndicatorView.image = image
  187. #if os(macOS)
  188. // Need for gif to animate on macOS
  189. animatedImageIndicatorView.imageScaling = .scaleNone
  190. animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  191. #else
  192. animatedImageIndicatorView.contentMode = .center
  193. #endif
  194. }
  195. func startAnimatingView() {
  196. #if os(macOS)
  197. animatedImageIndicatorView.animates = true
  198. #else
  199. animatedImageIndicatorView.startAnimating()
  200. #endif
  201. animatedImageIndicatorView.isHidden = false
  202. }
  203. func stopAnimatingView() {
  204. #if os(macOS)
  205. animatedImageIndicatorView.animates = false
  206. #else
  207. animatedImageIndicatorView.stopAnimating()
  208. #endif
  209. animatedImageIndicatorView.isHidden = true
  210. }
  211. }
  212. #endif