123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- //
- // 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
- }
- }
- }
|