KMMergeTableViewCell.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // KMMergeTableViewCell.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMMergeTableViewCellRemoveAction = (_ view: KMMergeTableViewCell, _ fileModel: KMFileAttribute) -> Void
  9. class KMMergeTableViewCell: NSTableCellView {
  10. @IBOutlet var contentView: NSView!
  11. @IBOutlet weak var pageCountLabel: NSTextField!
  12. @IBOutlet weak var indexLabel: NSTextField!
  13. @IBOutlet weak var fileNameLabel: NSTextField!
  14. @IBOutlet weak var pageRangeLabel: NSTextField!
  15. @IBOutlet weak var sizeLabel: NSTextField!
  16. @IBOutlet weak var fileImageView: NSImageView!
  17. @IBOutlet weak var pageRangeBox: NSComboBox!
  18. @IBOutlet weak var deleteButton: NSButton!
  19. @IBOutlet weak var contentBox: KMBox!
  20. var removeAction: KMMergeTableViewCellRemoveAction?
  21. var index: Int = 1 {
  22. didSet {
  23. self.reloadData()
  24. }
  25. }
  26. var fileModel: KMFileAttribute? {
  27. didSet {
  28. self.reloadData()
  29. }
  30. }
  31. // MARK: 初始化
  32. override init(frame frameRect: NSRect) {
  33. super.init(frame: frameRect)
  34. initContentView()
  35. setup()
  36. }
  37. required init?(coder decoder: NSCoder) {
  38. super.init(coder: decoder)
  39. initContentView()
  40. setup()
  41. fatalError("init(coder:) has not been implemented")
  42. }
  43. private func initContentView() {
  44. //绑定xib
  45. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  46. bundle: Bundle(for: self.classForCoder.self))!
  47. resource.instantiate(withOwner: self, topLevelObjects: nil)
  48. addSubview(contentView)
  49. contentView.translatesAutoresizingMaskIntoConstraints = false
  50. NSLayoutConstraint.activate([
  51. contentView.topAnchor.constraint(equalTo: topAnchor),
  52. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  53. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  54. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  55. contentView.updateConstraintsForSubtreeIfNeeded()
  56. }
  57. func setup() {
  58. pageRangeBox.removeAllItems()
  59. pageRangeBox.delegate = self
  60. pageRangeBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""),
  61. NSLocalizedString("Odd Pages Only", comment: ""),
  62. NSLocalizedString("Even Pages Only", comment: ""),
  63. NSLocalizedString("e.g. 1,3-5,10", comment: "")])
  64. pageRangeBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  65. self.updateLanguage()
  66. contentBox.moveCallback = { mouseEntered, mouseBox in
  67. if mouseEntered {
  68. self.deleteButton.isHidden = false
  69. } else {
  70. self.deleteButton.isHidden = true
  71. }
  72. }
  73. }
  74. func reloadData() {
  75. guard let fileModel = fileModel else { return }
  76. indexLabel.stringValue = index.description
  77. fileImageView.image = fileModel.fileImage
  78. if let oriFilePath = fileModel.oriFilePath {
  79. fileNameLabel.stringValue = oriFilePath.lastPathComponent
  80. } else {
  81. fileNameLabel.stringValue = fileModel.filePath.lastPathComponent
  82. }
  83. pageCountLabel.stringValue = "\(fileModel.pdfDocument?.pageCount ?? 0) \(NSLocalizedString("Pages", comment: ""))"
  84. sizeLabel.stringValue = self.fileSizeString(Float(fileModel.fileSize))
  85. self.pageRangeBox.isEditable = false
  86. if fileModel.pagesType == .all {
  87. pageRangeBox.selectItem(at: 0)
  88. } else if fileModel.pagesType == .odd {
  89. pageRangeBox.selectItem(at: 1)
  90. } else if fileModel.pagesType == .even {
  91. pageRangeBox.selectItem(at: 2)
  92. } else {
  93. pageRangeBox.cell?.stringValue = fileModel.pagesString
  94. self.pageRangeBox.isEditable = true
  95. window?.makeFirstResponder(pageRangeBox)
  96. }
  97. }
  98. func updateLanguage() {
  99. pageRangeLabel.stringValue = NSLocalizedString("Page Range", comment: "")
  100. }
  101. @IBAction func removeButtonAction(_ sender: Any) {
  102. guard let callBack = removeAction else { return }
  103. guard let fileModel = fileModel else { return }
  104. callBack(self, fileModel)
  105. }
  106. @IBAction func comboBoxAction(_ sender: NSComboBoxCell) {
  107. guard let fileModel = fileModel else { return }
  108. if sender.indexOfSelectedItem >= 0 {
  109. let type = KMPageRange.init(rawValue: sender.indexOfSelectedItem) ?? .all
  110. fileModel.pagesType = type
  111. }
  112. self.pageRangeBox.isEditable = false
  113. if fileModel.pagesType == .custom {
  114. fileModel.pagesString = sender.stringValue
  115. self.pageRangeBox.isEditable = true
  116. window?.makeFirstResponder(pageRangeBox)
  117. }
  118. }
  119. }
  120. extension KMMergeTableViewCell: NSComboBoxDelegate {
  121. }
  122. extension KMMergeTableViewCell {
  123. func fileSizeString(_ fSize: Float) -> String {
  124. let fileSize = fSize / 1024
  125. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  126. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  127. return String(format: "%0.1f %@", size, unit)
  128. }
  129. }