KMNTableHeaderCellView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // KMNTableHeaderCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/11/7.
  6. //
  7. import Cocoa
  8. class KMNTableHeaderCellView: NSTableCellView {
  9. private var contentBox: NSBox = {
  10. let box = NSBox()
  11. box.boxType = .custom
  12. box.titlePosition = .noTitle
  13. box.contentViewMargins = .zero
  14. box.borderWidth = 0
  15. return box
  16. }()
  17. private var titleLabel_: NSTextField = {
  18. let view = NSTextField(labelWithString: "")
  19. return view
  20. }()
  21. private var leftVLine_: NSView = {
  22. let view = NSView()
  23. return view
  24. }()
  25. private var rightVLine_: NSView = {
  26. let view = NSView()
  27. return view
  28. }()
  29. private var bottomLine_: NSView = {
  30. let view = NSView()
  31. return view
  32. }()
  33. private var button_: NSButton = {
  34. let view = NSButton()
  35. view.isBordered = false
  36. view.title = ""
  37. return view
  38. }()
  39. var titleLabel: NSTextField {
  40. get {
  41. return self.titleLabel_
  42. }
  43. }
  44. var leftLine: NSView {
  45. get {
  46. return self.leftVLine_
  47. }
  48. }
  49. var rightLine: NSView {
  50. get {
  51. return self.rightVLine_
  52. }
  53. }
  54. var bottomLine: NSView {
  55. get {
  56. return self.bottomLine_
  57. }
  58. }
  59. var itemClick: KMCommonClickBlock?
  60. override func draw(_ dirtyRect: NSRect) {
  61. super.draw(dirtyRect)
  62. // Drawing code here.
  63. }
  64. convenience init() {
  65. self.init(frame: .init(x: 0, y: 0, width: 40, height: 40))
  66. initSubviews()
  67. }
  68. override func awakeFromNib() {
  69. super.awakeFromNib()
  70. initSubviews()
  71. }
  72. func initSubviews() {
  73. addSubview(contentBox)
  74. contentBox.km_add_inset_constraint()
  75. contentBox.contentView?.addSubview(leftVLine_)
  76. contentBox.contentView?.addSubview(titleLabel_)
  77. contentBox.contentView?.addSubview(rightVLine_)
  78. contentBox.contentView?.addSubview(bottomLine_)
  79. leftVLine_.km_add_leading_constraint()
  80. leftVLine_.km_add_top_constraint(constant: 4)
  81. leftVLine_.km_add_bottom_constraint(constant: -4)
  82. leftVLine_.km_add_width_constraint(constant: 0.5)
  83. titleLabel_.km_add_leading_constraint(constant: 8)
  84. titleLabel_.km_add_centerY_constraint()
  85. rightVLine_.km_add_trailing_constraint()
  86. rightVLine_.km_add_top_constraint(constant: 4)
  87. rightVLine_.km_add_bottom_constraint(constant: -4)
  88. rightVLine_.km_add_width_constraint(constant: 0.5)
  89. bottomLine_.km_add_bottom_constraint(constant: 0)
  90. bottomLine_.km_add_leading_constraint(constant: 0)
  91. bottomLine_.km_add_trailing_constraint(constant: 0)
  92. bottomLine_.km_add_height_constraint(constant: 1)
  93. contentBox.fillColor = .clear
  94. contentBox.addSubview(button_)
  95. button_.bounds = contentBox.contentView?.bounds ?? .zero
  96. button_.autoresizingMask = [.width, .height]
  97. button_.target = self
  98. button_.action = #selector(_buttonAction)
  99. }
  100. // MARK: - Private Methods
  101. @objc private func _buttonAction() {
  102. self.itemClick?(1)
  103. }
  104. }