AccountRightCellView.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // AccountRightCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/30.
  6. //
  7. import Cocoa
  8. class AccountRightTitleCellView: NSTableCellView {
  9. private lazy var label_: NSTextField = {
  10. let view = NSTextField(labelWithString: "")
  11. view.font = .systemFont(ofSize: 16)
  12. view.textColor = KMAppearance.titleColor()
  13. return view
  14. }()
  15. private lazy var button_: NSButton = {
  16. let view = NSButton()
  17. view.isBordered = false
  18. view.title = ""
  19. return view
  20. }()
  21. var titleLabel: NSTextField {
  22. get {
  23. return self.label_
  24. }
  25. }
  26. var moreButton: NSButton {
  27. get {
  28. return self.button_
  29. }
  30. }
  31. var itemClick: KMCommonClickBlock?
  32. override func draw(_ dirtyRect: NSRect) {
  33. super.draw(dirtyRect)
  34. // Drawing code here.
  35. }
  36. convenience init() {
  37. self.init(frame: .init(x: 0, y: 0, width: 200, height: 111))
  38. self.initSubviews()
  39. self.initDefaultValue()
  40. }
  41. override func awakeFromNib() {
  42. super.awakeFromNib()
  43. self.initSubviews()
  44. self.initDefaultValue()
  45. }
  46. func initSubviews() {
  47. self.addSubview(label_)
  48. self.addSubview(self.button_)
  49. self.button_.target = self
  50. self.button_.action = #selector(_buttonAction)
  51. self.label_.km_add_leading_constraint(constant: 10)
  52. self.label_.km_add_top_constraint(constant: 16)
  53. self.button_.km_add_trailing_constraint(constant: -20)
  54. self.button_.km_add_top_constraint(constant: 16)
  55. }
  56. func initDefaultValue() {
  57. }
  58. @objc private func _buttonAction() {
  59. self.itemClick?(1)
  60. }
  61. }
  62. class AccountRightCellView: NSTableCellView {
  63. private lazy var contentBox_: NSBox = {
  64. let view = NSBox()
  65. view.boxType = .custom
  66. view.titlePosition = .noTitle
  67. view.contentViewMargins = .zero
  68. view.borderWidth = 0
  69. return view
  70. }()
  71. private lazy var backgroundIv: NSImageView = {
  72. let view = NSImageView()
  73. view.image = NSImage(named: "KMImageNameRightListBg")
  74. view.imageScaling = .scaleNone
  75. return view
  76. }()
  77. private lazy var label_: NSTextField = {
  78. let view = NSTextField(labelWithString: "")
  79. view.font = .systemFont(ofSize: 18)
  80. view.textColor = KMAppearance.titleColor()
  81. return view
  82. }()
  83. private lazy var button_: NSButton = {
  84. let view = NSButton()
  85. view.isBordered = false
  86. view.title = ""
  87. view.wantsLayer = true
  88. view.layer?.cornerRadius = 4
  89. return view
  90. }()
  91. private lazy var hLine: NSView = {
  92. let view = NSView()
  93. view.wantsLayer = true
  94. view.layer?.backgroundColor = NSColor(hex: "#EBEFF4").cgColor
  95. return view
  96. }()
  97. private lazy var devicelabel_: NSTextField = {
  98. let view = NSTextField(labelWithString: "")
  99. return view
  100. }()
  101. private lazy var expireDatelabel_: NSTextField = {
  102. let view = NSTextField(labelWithString: "")
  103. return view
  104. }()
  105. private lazy var statelabel_: NSTextField = {
  106. let view = NSTextField(labelWithString: "")
  107. return view
  108. }()
  109. var model: AccountRightInfoModel? {
  110. didSet {
  111. self.label_.stringValue = self.model?.name ?? ""
  112. let isSubscribing = (self.model?.automatically_subscribe ?? 0) == 1
  113. // self.button_.isHidden = !isSubscribing
  114. // self.statelabel_.isHidden = !isSubscribing
  115. self.button_.isHidden = true
  116. self.statelabel_.isHidden = true
  117. let status = self.model?.status ?? 0
  118. let expireTime = (self.model?.failure_time_text ?? "")
  119. if status == 1 { // 有效
  120. self.button_.layer?.borderWidth = 1
  121. self.button_.layer?.borderColor = NSColor(hex: "#CCCCCC").cgColor
  122. self.button_.layer?.backgroundColor = .white
  123. self.button_.title = NSLocalizedString("Cancel Subscription", comment: "")
  124. self.button_.setTitleColor(NSColor(hex: "#666666"))
  125. self.expireDatelabel_.attributedStringValue = .init(string: NSLocalizedString("Expires:", comment: "") + expireTime)
  126. } else if status == 2 {
  127. self.button_.layer?.borderWidth = 0
  128. self.button_.layer?.backgroundColor = KMAppearance.themeColor().cgColor
  129. self.button_.title = NSLocalizedString("Renew", comment: "")
  130. self.button_.setTitleColor(.white)
  131. var attri = NSMutableAttributedString(string: NSLocalizedString("Expires:", comment: ""))
  132. attri.append(.init(string: expireTime, attributes: [.foregroundColor : NSColor(hex: "#FF0000")]))
  133. self.expireDatelabel_.attributedStringValue = attri
  134. self.statelabel_.stringValue = NSLocalizedString("Expired", comment: "")
  135. self.statelabel_.textColor = NSColor(hex: "#FF0000")
  136. self.button_.isHidden = false
  137. self.statelabel_.isHidden = false
  138. }
  139. let num = self.model?.surplus_num ?? 0
  140. let totalNum = self.model?.total_num ?? 0
  141. if isSubscribing {
  142. var attri = NSMutableAttributedString(string: NSLocalizedString("Device", comment: "")+NSLocalizedString("Available:", comment: ""))
  143. if num <= 0 {
  144. attri.append(.init(string: "\(num)/\(totalNum)", attributes: [.foregroundColor : NSColor(hex: "#FF0000")]))
  145. } else {
  146. attri.append(.init(string: "\(num)/\(totalNum)"))
  147. }
  148. self.devicelabel_.attributedStringValue = attri
  149. self.statelabel_.stringValue = NSLocalizedString("In Subscription", comment: "")
  150. self.statelabel_.textColor = NSColor(hex: "#333333")
  151. self.button_.isHidden = false
  152. self.statelabel_.isHidden = false
  153. } else {
  154. self.devicelabel_.attributedStringValue = .init(string: NSLocalizedString("Device", comment: "") + NSLocalizedString("Available:", comment: "") + "\(num)/\(totalNum)", attributes: [.foregroundColor : NSColor(hex: "#333333").withAlphaComponent(0.3)])
  155. }
  156. }
  157. }
  158. var itemClick: KMCommonClickBlock?
  159. override func draw(_ dirtyRect: NSRect) {
  160. super.draw(dirtyRect)
  161. // Drawing code here.
  162. }
  163. convenience init() {
  164. self.init(frame: .init(x: 0, y: 0, width: 200, height: 111))
  165. self.initSubviews()
  166. self.initDefaultValue()
  167. }
  168. override func awakeFromNib() {
  169. super.awakeFromNib()
  170. self.initSubviews()
  171. self.initDefaultValue()
  172. }
  173. func initSubviews() {
  174. self.addSubview(self.backgroundIv)
  175. self.backgroundIv.km_add_inset_constraint(inset: .init(top: 8, left: 12, bottom: 14, right: 12))
  176. self.addSubview(self.contentBox_)
  177. self.contentBox_.km_add_inset_constraint(inset: .init(top: 8, left: 8, bottom: 14, right: 8))
  178. self.contentBox_.contentView?.addSubview(label_)
  179. self.contentBox_.contentView?.addSubview(self.button_)
  180. self.button_.target = self
  181. self.button_.action = #selector(_buttonAction)
  182. self.label_.km_add_leading_constraint(constant: 20)
  183. self.label_.km_add_top_constraint(constant: 12)
  184. self.button_.km_add_trailing_constraint(constant: -20)
  185. self.button_.km_add_top_constraint(constant: 9)
  186. self.button_.km_add_size_constraint(size: .init(width: 168, height: 32))
  187. self.contentBox_.contentView?.addSubview(self.hLine)
  188. self.hLine.km_add_leading_constraint(constant: 4)
  189. self.hLine.km_add_trailing_constraint(constant: -4)
  190. self.hLine.km_add_height_constraint(constant: 1)
  191. self.hLine.km_add_top_constraint(equalTo: self.label_, attribute: .bottom, constant: 14)
  192. self.contentBox_.contentView?.addSubview(self.devicelabel_)
  193. self.contentBox_.contentView?.addSubview(self.expireDatelabel_)
  194. self.contentBox_.contentView?.addSubview(self.statelabel_)
  195. self.devicelabel_.km_add_leading_constraint(constant: 20)
  196. self.devicelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20)
  197. self.expireDatelabel_.km_add_leading_constraint(equalTo: self.devicelabel_, attribute: .trailing, constant: 30)
  198. self.expireDatelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20)
  199. self.statelabel_.km_add_leading_constraint(equalTo: self.expireDatelabel_, attribute: .trailing, constant: 30)
  200. self.statelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20)
  201. }
  202. func initDefaultValue() {
  203. }
  204. @objc private func _buttonAction() {
  205. self.itemClick?(1)
  206. }
  207. }