// // KMNoteFooterCellView.swift // PDF Reader Pro // // Created by User-Tangchao on 2024/9/19. // import Cocoa import KMComponentLibrary class KMNoteFooterCellView: NSTableCellView, NibLoadable { @IBOutlet weak var replyBox: NSBox! @IBOutlet weak var inputBox: KMBox! @IBOutlet weak var inputTextF: KMTextField! @IBOutlet weak var replyButton: NSButton! @IBOutlet weak var cancelButton: NSButton! @IBOutlet weak var replyButtonBox: NSBox! @IBOutlet weak var operationBox: NSBox! @IBOutlet weak var operationIv: NSImageView! @IBOutlet weak var operationButton: NSButton! @IBOutlet weak var operationIvHover: KMCoverButton! @IBOutlet weak var inputBoxHeightConst: NSLayoutConstraint! @IBOutlet weak var replyButtonTopConst: NSLayoutConstraint! private lazy var inputRespButton_: NSButton = { let view = NSButton() view.isBordered = false view.title = KMLocalizedString("") view.target = self view.action = #selector(_inputRespButtonAction) return view }() var itemClick: KMCommonClickBlock? var inputDidChanged: KMValueDidChangeBlock? var model: KMBotaAnnotationFooterModel? private var flag_ = false override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func awakeFromNib() { super.awakeFromNib() self.flag_ = false // self.replyBox.fillColor = .red self.inputBox.cornerRadius = 0 self.inputBox.moveCallback = { [weak self] enter, theBox in if enter { theBox.borderColor = NSColor.km_init(hex: "#1770F4") } else { // let focus = theBox.window?.firstResponder == self?.inputTextF let focus = self?.flag_ ?? false if focus == false { theBox.borderColor = NSColor(white: 0, alpha: 0.2) } } } self.inputBox.downCallback = { [weak self] enter, theBox, _ in theBox.window?.makeFirstResponder(self?.inputTextF) } self.inputTextF.drawsBackground = false self.inputTextF.isBordered = false self.inputTextF.isBezeled = false self.inputTextF.focusRingType = .none self.inputTextF.backgroundColor = .clear self.inputTextF.wantsLayer = true self.inputTextF.layer?.backgroundColor = .clear self.inputTextF.delegate = self let cell = self.inputTextF.cell as? NSTextFieldCell self.inputTextF.textColor = KMAppearance.titleColor() self.inputTextF.firstResponderHandler = { [weak self] result in self?.flag_ = result for sv in self?.inputTextF.subviews ?? [] { for childsv in sv.subviews { if let data = childsv as? NSTextView { data.backgroundColor = .clear } } } if result { self?.inputBox.borderColor = NSColor.km_init(hex: "#1770F4") self?.inputRespButton_.isHidden = true if let range = self?.inputTextF.currentEditor()?.selectedRange { self?.inputTextF.currentEditor()?.selectedRange = NSMakeRange(range.length, 0) } } else { if KMAppearance.isDarkMode() { self?.inputBox.borderColor = NSColor(hex: "#56585A") } else { self?.inputBox.borderColor = NSColor(white: 0, alpha: 0.2) } self?.inputRespButton_.isHidden = false } } self.inputBox.contentView?.addSubview(self.inputRespButton_) self.inputRespButton_.frame = self.inputBox.contentView?.bounds ?? .zero self.inputRespButton_.autoresizingMask = [.width, .height] self.replyButton.title = KMLocalizedString("Done") self.replyButton.wantsLayer = true self.replyButtonBox.cornerRadius = 1 self.replyButtonBox.borderWidth = 0 self.replyButton.target = self self.replyButton.action = #selector(replyAction) self.cancelButton.title = KMLocalizedString("Cancel") self.cancelButton.target = self self.cancelButton.action = #selector(cancelAction) self.operationBox.borderWidth = 0 self.operationIv.image = NSImage(named: "KMImageNameBotaNoteOpration") self.operationButton.title = "" self.operationButton.target = self self.operationButton.action = #selector(operationAction) self.operationIvHover.wantsLayer = true self.operationIvHover.layer?.cornerRadius = 4 self.operationIvHover.coverAction = { btn, act in if act == .enter { btn.layer?.backgroundColor = NSColor(hex: "#477EDE").withAlphaComponent(0.1).cgColor } else if act == .exit { btn.layer?.backgroundColor = .clear } } self.updateUI(expand: false, animated: false) self.updateViewUI() NotificationCenter.default.addObserver(self, selector: #selector(updateViewUI), name: APPAppearanceChangedNotificationName, object: nil) } func updateUI(expand: Bool, animated: Bool) { if expand { // 64 if animated { self.inputBoxHeightConst.animator().constant = 64+6 } else { self.inputBoxHeightConst.constant = 64+6 } // 12 self.replyBox.isHidden = false } else { // 64 if animated { self.inputBoxHeightConst.animator().constant = 0 } else { self.inputBoxHeightConst.constant = 0 } self.replyBox.isHidden = true } } @objc func replyAction(_ sender: NSButton) { self.itemClick?(3, self.inputTextF.stringValue) } @objc func cancelAction(_ sender: NSButton) { self.itemClick?(4) } @objc func commentAction(_ sender: NSButton) { self.itemClick?(1) } @objc func operationAction(_ sender: NSButton) { self.itemClick?(2, sender) } @objc private func _inputRespButtonAction() { self.window?.makeFirstResponder(self.inputTextF) self.inputRespButton_.isHidden = true } @objc func updateViewUI() { if KMAppearance.isDarkMode() { self.inputBox.fillColor = NSColor(hex: "#393C3E") self.inputBox.borderColor = NSColor(hex: "#56585A") } else { self.inputBox.fillColor = .white self.inputBox.borderColor = NSColor(white: 0, alpha: 0.2) } let buttonColor = ComponentLibrary.shared.getComponentColorFromKey("comp-button/textPrimary-colorText-nor") let buttonFont = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular") self.replyButton.setTitleColor(color: buttonColor, font: buttonFont) self.cancelButton.setTitleColor(color: buttonColor, font: buttonFont) } override func updateLayer() { super.updateLayer() self.updateViewUI() } } extension KMNoteFooterCellView: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { if self.inputTextF.isEqual(to: obj.object) { if KMAppearance.isDarkMode() { self.inputBox.borderColor = NSColor(hex: "#56585A") } else { self.inputBox.borderColor = NSColor(white: 0, alpha: 0.2) } self.flag_ = false self.inputRespButton_.isHidden = false } } func controlTextDidChange(_ obj: Notification) { if self.inputTextF.isEqual(to: obj.object) { self.inputDidChanged?(self.inputTextF.stringValue, nil) } } func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool { switch commandSelector { case #selector(NSResponder.insertNewline(_:)): if let inputView = control as? NSTextField { if inputView == self.inputTextF { self.itemClick?(3, self.inputTextF.stringValue) } } return true default: return false } } }