123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //
- // KMPageRangeSelectView.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/21.
- //
- import Cocoa
- import KMComponentLibrary
- class KMPageRangeSelectView: NSView {
-
- var pageSelectView: ComponentSelect = ComponentSelect()
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- setUp()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- setUp()
- }
-
- func setUp() {
- pageSelectView.frame = self.bounds
- pageSelectView.autoresizingMask = [.width, .height]
- self.addSubview(pageSelectView)
-
- pageSelectView.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- placeholder: KMLocalizedString("e.g. 1,3-5,10"),
- errorText: nil,
- creatable: false,
- text: KMLocalizedString("All Pages"))
- pageSelectView.delegate = self
-
- let menuItemString:[String] = [KMLocalizedString("All Pages"),
- KMLocalizedString("Odd Pages Only"),
- KMLocalizedString("Even Pages Only"),
- KMLocalizedString("e.g. 1,3-5,10")]
-
- var menuItemArr: [ComponentMenuitemProperty] = []
- for language in menuItemString {
- let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: language,
- identifier: language)
-
- menuItemArr.append(itemProperty)
- }
- pageSelectView.updateMenuItemsArr(menuItemArr)
- pageSelectView.selectItemAtIndex(0)
-
- }
-
- //MARK: - Get
- func getSelectedPageString(_ document: CPDFDocument, _ pageIndexs: [Int]) -> String {
- var pages = [Int]()
-
- if pageSelectView.indexOfSelect() == 0 {
- pages = Array(0..<Int(document.pageCount))
- } else if pageSelectView.indexOfSelect() == 1 {
- pages = Array(stride(from: 0, to: Int(document.pageCount), by: 2))
- } else if pageSelectView.indexOfSelect() == 2 {
- pages = Array(stride(from: 1, to: Int(document.pageCount), by: 2))
- } else {
- pages = pageIndexs.map { $0 - 1 }
- }
-
- let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
-
- return pageIndexString
- }
-
- func getSelectedPageIndex(_ document: CPDFDocument) -> [Int] {
- let pageRangeSelectIndex = pageSelectView.indexOfSelect()
-
- let fileAttribute = KMNFileAttribute()
- fileAttribute.pdfDocument = document
- fileAttribute.password = document.password ?? ""
-
- if pageRangeSelectIndex == 0 {
- fileAttribute.bAllPage = true
- fileAttribute.pagesType = .AllPages
- } else if pageRangeSelectIndex == 1 {
- fileAttribute.bAllPage = false
- fileAttribute.pagesType = .OnlyOdd
- } else {
- if(document.pageCount <= 1) {
- fileAttribute.bAllPage = false
- fileAttribute.pagesType = .PagesString
- fileAttribute.pagesString = pageSelectView.properties.text ?? ""
- } else {
- fileAttribute.bAllPage = false
- if(pageRangeSelectIndex == 2) {
- fileAttribute.pagesType = .OnlyEven
- } else {
- fileAttribute.bAllPage = false
- fileAttribute.pagesType = .PagesString
- fileAttribute.pagesString = pageSelectView.properties.text ?? ""
- }
- }
- }
-
- let pageIndex = fileAttribute.fetchSelectPages()
- return pageIndex
- }
-
- }
- extension KMPageRangeSelectView: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- if menuItemProperty?.identifier == KMLocalizedString("e.g. 1,3-5,10") {
- view?.properties.creatable = true
- view?.properties.text = ""
- } else {
- view?.properties.creatable = false
- }
- view?.reloadData()
- }
- }
|