KMBackgroundPropertyController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //
  2. // KMBackgroundPropertyController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/6.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBackgroundPropertyControllerDelegate: AnyObject {
  10. //水印数据有更新
  11. @objc optional func backgroundPropertyControllerDidUpdate(_ controller: KMBackgroundPropertyController)
  12. //切换到模板界面
  13. @objc optional func backgroundPropertyControllerDidChangetoTemplate(_ controller: KMBackgroundPropertyController)
  14. //成功保存到模板
  15. @objc optional func backgroundPropertyControllerSaveTemplateSuccess(_ controller: KMBackgroundPropertyController, _ data: KMBackgroundModel)
  16. //取消修改模板信息
  17. @objc optional func backgroundPropertyControllerCancelTemplateEdit(_ controller: KMBackgroundPropertyController)
  18. //修改完成模板信息
  19. @objc optional func backgroundPropertyControllerFinishTemplateEdit(_ controller: KMBackgroundPropertyController)
  20. }
  21. class KMBackgroundPropertyController: NSViewController {
  22. @IBOutlet var contendView: NSView!
  23. @IBOutlet var leftTopButton: ComponentButton!
  24. @IBOutlet var titleLabel: NSTextField!
  25. @IBOutlet var templateButton: ComponentButton!
  26. @IBOutlet var typeTabsBGView: NSView!
  27. @IBOutlet var typeTabs: ComponentTabs!
  28. @IBOutlet var infoContendView: NSView!
  29. @IBOutlet var infoContendTopConst: NSLayoutConstraint!
  30. @IBOutlet var infoContendBottomConst: NSLayoutConstraint!
  31. //Text
  32. @IBOutlet var textBGView: NSView!
  33. @IBOutlet var colorLabel: NSTextField!
  34. @IBOutlet var fontColorGroup: ComponentCColorGroup!
  35. //File
  36. @IBOutlet var fileBGView: NSView!
  37. @IBOutlet var fileInputView: ComponentInput!
  38. @IBOutlet var fileInputAddonView: ComponentInputAddon!
  39. //Appearance
  40. @IBOutlet var appearanceBGView: NSView!
  41. @IBOutlet var appearanceLabel: NSTextField!
  42. @IBOutlet var appearance_RotateSelect: ComponentSelect!
  43. @IBOutlet var appearance_OpacitySelect: ComponentSelect!
  44. @IBOutlet var appearanceScaleCheckbox: ComponentCheckBox!
  45. @IBOutlet var appearanceScaleSelect: ComponentSelect!
  46. @IBOutlet var appearanceBGTopConst: NSLayoutConstraint!
  47. //Position
  48. @IBOutlet var positionBGView: NSView!
  49. @IBOutlet var positionLabel: NSTextField!
  50. @IBOutlet var positionItemView: ComponentCPosition!
  51. @IBOutlet var positionXInput: ComponentInputNumber!
  52. @IBOutlet var positionYInput: ComponentInputNumber!
  53. //Save
  54. @IBOutlet var saveTemplateBGView: NSView!
  55. @IBOutlet var saveButton: ComponentButton!
  56. private var textTabProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: KMLocalizedString("Color"))
  57. private var fileTabProperty = ComponentTabsProperty(tabsType: .underline_Fill, state: .normal, showIcon: false, title: KMLocalizedString("File"))
  58. weak open var delegate: KMBackgroundPropertyControllerDelegate?
  59. var originalDataDict: Dictionary<String, Any>?
  60. var backgroundData: KMBackgroundModel = KMBackgroundModel()
  61. var editSubType: KMPDFEditSubModeType = .none
  62. var isInBatchMode: Bool = false
  63. //MARK: - func
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. // Do view setup here.
  67. setupProperty()
  68. }
  69. func setupProperty() {
  70. contendView.wantsLayer = true
  71. contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  72. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  73. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  74. leftTopButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "watermark_arrowLeft"), keepPressState: false)
  75. leftTopButton.setTarget(self, action: #selector(leftTopButtonClicked(_:)))
  76. templateButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "icon_wm_template"), keepPressState: false)
  77. templateButton.setTarget(self, action: #selector(templateButtonClicked(_:)))
  78. typeTabs.updateItemProperty([textTabProperty, fileTabProperty])
  79. typeTabs.delegate = self
  80. //Text
  81. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.backgroundColors[0])
  82. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.backgroundColors[1])
  83. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.backgroundColors[2])
  84. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMAnnotationPropertiesColorManager.manager.backgroundColors[3])
  85. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMAnnotationPropertiesColorManager.manager.backgroundColors[4])
  86. fontColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  87. fontColorGroup.delegate = self
  88. //File
  89. fileInputView.properties = ComponentInputProperty(size: .s,
  90. state:.pressed ,
  91. placeholder: "Select Source File...",
  92. text: "",
  93. creatable: false)
  94. fileInputView.properties.propertyInfo.cornerRadius_topLeft = 0
  95. fileInputView.properties.propertyInfo.cornerRadius_topRight = 0
  96. fileInputView.properties.propertyInfo.cornerRadius_bottomLeft = 0
  97. fileInputView.properties.propertyInfo.cornerRadius_bottomRight = 0
  98. fileInputView.reloadData()
  99. fileInputView.delegate = self
  100. fileInputAddonView.properties = ComponentInputAddonProperty(size: .s,
  101. state: .normal,
  102. addOnBefore: false,
  103. onlyRead: false,
  104. addonType: .imageWithColor,
  105. iconImage: NSImage(named: "icon_folder"))
  106. fileInputAddonView.setTarget(self, action: #selector(chooseURLAction(_ :)))
  107. //Appearance
  108. appearanceLabel.stringValue = KMLocalizedString("Appearance")
  109. appearanceLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  110. appearanceLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  111. appearance_RotateSelect.properties = ComponentSelectProperties(size: .s,
  112. state: .normal,
  113. creatable: true,
  114. text: "0°",
  115. textUnit: "°",
  116. regexString: "0123456789-")
  117. var rotateItems: [ComponentMenuitemProperty] = []
  118. for string in ["0°", "45°", "-45°", "90°", "-90°"] {
  119. let item = ComponentMenuitemProperty(type: .normal, text: string)
  120. rotateItems.append(item)
  121. }
  122. appearance_RotateSelect.updateMenuItemsArr(rotateItems)
  123. appearance_RotateSelect.delegate = self
  124. appearance_OpacitySelect.properties = ComponentSelectProperties(size: .s,
  125. state: .normal,
  126. creatable: true,
  127. text: "100%",
  128. textUnit: "%",
  129. regexString: "0123456789")
  130. var opacityItems: [ComponentMenuitemProperty] = []
  131. for string in ["25%", "50%", "75%", "100%"] {
  132. let item = ComponentMenuitemProperty(type: .normal, text: string)
  133. opacityItems.append(item)
  134. }
  135. appearance_OpacitySelect.updateMenuItemsArr(opacityItems)
  136. appearance_OpacitySelect.delegate = self
  137. appearanceScaleCheckbox.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Scale relative to target page"), checkboxType: .normal)
  138. appearanceScaleCheckbox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  139. appearanceScaleSelect.properties = ComponentSelectProperties(size: .s,
  140. state: .normal,
  141. creatable: true,
  142. text: "100%",
  143. textUnit: "%",
  144. regexString: "0123456789")
  145. var appearanceScaleItems: [ComponentMenuitemProperty] = []
  146. for string in ["25%", "50%", "75%", "100%", "125%", "150%","200%"] {
  147. let item = ComponentMenuitemProperty(type: .normal, text: string)
  148. appearanceScaleItems.append(item)
  149. }
  150. appearanceScaleSelect.updateMenuItemsArr(appearanceScaleItems)
  151. appearanceScaleSelect.delegate = self
  152. //Position
  153. positionLabel.stringValue = KMLocalizedString("Position (mm)")
  154. positionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  155. positionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  156. positionItemView.properties = ComponentCPositionProperty(rowCount: 3, columnCount: 3, dash: false, selRow: 1, selColumn: 1)
  157. positionItemView.delegate = self
  158. positionXInput.properties = ComponentInputNumberProperty(alignment: .left,
  159. size: .s,
  160. minSize: -1000,
  161. maxSize: 1000,
  162. text: "0")
  163. positionXInput.delegate = self
  164. positionYInput.properties = ComponentInputNumberProperty(alignment: .left,
  165. size: .s,
  166. minSize: -1000,
  167. maxSize: 1000,
  168. text: "0")
  169. positionYInput.delegate = self
  170. //Save
  171. saveTemplateBGView.wantsLayer = true
  172. saveTemplateBGView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  173. saveButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .m, showLeftIcon: true, buttonText: KMLocalizedString("Save as Template"), icon: NSImage(named: "icon_wm_template_save"), keepPressState: false)
  174. saveButton.setTarget(self, action: #selector(saveButtonClicked(_:)))
  175. }
  176. func reloadData() {
  177. titleLabel.stringValue = KMLocalizedString("Add Background")
  178. leftTopButton.isHidden = true
  179. saveTemplateBGView.isHidden = false
  180. templateButton.properties.icon = NSImage(named: "icon_wm_template")
  181. templateButton.properties.isDisabled = false
  182. if self.isInBatchMode {
  183. infoContendBottomConst.constant = 0
  184. saveTemplateBGView.isHidden = true
  185. } else {
  186. infoContendBottomConst.constant = 72
  187. saveTemplateBGView.isHidden = false
  188. }
  189. infoContendTopConst.constant = 56
  190. typeTabsBGView.isHidden = false
  191. if editSubType == .edit {
  192. infoContendTopConst.constant = 8
  193. typeTabsBGView.isHidden = true
  194. titleLabel.stringValue = KMLocalizedString("Edit Background")
  195. leftTopButton.isHidden = false
  196. saveTemplateBGView.isHidden = true
  197. templateButton.properties.icon = NSImage(named: "edit_save")
  198. if backgroundData.type == .image {
  199. if backgroundData.imagePath == nil {
  200. templateButton.properties.isDisabled = true
  201. }
  202. }
  203. }
  204. templateButton.reloadData()
  205. fileBGView.isHidden = true
  206. textBGView.isHidden = true
  207. textTabProperty.state = .normal
  208. fileTabProperty.state = .normal
  209. if backgroundData.type == .color {
  210. textBGView.isHidden = false
  211. appearanceBGTopConst.constant = 88
  212. textTabProperty.state = .pressed
  213. } else if backgroundData.type == .image {
  214. fileBGView.isHidden = false
  215. appearanceBGTopConst.constant = 48
  216. fileTabProperty.state = .pressed
  217. }
  218. typeTabs.refreshItems()
  219. //Text
  220. fontColorGroup.currentColor = backgroundData.color
  221. fontColorGroup.refreshUI()
  222. //File
  223. fileInputView.properties.text = backgroundData.imagePath ?? ""
  224. fileInputView.reloadData()
  225. //Appearance
  226. appearance_RotateSelect.properties.text = String(format: "%.0f", backgroundData.rotation) + "°"
  227. appearance_RotateSelect.reloadData()
  228. if backgroundData.opacity > 1 {
  229. backgroundData.opacity = 1
  230. }
  231. appearance_OpacitySelect.properties.text = String(format: "%.0f", backgroundData.opacity*100) + "%"
  232. appearance_OpacitySelect.reloadData()
  233. appearanceScaleCheckbox.properties.checkboxType = backgroundData.isScale ? .selected : .normal
  234. appearanceScaleCheckbox.reloadData()
  235. appearanceScaleSelect.properties.isDisabled = backgroundData.isScale ? false : true
  236. appearanceScaleSelect.properties.text = String(format: "%.0f", backgroundData.scale*100) + "%"
  237. appearanceScaleSelect.reloadData()
  238. //Position
  239. if backgroundData.verticalMode == 2 {
  240. positionItemView.properties.selRow = 0
  241. } else if backgroundData.verticalMode == 1 {
  242. positionItemView.properties.selRow = 1
  243. } else if backgroundData.verticalMode == 0 {
  244. positionItemView.properties.selRow = 2
  245. }
  246. if backgroundData.horizontalMode == 0 {
  247. positionItemView.properties.selColumn = 0
  248. } else if backgroundData.horizontalMode == 1 {
  249. positionItemView.properties.selColumn = 1
  250. } else if backgroundData.horizontalMode == 2 {
  251. positionItemView.properties.selColumn = 2
  252. }
  253. positionItemView.reloadData()
  254. positionXInput.properties.text = String(format: "%.0f", backgroundData.horizontalSpace)
  255. positionXInput.reloadData()
  256. positionYInput.properties.text = String(format: "%.0f", backgroundData.verticalSpace)
  257. positionYInput.reloadData()
  258. if backgroundData.type == .color {
  259. saveButton.properties.isDisabled = false
  260. } else if backgroundData.type == .image {
  261. if backgroundData.imagePath != nil {
  262. saveButton.properties.isDisabled = false
  263. } else {
  264. saveButton.properties.isDisabled = true
  265. }
  266. }
  267. saveButton.reloadData()
  268. }
  269. //MARK: - Action
  270. @objc func leftTopButtonClicked(_ sender: ComponentButton) {
  271. if sender == leftTopButton {
  272. var isChanged = false
  273. if let dict = self.originalDataDict {
  274. isChanged = KMBackgroundManager.compareIsChangedModel(backgroundData, withDict: dict as NSDictionary)
  275. }
  276. if isChanged == true {
  277. let alert = NSAlert()
  278. alert.alertStyle = .critical
  279. alert.messageText = KMLocalizedString("Save template changes?")
  280. alert.informativeText = KMLocalizedString("Cancel and they will not be saved.")
  281. alert.addButton(withTitle: KMLocalizedString("Save"))
  282. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  283. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  284. if response == NSApplication.ModalResponse.alertFirstButtonReturn {
  285. let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
  286. self.delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
  287. } else {
  288. if let dict = self.originalDataDict {
  289. KMBackgroundManager.defaultManager.updateModel(self.backgroundData, withDict: dict as NSDictionary)
  290. let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
  291. }
  292. self.delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
  293. }
  294. }
  295. } else {
  296. delegate?.backgroundPropertyControllerCancelTemplateEdit?(self)
  297. }
  298. }
  299. }
  300. @objc func templateButtonClicked(_ sender: ComponentButton) {
  301. if editSubType == .add {
  302. delegate?.backgroundPropertyControllerDidChangetoTemplate?(self)
  303. } else if editSubType == .edit {
  304. let _ = KMBackgroundManager.defaultManager.updateTemplate(model: self.backgroundData)
  305. delegate?.backgroundPropertyControllerFinishTemplateEdit?(self)
  306. }
  307. }
  308. @objc func checkBoxClicked(_ sender: ComponentCheckBox) {
  309. if sender == appearanceScaleCheckbox {
  310. backgroundData.isScale = appearanceScaleCheckbox.properties.checkboxType == .selected ? true : false
  311. }
  312. reloadData()
  313. delegate?.backgroundPropertyControllerDidUpdate?(self)
  314. }
  315. @objc func saveButtonClicked(_ sender: ComponentButton) {
  316. let saveWindow: KMWatermarkSaveWindow = KMWatermarkSaveWindow(windowNibName: "KMWatermarkSaveWindow")
  317. if backgroundData.type == .color {
  318. saveWindow.nameValue = backgroundData.name
  319. } else if backgroundData.type == .image {
  320. saveWindow.nameValue = backgroundData.imagePath?.lastPathComponent ?? ""
  321. }
  322. saveWindow.saveHandler = {[weak self] string in
  323. guard let weakSelf = self else { return }
  324. DispatchQueue.main.async {
  325. weakSelf.backgroundData.name = string ?? ""
  326. if KMBackgroundManager.defaultManager.addTemplate(model: weakSelf.backgroundData) {
  327. weakSelf.delegate?.backgroundPropertyControllerSaveTemplateSuccess?(weakSelf, weakSelf.backgroundData)
  328. }
  329. }
  330. }
  331. saveWindow.own_beginSheetModal(for: view.window) { string in
  332. }
  333. }
  334. @objc func chooseURLAction(_ sender: NSView) {
  335. self.chooseURLAction(sender, nil)
  336. }
  337. func chooseURLAction(_ sender: NSView, _ filePath: String? = nil) {
  338. let openPanel = NSOpenPanel()
  339. openPanel.canChooseDirectories = false
  340. openPanel.canChooseFiles = true
  341. openPanel.allowsMultipleSelection = false
  342. openPanel.allowedFileTypes = ["jpg", "jpeg", "png", "pdf"]
  343. if let path = filePath {
  344. openPanel.directoryURL = NSURL.fileURL(withPath: path)
  345. }
  346. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  347. if result == NSApplication.ModalResponse.OK {
  348. guard let url = openPanel.url else { return }
  349. let filePath = url.path
  350. let outFolder = KMBackgroundManager.defaultManager.kBackgroundImageFolder
  351. let fileName = filePath.getLastComponentDeleteExtension
  352. if filePath.extension.lowercased() == ".pdf" {
  353. let pdf = CPDFDocument(url: url)
  354. guard !pdf!.isEncrypted else { return }
  355. if let image = self.generateThumbnail(for: url) {
  356. if let outFolderPath = outFolder?.stringByAppendingPathComponent(fileName + ".png") {
  357. try?image.pngData()?.write(to: URL(fileURLWithPath: outFolderPath))
  358. if FileManager.default.fileExists(atPath: outFolderPath) {
  359. self.backgroundData.imagePath = outFolderPath
  360. self.delegate?.backgroundPropertyControllerDidUpdate?(self)
  361. self.reloadData()
  362. }
  363. }
  364. }
  365. } else {
  366. let image = NSImage(contentsOfFile: url.path)
  367. if image == nil || NSImage.isDamageImage(image, imagePath: url.path) {
  368. let alert = NSAlert()
  369. alert.alertStyle = .critical
  370. alert.messageText = "The file \"\(url.lastPathComponent)\" could not be opened."
  371. alert.informativeText = "It may be damaged or use a file format that PDF Reader Pro doesn’t recognize."
  372. alert.addButton(withTitle: "Cancel")
  373. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  374. if response == NSApplication.ModalResponse.alertFirstButtonReturn {
  375. // Cancel button clicked
  376. }
  377. }
  378. return
  379. }
  380. if let resultPath = outFolder?.stringByAppendingPathComponent(fileName + ".png") {
  381. try?FileManager.default.copyItem(atPath: url.path, toPath: resultPath)
  382. self.backgroundData.imagePath = resultPath
  383. }
  384. self.delegate?.backgroundPropertyControllerDidUpdate?(self)
  385. self.reloadData()
  386. }
  387. }
  388. }
  389. }
  390. func generateThumbnail(for pdfURL: URL) -> NSImage? {
  391. // 创建PDF文档
  392. guard let pdfDocument = CGPDFDocument(pdfURL as CFURL),
  393. let pdfPage = pdfDocument.page(at: 1) else {
  394. print("打开PDF文件失败")
  395. return nil
  396. }
  397. // 获取页面尺寸
  398. let pageRect = pdfPage.getBoxRect(.mediaBox)
  399. // 创建图形上下文
  400. let size = NSSize(width: 200, height: 300)
  401. let bitsPerComponent = 8
  402. let bytesPerPixel = 4
  403. let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
  404. let context = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: bitsPerComponent, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: bitmapInfo)
  405. // 绘制PDF页面
  406. context?.saveGState()
  407. context?.translateBy(x: 0, y: size.height)
  408. context?.scaleBy(x: 1.0, y: -1.0) // 反转坐标系
  409. context?.scaleBy(x: size.width / pageRect.width, y: size.height / pageRect.height) // 缩放
  410. context?.drawPDFPage(pdfPage)
  411. context?.restoreGState()
  412. // 创建NSImage
  413. if let cgImage = context?.makeImage() {
  414. return NSImage(cgImage: cgImage, size: NSSize(width: size.width, height: size.height))
  415. } else {
  416. return nil
  417. }
  418. }
  419. //MARK: - Mouse
  420. override func mouseDown(with event: NSEvent) {
  421. super.mouseDown(with: event)
  422. view.window?.makeFirstResponder(nil)
  423. }
  424. }
  425. //MARK: - ComponentTabsDelegate
  426. extension KMBackgroundPropertyController: ComponentTabsDelegate {
  427. func componentTabsDidSelected(_ view: ComponentTabs, _ property: ComponentTabsProperty) {
  428. if property == textTabProperty {
  429. backgroundData.type = .color
  430. } else if property == fileTabProperty {
  431. backgroundData.type = .image
  432. }
  433. reloadData()
  434. delegate?.backgroundPropertyControllerDidUpdate?(self)
  435. }
  436. }
  437. //MARK: - ComponentCColorDelegate
  438. extension KMBackgroundPropertyController: ComponentCColorDelegate {
  439. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  440. if view == fontColorGroup {
  441. backgroundData.color = color ?? NSColor.clear
  442. }
  443. reloadData()
  444. delegate?.backgroundPropertyControllerDidUpdate?(self)
  445. }
  446. }
  447. //MARK: - ComponentSelectDelegate
  448. extension KMBackgroundPropertyController: ComponentSelectDelegate {
  449. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  450. if view == appearance_OpacitySelect {
  451. if let text = menuItemProperty?.text {
  452. let result = text.stringByDeleteCharString("%")
  453. backgroundData.opacity = result.stringToCGFloat()/100
  454. }
  455. } else if view == appearance_RotateSelect {
  456. if let text = menuItemProperty?.text {
  457. let result = text.stringByDeleteCharString("°")
  458. backgroundData.rotation = result.stringToCGFloat()
  459. }
  460. } else if view == appearanceScaleSelect {
  461. if let text = menuItemProperty?.text {
  462. let result = text.stringByDeleteCharString("%")
  463. backgroundData.scale = result.stringToCGFloat()/100
  464. }
  465. }
  466. reloadData()
  467. delegate?.backgroundPropertyControllerDidUpdate?(self)
  468. }
  469. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  470. if view == appearance_OpacitySelect {
  471. if let text = appearance_OpacitySelect.properties.text {
  472. let result = text.stringByDeleteCharString("%")
  473. backgroundData.opacity = result.stringToCGFloat()/100
  474. }
  475. } else if view == appearance_RotateSelect {
  476. if let text = appearance_RotateSelect.properties.text {
  477. let result = text.stringByDeleteCharString("°")
  478. backgroundData.rotation = result.stringToCGFloat()
  479. }
  480. } else if view == appearanceScaleSelect {
  481. if let text = appearanceScaleSelect.properties.text {
  482. let result = text.stringByDeleteCharString("%")
  483. backgroundData.scale = min(10, result.stringToCGFloat()/100)
  484. }
  485. }
  486. reloadData()
  487. delegate?.backgroundPropertyControllerDidUpdate?(self)
  488. }
  489. }
  490. //MARK: - ComponentCPositionDelegate
  491. extension KMBackgroundPropertyController: ComponentCPositionDelegate {
  492. func componentCPositionDidChoose(_ view: NSView, _ row: Int, _ column: Int) {
  493. if view == positionItemView {
  494. if row == 0 {
  495. backgroundData.verticalMode = 2
  496. } else if row == 1 {
  497. backgroundData.verticalMode = 1
  498. } else if row == 2 {
  499. backgroundData.verticalMode = 0
  500. }
  501. if column == 0 {
  502. backgroundData.horizontalMode = 0
  503. } else if column == 1 {
  504. backgroundData.horizontalMode = 1
  505. } else if column == 2 {
  506. backgroundData.horizontalMode = 2
  507. }
  508. }
  509. reloadData()
  510. delegate?.backgroundPropertyControllerDidUpdate?(self)
  511. }
  512. }
  513. //MARK: - ComponentInputNumberDelegate
  514. extension KMBackgroundPropertyController: ComponentInputNumberDelegate {
  515. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  516. if inputNumber == positionXInput {
  517. if let text = inputNumber?.properties.text {
  518. backgroundData.horizontalSpace = text.stringToCGFloat()
  519. }
  520. } else if inputNumber == positionYInput {
  521. if let text = inputNumber?.properties.text {
  522. backgroundData.verticalSpace = text.stringToCGFloat()
  523. }
  524. }
  525. reloadData()
  526. delegate?.backgroundPropertyControllerDidUpdate?(self)
  527. }
  528. }
  529. //MARK: - ComponentInputDelegate
  530. extension KMBackgroundPropertyController: ComponentInputDelegate {
  531. func componentInputDidCoverButtonClicked(inputView: ComponentInput) {
  532. if inputView.properties.text.count > 0 && inputView.properties.creatable == false {
  533. self.chooseURLAction(inputView, inputView.properties.text)
  534. }
  535. }
  536. }