Indicator.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // MARK: - Indicator Protocol
  37. public protocol Indicator {
  38. func startAnimatingView()
  39. func stopAnimatingView()
  40. var viewCenter: CGPoint { get set }
  41. var view: IndicatorView { get }
  42. }
  43. extension Indicator {
  44. #if os(macOS)
  45. public var viewCenter: CGPoint {
  46. get {
  47. let frame = view.frame
  48. return CGPoint(x: frame.origin.x + frame.size.width / 2.0, y: frame.origin.y + frame.size.height / 2.0 )
  49. }
  50. set {
  51. let frame = view.frame
  52. let newFrame = CGRect(x: newValue.x - frame.size.width / 2.0,
  53. y: newValue.y - frame.size.height / 2.0,
  54. width: frame.size.width,
  55. height: frame.size.height)
  56. view.frame = newFrame
  57. }
  58. }
  59. #else
  60. public var viewCenter: CGPoint {
  61. get {
  62. return view.center
  63. }
  64. set {
  65. view.center = newValue
  66. }
  67. }
  68. #endif
  69. }
  70. // MARK: - ActivityIndicator
  71. // Displays a NSProgressIndicator / UIActivityIndicatorView
  72. struct ActivityIndicator: Indicator {
  73. #if os(macOS)
  74. private let activityIndicatorView: NSProgressIndicator
  75. #else
  76. private let activityIndicatorView: UIActivityIndicatorView
  77. #endif
  78. var view: IndicatorView {
  79. return activityIndicatorView
  80. }
  81. func startAnimatingView() {
  82. #if os(macOS)
  83. activityIndicatorView.startAnimation(nil)
  84. #else
  85. activityIndicatorView.startAnimating()
  86. #endif
  87. activityIndicatorView.isHidden = false
  88. }
  89. func stopAnimatingView() {
  90. #if os(macOS)
  91. activityIndicatorView.stopAnimation(nil)
  92. #else
  93. activityIndicatorView.stopAnimating()
  94. #endif
  95. activityIndicatorView.isHidden = true
  96. }
  97. init() {
  98. #if os(macOS)
  99. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  100. activityIndicatorView.controlSize = .small
  101. activityIndicatorView.style = .spinningStyle
  102. #else
  103. #if os(tvOS)
  104. let indicatorStyle = UIActivityIndicatorViewStyle.white
  105. #else
  106. let indicatorStyle = UIActivityIndicatorViewStyle.gray
  107. #endif
  108. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:indicatorStyle)
  109. activityIndicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleTopMargin]
  110. #endif
  111. }
  112. }
  113. // MARK: - ImageIndicator
  114. // Displays an ImageView. Supports gif
  115. struct ImageIndicator: Indicator {
  116. private let animatedImageIndicatorView: ImageView
  117. var view: IndicatorView {
  118. return animatedImageIndicatorView
  119. }
  120. init?(imageData data: Data, processor: ImageProcessor = DefaultImageProcessor.default, options: KingfisherOptionsInfo = KingfisherEmptyOptionsInfo) {
  121. var options = options
  122. // Use normal image view to show gif, so we need to preload all gif data.
  123. if !options.preloadAllGIFData {
  124. options.append(.preloadAllGIFData)
  125. }
  126. guard let image = processor.process(item: .data(data), options: options) else {
  127. return nil
  128. }
  129. animatedImageIndicatorView = ImageView()
  130. animatedImageIndicatorView.image = image
  131. #if os(macOS)
  132. // Need for gif to animate on macOS
  133. self.animatedImageIndicatorView.imageScaling = .scaleNone
  134. self.animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  135. #else
  136. animatedImageIndicatorView.contentMode = .center
  137. animatedImageIndicatorView.autoresizingMask = [.flexibleLeftMargin,
  138. .flexibleRightMargin,
  139. .flexibleBottomMargin,
  140. .flexibleTopMargin]
  141. #endif
  142. }
  143. func startAnimatingView() {
  144. #if os(macOS)
  145. animatedImageIndicatorView.animates = true
  146. #else
  147. animatedImageIndicatorView.startAnimating()
  148. #endif
  149. animatedImageIndicatorView.isHidden = false
  150. }
  151. func stopAnimatingView() {
  152. #if os(macOS)
  153. animatedImageIndicatorView.animates = false
  154. #else
  155. animatedImageIndicatorView.stopAnimating()
  156. #endif
  157. animatedImageIndicatorView.isHidden = true
  158. }
  159. }