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.

76 lines
3.1 KiB

  1. //
  2. // Placeholder.swift
  3. // Kingfisher
  4. //
  5. // Created by Tieme van Veen on 28/08/2017.
  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 os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. /// Represents a placeholder type which could be set while loading as well as
  32. /// loading finished without getting an image.
  33. public protocol Placeholder {
  34. /// How the placeholder should be added to a given image view.
  35. func add(to imageView: ImageView)
  36. /// How the placeholder should be removed from a given image view.
  37. func remove(from imageView: ImageView)
  38. }
  39. /// Default implementation of an image placeholder. The image will be set or
  40. /// reset directly for `image` property of the image view.
  41. extension Image: Placeholder {
  42. /// How the placeholder should be added to a given image view.
  43. public func add(to imageView: ImageView) { imageView.image = self }
  44. /// How the placeholder should be removed from a given image view.
  45. public func remove(from imageView: ImageView) { imageView.image = nil }
  46. }
  47. /// Default implementation of an arbitrary view as placeholder. The view will be
  48. /// added as a subview when adding and be removed from its super view when removing.
  49. ///
  50. /// To use your customize View type as placeholder, simply let it conforming to
  51. /// `Placeholder` by `extension MyView: Placeholder {}`.
  52. extension Placeholder where Self: View {
  53. /// How the placeholder should be added to a given image view.
  54. public func add(to imageView: ImageView) {
  55. imageView.addSubview(self)
  56. translatesAutoresizingMaskIntoConstraints = false
  57. centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
  58. centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
  59. heightAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
  60. widthAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
  61. }
  62. /// How the placeholder should be removed from a given image view.
  63. public func remove(from imageView: ImageView) {
  64. removeFromSuperview()
  65. }
  66. }