Indicator.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  6. //
  7. // Copyright (c) 2017 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. 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. // Alrady 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 = UIActivityIndicatorViewStyle.white
  123. #else
  124. let indicatorStyle = UIActivityIndicatorViewStyle.gray
  125. #endif
  126. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:indicatorStyle)
  127. activityIndicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleTopMargin]
  128. #endif
  129. }
  130. }
  131. // MARK: - ImageIndicator
  132. // Displays an ImageView. Supports gif
  133. class ImageIndicator: Indicator {
  134. private let animatedImageIndicatorView: ImageView
  135. var view: IndicatorView {
  136. return animatedImageIndicatorView
  137. }
  138. init?(imageData data: Data, processor: ImageProcessor = DefaultImageProcessor.default, options: KingfisherOptionsInfo = KingfisherEmptyOptionsInfo) {
  139. var options = options
  140. // Use normal image view to show animations, so we need to preload all animation data.
  141. if !options.preloadAllAnimationData {
  142. options.append(.preloadAllAnimationData)
  143. }
  144. guard let image = processor.process(item: .data(data), options: options) else {
  145. return nil
  146. }
  147. animatedImageIndicatorView = ImageView()
  148. animatedImageIndicatorView.image = image
  149. animatedImageIndicatorView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
  150. #if os(macOS)
  151. // Need for gif to animate on macOS
  152. self.animatedImageIndicatorView.imageScaling = .scaleNone
  153. self.animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  154. #else
  155. animatedImageIndicatorView.contentMode = .center
  156. animatedImageIndicatorView.autoresizingMask = [.flexibleLeftMargin,
  157. .flexibleRightMargin,
  158. .flexibleBottomMargin,
  159. .flexibleTopMargin]
  160. #endif
  161. }
  162. func startAnimatingView() {
  163. #if os(macOS)
  164. animatedImageIndicatorView.animates = true
  165. #else
  166. animatedImageIndicatorView.startAnimating()
  167. #endif
  168. animatedImageIndicatorView.isHidden = false
  169. }
  170. func stopAnimatingView() {
  171. #if os(macOS)
  172. animatedImageIndicatorView.animates = false
  173. #else
  174. animatedImageIndicatorView.stopAnimating()
  175. #endif
  176. animatedImageIndicatorView.isHidden = true
  177. }
  178. }