KMAILanguagePopVC.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // KMAILanguagePopVC.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/5/24.
  6. //
  7. import Cocoa
  8. typealias aiLanguagePopCellViewDownCallback = (_ language: String) -> Void
  9. class KMAILanguagePopTableviewCell: NSTableCellView {
  10. @IBOutlet weak var mainBox: KMBox!
  11. @IBOutlet weak var languageLabel: NSTextField!
  12. var selectUrls: [URL] = []
  13. var isSelect: Bool = false
  14. var selectString: String = ""
  15. // MARK: Init
  16. override func awakeFromNib() {
  17. super.awakeFromNib()
  18. self.mainBox.fillColor = .clear
  19. self.languageLabel.textColor = NSColor.km_init(hex: "#252629")
  20. self.languageLabel.font = NSFont.SFProTextRegularFont(14.0)
  21. self.mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  22. if !self.isSelect {
  23. if mouseEntered {
  24. self.mainBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  25. } else {
  26. self.mainBox.fillColor = .clear
  27. }
  28. }
  29. }
  30. self.mainBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  31. if downEntered {
  32. if !self.isSelect {
  33. self.mainBox.fillColor = NSColor.km_init(hex: "#1770F4", alpha: 0.1)
  34. }
  35. }
  36. }
  37. }
  38. // MARK: Private Methods
  39. func refreshAIUI() -> Void {
  40. self.mainBox.fillColor = .clear
  41. }
  42. // MARK: Action
  43. }
  44. class KMAILanguagePopVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
  45. @IBOutlet weak var tableView: NSTableView!
  46. @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint!
  47. @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint!
  48. var languages: [String] = []
  49. var downCallback: aiLanguagePopCellViewDownCallback?
  50. var selectString: String = ""
  51. func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self {
  52. self.languages = popViewDataArr
  53. return self
  54. }
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. // Do view setup here.
  58. NotificationCenter.default.addObserver(self, selector: #selector(tableViewDidScroll(_:)), name: NSScrollView.didLiveScrollNotification, object: tableView.enclosingScrollView)
  59. }
  60. override func viewDidAppear() {
  61. super.viewDidAppear()
  62. self.tableView.reloadData()
  63. }
  64. // MARK: NSTableViewDelegate
  65. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  66. return 32.0;
  67. }
  68. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  69. let language: String = languages[row] as! String
  70. let identifier = tableColumn?.identifier
  71. let cellView: KMAILanguagePopTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMAILanguagePopTableviewCell
  72. cellView.languageLabel.stringValue = language
  73. cellView.refreshAIUI()
  74. if language == self.selectString {
  75. cellView.mainBox.fillColor = NSColor.km_init(hex: "#0A82FF", alpha: 0.75)
  76. cellView.isSelect = true
  77. } else {
  78. cellView.isSelect = false
  79. }
  80. return cellView
  81. }
  82. func tableViewSelectionDidChange(_ notification: Notification) {
  83. let tableView: NSTableView = notification.object as? NSTableView ?? NSTableView()
  84. if tableView == self.tableView {
  85. if (self.tableView?.selectedRow != -1) {
  86. if let callback = self.downCallback {
  87. callback(self.languages[self.tableView!.selectedRow])
  88. }
  89. }
  90. }
  91. }
  92. @objc func tableViewDidScroll(_ notification: Notification) {
  93. self.tableView.reloadData()
  94. }
  95. // MARK: NSTableViewDataSource
  96. func numberOfRows(in tableView: NSTableView) -> Int {
  97. return languages.count
  98. }
  99. }