KMBOTAOutlineCellView.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // KMBOTAOutlineCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/4/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMBOTAOutlineCellViewIconAction = (_ view: KMBOTAOutlineCellView ) -> ()
  10. class KMBOTAOutlineCellView: NSTableCellView, NibLoadable {
  11. @IBOutlet weak var titleLabel: KMTextField!
  12. @IBOutlet weak var titleLabelBox: KMBox!
  13. @IBOutlet weak var iconButton: NSButton!
  14. var iconAction: KMBOTAOutlineCellViewIconAction?
  15. var model: KMBOTAOutlineItem? {
  16. didSet {
  17. self.reloadData()
  18. }
  19. }
  20. private lazy var hoverBox_: NSBox = {
  21. let box = NSBox()
  22. box.boxType = .custom
  23. box.titlePosition = .noTitle
  24. box.contentViewMargins = .zero
  25. box.borderWidth = 0
  26. return box
  27. }()
  28. var hoverBox: NSBox {
  29. get {
  30. return hoverBox_
  31. }
  32. }
  33. override func awakeFromNib() {
  34. self.setup()
  35. }
  36. func setup() {
  37. self.titleLabel.maximumNumberOfLines = 0
  38. self.addSubview(hoverBox_)
  39. hoverBox_.km_add_leading_constraint(equalTo: iconButton, attribute: .trailing, constant: 4)
  40. hoverBox_.km_add_top_constraint()
  41. hoverBox_.km_add_bottom_constraint()
  42. hoverBox_.km_add_trailing_constraint(constant: -16)
  43. titleLabel.delegate = self
  44. titleLabel.firstResponderHandler = { [weak self] firstResp in
  45. if firstResp {
  46. DispatchQueue.main.async {
  47. self?.titleLabelBox.borderWidth = 1
  48. self?.titleLabelBox.borderColor = NSColor.km_init(hex: "#4982E6")
  49. if KMAppearance.isDarkMode() {
  50. self?.titleLabelBox.fillColor = .black
  51. } else {
  52. self?.titleLabelBox.fillColor = .white
  53. }
  54. }
  55. }
  56. }
  57. self.updateUI()
  58. self.updateLanguage()
  59. }
  60. func reloadData() {
  61. guard let data = self.model else { return }
  62. let isItemExpanded = data.isItemExpanded
  63. if isItemExpanded {
  64. iconButton.image = NSImage(named: "KMImageNameBotaNoteExpand")
  65. } else {
  66. iconButton.image = NSImage(named: "KMImageNameBotaNoteNoExpand")
  67. }
  68. }
  69. func updateUI() {
  70. self.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  71. self.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  72. }
  73. func updateLanguage() {
  74. self.reloadData()
  75. }
  76. @IBAction func iconButtonAction(_ sender: Any) {
  77. guard let callBack = iconAction else { return }
  78. callBack(self)
  79. }
  80. }
  81. extension KMBOTAOutlineCellView: NSTextFieldDelegate {
  82. func controlTextDidEndEditing(_ obj: Notification) {
  83. if self.titleLabel.isEqual(to: obj.object) {
  84. self.titleLabelBox.borderWidth = 0
  85. self.titleLabelBox.fillColor = .clear
  86. }
  87. }
  88. }