// // KMBrowserWindowController.swift // PDF Reader Pro // // Created by wanjun on 2022/12/9. // import Cocoa @objcMembers class KMBrowserWindowController: CTBrowserWindowController { var rightSegmentControl: KMSegmentedBox? var modeType: KMHomeToolState = .Home var filterType: Int = 0 var mainViewController: NSViewController? var isShowingTokenTimeOutAlert: Bool = false var currentTimer: Timer? private(set) var isMultiTabMode: Bool = false var rightMessageVC: KMVerificationMessageViewController! var rightToolbarItemView: NSView! private var homeVC: KMHomeViewController? var savedNormalSetup: NSMutableDictionary = NSMutableDictionary() // 记录当前显示模式 // var _mwcFlags: MwcFlags = MwcFlags() deinit { KMPrint("KMBrowserWindowController deinit...") // self.removeObserverForAppearanceChange() DistributedNotificationCenter.default().removeObserver(self) NotificationCenter.default.removeObserver(self) } override func loadWindow() { super.loadWindow() // self.window?.titlebarAppearsTransparent = true } override func windowDidLoad() { super.windowDidLoad() NSImage.makeTextAlignImages() NSImage.makeAdornImages() window?.backgroundColor = NSColor.km_init(hex: "#DFE1E5") self.window?.appearance = NSApp.appearance self.window?.titlebarAppearsTransparent = true // self.window?.standardWindowButton(.closeButton)?.superview?.needsLayout = true // window?.setFrameAutosaveName("") rightTabStripView_.delete = self homeRightTabStripView_.delete = self rightTabStripView_.updateView() fileUploadPanel.delete = self homeRightTabStripView_.homeRightSearchField.delegate = self isMultiTabMode = true // self.addObserverForAppearanceChange() refreshToolBar(homeToolState: .Home) NotificationCenter.default.addObserver(self, selector: #selector(closeAllTabs(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseAllTabs"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(openNewWindow(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerOpenNewWindow"), object: nil) DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil) // if (KMWelcomeWindowController.welcomeHasShow() == false) { // //AI 版本禁掉首次开启帮助文档 //// KMTools.openQuickStartStudy() // // DispatchQueue.main.async { // let welcome = KMWelcomeWindowController() // self.km_beginSheet(windowC: welcome) // welcome.itemClick = { [weak self] idx, param in // if (idx == 1) { // 关闭 // self?.km_endSheet() // } else if (idx == 2) { // 以后提醒 // self?.km_endSheet() // } else if (idx == 3) { // 注册 // self?.km_endSheet() // // guard let window_ = self?.window else { // return // } // _ = KMLoginWindowController.show(window: window_, .Batch, .register) // } // } // } // } updateViewColor() var tabbingMode = NSWindow.TabbingMode.automatic let openDocumentType = KMPreference.shared.openDocumentType if openDocumentType == .inSameWindow { tabbingMode = .preferred } else if openDocumentType == .newWindow { tabbingMode = .disallowed } #if VERSION_FREE if KMMemberInfo.shared.isMemberAllFunction == false { tabbingMode = .disallowed } #endif // self.window?.tabbingMode = tabbingMode let windowSizeOption = KMPreferenceManager.shared.openFileType // if (hasWindowSetup) { // NSString *rectString = [savedNormalSetup objectForKey:MAINWINDOWFRAME_KEY]; // if (rectString) // [window setFrame:NSRectFromString(rectString) display:NO]; // } else if (windowSizeOption == .maxim) { Task { self.window?.setFrame(NSScreen.main?.visibleFrame ?? (self.window?.frame ?? .zero), display: false) } } // } if windowSizeOption == .fit { self.performFit(nil) } } @objc func closeAction() { self.window?.performClose(nil) } override func windowShouldClose(_ sender: NSWindow) -> Bool { //#if DEBUG // if self.browser.tabStripModel.count() > 1 { // self.browser.windowDidBeginToClose() // return false // } //#else // if let cnt = self.browser?.tabStripModel?.count(), cnt > 1 { // if self.browser?.window != nil { // self.browser?.windowDidBeginToClose() // } // return false // } //#endif if let cnt = self.browser?.tabStripModel?.count(), cnt > 1 { if self.browser?.window != nil { self.browser?.windowDidBeginToClose() } return false } return true } override func windowWillClose(_ notification: Notification) { // iRate.sharedInstance().appLaunched() } override func layoutSubviews() { super.layoutSubviews() // if let button = (self.window as? CTBrowserWindow)?.standardWindowButton(.closeButton) { // button.frame = NSMakeRect(button.frame.minX, 0, button.frame.width, button.frame.height) // } // if let button = (self.window as? CTBrowserWindow)?.standardWindowButton(.zoomButton) { // button.frame = NSMakeRect(button.frame.minX, 0, button.frame.width, button.frame.height) // } // if let button = (self.window as? CTBrowserWindow)?.standardWindowButton(.miniaturizeButton) { // button.frame = NSMakeRect(button.frame.minX, 0, button.frame.width, button.frame.height) // } } // MARK: Dark&Light // func addObserverForAppearanceChange() -> Void { // window?.contentView?.addObserver(self, forKeyPath: "effectiveAppearance", options: .new, context: nil) // } // func removeObserverForAppearanceChange() -> Void { // window?.contentView?.removeObserver(self, forKeyPath: "effectiveAppearance") // } // override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { // if keyPath == "effectiveAppearance" { // self.updateViewColor() // } // } @objc private func _themeChanged(_ sender: Notification) { let isDarkModel = KMAdvertisementConfig.isDarkModel() if isDarkModel { self.window?.appearance = .init(named: .darkAqua) } else { self.window?.appearance = .init(named: .aqua) } self.window?.contentView?.appearance = self.window?.appearance ?? .init(named: .aqua) self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua) } override func updateViewColor() { super.updateViewColor() window?.backgroundColor = KMTabAppearance.tabsViewBackgroundColor() } // MARK: Public Methods func refreshToolBar(homeToolState toolState: KMHomeToolState) -> Void { modeType = toolState let document = browser?.activeTabContents() if document != nil { if document!.isHome { homeRightTabStripView_.isHidden = modeType == .Home } else { self.homeRightStripView.isHidden = true } } else { self.homeRightStripView.isHidden = true } homeRightTabStripView_.homeRefreshButton.isEnabled = true homeRightTabStripView_.homeUploadButton.isEnabled = true filterType = 0 rightTabStripView_.sortPopUpButton.removeAllItems() rightTabStripView_.filterPopUpButton.removeAllItems() rightTabStripView_.updateView() } override var hasToolbar: Bool { get { return false } } func showSnapshots(setups: NSArray?) { let doc = self.document as? KMMainDocument doc?.mainViewController?.showSnapshots(setups: setups) } var km_browser: KMBrowser? { get { return self.browser as? KMBrowser } } // MARK: Private Methods func openDocumentWindow() -> Void { // let panel = NSOpenPanel() // panel.allowsMultipleSelection = false // panel.allowedFileTypes = ["pdf"] // panel.beginSheetModal(for: NSApp.mainWindow!) { response in // if response == .OK { // let openPath = panel.url?.path // let selectDocument = self.selectTabContents(path: openPath!) // if selectDocument != nil { // let currentIndex = selectDocument!.browser.tabStripModel.index(of: selectDocument) // self.browser.tabStripModel.selectTabContents(at: Int32(currentIndex), userGesture: true) // return // } // if !panel.url!.path.isPDFValid() { // let alert = NSAlert() // alert.alertStyle = .critical // alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "") // alert.runModal() // return // } // NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { pdfDocument, documentWasAlreadyOpen, error in // print("openDocumentWindow") // } // } // } // MARK TODO: + 号开启 // self.browser.addHomeTabContents() self.tabStripView?.addTabButton?.layer?.backgroundColor = .clear self.km_browser?.addNewTabContents() } // override func openDocument(_ sender: Any!) { // self.openDocumentWindow() // } func selectTabContents(path filePath: String) -> KMMainDocument? { let document = NSDocumentController.shared.document(for: URL(fileURLWithPath: filePath)) var selectDocument: KMMainDocument? if document != nil { if document!.isKind(of: KMMainDocument.self) { selectDocument = document as! KMMainDocument } } return selectDocument } // MARK: Getter override var rightStripView: NSView! { get { if self.rightToolbarItemView == nil { self.rightToolbarItemView = NSView.init(frame: NSMakeRect(0, 10, 184, 40)) self.rightToolbarItemView.wantsLayer = true if self.rightMessageVC == nil { self.rightMessageVC = KMVerificationMessageViewController.init() } self.rightMessageVC.frameUpdateHandle = { [weak self] rect in guard let rsView = self?.rightToolbarItemView else { return } var superRect = rsView.frame if (superRect.size.width < self!.rightMessageVC.view.frame.size.width + 8) { superRect.size.width = self!.rightMessageVC.view.frame.size.width + 8 } if let supView = rsView.superview { let viewRect = supView.frame if (superRect.origin.x) + (superRect.size.width) > (viewRect.origin.x) + (viewRect.size.width) { let offsetValue = ((superRect.origin.x) + (superRect.size.width) - (viewRect.origin.x) - (viewRect.size.width)) superRect.origin.x -= offsetValue } } self?.rightToolbarItemView.frame = superRect } var rect = self.rightMessageVC.view.frame; rect.origin.x = NSWidth(self.rightToolbarItemView.frame) - rect.size.width - 8 rect.origin.y = (NSHeight(self.rightToolbarItemView.frame) - rect.size.height)/2 self.rightMessageVC.view.frame = rect self.rightMessageVC.view.autoresizingMask = [ .maxXMargin] self.rightToolbarItemView.addSubview(self.rightMessageVC.view) } return self.rightToolbarItemView } } override var homeRightStripView: NSView! { get { return homeRightTabStripView_ } } // MARK: Button Action func commandDispatch(_ sender: Any) -> Void { var needShowChooseWindow = false //#if VERSION_FREE //#if VERSION_DMG if (!KMMemberInfo.shared.isMemberAllFunction) { needShowChooseWindow = true } //#endif if needShowChooseWindow { 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 let winC = self?.kmCurrentWindowC as? KMTabbingHintWindowController let newTab = winC?.newTabInWindowButton.state == .on if continueOrNot { if KMMemberInfo.shared.isMemberAllFunction && newTab { self?.openDocumentWindow() } else { self?.reopenDocument(forPaths: nil) } } else { } } self.km_beginSheet(windowC: tabbingWin) } } } else { openDocumentWindow() } } func reopenDocument(forPaths path: String?) -> Void { if path == nil { let browser = KMBrowser.init() as KMBrowser browser.windowController = KMBrowserWindowController.init(browser: browser) browser.addHomeTabContents() browser.windowController.showWindow(self) }else { NSDocumentController.shared.km_safe_openDocument(withContentsOf: URL(fileURLWithPath: path!), display: true) { doc, open, err in } } } func closeAllTabs(_ sender: Any) -> Void { if let noti = sender as? NSNotification, noti.object is CTTabController { if let window_ = (noti.object as! CTTabController).view.window, window_.isEqual(to: self.window) == false { return } } if self.browser != nil { self.browser.closeAllTabs() } } func openNewWindow(_ sender: Any) -> Void { if let noti = sender as? NSNotification, noti.object is CTTabController { if let window_ = (noti.object as! CTTabController).view.window, window_.isEqual(to: self.window) == false { return } } // 分离 Tab let windowC = self.detachTab(toNewWindow: self.activeTabView() as? CTTabView) // 新增 home Tab (windowC as? CTBrowserWindowController)?.browser?.addHomeTabContentsWithDrag() windowC?.showWindow(nil) // if self.browser != nil { // let activeInt = self.browser.activeTabIndex() // let activeTab = self.browser.activeTabContents() // // self.browser.closeTab(at: activeInt, makeHistory: false) // // let browser: KMBrowser = KMBrowser.init() // browser.windowController = KMBrowserWindowController.init(browser: browser) // browser.addHomeTabContents() // browser.windowController.showWindow(self) // //// browser.add(activeTab, inForeground: false) //// browser.selectTab(at: 1) // if activeTab?.fileURL == nil { // return // } // let pdfDoc = CPDFDocument.init(url: (activeTab?.fileURL)!) // let document = NSDocumentController.shared.document(for: (activeTab?.fileURL)!) // KMMainDocument().tryToUnlockDocument(pdfDoc!) // // if ((pdfDoc?.isLocked)! == true) { // KMPasswordInputWindow.openWindow(window: self.window!, url: (activeTab?.fileURL)!) { result, password in // if result == .cancel { /// 关闭 // return // } // /// 解密成功 // var selectDocument: KMMainDocument? = nil // if ((document?.isKind(of: KMMainDocument.self)) != nil) { // selectDocument = (document as! KMMainDocument) // } // if selectDocument != nil { // let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument) // selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true) // if (selectDocument?.browser.window.isVisible)! as Bool { // selectDocument?.browser.window.orderFront(nil) // } else if (selectDocument?.browser.window.isMiniaturized)! as Bool { // selectDocument?.browser.window.orderFront(nil) // } // } else { // NSDocumentController.shared.km_safe_openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { document, _, error in // if (error == nil) { // (document as! KMMainDocument).mainViewController?.model.password = password // } // } // } // } // } else { // var selectDocument: KMMainDocument? = nil // if ((document?.isKind(of: KMMainDocument.self)) != nil) { // selectDocument = (document as! KMMainDocument) // } // if selectDocument != nil { // let currentIndex = selectDocument?.browser.tabStripModel.index(of: selectDocument) // selectDocument?.browser.tabStripModel.selectTabContents(at: Int32(currentIndex!), userGesture: true) // if (selectDocument?.browser.window.isVisible)! as Bool { // selectDocument?.browser.window.orderFront(nil) // } else if (selectDocument?.browser.window.isMiniaturized)! as Bool { // selectDocument?.browser.window.orderFront(nil) // } // } else { // NSDocumentController.shared.km_safe_openDocument(withContentsOf: (activeTab?.fileURL)!, display: true) { _, _, _ in // // } // } // } // } } func canMergeAllWindow() -> Bool { return self.fetchBrowserWindowControllers().count > 0 } fileprivate func fetchBrowserWindowControllers() -> [KMBrowserWindowController] { var windowArray: [KMBrowserWindowController] = [] for window in NSApp.windows { guard let windowController = window.windowController, windowController is KMBrowserWindowController else { continue } if let _tabScripC = (windowController as? KMBrowserWindowController)?.tabStripController, _tabScripC.viewsCount() <= 1 { continue } if (window.isVisible && (windowController.isEqual(to: self) == false)) { windowArray.append(windowController as! KMBrowserWindowController) } } return windowArray } func mergeAllWindow(_ sender: Any) { let windowArray = self.fetchBrowserWindowControllers() if (windowArray.count <= 0) { return } for windowC in windowArray { // 拼接窗口 [拼接窗口里的标签,除了home标签] self.append(windowC, toTabView: NSView()) if let browser = windowC.browser { if (browser.tabCount() <= 1 && browser.activeTabContents().isHome) { windowC.close() } } } } override func tabDidSelect(_ notification: Notification!) { super.tabDidSelect(notification) if let _userInfo = notification.userInfo, let _newContents = _userInfo[CTTabNewContentsUserInfoKey] as? KMMainDocument { _newContents.addWindowController(self) _newContents.mainViewController?.showMeasureFloatingWindowsIfNeed() } if let _userInfo = notification.userInfo, let _oldContents = _userInfo[CTTabContentsUserInfoKey] as? KMMainDocument { _oldContents.mainViewController?.hideMeasureFloatingWindows() } } override func layoutTabContentArea(_ frame: NSRect) { super.layoutTabContentArea(frame) self.rightStripView.autoresizingMask = [.minXMargin, .minYMargin] } override func openDocument(_ sender: Any!) { guard let _window = self.window else { KMPrint("openDocument error: window is nil.", beep: true) return } if let cnt = self.browser?.tabCount(), cnt > 1{ if !KMMemberInfo.shared.isMemberAllFunction { 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?.reopenDocument(forPaths: nil) } else { } } self.km_beginSheet(windowC: tabbingWin) } return } if KMPreference.shared.openDocumentType == .newWindow { self.reopenDocument(forPaths: nil) return } } NSOpenPanel.km_open_multi_success(_window) { panel in panel.allowedFileTypes = KMTools.imageExtensions + KMTools.pdfExtensions } completion: { urls in var imageUrls: [URL] = [] for url in urls { if KMTools.isPDFType(url.pathExtension.lowercased()) { NSDocumentController.shared.km_safe_openDocument(withContentsOf: url, display: true) { _, _, _ in } } else { imageUrls.append(url) } } if imageUrls.isEmpty == false { NSApplication.ShowImageToPDFWindow(urls: imageUrls) } } } } // MARK: - KMInterfaceThemeChangedProtocol extension KMBrowserWindowController: KMInterfaceThemeChangedProtocol { func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { Task { @MainActor in self.updateViewColor() let selector = #selector(interfaceThemeDidChanged) let mainWindow = self.window var responder = mainWindow?.firstResponder while (responder != nil) { // KMPrint("res: \(responder)") // if self.isEqual(to: responder) { responder = responder?.nextResponder continue } if let res = responder?.responds(to: selector), res { responder?.perform(selector, with: appearance) } responder = responder!.nextResponder } let childWins = self.window?.childWindows ?? [] for childWin in childWins { if let winC = childWin.windowController { if winC.responds(to: selector) { winC.perform(selector, with: appearance) } } else { if childWin.responds(to: selector) { childWin.perform(selector, with: appearance) } } } } } } // MARK: - KMToolbarRightViewDelegate extension KMBrowserWindowController: KMToolbarRightViewDelegate { func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?) { self.trackEvent_upgrade() // KMComparativeTableViewController.show(window: self.window!, .upgrade) DispatchQueue.main.async { KMProductCompareWC.shared.orientation = false KMProductCompareWC.shared.showWindow(nil) } } func userInfoButtonAction(_ sender: NSButton) { if KMLightMemberManager.manager.isLogin() { Task { @MainActor in if await KMLightMemberManager.manager.canUseAdvanced(needNetworking: true) { KMAccountInfoWindowController.show(window: self.window!) } else { KMLoginWindowController.show(window: self.window!) } } } else { KMLoginWindowController.show(window: self.window!) } } func homeRefreshButtonAction(_ sender: NSButton?) { self.layoutSubviews() } func homeUploadButtonAction(_ sender: NSButton) { } func homeMenuSortAction(_ sender: NSPopUpButton) { } func homeMenuFilterAction(_ sender: NSPopUpButton) { } } // MARK: - KMUploadFileDelegate extension KMBrowserWindowController: KMUploadFileDelegate { // override func cancelOperation(_ sender: Any?) { // // } } // MARK: - NSSearchFieldDelegate extension KMBrowserWindowController: NSSearchFieldDelegate { func controlTextDidChange(_ obj: Notification) { } func controlTextDidEndEditing(_ obj: Notification) { } func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool { var result = false return result } } // MARK: - // MARK: Menu Actions extension KMBrowserWindowController { func canResponseDocumentAction() -> Bool { if (self.browser == nil) { return false } guard let _ = self.browser.activeTabContents() as? KMMainDocument else { return false } return true } } // MARK: - // MARK: File Menu extension KMBrowserWindowController: NSMenuDelegate, NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { if (self.browser == nil) { return false } guard let action = menuItem.action else { return false } guard let document = self.browser.activeTabContents() as? KMMainDocument else { return false } if (KMSystemMenu.isFileSelector(sel: action)) { if action == KMSystemMenu.File.screenShotSelectAreaSelector || action == KMSystemMenu.File.screenShotWindowSelector || action == KMSystemMenu.File.screenShotFullScreenSelector || action == KMSystemMenu.File.screenShotFullScreenDelaySelector { return true } if (document.isHome) { if (menuItem.action == KMSystemMenu.File.closeTagPageSelector || menuItem.action == KMSystemMenu.File.propertySelector || menuItem.action == KMSystemMenu.File.showInFinderSelector || menuItem.action == KMSystemMenu.File.printSelector || menuItem.action == KMSystemMenu.File.saveAsFlattenedPDFSelector) { return false } return document.homeViewController!.validateMenuItem(menuItem) } else { return document.mainViewController!.validateMenuItem(menuItem) } } if (KMSystemMenu.isEditSelector(sel: action)) { return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem) } if (KMSystemMenu.isViewSelector(sel: action)) { if action == KMSystemMenu.Edit.changeFontSelector { return true } else { return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem) } } if (KMSystemMenu.isAnnotationSelector(sel: action)) { return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem) } if (KMSystemMenu.isPDFSelector(sel: action)) { return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem) } if (KMSystemMenu.isGotoSelector(sel: action)) { return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem) } if (KMSystemMenu.isToolSelector(sel: action)) { if action == KMSystemMenu.Tool.orderFrontNotesPanelSelector { if KMNotesPanelController.sharedControllerExists() { if let data = KMNotesPanelController.shared.window?.isVisible, data { menuItem.title = NSLocalizedString("Hide Note Type", comment: "Menu item title") } else { menuItem.title = NSLocalizedString("Show Note Type", comment: "Menu item title") } } else { menuItem.title = NSLocalizedString("Show Note Type", comment: "Menu item title") } return true } else { return document.isHome ? false : document.mainViewController!.validateMenuItem(menuItem) } } if (KMSystemMenu.isWindowSelector(sel: action)) { return document.isHome ? document.homeViewController!.validateMenuItem(menuItem) : document.mainViewController!.validateMenuItem(menuItem) } if action == #selector(redactAction) { return document.isHome ? false : true } if action == #selector(pasteAsPlainText) { return document.isHome ? false : true } if action == #selector(addWatermark) || action == #selector(templateWatermark) || action == #selector(removeWatermark) || action == #selector(batchAddWatermark) || action == #selector(batchRemoveWatermark) { return document.isHome ? false : true } if action == #selector(addBackground) || action == #selector(removeBackground) || action == #selector(batchAddBackground) || action == #selector(batchRemoveBackground) || action == #selector(templateBackground) { return document.isHome ? false : true } if action == #selector(addHeaderFooter) || action == #selector(removeHeaderFooter) || action == #selector(batchAddHeaderFooter) || action == #selector(batchRemoveHeaderFooter) || action == #selector(templateHeaderFooter) { return document.isHome ? false : true } if action == #selector(addHeaderFooterBates) || action == #selector(removeHeaderFooterBates) || action == #selector(batchAddBates) || action == #selector(batchRemoveBates) || action == #selector(templateBates) { return document.isHome ? false : true } if action == #selector(alignLeft) || action == #selector(alignCenter) || action == #selector(alignRight){ return document.isHome ? false : true } return true } } extension KMBrowserWindowController { @IBAction func menuItemAction_openFile(_ sender: Any) { super.openDocument(sender) } @IBAction func importFromFile(_ sender: Any) { let document = self.browser?.activeTabContents() as? KMMainDocument if let data = document?.isHome, data { document?.homeViewController?.importFromFile(sender) } else { self.homeVC = KMHomeViewController() self.homeVC?.importFromFile(sender) } } @IBAction func openBlankPage(_ sender: Any) { let document = self.browser?.activeTabContents() as? KMMainDocument if let data = document?.isHome, data { document?.homeViewController?.openBlankPage() } else { self.homeVC = KMHomeViewController() self.homeVC?.openBlankPage() } } @IBAction func importFromScanner(_ sender: Any) { let document = self.browser?.activeTabContents() as? KMMainDocument if let data = document?.isHome, data { document?.homeViewController?.importFromScanner() } else { self.homeVC = KMHomeViewController() self.homeVC?.importFromScanner() } } @IBAction func menuItemAction_newTab(_ sender: Any) { self.openDocumentWindow() } @IBAction func pasteAsPlainText(_ sender: Any?) { KMPrint("pasteAsPlainText") } @IBAction func deselectAll(_ sender: Any?) { KMPrint("deselectAll") } @IBAction func searchPDF(_ sender: Any?) { if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.leftSideViewController.showSearchMode("") } } } @IBAction func changeColor(_ sender: Any?) { KMPrint("changeColor ...") } @IBAction func changeAttributes(_ sender: Any?) { KMPrint("changeAttributes ...") } @IBAction func alignLeft(_ sender: Any?) { KMPrint("alignLeft ...") } @IBAction func alignRight(_ sender: Any?) { KMPrint("alignRight ...") } @IBAction func alignCenter(_ sender: Any?) { KMPrint("alignCenter ...") } @IBAction func createNewNote(_ sender: Any?) { KMPrint("createNewNote ...") } @IBAction func toggleHideNotes(_ sender: Any?) { KMPrint("toggleHideNotes ...") } @IBAction func changeDisplaySinglePages(_ sender: Any?) { KMPrint("changeDisplaySinglePages ...") } @IBAction func changeDisplayContinuous(_ sender: Any?) { KMPrint("changeDisplayContinuous ...") } @IBAction func doGoToNextPage(_ sender: Any?) { KMPrint("doGoToNextPage ...") } @IBAction func doGoToPreviousPage(_ sender: Any?) { KMPrint("doGoToPreviousPage ...") } @IBAction func doGoToFirstPage(_ sender: Any?) { KMPrint("doGoToFirstPage ...") } @IBAction func doGoToLastPage(_ sender: Any?) { KMPrint("doGoToLastPage ...") } @IBAction func allGoToNextPage(_ sender: Any?) { KMPrint("allGoToNextPage ...") } @IBAction func allGoToPreviousPage(_ sender: Any?) { KMPrint("allGoToPreviousPage ...") } @IBAction func allGoToFirstPage(_ sender: Any?) { KMPrint("allGoToFirstPage ...") } @IBAction func allGoToLastPage(_ sender: Any?) { KMPrint("allGoToLastPage ...") } @IBAction func doGoToPage(_ sender: Any?) { KMPrint("doGoToPage ...") } @IBAction func doGoBack(_ sender: Any?) { KMPrint("doGoBack ...") } @IBAction func doGoForward(_ sender: Any?) { KMPrint("doGoForward ...") } @IBAction func doZoomToPhysicalSize(_ sender: Any?) { KMPrint("doZoomToPhysicalSize ...") } @IBAction func alternateZoomToFit(_ sender: Any?) { KMPrint("alternateZoomToFit ...") } @IBAction func doZoomToSelection(_ sender: Any?) { KMPrint("doZoomToSelection ...") } @IBAction func doAutoScale(_ sender: Any?) { KMPrint("doAutoScale ...") } // @IBAction func delete(_ sender: Any?) { // KMPrint("delete ...") // } // @IBAction func paste(_ sender: Any?) { // KMPrint("paste ...") // } @IBAction func alternatePaste(_ sender: Any?) { KMPrint("alternatePaste ...") } @IBAction func copy(_ sender: Any?) { KMPrint("copy ...") } @IBAction func cut(_ sender: Any?) { KMPrint("cut ...") } @IBAction func toggleFullscreen(_ sender: Any?) { KMPrint("toggleFullscreen ...") } @IBAction func toggleRightSidePane(_ sender: Any?) { KMPrint("toggleRightSidePane ...") } @IBAction func changeRightSidePaneState(_ sender: Any?) { KMPrint("changeRightSidePaneState ...") } @IBAction func changeFindPaneState(_ sender: Any?) { KMPrint("changeFindPaneState ...") } @IBAction func toggleSplitPDF(_ sender: Any?) { KMPrint("toggleSplitPDF ...") } @IBAction func search(_ sender: Any?) { KMPrint("search ...") } @IBAction func searchNotes(_ sender: Any?) { KMPrint("searchNotes ...") } @IBAction func password(_ sender: Any?) { KMPrint("password ...") } @IBAction func toggleCaseInsensitiveSearch(_ sender: Any?) { KMPrint("toggleCaseInsensitiveSearch ...") } @IBAction func toggleWholeWordSearch(_ sender: Any?) { KMPrint("toggleWholeWordSearch ...") } @IBAction func toggleCaseInsensitiveNoteSearch(_ sender: Any?) { KMPrint("toggleCaseInsensitiveNoteSearch ...") } @IBAction func performFindPanelAction(_ sender: NSMenuItem) { var forward = true var findString: String? switch sender.tag { case 1: if let document = self.browser?.activeTabContents() as? KMMainDocument { if document.isHome == false { document.mainViewController?.toolbarController.showFindBar() } } case 3: if let document = self.browser?.activeTabContents() as? KMMainDocument { if document.isHome == false { document.mainViewController?.toolbarController.showFindString(forward: false) } } case 2: if let document = self.browser?.activeTabContents() as? KMMainDocument { if document.isHome == false { document.mainViewController?.toolbarController.showFindString(forward: true) } } case 7: if let document = self.browser?.activeTabContents() as? KMMainDocument { if document.isHome == false { document.mainViewController?.toolbarController.showAllAction() } } default: break } } @IBAction func scrollUp(_ sender: Any?) { KMPrint("scrollUp ...") } @IBAction func scrollDown(_ sender: Any?) { KMPrint("scrollDown ...") } @IBAction func scrollRight(_ sender: Any?) { KMPrint("scrollRight ...") } @IBAction func scrollLeft(_ sender: Any?) { KMPrint("scrollLeft ...") } @IBAction func selectSelectedNote(_ sender: Any?) { KMPrint("selectSelectedNote ...") } @IBAction func goToSelectedOutlineItem(_ sender: Any?) { KMPrint("goToSelectedOutlineItem ...") } @IBAction func goToSelectedFindResults(_ sender: Any?) { KMPrint("goToSelectedFindResults ...") } @IBAction func toggleSelectedSnapshots(_ sender: Any?) { KMPrint("toggleSelectedSnapshots ...") } @IBAction func selfSignProfile(_ sender: Any?) { KMPrint("selfSignProfile ...") } @IBAction func link(_ sender: Any?) { KMPrint("link ...") } @IBAction func insertPage(_ sender: Any?) { KMPrint("insertPage ...") } @IBAction func split(_ sender: Any?) { KMPrint("split ...") } @IBAction func comparison(_ sender: Any?) { KMPrint("comparison ...") } @IBAction func contentComparison(_ sender: Any?) { KMPrint("contentComparison ...") } @IBAction func coverageComparison(_ sender: Any?) { KMPrint("coverageComparison ...") } @IBAction func readMode(_ sender: Any?) { KMPrint("readMode ...") } @IBAction func changeFormType(_ sender: Any?) { KMPrint("changeFormType ...") } @IBAction func changeSelfSignType(_ sender: Any?) { KMPrint("changeSelfSignType ...") } @IBAction func clearAllForm(_ sender: Any?) { KMPrint("clearAllForm ...") } @IBAction func thumbnailSizeScaling(_ sender: Any?) { KMPrint("thumbnailSizeScaling ...") } @IBAction func redactAction(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if document.isHome == false { document.mainViewController?.menuItemAction_redact(sender) } } } } // MARK: - KMSystemFileMenuProtocol extension KMBrowserWindowController: KMSystemFileMenuProtocol { func menuItemClick_saveAsFlattenedPDF(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.menuItemClick_saveAsFlattenedPDF(sender) } } } func menuItemClick_mergePDF(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemClick_mergePDF(sender) } else { document.mainViewController?.menuItemClick_mergePDF(sender) } } } func menuItemClick_Compress(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemClick_Compress(sender) } else { document.mainViewController?.menuItemClick_Compress(sender) } } } func menuItemClick_ExportNote(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.menuItemClick_ExportNote(sender) } } } func menuItemAction_ConvertToWord(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToWord(sender) } else { document.mainViewController?.menuItemAction_ConvertToWord(sender) } } } func menuItemAction_ConvertToExcel(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToExcel(sender) } else { document.mainViewController?.menuItemAction_ConvertToExcel(sender) } } } func menuItemAction_ConvertToPPT(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToPPT(sender) } else { document.mainViewController?.menuItemAction_ConvertToPPT(sender) } } } func menuItemAction_ConvertToRTF(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToRTF(sender) } else { document.mainViewController?.menuItemAction_ConvertToRTF(sender) } } } func menuItemAction_ConvertToHTML(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToHTML(sender) } else { document.mainViewController?.menuItemAction_ConvertToHTML(sender) } } } func menuItemAction_ConvertToText(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToText(sender) } else { document.mainViewController?.menuItemAction_ConvertToText(sender) } } } func menuItemAction_ConvertToCSV(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToCSV(sender) } else { document.mainViewController?.menuItemAction_ConvertToCSV(sender) } } } func menuItemAction_ConvertToImage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_ConvertToImage(sender) } else { document.mainViewController?.menuItemAction_ConvertToImage(sender) } } } func menuItemClick_SettingPassword(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemClick_SettingPassword(sender) } else { document.mainViewController?.menuItemClick_SettingPassword(sender) } } } func menuItemClick_RemovePassword(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemClick_RemovePassword(sender) } } func menuItemAction_closeWindow(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_closeWindow(sender) } else { document.mainViewController?.menuItemAction_closeWindow(sender) } } } func menuItemAction_closeAllWindows(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_closeAllWindows(sender) } else { document.mainViewController?.menuItemAction_closeAllWindows(sender) } } } func menuItemAction_closeTagPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_closeTagPage(sender) } } func menuItemAction_showInFinder(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_showInFinder(sender) } } func menuItemAction_property(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_property(sender) } } func menuItemAction_print(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_print(sender) } } func isEncapsulatedPostScriptData(_ data: Data) -> Bool { let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5) let rg: Range = (14..<20) return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil)) } func convertTIFFDataToPDF(_ tiffData: Data) -> Data? { guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil } let pdfData = NSMutableData(capacity: tiffData.count) let consumer = CGDataConsumer(data: pdfData! as CFMutableData)! var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height)) let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil) ctxt!.beginPDFPage(nil) ctxt!.draw(cgImage, in: rect) ctxt!.endPDFPage() ctxt!.closePDF() return pdfData as? Data } func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer?) -> Any? { var document: CPDFDocument? = nil var data: Data? = nil if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) { // pboard.types data = pboard.data(forType: NSPasteboard.PasteboardType.pdf) } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) { // pboard.types data = pboard.data(forType: NSPasteboard.PasteboardType.postScript) } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) { // pboard.types data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data()) } else { let images = pboard.readObjects(forClasses: [NSImage.self], options: [:]) let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:]) if images?.count ?? 0 > 0 { data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!) } else if strings?.count ?? 0 > 0 { data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!) } } if let data = data { _ = NSDocumentController.shared document = CPDFDocument(data: data) let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String) let filePath = savePath.deletingLastPathComponent if FileManager.default.fileExists(atPath: filePath) == false { try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false) } document?.write(to: URL(fileURLWithPath: savePath)) NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), 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 } } } } else if let outError = outError { outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")]) } return document } func openDocument(withURLFromPasteboard pboard: NSPasteboard, showNotes: Bool, error outError: inout NSError?) -> Any? { let theURLs = NSURL.readURLs(from: pboard) let url = theURLs?.count ?? 0 > 0 ? theURLs?[0] : nil let theURL: NSURL? = url as? NSURL var documentC = NSDocumentController.shared var document: NSDocument? = nil if (theURL as AnyObject).isFileURL == true { var _: NSError? = nil let type = try? documentC.typeForContents(of: theURL as! URL)//ForContents(ofURL: theURL, error: &error) if showNotes == false || NSDocument.readableTypes.contains(type ?? "") { documentC.openDocument(withContentsOf: theURL as! URL, display: true, completionHandler: { resultDocument, success, err in document = resultDocument })//openDocument(withContentsOfURL: theURL, display: true, error: &outError) } else if NSDocument.readableTypes.contains(type ?? "") { for doc in documentC.documents { let sel = NSSelectorFromString("sourceFileURL") if doc.responds(to: sel) && doc.fileURL == theURL as? URL { document = doc break } } if let document: NSDocument = document { document.showWindows() } else { var data: Data? = nil // if NSWorkspace.shared.type(type ?? "", conformsToType: KMPDFBundleDocumentType) { // let skimFileURL = FileManager.default.exte // data = skimFileURL != nil ? try? Data(contentsOf: skimFileURL!) : nil // } else { // data = SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true, error: &error) // } // let documentNew: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument if let document = try? documentC.makeUntitledDocument(ofType: KMNotesDocumentType) { document.fileURL = URL(fileURLWithPath: theURL?.path ?? "") // if data == nil || document.read(fromData: data!, ofType: SKNotesDocumentType, error: &error) { documentC.addDocument(document) document.makeWindowControllers() document.showWindows() // } else { // document = nil // if let error = error { // outError = error // } // } } } } } // else if showNotes == false && theURL != nil { // document = SKDownloadController.shared().addDownload(for: theURL!) // } else if outError != nil { // outError = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")) // } return document } @IBAction func newDocumentFromClipboard(_ sender: Any?) { KMPrint("newDocumentFromClipboard") var error: NSError? let pboard = NSPasteboard.general var document = openDocumentWithImageFromPasteboard(pboard, error: &error) if document == nil{ document = openDocument(withURLFromPasteboard: pboard, showNotes: false, error: &error) } } @IBAction func newDocumentFromImage(_ sender: Any?) { createPDFFromImage() } @IBAction func importFromWebPage(_ sender: Any?) { let document = self.browser?.activeTabContents() as? KMMainDocument if let data = document?.isHome, data { document?.homeViewController?.importFromWebPage() } else { self.homeVC = KMHomeViewController() self.homeVC?.importFromWebPage() } } @IBAction 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) } // try? FileManager.default.removeItem(atPath: savePath) } else { } } } } } func checkOpenNewDocument(path: String) -> Bool { let tabCount = self.km_browser?.tabCount() ?? 0 if tabCount > 1{ if !KMMemberInfo.shared.isMemberAllFunction { 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 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 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 } @IBAction 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 { } } } } } } @IBAction 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 { } } } } } @IBAction 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 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 ?? "" } } // MARK: - KMSystemViewMenuProtocol extension KMBrowserWindowController: KMSystemViewMenuProtocol { func createPDFFromImage() { let openPanel = NSOpenPanel() openPanel.allowedFileTypes = KMTools.imageExtensions openPanel.allowsMultipleSelection = true openPanel.message = NSLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "") openPanel.beginSheetModal(for: NSWindow.currentWindow()) { response in if response == .OK { self.creatPdfFromImg(urls: openPanel.urls) } } } func creatPdfFromImg(urls: Array) { // let savePath = self.newDocumentTempSavePath("convertToPDF.pdf") let pdf = CPDFDocument() // var isSucceed = true let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String) let filePath = savePath.deletingLastPathComponent if FileManager.default.fileExists(atPath: filePath) == false { try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false) } for fileURL in urls { if let imag = NSImage(contentsOfFile: fileURL.path ) { _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0) pdf?.write(to: URL(fileURLWithPath: savePath)) } } NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), 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 } } // self.homeContentView?.reloadData() } } func createPdfWithParam() { let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String) let filePath = savePath.deletingLastPathComponent if FileManager.default.fileExists(atPath: filePath) == false { try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false) } let pdfDocument = CPDFDocument() pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0) pdfDocument?.write(to: URL(fileURLWithPath: savePath)) NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), 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 } } // self.homeContentView?.reloadData() } } 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 } } } } func newDocumentTempSavePath(_ fileName: String) -> String { let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!) let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName)) return append2 } func fetchUniquePath(_ originalPath: String) -> String { var path = originalPath let dManager = FileManager.default if !dManager.fileExists(atPath: path) { if path.extension.count < 1 { path = path.stringByAppendingPathExtension("pdf") } return path } else { let originalFullFileName = path.lastPathComponent let originalFileName = path.lastPathComponent.deletingPathExtension.lastPathComponent let originalExtension = path.extension let startIndex: Int = 0 let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1 let fileLocatePath = originalPath.substring(to: endIndex) var i = 1 while (1 != 0) { var newName = String(format: "%@%ld", originalFileName, i) newName = String(format: "%@%@", newName, originalExtension) let newPath = fileLocatePath.stringByAppendingPathComponent(newName) if !dManager.fileExists(atPath: newPath) { return newPath } else { i+=1 continue } } } } func menuItemAction_adjustWidth(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_adjustWidth(sender) } } func menuItemAction_adjustPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_adjustPage(sender) } } func menuItemAction_size(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_size(sender) } } func menuItemAction_zoomOut(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_zoomOut(sender) } } func menuItemAction_zoomIn(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_zoomIn(sender) } } func menuItemAction_singlePage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_singlePage(sender) } } func menuItemAction_singlePageContinue(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_singlePageContinue(sender) } } func menuItemAction_doublePage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_doublePage(sender) } } func menuItemAction_doublePageContinue(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_doublePageContinue(sender) } } func menuItemAction_bookMode(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_bookMode(sender) } } func menuItemAction_readMode(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_readMode(sender) } } func menuItemAction_showSplitPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_showSplitPage(sender) } } func menuItemAction_rotateLeft(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_rotateLeft(sender) } } func menuItemAction_rotateRight(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_rotateRight(sender) } } func menuItemAction_rotateAllPageLeft(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_rotateAllPageLeft(sender) } } func menuItemAction_rotateAllPageRight(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_rotateAllPageRight(sender) } } func menuItemAction_view_readMode(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_view_readMode(sender) } } func menuItemAction_hiddenLeftSide(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_hiddenLeftSide(sender) } } func menuItemAction_hiddenRightSide(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_hiddenRightSide(sender) } } func menuItemAction_thumai(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_thumai(sender) } } func menuItemAction_outline(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_outline(sender) } } func menuItemAction_bookmark(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_bookmark(sender) } } func menuItemAction_annotation(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_annotation(sender) } } func menuItemAction_search(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_search(sender) } } func menuItemAction_hiddenPageIndicator(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_hiddenPageIndicator(sender) } } @IBAction func togglePresentation(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.togglePresentation(sender) } } @IBAction func chooseTransition(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.chooseTransition(sender) } } @IBAction func toggleAutoFlow(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleAutoFlow(sender) } } @IBAction func chooseAutoFlowSetting(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.chooseAutoFlowSetting(sender) } } @IBAction func toggleReadingBar(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleReadingBar(sender) } } @IBAction func toggleLeftSidePane(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleLeftSidePane(sender) } } @IBAction func changeLeftSidePaneState(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.changeLeftSidePaneState(sender) } } @IBAction func splitViewAction(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.splitViewAction(sender) } } @IBAction func themesColor(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.themesColor(sender) } } @IBAction func toggleStatusBar(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleStatusBar(sender) } } @IBAction func toggleToolbarShow(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleToolbarShow(sender) } } } // MARK: - KMSystemEditMenuProtocol extension KMBrowserWindowController: KMSystemEditMenuProtocol { func menuItemAction_find(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_find(sender) } } @IBAction func changeFont(_ sender: Any?) { KMPrint("changeFont ...") } } // MARK: - Annotation Menu extension KMBrowserWindowController { @IBAction func menuItemAction_highlight(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_highlight(sender) } } @IBAction func menuItemAction_underline(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_underline(sender) } } @IBAction func menuItemAction_deleteLine(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_deleteLine(sender) } } @IBAction func menuItemAction_freehand(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_freehand(sender) } } @IBAction func menuItemAction_text(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_text(sender) } } @IBAction func menuItemAction_note(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_note(sender) } } @IBAction func menuItemAction_squre(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_squre(sender) } } @IBAction func menuItemAction_circle(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_circle(sender) } } @IBAction func menuItemAction_arrow(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_arrow(sender) } } @IBAction func menuItemAction_line(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_line(sender) } } // link @IBAction func menuItemAction_link(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_link(sender) } } @IBAction func menuItemAction_linkPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_linkPage(sender) } } // stamp @IBAction func menuItemAction_stamp(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_stamp(sender) } } @IBAction func notesLink(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.notesLink(sender) } } @IBAction func notesStamp(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.notesStamp(sender) } } @IBAction func menuItemAction_signure(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_signure(sender) } } @IBAction func image(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.image(sender) } } @IBAction func table(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.table(sender) } } @IBAction func addForm(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.addForm(sender) } } @IBAction func editNote(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.editNote(sender) } } @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_hiddenAllAnnotation(sender) } } @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_clearAllAnnotation(sender) } } } // MARK: - KMSystemPDFMenuProtocol extension KMBrowserWindowController: KMSystemPDFMenuProtocol { @IBAction func changeDisplayMode(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.changeDisplayMode(sender) } } @IBAction func toggleDisplayAsBook(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleDisplayAsBook(sender) } } @IBAction func toggleDisplayPageBreaks(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleDisplayPageBreaks(sender) } } @IBAction func changeDisplayBox(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.changeDisplayBox(sender) } } @IBAction func toggleAutoScale(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.toggleAutoScale(sender) } } @IBAction func doZoomToActualSize(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.doZoomToActualSize(sender) } } @IBAction func doZoomToFit(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.doZoomToFit(sender) } } @IBAction func doZoomIn(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.doZoomIn(sender) } } @IBAction func doZoomOut(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.doZoomOut(sender) } } @IBAction func doMarqueeZoomTool(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.doMarqueeZoomTool(sender) } } @IBAction func highlightFormFiled(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.highlightFormFiled(sender) } } @IBAction func highlightLinks(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.highlightLinks(sender) } } @IBAction func resetForm(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.resetForm(sender) } } @IBAction func removeAllAnnotation(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.removeAllAnnotation(sender) } } @IBAction func savePDFSettingToDefaults(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.savePDFSettingToDefaults(sender) } } } // MARK: - KMSystemGotoMenuProtocol extension KMBrowserWindowController: KMSystemGotoMenuProtocol { func menuItemAction_nextPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_nextPage(sender) } } func menuItemAction_forwardPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_forwardPage(sender) } } func menuItemAction_firstPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_firstPage(sender) } } func menuItemAction_lastPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_lastPage(sender) } } func menuItemAction_forward(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_forward(sender) } } func menuItemAction_goback(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_goback(sender) } } func menuItemAction_gotoPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_gotoPage(sender) } } @IBAction func goToMarkedPage(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.goToMarkedPage(sender) } } @IBAction func markPage(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.markPage(sender) } } } // MARK: - KMSystemToolMenuProtocol extension KMBrowserWindowController: KMSystemToolMenuProtocol { @IBAction func getInfo(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.getInfo(sender) } } func orderFrontNotesPanel(_ sender: Any?) { let windowC = KMNotesPanelController.shared if let win = windowC.window { if win.isVisible { win.orderOut(sender) } else { win.orderFront(sender) } } } func menuItemAction_scrolTool(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_scrolTool(sender) } } func menuItemAction_zoomOutTool(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_zoomOutTool(sender) } } func menuItemAction_selectTool(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument, document.isHome == false { document.mainViewController?.menuItemAction_selectTool(sender) } } @IBAction func changeToolMode(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.changeToolMode(sender) } } } @IBAction func compression(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.compression(sender) } } } @IBAction func merge(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.merge(sender) } } } @IBAction func convert(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.convert(sender) } } } @IBAction func imageToPDF(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.imageToPDF(sender) } } } @IBAction func ocr(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.ocr(sender) } } } @IBAction func extractImage(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.extractImage(sender) } } } @IBAction func pageEdit(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.pageEdit(sender) } } } @IBAction func textEditAction(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.textEditAction(sender) } } } @IBAction func security(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.security(sender) } } } @IBAction func decryption(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.decryption(sender) } } } @IBAction func cropAll(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.cropAll(sender) } } } @IBAction func crop(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.crop(sender) } } } @IBAction func autoCropAll(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.autoCropAll(sender) } } } @IBAction func smartAutoCropAll(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.smartAutoCropAll(sender) } } } @IBAction func cropOptionsModel(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { // document.mainViewController?.cropOptionsModel(sender) } } } @IBAction func selectToolModel(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.selectToolModel(sender) } } } @IBAction func takeSnapshot(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.takeSnapshot(sender) } } } @IBAction func takeSnapshotSelectContent(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.takeSnapshotSelectContent(sender) } } } @IBAction func changeAnnotationMode(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.changeAnnotationMode(sender) } } } @IBAction func addBackground(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.addBackground(sender) } } } @IBAction func removeBackground(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.removeBackground(sender) } } } @IBAction func addWatermark(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.addWatermark(sender) } } } @IBAction func editWatermark(_ sender: Any?) { KMPrint("editWatermark ...") } @IBAction func batchAddWatermark(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchAddWatermark(sender) } } } @IBAction func batchRemoveWatermark(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchRemoveWatermark(sender) } } } @IBAction func removeWatermark(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.removeWatermark(sender) } } } @IBAction func templateWatermark(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.templateWatermark(sender) } } } @IBAction func batchAddBackground(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchAddBackground(sender) } } } @IBAction func batchRemoveBackground(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchRemoveBackground(sender) } } } @IBAction func templateBackground(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.templateBackground(sender) } } } @IBAction func addHeaderFooter(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.addHeaderFooter(sender) } } } @IBAction func removeHeaderFooter(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.removeHeaderFooter(sender) } } } @IBAction func batchAddHeaderFooter(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchAddHeaderFooter(sender) } } } @IBAction func batchRemoveHeaderFooter(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchRemoveHeaderFooter(sender) } } } @IBAction func templateHeaderFooter(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.templateHeaderFooter(sender) } } } @IBAction func addHeaderFooterBates(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.addHeaderFooterBates(sender) } } } @IBAction func removeHeaderFooterBates(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.removeHeaderFooterBates(sender) } } } @IBAction func batchAddBates(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchAddBates(sender) } } } @IBAction func batchRemoveBates(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.batchRemoveBates(sender) } } } @IBAction func templateBates(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.templateBates(sender) } } } @IBAction func togglePoster(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.togglePoster(sender) } } } @IBAction func toggleMultiple(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.toggleMultiple(sender) } } } @IBAction func toggleBooklet(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.toggleBooklet(sender) } } } @IBAction func rotateRight(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.rotateRight(sender) } } } @IBAction func rotateLeft(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.rotateLeft(sender) } } } @IBAction func rotateAllRight(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.rotateAllRight(sender) } } } @IBAction func rotateAllLeft(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.rotateAllLeft(sender) } } } @IBAction func autoSelectContent(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.autoSelectContent(sender) } } } } // MARK: - KMSystemWindowMenuRrotocol extension KMBrowserWindowController: KMSystemWindowMenuRrotocol { func menuItemAction_showForwardTagPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_showForwardTagPage(sender) } else { document.mainViewController?.menuItemAction_showForwardTagPage(sender) } } } func menuItemAction_showNextTagPage(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_showNextTagPage(sender) } else { document.mainViewController?.menuItemAction_showNextTagPage(sender) } } } func menuItemAction_newTagPageToNewWindow(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_newTagPageToNewWindow(sender) } else { document.mainViewController?.menuItemAction_newTagPageToNewWindow(sender) } } } func menuItemAction_mergeAllWindow(_ sender: Any) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { document.homeViewController?.menuItemAction_mergeAllWindow(sender) } else { document.mainViewController?.menuItemAction_mergeAllWindow(sender) } } } @IBAction func performFit(_ sender: Any?) { if (self.canResponseDocumentAction() == false) { return } if let document = self.browser?.activeTabContents() as? KMMainDocument { if (document.isHome) { } else { document.mainViewController?.performFit(sender) } } } } // MARK: - Analytics (埋点) extension KMBrowserWindowController { func trackEvent_upgrade() -> Void { KMAnalytics.trackEvent(eventName: "Btn_Tbr_Upgrade", parameters: [ KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.tbr, KMAnalytics.Parameter.labelKey : KMAnalytics.Label.tbr_Btn], platform: .AppCenter, appTarget: .all) } }