You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.3 KiB

  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 canImport(AppKit)
  27. import AppKit
  28. public typealias IndicatorView = NSView
  29. #else
  30. import UIKit
  31. public typealias IndicatorView = UIView
  32. #endif
  33. /// Represents the activity indicator type which should be added to
  34. /// an image view when an image is being downloaded.
  35. ///
  36. /// - none: No indicator.
  37. /// - activity: Uses the system activity indicator.
  38. /// - image: Uses an image as indicator. GIF is supported.
  39. /// - custom: Uses a custom indicator. The type of associated value should conform to the `Indicator` protocol.
  40. public enum IndicatorType {
  41. /// No indicator.
  42. case none
  43. /// Uses the system activity indicator.
  44. case activity
  45. /// Uses an image as indicator. GIF is supported.
  46. case image(imageData: Data)
  47. /// Uses a custom indicator. The type of associated value should conform to the `Indicator` protocol.
  48. case custom(indicator: Indicator)
  49. }
  50. /// An indicator type which can be used to show the download task is in progress.
  51. public protocol Indicator {
  52. /// Called when the indicator should start animating.
  53. func startAnimatingView()
  54. /// Called when the indicator should stop animating.
  55. func stopAnimatingView()
  56. /// Center offset of the indicator. Kingfisher will use this value to determine the position of
  57. /// indicator in the super view.
  58. var centerOffset: CGPoint { get }
  59. /// The indicator view which would be added to the super view.
  60. var view: IndicatorView { get }
  61. }
  62. extension Indicator {
  63. /// Default implementation of `centerOffset` of `Indicator`. The default value is `.zero`, means that there is
  64. /// no offset for the indicator view.
  65. public var centerOffset: CGPoint { return .zero }
  66. }
  67. // Displays a NSProgressIndicator / UIActivityIndicatorView
  68. final class ActivityIndicator: Indicator {
  69. #if os(macOS)
  70. private let activityIndicatorView: NSProgressIndicator
  71. #else
  72. private let activityIndicatorView: UIActivityIndicatorView
  73. #endif
  74. private var animatingCount = 0
  75. var view: IndicatorView {
  76. return activityIndicatorView
  77. }
  78. func startAnimatingView() {
  79. if animatingCount == 0 {
  80. #if os(macOS)
  81. activityIndicatorView.startAnimation(nil)
  82. #else
  83. activityIndicatorView.startAnimating()
  84. #endif
  85. activityIndicatorView.isHidden = false
  86. }
  87. animatingCount += 1
  88. }
  89. func stopAnimatingView() {
  90. animatingCount = max(animatingCount - 1, 0)
  91. if animatingCount == 0 {
  92. #if os(macOS)
  93. activityIndicatorView.stopAnimation(nil)
  94. #else
  95. activityIndicatorView.stopAnimating()
  96. #endif
  97. activityIndicatorView.isHidden = true
  98. }
  99. }
  100. init() {
  101. #if os(macOS)
  102. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  103. activityIndicatorView.controlSize = .small
  104. activityIndicatorView.style = .spinning
  105. #else
  106. #if os(tvOS)
  107. let indicatorStyle = UIActivityIndicatorView.Style.white
  108. #else
  109. let indicatorStyle = UIActivityIndicatorView.Style.gray
  110. #endif
  111. #if swift(>=4.2)
  112. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  113. #else
  114. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: indicatorStyle)
  115. #endif
  116. #endif
  117. }
  118. }
  119. // MARK: - ImageIndicator
  120. // Displays an ImageView. Supports gif
  121. final class ImageIndicator: Indicator {
  122. private let animatedImageIndicatorView: ImageView
  123. var view: IndicatorView {
  124. return animatedImageIndicatorView
  125. }
  126. init?(
  127. imageData data: Data,
  128. processor: ImageProcessor = DefaultImageProcessor.default,
  129. options: KingfisherParsedOptionsInfo? = nil)
  130. {
  131. var options = options ?? KingfisherParsedOptionsInfo(nil)
  132. // Use normal image view to show animations, so we need to preload all animation data.
  133. if !options.preloadAllAnimationData {
  134. options.preloadAllAnimationData = true
  135. }
  136. guard let image = processor.process(item: .data(data), options: options) else {
  137. return nil
  138. }
  139. animatedImageIndicatorView = ImageView()
  140. animatedImageIndicatorView.image = image
  141. #if os(macOS)
  142. // Need for gif to animate on macOS
  143. animatedImageIndicatorView.imageScaling = .scaleNone
  144. animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  145. #else
  146. animatedImageIndicatorView.contentMode = .center
  147. #endif
  148. }
  149. func startAnimatingView() {
  150. #if os(macOS)
  151. animatedImageIndicatorView.animates = true
  152. #else
  153. animatedImageIndicatorView.startAnimating()
  154. #endif
  155. animatedImageIndicatorView.isHidden = false
  156. }
  157. func stopAnimatingView() {
  158. #if os(macOS)
  159. animatedImageIndicatorView.animates = false
  160. #else
  161. animatedImageIndicatorView.stopAnimating()
  162. #endif
  163. animatedImageIndicatorView.isHidden = true
  164. }
  165. }