// // KMPreferenceMarkupController.swift // PDF Master // // Created by tangchao on 2023/2/3. // import Cocoa /// 偏好设置 Markup控制器 class KMPreferenceMarkupController: NSViewController { @IBOutlet weak var markupColorLabel: NSTextField! @IBOutlet weak var markupColorBox: NSBox! var markupColorView = KMPreferenceMarkupColorView() @IBOutlet weak var markupFontLabel: NSTextField! @IBOutlet weak var markupFontBox: NSBox! @IBOutlet weak var textLabel: NSTextField! @IBOutlet weak var textComboBox: NSComboBox! @IBOutlet weak var noteLabel: NSTextField! @IBOutlet weak var noteComboBox: NSComboBox! @IBOutlet weak var aligementView: KMPreferenceAligementView! var currentColorItemView: KMPreferenceMarkupColorItemView? lazy var fontNames = KMPreferenceManager.supportFonts deinit { KMPrint("KMPreferenceMarkupController 已释放.") if (NSColorPanel.sharedColorPanelExists) { let panel = NSColorPanel.shared panel.setTarget(nil) panel.setAction(nil) panel.close() } } override func viewDidLoad() { super.viewDidLoad() self.markupColorLabel.stringValue = NSLocalizedString("Markup Default Color", comment: "") self.markupFontLabel.stringValue = NSLocalizedString("Markup Default Font", comment: "") self.textLabel.stringValue = NSLocalizedString("Text:", comment: "") self.noteLabel.stringValue = NSLocalizedString("Note", comment: "") self.markupColorBox.contentView?.addSubview(self.markupColorView) self.markupColorView.frame = self.markupColorBox.contentView!.bounds self.markupColorView.autoresizingMask = [.width, .height] let keys: [KMPreferenceMarkupColorKey] = [.highlight, .rectangleFill, .strikthrough, .rectangleBorder, .underline, .circleFill, .pen, .circleBorder, .text, .line, .note, .arrow] self.markupColorView.initKeys(keys) self.markupColorView.itemClick = { [weak self] itemView in self!.currentColorItemView = itemView var color: NSColor? let cgColor = itemView?.button.layer?.backgroundColor if (cgColor != nil) { color = NSColor(cgColor: cgColor!) } let panel = NSColorPanel.shared if (color != nil) { panel.color = color! } panel.setTarget(self) panel.setAction(#selector(self!.colorPanelAction)) panel.orderFront(nil) } let labels = [NSLocalizedString("Highlight:", comment: ""), NSLocalizedString("Rectangle fill:", comment: ""), "\(NSLocalizedString("Strikethrough", comment: "")):", NSLocalizedString("Rectangle border:", comment: ""), NSLocalizedString("Underline:", comment: ""), NSLocalizedString("Circle fill:", comment: ""), NSLocalizedString("Pen:", comment: ""), NSLocalizedString("Circle border:", comment: ""), NSLocalizedString("Text:", comment: ""), NSLocalizedString("Line:", comment: ""), NSLocalizedString("Anchored:", comment: ""), NSLocalizedString("Arrow:", comment: "")] var i: Int = 0 for itemView in self.markupColorView.itemViews { itemView.label.stringValue = labels[i] itemView.button.wantsLayer = true itemView.button.layer?.cornerRadius = 10 itemView.button.layer?.borderWidth = 1 itemView.button.layer?.borderColor = NSColor(hex: "#0000001A").cgColor i += 1 } self.textComboBox.removeAllItems() self.textComboBox.addItems(withObjectValues: self.fontNames) self.noteComboBox.removeAllItems() self.noteComboBox.addItems(withObjectValues: self.fontNames) for comboBox in [self.textComboBox, self.noteComboBox] { comboBox?.delegate = self } self.aligementView.itemClick = { index in if (index == 0) { KMPreferenceManager.shared.markupFontTextAligment = .left } else if (index == 1) { KMPreferenceManager.shared.markupFontTextAligment = .center } else if (index == 2) { KMPreferenceManager.shared.markupFontTextAligment = .right } } self.initUIProperty() self.initData() } private func initUIProperty() { self.markupColorBox.borderColor = NSColor(white: 0, alpha: 0.05) self.markupColorBox.fillColor = NSColor.white self.markupColorView.vLine.layer?.backgroundColor = NSColor(white: 0, alpha: 0.1).cgColor self.markupFontBox.borderColor = NSColor(white: 0, alpha: 0.05) self.markupFontBox.fillColor = NSColor(white: 0, alpha: 0.05) self.markupColorLabel.textColor = NSColor(hex: "#252629") self.markupFontLabel.textColor = NSColor(hex: "#252629") self.textLabel.textColor = NSColor(hex: "#000000D9") self.noteLabel.textColor = NSColor(hex: "#000000D9") for itemView in self.markupColorView.itemViews { itemView.label.textColor = NSColor(hex: "#000000D9") } } private func initData() { for itemView in self.markupColorView.itemViews { var color: NSColor? if (itemView.key == .highlight) { color = KMPreferenceManager.shared.markupHighlightColor } else if (itemView.key == .rectangleFill) { color = KMPreferenceManager.shared.markupRectangleFillColor } else if (itemView.key == .strikthrough) { color = KMPreferenceManager.shared.markupStrikthroughColor } else if (itemView.key == .rectangleBorder) { color = KMPreferenceManager.shared.markupRectangleBorderColor } else if (itemView.key == .underline) { color = KMPreferenceManager.shared.markupUnderlineColor } else if (itemView.key == .circleFill) { color = KMPreferenceManager.shared.markupCircleFillColor } else if (itemView.key == .pen) { color = KMPreferenceManager.shared.markupPenColor } else if (itemView.key == .circleBorder) { color = KMPreferenceManager.shared.markupCircleBorderColor } else if (itemView.key == .text) { color = KMPreferenceManager.shared.markupTextColor } else if (itemView.key == .line) { color = KMPreferenceManager.shared.markupLineColor } else if (itemView.key == .note) { color = KMPreferenceManager.shared.markupNoteColor } else if (itemView.key == .arrow) { color = KMPreferenceManager.shared.markupArrowColor } if (color != nil) { var red: CGFloat = 0.0 var green: CGFloat = 0.0 var blue: CGFloat = 0.0 var alpha: CGFloat = 0.0 color!.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) if (red == 0 && green == 0 && blue == 0 && alpha == 0) { itemView.button.image = NSImage(named: "KMImageNamePreferenceClearColor") } else { itemView.button.image = nil itemView.button.layer?.backgroundColor = color!.cgColor } } } self.textComboBox.stringValue = KMPreferenceManager.shared.markupFontTextString let aliigment = KMPreferenceManager.shared.markupFontTextAligment if (aliigment == .left) { self.aligementView.selectIndex(index: 0) } else if (aliigment == .center) { self.aligementView.selectIndex(index: 1) } else if (aliigment == .right) { self.aligementView.selectIndex(index: 2) } self.noteComboBox.stringValue = KMPreferenceManager.shared.markupFontNoteString } @objc private func colorPanelAction(sender: NSColorPanel) { if (self.currentColorItemView == nil) { return } let itemView = self.currentColorItemView! itemView.button.image = nil itemView.button.layer?.backgroundColor = sender.color.cgColor if (itemView.key == .highlight) { KMPreferenceManager.shared.markupHighlightColor = sender.color } else if (itemView.key == .rectangleFill) { KMPreferenceManager.shared.markupRectangleFillColor = sender.color } else if (itemView.key == .strikthrough) { KMPreferenceManager.shared.markupStrikthroughColor = sender.color } else if (itemView.key == .rectangleBorder) { KMPreferenceManager.shared.markupRectangleBorderColor = sender.color } else if (itemView.key == .underline) { KMPreferenceManager.shared.markupUnderlineColor = sender.color } else if (itemView.key == .circleFill) { KMPreferenceManager.shared.markupCircleFillColor = sender.color } else if (itemView.key == .pen) { KMPreferenceManager.shared.markupPenColor = sender.color } else if (itemView.key == .circleBorder) { KMPreferenceManager.shared.markupCircleBorderColor = sender.color } else if (itemView.key == .text) { KMPreferenceManager.shared.markupTextColor = sender.color } else if (itemView.key == .line) { KMPreferenceManager.shared.markupLineColor = sender.color } else if (itemView.key == .note) { KMPreferenceManager.shared.markupNoteColor = sender.color } else if (itemView.key == .arrow) { KMPreferenceManager.shared.markupArrowColor = sender.color } } } extension KMPreferenceMarkupController: NSComboBoxDelegate { func comboBoxSelectionDidChange(_ notification: Notification) { if (self.textComboBox.isEqual(to: notification.object)) { var index = self.textComboBox.indexOfSelectedItem if (index < 0) { index = 0 } KMPreferenceManager.shared.markupFontTextString = self.fontNames[index] } else if (self.noteComboBox.isEqual(to: notification.object)) { var index = self.noteComboBox.indexOfSelectedItem if (index < 0) { index = 0 } KMPreferenceManager.shared.markupFontNoteString = self.fontNames[index] } } } extension KMPreferenceMarkupController: KMPreferenceDealDataChangeProtocol { func refreshUI() { DispatchQueue.main.async { self.initData() } } }