KMBOTAOutlineCellView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. self.titleLabel.stringValue = data.outline.label ?? ""
  70. if data.isItemExpanded {
  71. self.iconButton.image = NSImage(named: "KMImageNameArrowDown")
  72. } else {
  73. self.iconButton.image = NSImage(named: "KMImageNameArrowRight")
  74. }
  75. if data.outline.numberOfChildren > 0 {
  76. self.iconButton.isHidden = false
  77. // self.iconImageWidthConstrailnt.constant = 22
  78. } else {
  79. self.iconButton.isHidden = true
  80. // self.iconImageWidthConstrailnt.constant = 0
  81. }
  82. }
  83. func updateUI() {
  84. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  85. self.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  86. }
  87. func updateLanguage() {
  88. self.reloadData()
  89. }
  90. @IBAction func iconButtonAction(_ sender: Any) {
  91. guard let callBack = iconAction else { return }
  92. callBack(self)
  93. }
  94. }