// // KMAILanguagePopVC.swift // PDF Master // // Created by wanjun on 2023/5/24. // import Cocoa typealias aiLanguagePopCellViewDownCallback = (_ language: String) -> Void class KMAILanguagePopTableviewCell: NSTableCellView { @IBOutlet weak var mainBox: KMBox! @IBOutlet weak var languageLabel: NSTextField! var selectUrls: [URL] = [] var isSelect: Bool = false var selectString: String = "" // MARK: Init override func awakeFromNib() { super.awakeFromNib() self.mainBox.fillColor = .clear self.languageLabel.textColor = NSColor(hex: "#252629") self.languageLabel.font = NSFont.SFProTextRegular(14.0) self.mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in if !self.isSelect { if mouseEntered { self.mainBox.fillColor = NSColor(hex: "#EDEEF0") } else { self.mainBox.fillColor = .clear } } } self.mainBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in if downEntered { if !self.isSelect { self.mainBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1) } } } } // MARK: Private Methods func refreshAIUI() -> Void { self.mainBox.fillColor = .clear } // MARK: Action } class KMAILanguagePopVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { @IBOutlet weak var tableView: NSTableView! @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint! @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint! var languages: [String] = [] var downCallback: aiLanguagePopCellViewDownCallback? var selectString: String = "" func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self { self.languages = popViewDataArr return self } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. NotificationCenter.default.addObserver(self, selector: #selector(tableViewDidScroll(_:)), name: NSScrollView.didLiveScrollNotification, object: tableView.enclosingScrollView) } override func viewDidAppear() { super.viewDidAppear() self.tableView.reloadData() } // MARK: NSTableViewDelegate func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { return 32.0; } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let language: String = languages[row] as! String let identifier = tableColumn?.identifier let cellView: KMAILanguagePopTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMAILanguagePopTableviewCell cellView.languageLabel.stringValue = language cellView.refreshAIUI() if language == self.selectString { cellView.mainBox.fillColor = NSColor(hex: "#0A82FF", alpha: 0.75) cellView.isSelect = true } else { cellView.isSelect = false } return cellView } func tableViewSelectionDidChange(_ notification: Notification) { let tableView: NSTableView = notification.object as? NSTableView ?? NSTableView() if tableView == self.tableView { if (self.tableView?.selectedRow != -1) { if let callback = self.downCallback { callback(self.languages[self.tableView!.selectedRow]) } } } } @objc func tableViewDidScroll(_ notification: Notification) { self.tableView.reloadData() } // MARK: NSTableViewDataSource func numberOfRows(in tableView: NSTableView) -> Int { return languages.count } }