Browse Source

【UI替换】打印 - 默认打印功能实现

lizhe 1 year ago
parent
commit
4d9fdc3992

+ 29 - 12
PDF Office/PDF Master/Class/PDFTools/Print/KMPrintWindowController.swift

@@ -8,6 +8,8 @@
 import Cocoa
 import PDFKit
 
+typealias KMPrintWindowControllerPrintTypeChange = (_ controller: KMPrintWindowController, _ type: PrintModelType) -> Void
+
 class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
     @IBOutlet weak var titleLabel: NSTextField!
     @IBOutlet weak var preview: KMPrintPreviewView!
@@ -16,6 +18,9 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
     @IBOutlet weak var bottomView: KMPrintBottomView!
     
     var pdfDocument: CPDFDocument? = nil
+    
+    var changeTypeAction: KMPrintWindowControllerPrintTypeChange?
+    
     var chooseData: KMPrintModel = KMPrintModel()
     var inputType: DataNavigationViewButtonActionType?
     var presenter: KMPrintPresenter = KMPrintPresenter()
@@ -300,14 +305,14 @@ extension KMPrintWindowController: KMPrintBottomViewDelegate {
     
     func printerAction() {
         KMPrint("调用打印机打印")
-        if self.chooseData.url != nil {
-            let document = PDFDocument.init(url: self.chooseData.url!)
-            let printInfo = NSPrintInfo.shared
-            let printOperation: NSPrintOperation = document!.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)!
-            let printPanel = printOperation.printPanel
-            printPanel.options = [.showsOrientation, .showsPageRange, .showsPaperSize, .showsScaling, .showsPreview]
-            printOperation.runModal(for: self.window!, delegate: self, didRun: nil, contextInfo: nil)
-        }
+        
+        guard let url = self.presenter.printData.url else { return }
+        let document = PDFDocument.init(url: url)
+        let printInfo = NSPrintInfo.shared
+        let printOperation: NSPrintOperation = document!.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)!
+        let printPanel = printOperation.printPanel
+        printPanel.options = [.showsOrientation, .showsPageRange, .showsPaperSize, .showsScaling, .showsPreview]
+        printOperation.runModal(for: self.window!, delegate: self, didRun: nil, contextInfo: nil)
     }
     
     func cancelAction() {
@@ -318,13 +323,25 @@ extension KMPrintWindowController: KMPrintBottomViewDelegate {
     }
     
     func printAction() {
-        
         KMPrint("打印机直接打印")
-        let document = PDFDocument.init(url: self.chooseData.url!)
+        guard let url = self.presenter.printData.url else { return }
+        let document = PDFDocument.init(url: url)
         let printInfo = NSPrintInfo.shared
         let printOperation: NSPrintOperation = document!.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)!
-        printOperation.showsPrintPanel = false
-//        printOperation.run()
+        printOperation.showsPrintPanel = true //是否弹出打印机设置界面
+        printOperation.run()
+    }
+    
+    func posterAction() {
+        self.changeTypeAction?(self, .poster)
+    }
+
+    func multipageAction() {
+        self.changeTypeAction?(self, .multipage)
+    }
+    
+    func bookletAction() {
+        self.changeTypeAction?(self, .pamphlet)
     }
 }
 

+ 9 - 9
PDF Office/PDF Master/Class/PDFTools/Print/Model/KMPrintPageModel.swift

@@ -79,21 +79,21 @@ enum KMPrintContentType: String, CaseIterable {
     }
 }
 
+//类型
+enum PrintModelType {
+    case size //大小
+    case poster //海报
+    case multipage //多页
+    case pamphlet //小册子
+}
+
 struct KMPrintPageOperation {
-    var type: ModelType = .size
+    var type: PrintModelType = .size
     var size: Size = Size()
     var poster: Poster = Poster()
     var multipage: Multipage = Multipage()
     var pamphlet: Pamphlet = Pamphlet()
     
-    //类型
-    enum ModelType {
-        case size //大小
-        case poster //海报
-        case multipage //多页
-        case pamphlet //小册子
-    }
-    
     struct PageOfPaper {
         var type: PageType = .page
         var point: CGPoint = CGPoint(x: 1, y: 1)

+ 4 - 2
PDF Office/PDF Master/Class/PDFTools/Print/Presenter/KMPrintPresenter.swift

@@ -25,7 +25,8 @@ class KMPrintPresenter: NSObject {
         self.delegate = delegate
         self.document = document
         self.printData = data
-        self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
+        let pdfDocument = self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
+        self.printData.url = pdfDocument.documentURL
     }
     
     /**
@@ -33,7 +34,8 @@ class KMPrintPresenter: NSObject {
      */
     func reloadData() {
         guard let document = document else { return }
-        self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
+        let pdfDocument = self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
+        self.printData.url = pdfDocument.documentURL
     }
     
     /**

+ 46 - 30
PDF Office/PDF Master/Class/PDFTools/Print/View/BottomView/KMPrintBottomView.swift

@@ -34,36 +34,36 @@ class KMPrintBottomView: KMBaseXibView {
     override func setup() {
         super.setup()
         
-        self.printerButton.wantsLayer = true
-        self.printerButton.layer?.borderWidth = 1
-        self.printerButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
-        self.printerButton.layer?.cornerRadius = 4
-        self.printerButton.title = NSLocalizedString("Printer", comment: "")
-        self.printerButton.font = NSFont.SFProTextRegularFont(14)
-        self.printerButton.contentTintColor = NSColor.km_init(hex: "#252629")
-        
-        self.cancelButton.wantsLayer = true
-        self.cancelButton.layer?.borderWidth = 1
-        self.cancelButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
-        self.cancelButton.layer?.cornerRadius = 4
-        self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
-        self.cancelButton.font = NSFont.SFProTextRegularFont(14)
-        self.cancelButton.contentTintColor = NSColor.km_init(hex: "#252629")
-        
-        self.savePDFButton.wantsLayer = true
-        self.savePDFButton.layer?.borderWidth = 1
-        self.savePDFButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
-        self.savePDFButton.layer?.cornerRadius = 4
-        self.savePDFButton.title = NSLocalizedString("Save as PDF", comment: "")
-        self.savePDFButton.font = NSFont.SFProTextRegularFont(14)
-        self.savePDFButton.contentTintColor = NSColor.km_init(hex: "#252629")
-        
-        self.printButton.wantsLayer = true
-        self.printButton.layer?.backgroundColor = NSColor.km_init(hex: "#1770F4").cgColor
-        self.printButton.layer?.cornerRadius = 4
-        self.printButton.title = NSLocalizedString("Printer", comment: "")
-        self.printButton.font = NSFont.SFProTextRegularFont(14)
-        self.printButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
+//        self.printerButton.wantsLayer = true
+//        self.printerButton.layer?.borderWidth = 1
+//        self.printerButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
+//        self.printerButton.layer?.cornerRadius = 4
+//        self.printerButton.title = NSLocalizedString("Printer", comment: "")
+//        self.printerButton.font = NSFont.SFProTextRegularFont(14)
+//        self.printerButton.contentTintColor = NSColor.km_init(hex: "#252629")
+//        
+//        self.cancelButton.wantsLayer = true
+//        self.cancelButton.layer?.borderWidth = 1
+//        self.cancelButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
+//        self.cancelButton.layer?.cornerRadius = 4
+//        self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
+//        self.cancelButton.font = NSFont.SFProTextRegularFont(14)
+//        self.cancelButton.contentTintColor = NSColor.km_init(hex: "#252629")
+//        
+//        self.savePDFButton.wantsLayer = true
+//        self.savePDFButton.layer?.borderWidth = 1
+//        self.savePDFButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
+//        self.savePDFButton.layer?.cornerRadius = 4
+//        self.savePDFButton.title = NSLocalizedString("Save as PDF", comment: "")
+//        self.savePDFButton.font = NSFont.SFProTextRegularFont(14)
+//        self.savePDFButton.contentTintColor = NSColor.km_init(hex: "#252629")
+//        
+//        self.printButton.wantsLayer = true
+//        self.printButton.layer?.backgroundColor = NSColor.km_init(hex: "#1770F4").cgColor
+//        self.printButton.layer?.cornerRadius = 4
+//        self.printButton.title = NSLocalizedString("Printer", comment: "")
+//        self.printButton.font = NSFont.SFProTextRegularFont(14)
+//        self.printButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
     }
     
     //刷新界面UI 和 数据
@@ -129,6 +129,18 @@ class KMPrintBottomView: KMBaseXibView {
             self.delegate?.savePDFAction()
         }
     }
+    
+    @IBAction func bookletButtonAction(_ sender: Any) {
+        self.delegate?.bookletAction()
+    }
+    
+    @IBAction func multipleButtonAction(_ sender: Any) {
+        self.delegate?.multipageAction()
+    }
+    
+    @IBAction func poseterButtonAction(_ sender: Any) {
+        self.delegate?.posterAction()
+    }
 }
 
 protocol KMPrintBottomViewDelegate {
@@ -136,4 +148,8 @@ protocol KMPrintBottomViewDelegate {
     func cancelAction()
     func printAction()
     func savePDFAction()
+    
+    func posterAction()
+    func multipageAction()
+    func bookletAction()
 }

+ 66 - 46
PDF Office/PDF Master/Class/PDFTools/Print/View/BottomView/KMPrintBottomView.xib

@@ -1,93 +1,113 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22155" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
         <deployment identifier="macosx"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22155"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
         <customObject id="-2" userLabel="File's Owner" customClass="KMPrintBottomView">
             <connections>
-                <outlet property="cancelButton" destination="IND-Zh-OtI" id="Mn7-jj-JdZ"/>
-                <outlet property="printButton" destination="Ct8-lL-0Xy" id="ogq-u3-mLv"/>
-                <outlet property="printerButton" destination="iuC-bV-Bbq" id="kAR-y4-zOU"/>
-                <outlet property="savePDFButton" destination="9tn-cj-ikB" id="ODA-XC-nVr"/>
+                <outlet property="cancelButton" destination="Ayk-Hy-6Mb" id="HNS-Xh-3aU"/>
+                <outlet property="printButton" destination="hjt-Ec-LG7" id="IUp-w6-Ojd"/>
             </connections>
         </customObject>
         <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
         <customObject id="-3" userLabel="Application" customClass="NSObject"/>
         <customView translatesAutoresizingMaskIntoConstraints="NO" id="zFh-KB-UcZ">
-            <rect key="frame" x="0.0" y="0.0" width="750" height="48"/>
+            <rect key="frame" x="0.0" y="0.0" width="586" height="48"/>
             <subviews>
-                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="iuC-bV-Bbq">
-                    <rect key="frame" x="16" y="8" width="80" height="32"/>
-                    <constraints>
-                        <constraint firstAttribute="height" constant="32" id="ITz-4g-1VQ"/>
-                        <constraint firstAttribute="width" constant="80" id="LVe-hH-Xnn"/>
-                    </constraints>
-                    <buttonCell key="cell" type="square" title="Printer" bezelStyle="shadowlessSquare" alignment="center" imageScaling="proportionallyDown" inset="2" id="KfZ-BQ-Q2m">
+                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="XoC-G4-SMT">
+                    <rect key="frame" x="13" y="7" width="83" height="33"/>
+                    <buttonCell key="cell" type="push" title="Poster" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Wov-Hx-VLR">
                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                         <font key="font" metaFont="system"/>
                     </buttonCell>
+                    <constraints>
+                        <constraint firstAttribute="width" constant="69" id="5UY-qe-Hhf"/>
+                        <constraint firstAttribute="height" constant="21" id="ciE-k1-cm5"/>
+                    </constraints>
                     <connections>
-                        <action selector="printerAction:" target="-2" id="xva-4g-SFD"/>
+                        <action selector="poseterButtonAction:" target="-2" id="zaa-aw-AEV"/>
                     </connections>
                 </button>
-                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Ct8-lL-0Xy">
-                    <rect key="frame" x="654" y="8" width="80" height="32"/>
-                    <constraints>
-                        <constraint firstAttribute="height" constant="32" id="9da-JS-jKo"/>
-                        <constraint firstAttribute="width" constant="80" id="Ecl-xz-TGO"/>
-                    </constraints>
-                    <buttonCell key="cell" type="square" title="Print" bezelStyle="shadowlessSquare" alignment="center" imageScaling="proportionallyDown" inset="2" id="QUI-d8-l0N">
+                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Pvn-3t-pYc">
+                    <rect key="frame" x="102" y="7" width="83" height="33"/>
+                    <buttonCell key="cell" type="push" title="Multiple" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Rwl-n4-Wg6">
                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                         <font key="font" metaFont="system"/>
                     </buttonCell>
+                    <constraints>
+                        <constraint firstAttribute="height" constant="21" id="XAa-yl-s36"/>
+                        <constraint firstAttribute="width" constant="69" id="fyf-K9-WXR"/>
+                    </constraints>
                     <connections>
-                        <action selector="printAction:" target="-2" id="WqN-xJ-yBp"/>
+                        <action selector="multipleButtonAction:" target="-2" id="q2b-xX-Dh6"/>
                     </connections>
                 </button>
-                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="9tn-cj-ikB">
-                    <rect key="frame" x="524" y="8" width="114" height="32"/>
-                    <constraints>
-                        <constraint firstAttribute="width" constant="114" id="QGq-h5-uiI"/>
-                        <constraint firstAttribute="height" constant="32" id="tSH-Hq-IMN"/>
-                    </constraints>
-                    <buttonCell key="cell" type="square" title="Save as PDF" bezelStyle="shadowlessSquare" alignment="center" imageScaling="proportionallyDown" inset="2" id="4xq-ct-ZZd">
+                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Gke-Bn-OKJ">
+                    <rect key="frame" x="191" y="7" width="83" height="33"/>
+                    <buttonCell key="cell" type="push" title="Booklet" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="f4A-vO-fi9">
                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                         <font key="font" metaFont="system"/>
                     </buttonCell>
+                    <constraints>
+                        <constraint firstAttribute="height" constant="21" id="hUQ-Td-kLn"/>
+                        <constraint firstAttribute="width" constant="69" id="p5E-WO-2K9"/>
+                    </constraints>
                     <connections>
-                        <action selector="savePDFAction:" target="-2" id="sVH-GW-veE"/>
+                        <action selector="bookletButtonAction:" target="-2" id="UMV-gz-7O2"/>
                     </connections>
                 </button>
-                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="IND-Zh-OtI">
-                    <rect key="frame" x="428" y="8" width="80" height="32"/>
+                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Ayk-Hy-6Mb">
+                    <rect key="frame" x="401" y="7" width="83" height="33"/>
+                    <buttonCell key="cell" type="push" title="Cancel" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="4uM-k8-JhQ">
+                        <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
+                        <font key="font" metaFont="system"/>
+                        <string key="keyEquivalent" base64-UTF8="YES">
+Gw
+</string>
+                    </buttonCell>
                     <constraints>
-                        <constraint firstAttribute="width" constant="80" id="1Y2-Tv-HBB"/>
-                        <constraint firstAttribute="height" constant="32" id="7WY-DT-n2M"/>
+                        <constraint firstAttribute="width" constant="69" id="aPV-YQ-kHt"/>
+                        <constraint firstAttribute="height" constant="21" id="io1-r2-j68"/>
                     </constraints>
-                    <buttonCell key="cell" type="square" title="Cancel" bezelStyle="shadowlessSquare" alignment="center" imageScaling="proportionallyDown" inset="2" id="fe7-PR-hsI">
+                    <connections>
+                        <action selector="cancelAction:" target="-2" id="miQ-kV-4fm"/>
+                    </connections>
+                </button>
+                <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="hjt-Ec-LG7">
+                    <rect key="frame" x="490" y="7" width="83" height="33"/>
+                    <buttonCell key="cell" type="push" title="Print" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="FqK-xu-iCI">
                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                         <font key="font" metaFont="system"/>
+                        <string key="keyEquivalent" base64-UTF8="YES">
+DQ
+</string>
                     </buttonCell>
+                    <constraints>
+                        <constraint firstAttribute="height" constant="21" id="Xi5-ST-NiP"/>
+                        <constraint firstAttribute="width" constant="69" id="kPr-yH-HAI"/>
+                    </constraints>
                     <connections>
-                        <action selector="cancelAction:" target="-2" id="BSn-iO-1L8"/>
+                        <action selector="printAction:" target="-2" id="uJS-Vx-1rN"/>
                     </connections>
                 </button>
             </subviews>
             <constraints>
-                <constraint firstItem="9tn-cj-ikB" firstAttribute="centerY" secondItem="zFh-KB-UcZ" secondAttribute="centerY" id="4fN-eV-qch"/>
-                <constraint firstItem="9tn-cj-ikB" firstAttribute="leading" secondItem="IND-Zh-OtI" secondAttribute="trailing" constant="16" id="FiR-A1-g8z"/>
-                <constraint firstItem="Ct8-lL-0Xy" firstAttribute="leading" secondItem="9tn-cj-ikB" secondAttribute="trailing" constant="16" id="OIt-52-fsi"/>
-                <constraint firstItem="IND-Zh-OtI" firstAttribute="centerY" secondItem="zFh-KB-UcZ" secondAttribute="centerY" id="TMC-f1-rJy"/>
-                <constraint firstItem="iuC-bV-Bbq" firstAttribute="leading" secondItem="zFh-KB-UcZ" secondAttribute="leading" constant="16" id="URe-KF-Mfc"/>
+                <constraint firstItem="Gke-Bn-OKJ" firstAttribute="centerY" secondItem="XoC-G4-SMT" secondAttribute="centerY" id="1Ai-AJ-d1m"/>
+                <constraint firstItem="XoC-G4-SMT" firstAttribute="leading" secondItem="zFh-KB-UcZ" secondAttribute="leading" constant="20" id="65s-vm-jnZ"/>
+                <constraint firstAttribute="trailing" secondItem="hjt-Ec-LG7" secondAttribute="trailing" constant="20" id="7Sr-Qv-kfF"/>
+                <constraint firstItem="Pvn-3t-pYc" firstAttribute="leading" secondItem="XoC-G4-SMT" secondAttribute="trailing" constant="20" id="7pr-9B-fk2"/>
+                <constraint firstItem="Ayk-Hy-6Mb" firstAttribute="centerY" secondItem="XoC-G4-SMT" secondAttribute="centerY" id="Aqg-0R-0Ut"/>
+                <constraint firstItem="Pvn-3t-pYc" firstAttribute="centerY" secondItem="XoC-G4-SMT" secondAttribute="centerY" id="EjH-EI-AuH"/>
+                <constraint firstItem="XoC-G4-SMT" firstAttribute="centerY" secondItem="zFh-KB-UcZ" secondAttribute="centerY" id="W61-pR-Fhf"/>
                 <constraint firstAttribute="height" constant="48" id="Wh7-M9-jtZ"/>
-                <constraint firstItem="Ct8-lL-0Xy" firstAttribute="centerY" secondItem="zFh-KB-UcZ" secondAttribute="centerY" id="cSM-BF-UYL"/>
-                <constraint firstItem="iuC-bV-Bbq" firstAttribute="centerY" secondItem="zFh-KB-UcZ" secondAttribute="centerY" id="rXv-nF-6V7"/>
-                <constraint firstAttribute="trailing" secondItem="Ct8-lL-0Xy" secondAttribute="trailing" constant="16" id="ztl-B3-RjW"/>
+                <constraint firstItem="Gke-Bn-OKJ" firstAttribute="leading" secondItem="Pvn-3t-pYc" secondAttribute="trailing" constant="20" id="YZz-yh-HBZ"/>
+                <constraint firstItem="hjt-Ec-LG7" firstAttribute="leading" secondItem="Ayk-Hy-6Mb" secondAttribute="trailing" constant="20" id="dWj-cF-qST"/>
+                <constraint firstItem="hjt-Ec-LG7" firstAttribute="centerY" secondItem="XoC-G4-SMT" secondAttribute="centerY" id="mAC-Pm-KCC"/>
             </constraints>
-            <point key="canvasLocation" x="156" y="172"/>
+            <point key="canvasLocation" x="74" y="172"/>
         </customView>
     </objects>
 </document>