// // AccountBenefitsController.swift // PDF Reader Pro // // Created by User-Tangchao on 2024/10/29. // import Cocoa class AccountBenefitsController: NSViewController { @IBOutlet weak var leftBox: NSBox! @IBOutlet weak var rightBox: NSBox! @IBOutlet weak var leftContentBox: NSBox! @IBOutlet weak var leftBackgroundIv: NSImageView! @IBOutlet weak var leftTitleLabel: NSTextField! @IBOutlet weak var leftRightListBox: NSBox! @IBOutlet weak var tabBox: NSBox! @IBOutlet weak var tabSegmentLabel: NSTextField! @IBOutlet weak var tabSegmentLabel2: NSTextField! @IBOutlet weak var tabSegmentLabel3: NSTextField! @IBOutlet weak var tabBottomLine: NSView! @IBOutlet weak var tabSliderView: NSView! @IBOutlet weak var scrollView: NSScrollView! @IBOutlet weak var tableView: NSTableView! @IBOutlet weak var tabSegmentButton: NSButton! @IBOutlet weak var tabSegmentButton2: NSButton! @IBOutlet weak var tabSegmentButton3: NSButton! @IBOutlet weak var tabSliderLeadConst: NSLayoutConstraint! var rightDatas: [String] = [] { didSet { self.rightListView?.rightDatas = self.rightDatas } } var rightListView: AccountRightListView? var datas: [ProductListModel] = [] private var tabIndex_: Int = 0 private var currentModel_: ProductListModel? convenience init() { self.init(nibName: "AccountBenefitsController", bundle: MainBundle) } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. self.leftBox.borderWidth = 0 self.leftContentBox.borderWidth = 0 self.leftTitleLabel.stringValue = NSLocalizedString("Purchase and unlock all features", comment: "") self.leftTitleLabel.font = .systemFont(ofSize: 18) self.leftTitleLabel.textColor = KMAppearance.themeColor() self.leftBackgroundIv.image = NSImage(named: "KMImageNameAccountBenefitBg") self.leftRightListBox.borderWidth = 0 self.leftRightListBox.fillColor = .clear let rightListView = AccountRightListView.createFromNib() self.leftRightListBox.contentView = rightListView self.rightListView = rightListView rightListView?.rightDatas = self.rightDatas self.rightBox.borderWidth = 0 self.tabBox.borderWidth = 0 // self.tabSegmentLabel.stringValue = NSLocalizedString("Win+Mac", comment: "") self.tabSegmentLabel.font = .systemFont(ofSize: 16) // self.tabSegmentLabel2.stringValue = NSLocalizedString("Win", comment: "") self.tabSegmentLabel2.font = .systemFont(ofSize: 16) // self.tabSegmentLabel3.stringValue = NSLocalizedString("Mac", comment: "") self.tabSegmentLabel3.font = .systemFont(ofSize: 16) if let model = self.datas.safe_element(for: 0) as? ProductListModel { let msg = model.name ?? "" self.tabSegmentLabel.stringValue = NSLocalizedString(msg, comment: "") } if let model = self.datas.safe_element(for: 1) as? ProductListModel { let msg = model.name ?? "" self.tabSegmentLabel2.stringValue = NSLocalizedString(msg, comment: "") } if let model = self.datas.safe_element(for: 2) as? ProductListModel { let msg = model.name ?? "" self.tabSegmentLabel3.stringValue = NSLocalizedString(msg, comment: "") } self.tabSegmentButton.target = self self.tabSegmentButton.action = #selector(_tabSegmentAction) // self.tabSegmentButton.wantsLayer = true // self.tabSegmentButton.layer?.backgroundColor = NSColor.red.cgColor self.tabSegmentButton2.target = self self.tabSegmentButton2.action = #selector(_tabSegmentAction) self.tabSegmentButton3.target = self self.tabSegmentButton3.action = #selector(_tabSegmentAction) self.tabBottomLine.wantsLayer = true self.tabBottomLine.layer?.backgroundColor = NSColor(hex: "#F4F4F4").cgColor self.tabSliderView.wantsLayer = true self.tabSliderView.layer?.backgroundColor = KMAppearance.themeColor().cgColor self.tableView.delegate = self self.tableView.dataSource = self self.tableView.selectionHighlightStyle = .none self._selectTab(index: 0) } private func _selectTab(index: Int) { self.tabIndex_ = index if index == 0 { self.tabSegmentLabel.textColor = KMAppearance.themeColor() self.tabSegmentLabel2.textColor = KMAppearance.titleColor() self.tabSegmentLabel3.textColor = KMAppearance.titleColor() let x = self.tabSegmentLabel.frame.origin.x self.tabSliderLeadConst.constant = x + 19 } else if index == 1 { self.tabSegmentLabel.textColor = KMAppearance.titleColor() self.tabSegmentLabel2.textColor = KMAppearance.themeColor() self.tabSegmentLabel3.textColor = KMAppearance.titleColor() let x = self.tabSegmentLabel2.frame.origin.x self.tabSliderLeadConst.constant = x + 4 } else if index == 2 { self.tabSegmentLabel.textColor = KMAppearance.titleColor() self.tabSegmentLabel2.textColor = KMAppearance.titleColor() self.tabSegmentLabel3.textColor = KMAppearance.themeColor() let x = self.tabSegmentLabel3.frame.origin.x self.tabSliderLeadConst.constant = x + 4 } guard let model = self.datas.safe_element(for: index) as? ProductListModel else { return } self.currentModel_ = model DispatchQueue.main.async { self.tableView.reloadData() } } @objc private func _tabSegmentAction(_ sender: NSButton) { if self.tabSegmentButton.isEqual(to: sender) { self._selectTab(index: 0) } else if self.tabSegmentButton2.isEqual(to: sender) { self._selectTab(index: 1) } else if self.tabSegmentButton3.isEqual(to: sender) { self._selectTab(index: 2) } } } extension AccountBenefitsController: NSTableViewDelegate, NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { // return 3 return self.currentModel_?.list?.count ?? 0 } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { var view = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cell"), owner: self) as? AccountBenefitCellView if view == nil { view = AccountBenefitCellView() } let model = self.currentModel_?.list?[row] // "1-Month Plan (%d Devices)" let titleString = (model?.name ?? "")+" (%d Devices)" let nameString = NSLocalizedString(titleString, comment: "") view?.titleLabel.stringValue = String(format: nameString, model?.devices_num ?? 0) view?.subTitleLabel.stringValue = "$89.99  $139.99" let attri = NSMutableAttributedString(string: "$"+String(format: "%.2f", model?.actual_price ?? 0)+" ", attributes: [.font : NSFont.systemFont(ofSize: 24), .foregroundColor : KMAppearance.themeColor()]) attri.append(.init(string: "$"+String(format: "%.2f", model?.virtual_price ?? 0), attributes: [.font : NSFont.systemFont(ofSize: 16), .strikethroughStyle : 1])) view?.subTitleLabel.attributedStringValue = attri view?.hotIv.isHidden = (model?.is_hot ?? 0) == 1 view?.itemClick = { idx, _ in if idx == 1 { // 购买 KMTools.openURL(urlString: model?.buy_url) } } return view } func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { return 126; } }