NCBackgroundImageColor.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. public protocol NCBackgroundImageColorDelegate {
  26. func colorPickerCancel()
  27. func colorPickerWillChange(color: UIColor)
  28. func colorPickerDidChange(lightColor: String, darkColor: String, useForAll: Bool)
  29. }
  30. // optional func
  31. public extension NCBackgroundImageColorDelegate {
  32. func colorPickerCancel() {}
  33. func colorPickerWillChange(color: UIColor) { }
  34. func colorPickerDidChange(lightColor: String, darkColor: String, useForAll: Bool) { }
  35. }
  36. class NCBackgroundImageColor: UIViewController {
  37. @IBOutlet weak var titleLabel: UILabel!
  38. @IBOutlet weak var chromaColorPickerView: UIView!
  39. @IBOutlet weak var whiteButton: UIButton!
  40. @IBOutlet weak var orangeButton: UIButton!
  41. @IBOutlet weak var redButton: UIButton!
  42. @IBOutlet weak var greenButton: UIButton!
  43. @IBOutlet weak var blackButton: UIButton!
  44. @IBOutlet weak var darkmodeLabel: UILabel!
  45. @IBOutlet weak var darkmodeSwitch: UISwitch!
  46. @IBOutlet weak var useForAllLabel: UILabel!
  47. @IBOutlet weak var useForAllSwitch: UISwitch!
  48. @IBOutlet weak var defaultButton: UIButton!
  49. @IBOutlet weak var cancelButton: UIButton!
  50. @IBOutlet weak var okButton: UIButton!
  51. private let colorPicker = ChromaColorPicker()
  52. private let brightnessSlider = ChromaBrightnessSlider()
  53. private var colorHandle: ChromaColorHandle?
  54. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  55. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  56. private var darkColor = ""
  57. private var lightColor = ""
  58. var delegate: NCBackgroundImageColorDelegate?
  59. var setupColor: UIColor?
  60. var layoutForView: NCGlobal.layoutForViewType?
  61. let width: CGFloat = 300
  62. let height: CGFloat = 485
  63. // MARK: - View Life Cycle
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. setupColorPicker()
  67. setupBrightnessSlider()
  68. setupColorPickerHandles()
  69. titleLabel.text = NSLocalizedString("_background_", comment: "")
  70. darkmodeLabel.text = NSLocalizedString("_dark_mode_", comment: "")
  71. useForAllLabel.text = NSLocalizedString("_as_default_color_", comment: "")
  72. defaultButton.setTitle(NSLocalizedString("_default_color_", comment: ""), for: .normal)
  73. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  74. okButton.setTitle(NSLocalizedString("_ok_", comment: ""), for: .normal)
  75. whiteButton.backgroundColor = .white
  76. whiteButton.layer.cornerRadius = 5
  77. whiteButton.layer.borderWidth = 0.5
  78. whiteButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  79. whiteButton.layer.masksToBounds = true
  80. orangeButton.backgroundColor = .orange
  81. orangeButton.layer.cornerRadius = 5
  82. orangeButton.layer.borderWidth = 0.5
  83. orangeButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  84. orangeButton.layer.masksToBounds = true
  85. redButton.backgroundColor = .red
  86. redButton.layer.cornerRadius = 5
  87. redButton.layer.borderWidth = 0.5
  88. redButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  89. redButton.layer.masksToBounds = true
  90. greenButton.backgroundColor = .green
  91. greenButton.layer.cornerRadius = 5
  92. greenButton.layer.borderWidth = 0.5
  93. greenButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  94. greenButton.layer.masksToBounds = true
  95. blackButton.backgroundColor = .black
  96. blackButton.layer.cornerRadius = 5
  97. blackButton.layer.borderWidth = 0.5
  98. blackButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  99. blackButton.layer.masksToBounds = true
  100. defaultButton.layer.cornerRadius = 15
  101. defaultButton.layer.borderWidth = 0.5
  102. defaultButton.layer.borderColor = UIColor.gray.cgColor
  103. defaultButton.layer.masksToBounds = true
  104. }
  105. override func viewWillAppear(_ animated: Bool) {
  106. super.viewWillAppear(animated)
  107. if traitCollection.userInterfaceStyle == .dark {
  108. darkmodeSwitch.isOn = true
  109. } else {
  110. darkmodeSwitch.isOn = false
  111. }
  112. useForAllSwitch.isOn = false
  113. // Color for this view
  114. if let layoutForView = layoutForView {
  115. darkColor = layoutForView.darkColorBackground
  116. lightColor = layoutForView.lightColorBackground
  117. }
  118. // Color for all folders
  119. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  120. if darkColor == "" {
  121. darkColor = activeAccount.darkColorBackground
  122. }
  123. if lightColor == "" {
  124. lightColor = activeAccount.lightColorBackground
  125. }
  126. }
  127. // set color
  128. if darkmodeSwitch.isOn {
  129. if let color = UIColor.init(hex: darkColor) {
  130. changeColor(color)
  131. } else {
  132. changeColor(.black)
  133. }
  134. } else {
  135. if let color = UIColor.init(hex: lightColor) {
  136. changeColor(color)
  137. } else {
  138. changeColor(.white)
  139. }
  140. }
  141. }
  142. override func viewDidAppear(_ animated: Bool) {
  143. super.viewDidAppear(animated)
  144. }
  145. // MARK: - Action
  146. @IBAction func whiteButtonAction(_ sender: UIButton) {
  147. changeColor(.white)
  148. }
  149. @IBAction func orangeButtonAction(_ sender: UIButton) {
  150. changeColor(.orange)
  151. }
  152. @IBAction func redButtonAction(_ sender: UIButton) {
  153. changeColor(.red)
  154. }
  155. @IBAction func greenButtonAction(_ sender: UIButton) {
  156. changeColor(.green)
  157. }
  158. @IBAction func blackButtonAction(_ sender: UIButton) {
  159. changeColor(.black)
  160. }
  161. @IBAction func darkmodeAction(_ sender: UISwitch) {
  162. if sender.isOn {
  163. if darkColor == "" {
  164. changeColor(.black)
  165. } else {
  166. if let color = UIColor.init(hex: darkColor) {
  167. changeColor(color)
  168. }
  169. }
  170. } else {
  171. if lightColor == "" {
  172. changeColor(.white)
  173. } else {
  174. if let color = UIColor.init(hex: lightColor) {
  175. changeColor(color)
  176. }
  177. }
  178. }
  179. }
  180. @IBAction func defaultAction(_ sender: Any) {
  181. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  182. if darkmodeSwitch.isOn {
  183. if useForAllSwitch.isOn {
  184. darkColor = ""
  185. changeColor(.black)
  186. } else {
  187. if let color = UIColor.init(hex: activeAccount.darkColorBackground) {
  188. darkColor = activeAccount.darkColorBackground
  189. changeColor(color)
  190. } else {
  191. darkColor = ""
  192. changeColor(.black)
  193. }
  194. }
  195. } else {
  196. if useForAllSwitch.isOn {
  197. lightColor = "#FFFFFF"
  198. changeColor(.white)
  199. } else {
  200. if let color = UIColor.init(hex: activeAccount.lightColorBackground) {
  201. lightColor = activeAccount.lightColorBackground
  202. changeColor(color)
  203. } else {
  204. lightColor = "#FFFFFF"
  205. changeColor(.white)
  206. }
  207. }
  208. }
  209. }
  210. }
  211. @IBAction func cancelAction(_ sender: Any) {
  212. self.delegate?.colorPickerCancel()
  213. dismiss(animated: true)
  214. }
  215. @IBAction func okAction(_ sender: Any) {
  216. var lightColor = self.lightColor
  217. var darkColor = self.darkColor
  218. if lightColor == "#FFFFFF" { lightColor = "" }
  219. if darkColor == "#000000" { darkColor = "" }
  220. self.delegate?.colorPickerDidChange(lightColor: lightColor, darkColor: darkColor, useForAll: useForAllSwitch.isOn)
  221. dismiss(animated: true)
  222. }
  223. // MARK: - ChromaColorPicker
  224. private func setupColorPicker() {
  225. colorPicker.delegate = self
  226. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  227. view.addSubview(colorPicker)
  228. NSLayoutConstraint.activate([
  229. colorPicker.leadingAnchor.constraint(equalTo: chromaColorPickerView.leadingAnchor, constant: 20),
  230. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  231. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  232. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  233. ])
  234. }
  235. private func setupBrightnessSlider() {
  236. brightnessSlider.connect(to: colorPicker)
  237. // Style
  238. brightnessSlider.trackColor = UIColor.blue
  239. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  240. // Layout
  241. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  242. view.addSubview(brightnessSlider)
  243. NSLayoutConstraint.activate([
  244. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  245. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  246. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  247. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  248. ])
  249. }
  250. private func setupColorPickerHandles() {
  251. colorHandle = colorPicker.addHandle(at: setupColor)
  252. }
  253. private func changeColor(_ color: UIColor) {
  254. colorHandle?.color = color
  255. colorPicker.setNeedsLayout()
  256. brightnessSlider.trackColor = color
  257. if darkmodeSwitch.isOn {
  258. darkColor = color.hexString
  259. } else {
  260. lightColor = color.hexString
  261. }
  262. self.delegate?.colorPickerWillChange(color: color)
  263. }
  264. }
  265. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  266. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  267. if darkmodeSwitch.isOn {
  268. darkColor = color.hexString
  269. } else {
  270. lightColor = color.hexString
  271. }
  272. self.delegate?.colorPickerWillChange(color: color)
  273. }
  274. }