NCBackgroundImageColor.swift 4.8 KB

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