NCBackgroundImageColor.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // NCBackgroundImageColor.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 NCBackgroundImageColor: UIViewController {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var chromaColorPickerView: UIView!
  28. @IBOutlet weak var cancelButton: UIButton!
  29. @IBOutlet weak var okButton: UIButton!
  30. private let colorPicker = ChromaColorPicker()
  31. private let brightnessSlider = ChromaBrightnessSlider()
  32. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  33. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  34. private var colorHexString = ""
  35. public var collectionViewCommon: NCCollectionViewCommon?
  36. let width: CGFloat = 300
  37. let height: CGFloat = 500
  38. // MARK: - View Life Cycle
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. setupColorPicker()
  42. setupBrightnessSlider()
  43. setupColorPickerHandles()
  44. titleLabel.text = NSLocalizedString("_background_", comment: "")
  45. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  46. okButton.setTitle(NSLocalizedString("_ok_", comment: ""), for: .normal)
  47. }
  48. override func viewWillAppear(_ animated: Bool) {
  49. super.viewWillAppear(animated)
  50. }
  51. override func viewDidAppear(_ animated: Bool) {
  52. super.viewDidAppear(animated)
  53. }
  54. // MARK: - Action
  55. @IBAction func cancelAction(_ sender: Any) {
  56. collectionViewCommon?.setLayout()
  57. dismiss(animated: true)
  58. }
  59. @IBAction func okAction(_ sender: Any) {
  60. }
  61. // MARK: - ChromaColorPicker
  62. private func setupColorPicker() {
  63. colorPicker.delegate = self
  64. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  65. view.addSubview(colorPicker)
  66. NSLayoutConstraint.activate([
  67. colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  68. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  69. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  70. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  71. ])
  72. }
  73. private func setupBrightnessSlider() {
  74. brightnessSlider.connect(to: colorPicker)
  75. // Style
  76. brightnessSlider.trackColor = UIColor.blue
  77. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  78. // Layout
  79. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  80. view.addSubview(brightnessSlider)
  81. NSLayoutConstraint.activate([
  82. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  83. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  84. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  85. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  86. ])
  87. }
  88. private func setupColorPickerHandles() {
  89. let peachColor = collectionViewCommon?.collectionView.backgroundColor //UIColor(red: 1, green: 203 / 255, blue: 164 / 255, alpha: 1)
  90. colorPicker.addHandle(at: peachColor)
  91. }
  92. }
  93. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  94. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  95. colorHexString = color.hexString
  96. collectionViewCommon?.collectionView.backgroundColor = color
  97. }
  98. }