KMPageRangeSelectView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // KMPageRangeSelectView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/21.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMPageRangeSelectView: NSView {
  10. var pageSelectView: ComponentSelect = ComponentSelect()
  11. override init(frame frameRect: NSRect) {
  12. super.init(frame: frameRect)
  13. setUp()
  14. }
  15. required init?(coder: NSCoder) {
  16. super.init(coder: coder)
  17. setUp()
  18. }
  19. func setUp() {
  20. pageSelectView.frame = self.bounds
  21. pageSelectView.autoresizingMask = [.width, .height]
  22. self.addSubview(pageSelectView)
  23. pageSelectView.properties = ComponentSelectProperties(size: .s,
  24. state: .normal,
  25. placeholder: KMLocalizedString("e.g. 1,3-5,10"),
  26. errorText: nil,
  27. creatable: false,
  28. text: KMLocalizedString("All Pages"))
  29. pageSelectView.delegate = self
  30. let menuItemString:[String] = [KMLocalizedString("All Pages"),
  31. KMLocalizedString("Odd Pages Only"),
  32. KMLocalizedString("Even Pages Only"),
  33. KMLocalizedString("e.g. 1,3-5,10")]
  34. var menuItemArr: [ComponentMenuitemProperty] = []
  35. for language in menuItemString {
  36. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  37. itemSelected: false,
  38. isDisabled: false,
  39. keyEquivalent: nil,
  40. text: language,
  41. identifier: language)
  42. menuItemArr.append(itemProperty)
  43. }
  44. pageSelectView.updateMenuItemsArr(menuItemArr)
  45. pageSelectView.selectItemAtIndex(0)
  46. }
  47. //MARK: - Get
  48. func getSelectedPageString(_ document: CPDFDocument, _ pageIndexs: [Int]) -> String {
  49. var pages = [Int]()
  50. if pageSelectView.indexOfSelect() == 0 {
  51. pages = Array(0..<Int(document.pageCount))
  52. } else if pageSelectView.indexOfSelect() == 1 {
  53. pages = Array(stride(from: 0, to: Int(document.pageCount), by: 2))
  54. } else if pageSelectView.indexOfSelect() == 2 {
  55. pages = Array(stride(from: 1, to: Int(document.pageCount), by: 2))
  56. } else {
  57. pages = pageIndexs.map { $0 - 1 }
  58. }
  59. let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
  60. return pageIndexString
  61. }
  62. func getSelectedPageIndex(_ document: CPDFDocument) -> [Int] {
  63. let pageRangeSelectIndex = pageSelectView.indexOfSelect()
  64. let fileAttribute = KMNFileAttribute()
  65. fileAttribute.pdfDocument = document
  66. fileAttribute.password = document.password ?? ""
  67. if pageRangeSelectIndex == 0 {
  68. fileAttribute.bAllPage = true
  69. fileAttribute.pagesType = .AllPages
  70. } else if pageRangeSelectIndex == 1 {
  71. fileAttribute.bAllPage = false
  72. fileAttribute.pagesType = .OnlyOdd
  73. } else {
  74. if(document.pageCount <= 1) {
  75. fileAttribute.bAllPage = false
  76. fileAttribute.pagesType = .PagesString
  77. fileAttribute.pagesString = pageSelectView.properties.text ?? ""
  78. } else {
  79. fileAttribute.bAllPage = false
  80. if(pageRangeSelectIndex == 2) {
  81. fileAttribute.pagesType = .OnlyEven
  82. } else {
  83. fileAttribute.bAllPage = false
  84. fileAttribute.pagesType = .PagesString
  85. fileAttribute.pagesString = pageSelectView.properties.text ?? ""
  86. }
  87. }
  88. }
  89. let pageIndex = fileAttribute.fetchSelectPages()
  90. return pageIndex
  91. }
  92. }
  93. extension KMPageRangeSelectView: ComponentSelectDelegate {
  94. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  95. if menuItemProperty?.identifier == KMLocalizedString("e.g. 1,3-5,10") {
  96. view?.properties.creatable = true
  97. view?.properties.text = ""
  98. } else {
  99. view?.properties.creatable = false
  100. }
  101. view?.reloadData()
  102. }
  103. }