123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // KMMergeTableViewCell.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/8.
- //
- import Cocoa
- typealias KMMergeTableViewCellRemoveAction = (_ view: KMMergeTableViewCell, _ fileModel: KMFileAttribute) -> Void
- class KMMergeTableViewCell: NSTableCellView {
- @IBOutlet var contentView: NSView!
-
- @IBOutlet weak var pageCountLabel: NSTextField!
- @IBOutlet weak var indexLabel: NSTextField!
- @IBOutlet weak var fileNameLabel: NSTextField!
- @IBOutlet weak var pageRangeLabel: NSTextField!
- @IBOutlet weak var sizeLabel: NSTextField!
- @IBOutlet weak var fileImageView: NSImageView!
- @IBOutlet weak var pageRangeBox: NSComboBox!
- @IBOutlet weak var deleteButton: NSButton!
- @IBOutlet weak var contentBox: KMBox!
-
- var removeAction: KMMergeTableViewCellRemoveAction?
-
- var index: Int = 1 {
- didSet {
- self.reloadData()
- }
- }
- var fileModel: KMFileAttribute? {
- didSet {
- self.reloadData()
- }
- }
-
- // MARK: 初始化
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- initContentView()
- setup()
- }
-
- required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
- initContentView()
- setup()
- fatalError("init(coder:) has not been implemented")
- }
-
- private func initContentView() {
- //绑定xib
- let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
- bundle: Bundle(for: self.classForCoder.self))!
- resource.instantiate(withOwner: self, topLevelObjects: nil)
- addSubview(contentView)
- contentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- contentView.topAnchor.constraint(equalTo: topAnchor),
- contentView.leftAnchor.constraint(equalTo: leftAnchor),
- contentView.rightAnchor.constraint(equalTo: rightAnchor),
- contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
- contentView.updateConstraintsForSubtreeIfNeeded()
- }
-
- func setup() {
-
- pageRangeBox.removeAllItems()
- pageRangeBox.delegate = self
- pageRangeBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""),
- NSLocalizedString("Odd Pages Only", comment: ""),
- NSLocalizedString("Even Pages Only", comment: ""),
- NSLocalizedString("e.g. 1,3-5,10", comment: "")])
- pageRangeBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
-
- self.updateLanguage()
-
- contentBox.moveCallback = { mouseEntered, mouseBox in
- if mouseEntered {
- self.deleteButton.isHidden = false
- } else {
- self.deleteButton.isHidden = true
- }
- }
- }
-
- func reloadData() {
- guard let fileModel = fileModel else { return }
- indexLabel.stringValue = index.description
- fileImageView.image = fileModel.fileImage
- if let oriFilePath = fileModel.oriFilePath {
- fileNameLabel.stringValue = oriFilePath.lastPathComponent
- } else {
- fileNameLabel.stringValue = fileModel.filePath.lastPathComponent
- }
-
- pageCountLabel.stringValue = "\(fileModel.pdfDocument?.pageCount ?? 0) \(NSLocalizedString("Pages", comment: ""))"
- sizeLabel.stringValue = self.fileSizeString(Float(fileModel.fileSize))
-
- self.pageRangeBox.isEditable = false
- if fileModel.pagesType == .all {
- pageRangeBox.selectItem(at: 0)
- } else if fileModel.pagesType == .odd {
- pageRangeBox.selectItem(at: 1)
- } else if fileModel.pagesType == .even {
- pageRangeBox.selectItem(at: 2)
- } else {
- pageRangeBox.cell?.stringValue = fileModel.pagesString
- self.pageRangeBox.isEditable = true
- window?.makeFirstResponder(pageRangeBox)
- }
- }
-
- func updateLanguage() {
- pageRangeLabel.stringValue = NSLocalizedString("Page Range", comment: "")
- }
-
- @IBAction func removeButtonAction(_ sender: Any) {
- guard let callBack = removeAction else { return }
- guard let fileModel = fileModel else { return }
-
- callBack(self, fileModel)
- }
-
- @IBAction func comboBoxAction(_ sender: NSComboBoxCell) {
- guard let fileModel = fileModel else { return }
- if sender.indexOfSelectedItem >= 0 {
- let type = KMPageRange.init(rawValue: sender.indexOfSelectedItem) ?? .all
- fileModel.pagesType = type
- }
-
- self.pageRangeBox.isEditable = false
- if fileModel.pagesType == .custom {
- fileModel.pagesString = sender.stringValue
-
- self.pageRangeBox.isEditable = true
- window?.makeFirstResponder(pageRangeBox)
- }
- }
- }
- extension KMMergeTableViewCell: NSComboBoxDelegate {
-
- }
- extension KMMergeTableViewCell {
- func fileSizeString(_ fSize: Float) -> String {
- let fileSize = fSize / 1024
- let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
- let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
- return String(format: "%0.1f %@", size, unit)
- }
- }
|