123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // KMSplitPDFViewController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/29.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMSplitPDFViewControllerDelegate: AnyObject {
-
- @objc optional func splitPDFViewControllerDidUpdateFilePath(_ controller: KMSplitPDFViewController)
-
- }
- class KMSplitPDFViewController: NSViewController {
- @IBOutlet var contendView: NSView!
-
- @IBOutlet var emptyView: ComponentEmpty!
-
- var pdfView: KMSecondaryPDFView?
- var viewManager: KMPDFViewManager?
- weak open var delegate: KMSplitPDFViewControllerDelegate?
- deinit {
- pdfView?.removeFromSuperview()
-
- KMPrint(self.className + " deinit.")
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- view.wantsLayer = true
- view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-low").cgColor
-
- setupProperty()
-
- reloadData()
- }
-
- func setupProperty() {
-
- emptyView.properties = ComponentEmptyProperty(emptyType: .add_File,
- text: KMLocalizedString("No recently opened file"),
- subText: KMLocalizedString("Click Open File or drag-and-drop file here to open them."))
- emptyView.setTarget(self, action: #selector(emptyViewAddFileClicked(_:)))
-
- if pdfView == nil {
- pdfView = KMSecondaryPDFView.init()
- }
- pdfView?.frame = contendView.bounds
- pdfView?.autoresizingMask = [.width, .height]
- contendView.addSubview(pdfView!)
-
- }
-
- func reloadData() {
- if let fileURL = viewManager?.splitPDFFileURL {
- emptyView.isHidden = true
- pdfView?.isHidden = false
-
- let document = CPDFDocument(url: fileURL)
- let isLocked = document?.isLocked ?? false
- if isLocked {
- if let pw = viewManager?.splitPDFFilePassword {
- document?.unlock(withPassword: pw)
- }
- }
- pdfView?.document = document
-
- } else {
- emptyView.isHidden = false
- pdfView?.isHidden = true
-
- }
- }
-
- @objc func emptyViewAddFileClicked(_ sender: NSView) {
- let openPanel = NSOpenPanel()
- openPanel.allowedFileTypes = ["pdf", "PDF"]
- openPanel.allowsMultipleSelection = false
- openPanel.beginSheetModal(for: self.view.window!) { [weak self] result in
- if result == NSApplication.ModalResponse.OK {
- if let url = openPanel.url {
- guard let weakSelf = self else { return }
-
- weakSelf.viewManager?.splitPDFFileURL = url
-
- let document = CPDFDocument(url: url)
- let isLocked = document?.isLocked ?? false
- if isLocked {
-
- }
- weakSelf.reloadData()
-
- weakSelf.delegate?.splitPDFViewControllerDidUpdateFilePath?(weakSelf)
- }
- }
- }
- }
-
- }
|