Indicator.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  6. //
  7. // Copyright (c) 2018 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(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. #if os(macOS)
  32. public typealias IndicatorView = NSView
  33. #else
  34. public typealias IndicatorView = UIView
  35. #endif
  36. public enum IndicatorType {
  37. /// No indicator.
  38. case none
  39. /// Use system activity indicator.
  40. case activity
  41. /// Use an image as indicator. GIF is supported.
  42. case image(imageData: Data)
  43. /// Use a custom indicator, which conforms to the `Indicator` protocol.
  44. case custom(indicator: Indicator)
  45. }
  46. // MARK: - Indicator Protocol
  47. public protocol Indicator {
  48. func startAnimatingView()
  49. func stopAnimatingView()
  50. var viewCenter: CGPoint { get set }
  51. var view: IndicatorView { get }
  52. }
  53. extension Indicator {
  54. #if os(macOS)
  55. public var viewCenter: CGPoint {
  56. get {
  57. let frame = view.frame
  58. return CGPoint(x: frame.origin.x + frame.size.width / 2.0, y: frame.origin.y + frame.size.height / 2.0 )
  59. }
  60. set {
  61. let frame = view.frame
  62. let newFrame = CGRect(x: newValue.x - frame.size.width / 2.0,
  63. y: newValue.y - frame.size.height / 2.0,
  64. width: frame.size.width,
  65. height: frame.size.height)
  66. view.frame = newFrame
  67. }
  68. }
  69. #else
  70. public var viewCenter: CGPoint {
  71. get {
  72. return view.center
  73. }
  74. set {
  75. view.center = newValue
  76. }
  77. }
  78. #endif
  79. }
  80. // MARK: - ActivityIndicator
  81. // Displays a NSProgressIndicator / UIActivityIndicatorView
  82. final class ActivityIndicator: Indicator {
  83. #if os(macOS)
  84. private let activityIndicatorView: NSProgressIndicator
  85. #else
  86. private let activityIndicatorView: UIActivityIndicatorView
  87. #endif
  88. private var animatingCount = 0
  89. var view: IndicatorView {
  90. return activityIndicatorView
  91. }
  92. func startAnimatingView() {
  93. animatingCount += 1
  94. // Already animating
  95. if animatingCount == 1 {
  96. #if os(macOS)
  97. activityIndicatorView.startAnimation(nil)
  98. #else
  99. activityIndicatorView.startAnimating()
  100. #endif
  101. activityIndicatorView.isHidden = false
  102. }
  103. }
  104. func stopAnimatingView() {
  105. animatingCount = max(animatingCount - 1, 0)
  106. if animatingCount == 0 {
  107. #if os(macOS)
  108. activityIndicatorView.stopAnimation(nil)
  109. #else
  110. activityIndicatorView.stopAnimating()
  111. #endif
  112. activityIndicatorView.isHidden = true
  113. }
  114. }
  115. init() {
  116. #if os(macOS)
  117. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  118. activityIndicatorView.controlSize = .small
  119. activityIndicatorView.style = .spinning
  120. #else
  121. #if os(tvOS)
  122. let indicatorStyle = UIActivityIndicatorView.Style.white
  123. #else
  124. let indicatorStyle = UIActivityIndicatorView.Style.gray
  125. #endif
  126. #if swift(>=4.2)
  127. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  128. #else
  129. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: indicatorStyle)
  130. #endif
  131. activityIndicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleTopMargin]
  132. #endif
  133. }
  134. }
  135. // MARK: - ImageIndicator
  136. // Displays an ImageView. Supports gif
  137. final class ImageIndicator: Indicator {
  138. private let animatedImageIndicatorView: ImageView
  139. var view: IndicatorView {
  140. return animatedImageIndicatorView
  141. }
  142. init?(imageData data: Data, processor: ImageProcessor = DefaultImageProcessor.default, options: KingfisherOptionsInfo = KingfisherEmptyOptionsInfo) {
  143. var options = options
  144. // Use normal image view to show animations, so we need to preload all animation data.
  145. if !options.preloadAllAnimationData {
  146. options.append(.preloadAllAnimationData)
  147. }
  148. guard let image = processor.process(item: .data(data), options: options) else {
  149. return nil
  150. }
  151. animatedImageIndicatorView = ImageView()
  152. animatedImageIndicatorView.image = image
  153. animatedImageIndicatorView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
  154. #if os(macOS)
  155. // Need for gif to animate on macOS
  156. self.animatedImageIndicatorView.imageScaling = .scaleNone
  157. self.animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  158. #else
  159. animatedImageIndicatorView.contentMode = .center
  160. animatedImageIndicatorView.autoresizingMask = [.flexibleLeftMargin,
  161. .flexibleRightMargin,
  162. .flexibleBottomMargin,
  163. .flexibleTopMargin]
  164. #endif
  165. }
  166. func startAnimatingView() {
  167. #if os(macOS)
  168. animatedImageIndicatorView.animates = true
  169. #else
  170. animatedImageIndicatorView.startAnimating()
  171. #endif
  172. animatedImageIndicatorView.isHidden = false
  173. }
  174. func stopAnimatingView() {
  175. #if os(macOS)
  176. animatedImageIndicatorView.animates = false
  177. #else
  178. animatedImageIndicatorView.stopAnimating()
  179. #endif
  180. animatedImageIndicatorView.isHidden = true
  181. }
  182. }