Browse Source

【2025】【菜单栏】菜单文件创建完善

niehaoyu 1 month ago
parent
commit
b50e7a3efa

+ 13 - 1
PDF Office/PDF Master/AppDelegate+MenuAction.swift

@@ -366,7 +366,19 @@ extension AppDelegate {
     }
     
     @IBAction func menuItemAction_NewFromWindow(_ sender: NSMenuItem) -> Void {
-        
+        if sender.tag == 0 {
+            if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
+                _windowC.screenShot_SelectArea(nil)
+            }
+        } else if sender.tag == 1 {
+            if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
+                _windowC.screenShot_Window(nil)
+            }
+        } else if sender.tag == 2 {
+            if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
+                _windowC.screenShot_FullScreen(nil)
+            }
+        }
     }
     
     @IBAction func menuItemAction_MergePDFFiles(_ sender: NSMenuItem) -> Void {

+ 2 - 2
PDF Office/PDF Master/Base.lproj/Main.storyboard

@@ -145,13 +145,13 @@
                                                                         <action selector="menuItemAction_NewFromWindow:" target="Voe-Tx-rLC" id="NzK-e1-hlj"/>
                                                                     </connections>
                                                                 </menuItem>
-                                                                <menuItem title="From Window…" id="XUM-IQ-nEo">
+                                                                <menuItem title="From Window…" tag="1" id="XUM-IQ-nEo">
                                                                     <modifierMask key="keyEquivalentModifierMask"/>
                                                                     <connections>
                                                                         <action selector="menuItemAction_NewFromWindow:" target="Voe-Tx-rLC" id="oEY-Da-jeL"/>
                                                                     </connections>
                                                                 </menuItem>
-                                                                <menuItem title="From Entire Screen" id="da8-Lp-LKs">
+                                                                <menuItem title="From Entire Screen" tag="2" id="da8-Lp-LKs">
                                                                     <modifierMask key="keyEquivalentModifierMask"/>
                                                                     <connections>
                                                                         <action selector="menuItemAction_NewFromWindow:" target="Voe-Tx-rLC" id="dSB-QV-1oZ"/>

+ 185 - 0
PDF Office/PDF Master/Class/ChromiumTabs/KMBrowserWindowController+CreateFile.swift

@@ -536,4 +536,189 @@ extension KMBrowserWindowController {
     }
     
     
+    func createBaseFoldPath() -> String {
+        let folderPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last?.appending("/\(Bundle.main.bundleIdentifier!)")
+        if (FileManager.default.fileExists(atPath: folderPath!) == false) {
+            try?FileManager.default.createDirectory(atPath: folderPath!, withIntermediateDirectories: false)
+        }
+        return folderPath ?? ""
+    }
+    
+    func filePathCheck(path: String) -> String {
+        var i: Int = 0
+        let fileURL = URL(fileURLWithPath: path)
+        var newPath: String = path
+        let fileManager = FileManager.default
+        while fileManager.fileExists(atPath: newPath) {
+            i += 1
+            
+            newPath = fileURL.deletingPathExtension().path
+            newPath.append("(\(i))")
+            newPath.append(".")
+            newPath.append(fileURL.pathExtension)
+        }
+        return newPath
+    }
+    
+    func screenShot_SelectArea(_ sender: Any?) {
+        KMScreenShotHandler.beginScreenshot_SelectRectWithCompleteHandler { ima in
+            if let image = ima {
+                DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
+                    let folderPath = self.createBaseFoldPath()
+                    let savePathOld = folderPath + "/screenShot.pdf"
+                    var savePath = self.filePathCheck(path: savePathOld)
+                    let newDocument = CPDFDocument()
+                    _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
+                    let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
+                    if writeSuccess ?? false {
+                        if self.checkOpenNewDocument(path: savePath) {
+                            self.savePdf(savePath)
+                        }
+                        
+                    } else {
+                        
+                    }
+                }
+            }
+        }
+    }
+    
+    func screenShot_Window(_ sender: Any?) {
+        KMPrint("screenShot_Window")
+        KMScreenShotHandler.beginScreenShot_SelectWindowCompleteHandler { ima in
+            if let image = ima {
+                if image.size.equalTo(.zero) == true {
+           
+                } else {
+                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
+                        let folderPath = self.createBaseFoldPath()
+                        let savePathOld = folderPath + "/screenShot.pdf"
+                        var savePath = self.filePathCheck(path: savePathOld)
+                        let newDocument = CPDFDocument()
+                        _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
+                        let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
+                        if writeSuccess == true {
+                            if self.checkOpenNewDocument(path: savePath) {
+                                self.savePdf(savePath)
+                            }
+                            //                        try? FileManager.default.removeItem(atPath: savePath)
+                        } else {
+                            
+                        }
+                    }
+                }
+            }
+        }
+    }
+    
+    func screenShot_FullScreenDelay(_ sender: Any?) {//不延迟
+        KMPrint("screenShot_FullScreenDelay")
+        KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(0) { ima in
+            if let image = ima {
+                DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
+                    let folderPath = self.createBaseFoldPath()
+                    let savePathOld = folderPath + "/screenShot.pdf"
+                    
+                    var savePath = self.filePathCheck(path: savePathOld)
+                    let newDocument = CPDFDocument()
+                    _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
+                    let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
+                    if writeSuccess == true {
+                        if self.checkOpenNewDocument(path: savePath) {
+                            self.savePdf(savePath)
+                        }
+//                        try? FileManager.default.removeItem(atPath: savePath)
+                    } else {
+                        
+                    }
+                }
+            }
+        }
+    }
+    
+    func screenShot_FullScreen(_ sender: Any?) {//延迟3秒
+        KMPrint("screenShot_FullScreen")
+        KMScreenShotHandler.beginScreenShot_FullSreenWithDelayTime(3) { ima in
+            if let image = ima {
+                DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
+                    let folderPath = self.createBaseFoldPath()
+                    let savePathOld = folderPath + "/screenShot.pdf"
+                    
+                    var savePath = self.filePathCheck(path: savePathOld)
+                    let newDocument = CPDFDocument()
+                    _ = newDocument?.km_insert(imageData: image.jpgData() ?? Data(), pageSize: image.size, at: newDocument?.pageCount ?? 0)
+                    let writeSuccess = newDocument?.write(to: URL(fileURLWithPath: savePath))
+                    if writeSuccess == true {
+                        self.savePdf(savePath)
+//                        try? FileManager.default.removeItem(atPath: savePath)
+                    } else {
+                        
+                    }
+                }
+            }
+        }
+    }
+    
+    func checkOpenNewDocument(path: String) -> Bool {
+        let tabCount = self.km_browser?.tabCount() ?? 0
+        if tabCount > 1{
+            if !IAPProductsManager.default().isAvailableAllFunction() {
+                let preferenceNoteShow = UserDefaults.standard.bool(forKey: KMTabbingHintShowFlag)
+                if preferenceNoteShow {
+                    menuItemAction_newTagPageToNewWindow("")
+                } else {
+                    if !KMDataManager.default.isTabbingWin{
+                        KMDataManager.default.isTabbingWin = true
+                        let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
+                        tabbingWin.selectCallBack = {[weak self] continueOrNot in
+                            KMDataManager.default.isTabbingWin = false
+                            if continueOrNot {
+                                self?.reopenDocumentForNewWindow(savePath: path)
+                            } else {
+                                
+                            }
+                        }
+                        self.km_beginSheet(windowC: tabbingWin)
+                    }
+                }
+                return false
+            }else {
+                if KMPreference.shared.openDocumentType == .newWindow {
+                    self.reopenDocumentForNewWindow(savePath: path)
+                    return false
+                }
+            }
+        }
+        return true
+    }
+     
+    func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
+        if (self.canResponseDocumentAction() == false) {
+            return
+        }
+        self.openNewWindow(sender)
+    }
+    
+    func reopenDocumentForNewWindow(savePath: String) {
+        let browser = KMBrowser.init() as KMBrowser
+        browser.windowController = KMBrowserWindowController.init(browser: browser)
+        browser.addHomeTabContents()
+        browser.windowController.showWindow(self)
+        self.savePdf(savePath)
+    }
+    
+    func savePdf(_ filePath: String) -> Void {
+        let docVc = KMDocumentController.shared
+        
+        docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
+            if error != nil {
+                NSApp.presentError(error!)
+            } else {
+                if document is KMMainDocument {
+                    let newDocument = document
+                    (newDocument as! KMMainDocument).isNewCreated = true
+                }
+            }
+        }
+    }
 }