// // AccountRightCellView.swift // PDF Reader Pro // // Created by User-Tangchao on 2024/10/30. // import Cocoa class AccountRightTitleCellView: NSTableCellView { private lazy var label_: NSTextField = { let view = NSTextField(labelWithString: "") view.font = .systemFont(ofSize: 16) view.textColor = KMAppearance.titleColor() return view }() private lazy var button_: NSButton = { let view = NSButton() view.isBordered = false view.title = "" return view }() var titleLabel: NSTextField { get { return self.label_ } } var moreButton: NSButton { get { return self.button_ } } var itemClick: KMCommonClickBlock? override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } convenience init() { self.init(frame: .init(x: 0, y: 0, width: 200, height: 111)) self.initSubviews() self.initDefaultValue() } override func awakeFromNib() { super.awakeFromNib() self.initSubviews() self.initDefaultValue() } func initSubviews() { self.addSubview(label_) self.addSubview(self.button_) self.button_.target = self self.button_.action = #selector(_buttonAction) self.label_.km_add_leading_constraint(constant: 10) self.label_.km_add_top_constraint(constant: 16) self.button_.km_add_trailing_constraint(constant: -20) self.button_.km_add_top_constraint(constant: 16) } func initDefaultValue() { } @objc private func _buttonAction() { self.itemClick?(1) } } class AccountRightCellView: NSTableCellView { private lazy var contentBox_: NSBox = { let view = NSBox() view.boxType = .custom view.titlePosition = .noTitle view.contentViewMargins = .zero view.borderWidth = 0 return view }() private lazy var backgroundIv: NSImageView = { let view = NSImageView() view.image = NSImage(named: "KMImageNameRightListBg") view.imageScaling = .scaleNone return view }() private lazy var label_: NSTextField = { let view = NSTextField(labelWithString: "") view.font = .systemFont(ofSize: 18) view.textColor = KMAppearance.titleColor() return view }() private lazy var button_: NSButton = { let view = NSButton() view.isBordered = false view.title = "" view.wantsLayer = true view.layer?.cornerRadius = 4 return view }() private lazy var hLine: NSView = { let view = NSView() view.wantsLayer = true view.layer?.backgroundColor = NSColor(hex: "#EBEFF4").cgColor return view }() private lazy var devicelabel_: NSTextField = { let view = NSTextField(labelWithString: "") return view }() private lazy var expireDatelabel_: NSTextField = { let view = NSTextField(labelWithString: "") return view }() private lazy var statelabel_: NSTextField = { let view = NSTextField(labelWithString: "") return view }() var model: AccountRightInfoModel? { didSet { // self.label_.stringValue = self.model?.name ?? "" let titleString = (model?.name ?? "")+" (%d Devices)" let nameString = NSLocalizedString(titleString, comment: "") self.label_.stringValue = String(format: nameString, model?.total_num ?? 0) let isSubscribing = (self.model?.automatically_subscribe ?? 0) == 1 // self.button_.isHidden = !isSubscribing // self.statelabel_.isHidden = !isSubscribing self.button_.isHidden = true self.statelabel_.isHidden = true let status = self.model?.status ?? 0 let expireTime = (self.model?.failure_time_text ?? "") if status == 1 { // 有效 self.button_.layer?.borderWidth = 1 self.button_.layer?.borderColor = NSColor(hex: "#CCCCCC").cgColor self.button_.layer?.backgroundColor = .white self.button_.title = NSLocalizedString("Cancel Subscription", comment: "") self.button_.setTitleColor(NSColor(hex: "#666666")) if model?.license_id == 3 { // 终身 self.expireDatelabel_.attributedStringValue = .init(string: NSLocalizedString("Expires:", comment: "") + NSLocalizedString("Unlimited", comment: "")) } else { self.expireDatelabel_.attributedStringValue = .init(string: NSLocalizedString("Expires:", comment: "") + expireTime) } } else if status == 2 { self.button_.layer?.borderWidth = 0 self.button_.layer?.backgroundColor = KMAppearance.themeColor().cgColor self.button_.title = NSLocalizedString("Renew", comment: "") self.button_.setTitleColor(.white) var attri = NSMutableAttributedString(string: NSLocalizedString("Expires:", comment: "")) attri.append(.init(string: expireTime, attributes: [.foregroundColor : NSColor(hex: "#FF0000")])) self.expireDatelabel_.attributedStringValue = attri self.statelabel_.stringValue = NSLocalizedString("Expired", comment: "") self.statelabel_.textColor = NSColor(hex: "#FF0000") self.button_.isHidden = false self.statelabel_.isHidden = false } let num = self.model?.surplus_num ?? 0 let totalNum = self.model?.total_num ?? 0 if isSubscribing { var attri = NSMutableAttributedString(string: NSLocalizedString("Device", comment: "")+NSLocalizedString("Available:", comment: "")) if num <= 0 { attri.append(.init(string: "\(num)/\(totalNum)", attributes: [.foregroundColor : NSColor(hex: "#FF0000")])) } else { attri.append(.init(string: "\(num)/\(totalNum)")) } self.devicelabel_.attributedStringValue = attri self.statelabel_.stringValue = NSLocalizedString("In Subscription", comment: "") self.statelabel_.textColor = NSColor(hex: "#333333") self.button_.isHidden = false self.statelabel_.isHidden = false } else { self.devicelabel_.attributedStringValue = .init(string: NSLocalizedString("Device", comment: "") + NSLocalizedString("Available:", comment: "") + "\(num)/\(totalNum)", attributes: [.foregroundColor : NSColor(hex: "#333333").withAlphaComponent(0.3)]) } } } var itemClick: KMCommonClickBlock? override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } convenience init() { self.init(frame: .init(x: 0, y: 0, width: 200, height: 111)) self.initSubviews() self.initDefaultValue() } override func awakeFromNib() { super.awakeFromNib() self.initSubviews() self.initDefaultValue() } func initSubviews() { self.addSubview(self.backgroundIv) self.backgroundIv.km_add_inset_constraint(inset: .init(top: 8, left: 12, bottom: 14, right: 12)) self.addSubview(self.contentBox_) self.contentBox_.km_add_inset_constraint(inset: .init(top: 8, left: 8, bottom: 14, right: 8)) self.contentBox_.contentView?.addSubview(label_) self.contentBox_.contentView?.addSubview(self.button_) self.button_.target = self self.button_.action = #selector(_buttonAction) self.label_.km_add_leading_constraint(constant: 20) self.label_.km_add_top_constraint(constant: 12) self.button_.km_add_trailing_constraint(constant: -20) self.button_.km_add_top_constraint(constant: 9) self.button_.km_add_size_constraint(size: .init(width: 168, height: 32)) self.contentBox_.contentView?.addSubview(self.hLine) self.hLine.km_add_leading_constraint(constant: 4) self.hLine.km_add_trailing_constraint(constant: -4) self.hLine.km_add_height_constraint(constant: 1) self.hLine.km_add_top_constraint(equalTo: self.label_, attribute: .bottom, constant: 14) self.contentBox_.contentView?.addSubview(self.devicelabel_) self.contentBox_.contentView?.addSubview(self.expireDatelabel_) self.contentBox_.contentView?.addSubview(self.statelabel_) self.devicelabel_.km_add_leading_constraint(constant: 20) self.devicelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20) self.expireDatelabel_.km_add_leading_constraint(equalTo: self.devicelabel_, attribute: .trailing, constant: 30) self.expireDatelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20) self.statelabel_.km_add_leading_constraint(equalTo: self.expireDatelabel_, attribute: .trailing, constant: 30) self.statelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20) } func initDefaultValue() { } @objc private func _buttonAction() { self.itemClick?(1) } }