KMCheckInViewItem.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // KMCheckInViewItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/24.
  6. //
  7. import Cocoa
  8. @objcMembers class KMGradientView: NSView {
  9. var colors: [NSColor] = [] {
  10. didSet {
  11. needsDisplay = true
  12. }
  13. }
  14. var xRadius: CGFloat = 0 {
  15. didSet {
  16. needsDisplay = true
  17. }
  18. }
  19. var yRadius: CGFloat = 0 {
  20. didSet {
  21. needsDisplay = true
  22. }
  23. }
  24. var angle: CGFloat = 0 {
  25. didSet {
  26. needsDisplay = true
  27. }
  28. }
  29. var contentInset: NSEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0) {
  30. didSet {
  31. needsDisplay = true
  32. }
  33. }
  34. override func draw(_ dirtyRect: NSRect) {
  35. super.draw(dirtyRect)
  36. wantsLayer = true
  37. let gradient = NSGradient(colors: colors)
  38. let x = contentInset.left
  39. let y = contentInset.top
  40. let w = max(NSWidth(self.bounds)-x-contentInset.right, 0)
  41. let h = max(NSHeight(self.bounds)-y-contentInset.bottom, 0)
  42. let frame = NSMakeRect(x, y, w, h)
  43. let path = NSBezierPath(roundedRect: frame, xRadius: xRadius, yRadius: yRadius)
  44. gradient?.draw(in: path, angle: angle)
  45. }
  46. }
  47. class KMCheckInGiftItemView: KMCheckInItemView {
  48. private lazy var numLabel_: NSTextField = {
  49. let view = NSTextField(labelWithString: "")
  50. return view
  51. }()
  52. private lazy var creditsLabel_: NSTextField = {
  53. let view = NSTextField(labelWithString: "")
  54. return view
  55. }()
  56. private lazy var giftIv_: NSImageView = {
  57. let view = NSImageView()
  58. return view
  59. }()
  60. private lazy var gradientV_: KMGradientView = {
  61. let view = KMGradientView()
  62. return view
  63. }()
  64. var numLabel: NSTextField {
  65. get {
  66. return numLabel_
  67. }
  68. }
  69. var creditsLabel: NSTextField {
  70. get {
  71. return creditsLabel_
  72. }
  73. }
  74. var giftIv: NSImageView {
  75. get {
  76. return giftIv_
  77. }
  78. }
  79. var gradientView: KMGradientView {
  80. get {
  81. return gradientV_
  82. }
  83. }
  84. override func initSubviews() {
  85. super.initSubviews()
  86. checkIv.isHidden = true
  87. backgroundView.addSubview(gradientV_)
  88. gradientV_.frame = backgroundView.bounds
  89. gradientV_.autoresizingMask = [.width, .height]
  90. contentBox.contentView?.addSubview(numLabel_)
  91. contentBox.contentView?.addSubview(creditsLabel_)
  92. contentBox.contentView?.addSubview(giftIv)
  93. numLabel_.mas_makeConstraints { make in
  94. make?.top.mas_equalTo()(15)
  95. make?.centerX.mas_equalTo()(0)
  96. }
  97. creditsLabel_.mas_makeConstraints { make in
  98. make?.top.equalTo()(numLabel_.mas_bottom)
  99. make?.centerX.mas_equalTo()(0)
  100. }
  101. giftIv.mas_makeConstraints { make in
  102. make?.centerX.mas_equalTo()(0)
  103. make?.top.mas_equalTo()(53)
  104. make?.size.mas_equalTo()(NSMakeSize(24, 24))
  105. }
  106. }
  107. }
  108. class KMCheckInItemView: NSView {
  109. private lazy var contentBox_: NSBox = {
  110. let view = NSBox()
  111. view.boxType = .custom
  112. view.borderWidth = 0
  113. view.contentViewMargins = .init(width: 0, height: 0)
  114. view.titlePosition = .noTitle
  115. return view
  116. }()
  117. private lazy var backgroundView_: NSView = {
  118. let view = NSView()
  119. return view
  120. }()
  121. private lazy var iv_: NSImageView = {
  122. let view = NSImageView()
  123. return view
  124. }()
  125. private lazy var nameLabel_: NSTextField = {
  126. let view = NSTextField(labelWithString: "")
  127. return view
  128. }()
  129. var contentBox: NSBox {
  130. get {
  131. return contentBox_
  132. }
  133. }
  134. var backgroundView: NSView {
  135. get {
  136. return backgroundView_
  137. }
  138. }
  139. var checkIv: NSImageView {
  140. get {
  141. return iv_
  142. }
  143. }
  144. var nameLabel: NSTextField {
  145. get {
  146. return nameLabel_
  147. }
  148. }
  149. convenience init() {
  150. self.init(frame: .init(x: 0, y: 0, width: 200, height: 30))
  151. initSubviews()
  152. }
  153. override func awakeFromNib() {
  154. super.awakeFromNib()
  155. initSubviews()
  156. }
  157. func initSubviews() {
  158. addSubview(contentBox_)
  159. contentBox_.contentView?.addSubview(backgroundView_)
  160. contentBox_.contentView?.addSubview(iv_)
  161. contentBox_.contentView?.addSubview(nameLabel_)
  162. contentBox_.mas_makeConstraints { make in
  163. make?.edges.mas_equalTo()(0)
  164. }
  165. backgroundView_.mas_makeConstraints { make in
  166. make?.leading.mas_equalTo()(6)
  167. make?.trailing.mas_equalTo()(-6)
  168. make?.top.mas_equalTo()(5)
  169. make?.height.mas_equalTo()(83)
  170. }
  171. iv_.mas_makeConstraints { make in
  172. make?.center.equalTo()(backgroundView_)
  173. }
  174. nameLabel_.mas_makeConstraints { make in
  175. make?.centerX.mas_equalTo()(0)
  176. make?.bottom.mas_equalTo()(-5)
  177. }
  178. }
  179. }
  180. class KMCheckInViewItem: NSCollectionViewItem {
  181. private lazy var contentBox_: NSBox = {
  182. let view = NSBox()
  183. view.boxType = .custom
  184. view.borderWidth = 0
  185. view.contentViewMargins = .init(width: 0, height: 0)
  186. view.titlePosition = .noTitle
  187. return view
  188. }()
  189. var contentBox: NSBox {
  190. get {
  191. return contentBox_
  192. }
  193. }
  194. override func viewDidLoad() {
  195. super.viewDidLoad()
  196. view.addSubview(contentBox_)
  197. contentBox_.frame = view.bounds
  198. contentBox_.autoresizingMask = [.width, .height]
  199. }
  200. }