KMNBotaAnnotationHeaderView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // KMNBotaAnnotationHeaderView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/6.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNSectionHeaderView: NSView {
  10. private lazy var contentBox_: NSBox = {
  11. let view = NSBox()
  12. view.boxType = .custom
  13. view.titlePosition = .noTitle
  14. view.contentViewMargins = .zero
  15. view.borderWidth = 0
  16. return view
  17. }()
  18. private lazy var expandButton_: NSButton = {
  19. let view = NSButton()
  20. view.isBordered = false
  21. view.imagePosition = .imageOnly
  22. return view
  23. }()
  24. private lazy var titleLabel_: NSTextField = {
  25. let view = NSTextField(labelWithString: "")
  26. return view
  27. }()
  28. private lazy var numLabel_: NSTextField = {
  29. let view = NSTextField(labelWithString: "")
  30. return view
  31. }()
  32. private var leftConst_: NSLayoutConstraint?
  33. private var topConst_: NSLayoutConstraint?
  34. private var rightConst_: NSLayoutConstraint?
  35. private var bottomConst_: NSLayoutConstraint?
  36. var expandButton: NSButton {
  37. get {
  38. return expandButton_
  39. }
  40. }
  41. var titleLabel: NSTextField {
  42. get {
  43. return titleLabel_
  44. }
  45. }
  46. var numLabel: NSTextField {
  47. get {
  48. return numLabel_
  49. }
  50. }
  51. var contentInset: NSEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0) {
  52. didSet {
  53. leftConst_?.constant = contentInset.left
  54. topConst_?.constant = contentInset.top
  55. rightConst_?.constant = -contentInset.right
  56. bottomConst_?.constant = -contentInset.bottom
  57. }
  58. }
  59. var itemClick: KMCommonClickBlock?
  60. convenience init() {
  61. self.init(frame: .init(x: 0, y: 0, width: 10, height: 10))
  62. }
  63. override init(frame frameRect: NSRect) {
  64. super.init(frame: frameRect)
  65. initSubviews()
  66. }
  67. required init?(coder: NSCoder) {
  68. super.init(coder: coder)
  69. initSubviews()
  70. }
  71. override func awakeFromNib() {
  72. super.awakeFromNib()
  73. initSubviews()
  74. }
  75. func initSubviews() {
  76. addSubview(contentBox_)
  77. leftConst_ = contentBox_.km_add_leading_constraint_r()
  78. rightConst_ = contentBox_.km_add_trailing_constraint_r()
  79. topConst_ = contentBox_.km_add_top_constraint_r()
  80. bottomConst_ = contentBox_.km_add_bottom_constraint_r()
  81. let wh: CGFloat = 16
  82. let vspace: CGFloat = 8
  83. contentBox_.contentView?.addSubview(expandButton)
  84. expandButton.km_add_leading_constraint()
  85. expandButton.km_add_size_constraint(size: .init(width: wh, height: wh))
  86. expandButton.km_add_centerY_constraint()
  87. contentBox_.contentView?.addSubview(titleLabel)
  88. titleLabel.km_add_centerY_constraint()
  89. titleLabel.km_add_leading_constraint(equalTo: expandButton, attribute: .trailing, constant: 2 * vspace)
  90. contentBox_.contentView?.addSubview(numLabel_)
  91. numLabel_.km_add_centerY_constraint()
  92. numLabel_.km_add_trailing_constraint(constant: -vspace)
  93. }
  94. }
  95. class KMNBotaAnnotationHeaderView: NSView {
  96. private lazy var titleLabel_: NSTextField = {
  97. let view = NSTextField(labelWithString: "")
  98. view.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  99. view.textColor = KMNColorTools.colorText_2()
  100. return view
  101. }()
  102. private lazy var searchButton_: ComponentButton = {
  103. let view = ComponentButton()
  104. let prop = ComponentButtonProperty()
  105. prop.type = .text_gray
  106. prop.size = .xxs
  107. prop.onlyIcon = true
  108. prop.icon = NSImage(named: "KMImageNameOutlineSearch")
  109. view.properties = prop
  110. return view
  111. }()
  112. private lazy var sortButton_: ComponentButton = {
  113. let view = ComponentButton()
  114. let prop = ComponentButtonProperty()
  115. prop.type = .text_gray
  116. prop.size = .xxs
  117. prop.onlyIcon = true
  118. prop.icon = NSImage(named: "KMImageNameBotaSearch")
  119. view.properties = prop
  120. return view
  121. }()
  122. private lazy var moreButton_: ComponentButton = {
  123. let view = ComponentButton()
  124. let prop = ComponentButtonProperty()
  125. prop.type = .text_gray
  126. prop.size = .xxs
  127. prop.onlyIcon = true
  128. prop.icon = NSImage(named: "KMImageNameOutlineMore")
  129. view.properties = prop
  130. return view
  131. }()
  132. private lazy var bottomLine_: NSView = {
  133. let view = NSView()
  134. view.wantsLayer = true
  135. return view
  136. }()
  137. var titleLabel: NSTextField {
  138. get {
  139. return titleLabel_
  140. }
  141. }
  142. var searchButton: ComponentButton {
  143. get {
  144. return searchButton_
  145. }
  146. }
  147. var sortButton: ComponentButton {
  148. get {
  149. return sortButton_
  150. }
  151. }
  152. var moreButton: ComponentButton {
  153. get {
  154. return moreButton_
  155. }
  156. }
  157. var bottomLine: NSView {
  158. get {
  159. return bottomLine_
  160. }
  161. }
  162. var itemClick: KMCommonClickBlock?
  163. convenience init() {
  164. self.init(frame: .init(x: 0, y: 0, width: 300, height: 40))
  165. initSubviews()
  166. }
  167. override func awakeFromNib() {
  168. super.awakeFromNib()
  169. initSubviews()
  170. }
  171. func initSubviews() {
  172. let margin: CGFloat = 12
  173. addSubview(titleLabel_)
  174. titleLabel_.km_add_leading_constraint(constant: margin)
  175. titleLabel_.km_add_centerY_constraint()
  176. let vspace: CGFloat = 8
  177. let wh: CGFloat = 24
  178. addSubview(moreButton_)
  179. moreButton_.km_add_size_constraint(size: .init(width: wh, height: wh))
  180. moreButton_.km_add_trailing_constraint(constant: -margin)
  181. moreButton_.km_add_centerY_constraint()
  182. moreButton_.setTarget(self, action: #selector(moreAction))
  183. addSubview(sortButton_)
  184. sortButton_.km_add_size_constraint(size: .init(width: wh, height: wh))
  185. sortButton_.km_add_trailing_constraint(equalTo: moreButton_, attribute: .leading, constant: -vspace)
  186. sortButton_.km_add_centerY_constraint()
  187. sortButton_.setTarget(self, action: #selector(sortAction))
  188. addSubview(searchButton_)
  189. searchButton_.km_add_size_constraint(size: .init(width: wh, height: wh))
  190. searchButton_.km_add_trailing_constraint(equalTo: sortButton_, attribute: .leading, constant: -vspace)
  191. searchButton_.km_add_centerY_constraint()
  192. searchButton_.setTarget(self, action: #selector(searchAction))
  193. addSubview(bottomLine_)
  194. bottomLine_.km_add_leading_constraint()
  195. bottomLine_.km_add_trailing_constraint()
  196. bottomLine_.km_add_bottom_constraint()
  197. bottomLine_.km_add_height_constraint(constant: 1)
  198. }
  199. @objc func searchAction() {
  200. itemClick?(1)
  201. }
  202. @objc func sortAction() {
  203. itemClick?(2)
  204. }
  205. @objc func moreAction() {
  206. itemClick?(3)
  207. }
  208. }