KMBOTAOutlineCellView.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // KMBOTAOutlineCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/4/2.
  6. //
  7. import Cocoa
  8. typealias KMBOTAOutlineCellViewIconAction = (_ view: KMBOTAOutlineCellView ) -> ()
  9. class KMBOTAOutlineCellView: NSTableCellView {
  10. @IBOutlet var contentView: NSView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var iconButton: NSButton!
  13. @IBOutlet weak var iconImageWidthConstrailnt: NSLayoutConstraint!
  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. // MARK: 初始化
  34. override init(frame frameRect: NSRect) {
  35. super.init(frame: frameRect)
  36. initContentView()
  37. setup()
  38. }
  39. required init?(coder decoder: NSCoder) {
  40. super.init(coder: decoder)
  41. initContentView()
  42. setup()
  43. fatalError("init(coder:) has not been implemented")
  44. }
  45. private func initContentView() {
  46. //绑定xib
  47. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  48. bundle: Bundle(for: self.classForCoder.self))!
  49. resource.instantiate(withOwner: self, topLevelObjects: nil)
  50. addSubview(contentView)
  51. contentView.translatesAutoresizingMaskIntoConstraints = false
  52. NSLayoutConstraint.activate([
  53. contentView.topAnchor.constraint(equalTo: topAnchor),
  54. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  55. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  56. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  57. contentView.updateConstraintsForSubtreeIfNeeded()
  58. }
  59. func setup() {
  60. self.titleLabel.maximumNumberOfLines = 0
  61. contentView.addSubview(hoverBox_)
  62. hoverBox_.km_add_leading_constraint(equalTo: iconButton, attribute: .trailing, constant: 4)
  63. hoverBox_.km_add_top_constraint()
  64. hoverBox_.km_add_bottom_constraint()
  65. hoverBox_.km_add_trailing_constraint(constant: -16)
  66. }
  67. func reloadData() {
  68. guard let data = self.model else { return }
  69. }
  70. func updateUI() {
  71. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  72. self.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  73. }
  74. func updateLanguage() {
  75. self.reloadData()
  76. }
  77. @IBAction func iconButtonAction(_ sender: Any) {
  78. guard let callBack = iconAction else { return }
  79. callBack(self)
  80. }
  81. }