NCBackgroundImage.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // NCBackgroundImage.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/05/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import ChromaColorPicker
  25. class NCBackgroundImage: UIViewController {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var closeButton: UIButton!
  28. @IBOutlet weak var chromaColorPickerView: UIView!
  29. private let colorPicker = ChromaColorPicker()
  30. private let brightnessSlider = ChromaBrightnessSlider()
  31. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  32. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  33. public var collectionViewCommon: NCCollectionViewCommon?
  34. let width: CGFloat = 300
  35. let height: CGFloat = 500
  36. // MARK: - View Life Cycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. setupColorPicker()
  40. setupBrightnessSlider()
  41. setupColorPickerHandles()
  42. titleLabel.text = NSLocalizedString("_background_", comment: "")
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. super.viewWillAppear(animated)
  46. }
  47. override func viewDidAppear(_ animated: Bool) {
  48. super.viewDidAppear(animated)
  49. }
  50. // MARK: - Action
  51. @IBAction func actionClose(_ sender: UIButton) {
  52. dismiss(animated: true)
  53. }
  54. // MARK: - ChromaColorPicker
  55. private func setupColorPicker() {
  56. colorPicker.delegate = self
  57. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  58. view.addSubview(colorPicker)
  59. NSLayoutConstraint.activate([
  60. colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  61. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  62. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  63. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  64. ])
  65. }
  66. private func setupBrightnessSlider() {
  67. brightnessSlider.connect(to: colorPicker)
  68. // Style
  69. brightnessSlider.trackColor = UIColor.blue
  70. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  71. // Layout
  72. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  73. view.addSubview(brightnessSlider)
  74. NSLayoutConstraint.activate([
  75. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  76. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  77. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  78. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  79. ])
  80. }
  81. private func setupColorPickerHandles() {
  82. let peachColor = collectionViewCommon?.collectionView.backgroundColor //UIColor(red: 1, green: 203 / 255, blue: 164 / 255, alpha: 1)
  83. colorPicker.addHandle(at: peachColor)
  84. }
  85. }
  86. extension NCBackgroundImage: ChromaColorPickerDelegate {
  87. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  88. print("x")
  89. /*
  90. colorDisplayView.backgroundColor = color
  91. // Here I can detect when the color is too bright to show a white icon
  92. // on the handle and change its tintColor.
  93. if handle === homeHandle, let imageView = homeHandle.accessoryView as? UIImageView {
  94. let colorIsBright = color.isLight
  95. UIView.animate(withDuration: 0.2, animations: {
  96. imageView.tintColor = colorIsBright ? .black : .white
  97. }, completion: nil)
  98. }
  99. */
  100. collectionViewCommon?.collectionView.backgroundColor = color
  101. }
  102. }