Indicator.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  6. //
  7. // Copyright (c) 2016 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. struct ActivityIndicator: Indicator {
  83. #if os(macOS)
  84. private let activityIndicatorView: NSProgressIndicator
  85. #else
  86. private let activityIndicatorView: UIActivityIndicatorView
  87. #endif
  88. var view: IndicatorView {
  89. return activityIndicatorView
  90. }
  91. func startAnimatingView() {
  92. #if os(macOS)
  93. activityIndicatorView.startAnimation(nil)
  94. #else
  95. activityIndicatorView.startAnimating()
  96. #endif
  97. activityIndicatorView.isHidden = false
  98. }
  99. func stopAnimatingView() {
  100. #if os(macOS)
  101. activityIndicatorView.stopAnimation(nil)
  102. #else
  103. activityIndicatorView.stopAnimating()
  104. #endif
  105. activityIndicatorView.isHidden = true
  106. }
  107. init() {
  108. #if os(macOS)
  109. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  110. activityIndicatorView.controlSize = .small
  111. activityIndicatorView.style = .spinningStyle
  112. #else
  113. #if os(tvOS)
  114. let indicatorStyle = UIActivityIndicatorViewStyle.white
  115. #else
  116. let indicatorStyle = UIActivityIndicatorViewStyle.gray
  117. #endif
  118. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:indicatorStyle)
  119. activityIndicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleTopMargin]
  120. #endif
  121. }
  122. }
  123. // MARK: - ImageIndicator
  124. // Displays an ImageView. Supports gif
  125. struct ImageIndicator: Indicator {
  126. private let animatedImageIndicatorView: ImageView
  127. var view: IndicatorView {
  128. return animatedImageIndicatorView
  129. }
  130. init?(imageData data: Data, processor: ImageProcessor = DefaultImageProcessor.default, options: KingfisherOptionsInfo = KingfisherEmptyOptionsInfo) {
  131. var options = options
  132. // Use normal image view to show gif, so we need to preload all gif data.
  133. if !options.preloadAllGIFData {
  134. options.append(.preloadAllGIFData)
  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. self.animatedImageIndicatorView.imageScaling = .scaleNone
  144. self.animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  145. #else
  146. animatedImageIndicatorView.contentMode = .center
  147. animatedImageIndicatorView.autoresizingMask = [.flexibleLeftMargin,
  148. .flexibleRightMargin,
  149. .flexibleBottomMargin,
  150. .flexibleTopMargin]
  151. #endif
  152. }
  153. func startAnimatingView() {
  154. #if os(macOS)
  155. animatedImageIndicatorView.animates = true
  156. #else
  157. animatedImageIndicatorView.startAnimating()
  158. #endif
  159. animatedImageIndicatorView.isHidden = false
  160. }
  161. func stopAnimatingView() {
  162. #if os(macOS)
  163. animatedImageIndicatorView.animates = false
  164. #else
  165. animatedImageIndicatorView.stopAnimating()
  166. #endif
  167. animatedImageIndicatorView.isHidden = true
  168. }
  169. }