KMAILanguagePopVC.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // KMAILanguagePopVC.swift
  3. // PDF Master
  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. // MARK: Init
  15. override func awakeFromNib() {
  16. super.awakeFromNib()
  17. self.mainBox.fillColor = .clear
  18. self.languageLabel.textColor = NSColor(hex: "#252629")
  19. self.languageLabel.font = NSFont.SFProTextRegular(14.0)
  20. self.mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  21. if !self.isSelect {
  22. if mouseEntered {
  23. self.mainBox.fillColor = NSColor(hex: "#EDEEF0")
  24. } else {
  25. self.mainBox.fillColor = .clear
  26. }
  27. }
  28. }
  29. self.mainBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  30. if downEntered {
  31. if !self.isSelect {
  32. self.mainBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1)
  33. }
  34. }
  35. }
  36. }
  37. // MARK: Private Methods
  38. func initializeUI(_ url: URL) -> Void {
  39. }
  40. // MARK: Action
  41. }
  42. class KMAILanguagePopVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
  43. @IBOutlet weak var tableView: NSTableView!
  44. @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint!
  45. @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint!
  46. var languages: [String] = []
  47. var downCallback: aiLanguagePopCellViewDownCallback?
  48. func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self {
  49. self.languages = popViewDataArr
  50. return self
  51. }
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54. // Do view setup here.
  55. }
  56. override func viewDidAppear() {
  57. super.viewDidAppear()
  58. self.tableView.reloadData()
  59. }
  60. // MARK: NSTableViewDelegate
  61. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  62. return 32.0;
  63. }
  64. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  65. let language: String = languages[row] as! String
  66. let identifier = tableColumn?.identifier
  67. let cellView: KMAILanguagePopTableviewCell = tableView.makeView(withIdentifier:identifier!, owner: self) as! KMAILanguagePopTableviewCell
  68. cellView.languageLabel.stringValue = language
  69. return cellView
  70. }
  71. func tableViewSelectionDidChange(_ notification: Notification) {
  72. let tableView: NSTableView = notification.object as? NSTableView ?? NSTableView()
  73. if tableView == self.tableView {
  74. if (self.tableView?.selectedRow != -1) {
  75. if let callback = self.downCallback {
  76. callback(self.languages[self.tableView!.selectedRow])
  77. }
  78. }
  79. }
  80. }
  81. // MARK: NSTableViewDataSource
  82. func numberOfRows(in tableView: NSTableView) -> Int {
  83. return languages.count
  84. }
  85. }