NCBackgroundImageColor.swift 5.5 KB

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