123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- //
- // KMMainViewController+UI.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2022/12/15.
- //
- import Foundation
- extension KMMainViewController {
- // MARK: - 退出全屏
-
-
- @IBAction func startSpeaking(_ sender: Any) {
- self.showTTSWindow()
- let ttsView = KMTTSWindowController.share
- ttsView.buttonItemClick_Play(ttsView.playButton)
- }
-
- @IBAction func stopSpeaking(_ sender: Any) {
- let ttsWindowC = KMTTSWindowController.share
- if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
- if let data = ttsWindowC.window?.isVisible, data {
- ttsWindowC.stopSpeaking()
- ttsWindowC.close()
- }
- }
- }
- }
- extension KMMainViewController {
- //通知
- func preferenceDidChangeNotification(notification:Notification) {
- let info : [AnyHashable : Any] = notification.userInfo ?? [:]
-
- if info.keys.contains(KMPreference.viewZoomScaleTypeKey) {
- self.selectZoom(KMPreferenceManager.shared.viewZoomScaleType)
- }
-
- if notification.name.rawValue == "pdfViewDocumentDidLoaded" || info.keys.contains(KMPreference.viewPageDisplayTypeKey) {
- }
-
- if info.keys.contains(KMPreference.displayBackgroundNormalColorKey) || notification.name.rawValue == "pdfViewDocumentDidLoaded" {
- self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
- self.listView.layoutDocumentView()
- }
-
- if (info.keys.contains(KMPreference.generalAuthorNameKey)) { // 作者名称
- CPDFKitConfig.sharedInstance().setAnnotationAuthor((info[KMPreference.generalAuthorNameKey] as! String))
- }
- if (info.keys.contains(KMPreference.highlightLinksKey)) {
- let hlLink = info[KMPreference.highlightLinksKey] as? Bool
- CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(hlLink == nil ? false : hlLink!)
- self.listView.setNeedsDisplayForVisiblePages()
- }
- if info.keys.contains(KMPreference.thumbPageSizeKey) {
- self.leftSideViewController.refreshUIOfThumbnailIfNeed(preference: true)
- }
- if info.keys.contains(KMPreference.thumbSnapshotSizeKey) {
- self.leftSideViewController.refreshUIOfSnapshotIfNeed(preference: true)
- }
- if info.keys.contains(KMPreference.outlineFontSizeKey) {
- self.leftSideViewController.updateTableFont()
- }
- if info.keys.contains(KMPreference.greekThresholdKey) {
- let value = KMPreference.shared.greekThreshold.cgFloat
- self.listView.setGreekingThreshold(value)
- }
- if info.keys.contains(KMPreference.antiAliasTextKey) {
- self.listView.setShouldAntiAlias(KMPreference.shared.antiAliasText)
- self.listView.applyDefaultInterpolationQuality()
- }
- if info.keys.contains(KMPreference.markupColorHighlightKey) {
-
- }
- if info.keys.contains(KMPreference.markupColorUnderlineKey) {
-
- }
- if info.keys.contains(KMPreference.markupColorStrikthroughKey) {
-
- }
- if info.keys.contains(KMPreference.markupColorPenKey) {
-
- }
- if info.keys.contains(KMPreference.editPDFPopWindowShowKey) {
- let show = KMPreference.shared.editPDFPopWindowIsShow
- let win = KMEditPDFPopToolBarWindow.shared
- if !show && win.isVisible {
- win.orderOut(nil)
- win.setIsVisible(false)
- }
- }
- }
-
- func showConvertNotesProgress() {
- guard let pdfDocument = self.document else {
- return
- }
- guard let _ = self.listView.document else {
- return
- }
- guard let _ = self.view.window else {
- return
- }
-
- let isNewCreated = (self.myDocument as? KMMainDocument)?.isNewCreated ?? false
- if isNewCreated {
- return
- }
-
- self.model.needConvertNotes = false
- Task { @MainActor in
- self.convertNotesUsingPDFDocument(pdfDocument)
- }
- }
-
- func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument, callback: (()->Void)? = nil) {
- guard let doc = self.listView.document else {
- return
- }
- DispatchQueue.main.async {
- self.beginProgressSheet(withMessage: "", maxValue: 0)
-
- let count = pdfDocument.pageCount
- DispatchQueue.global().async {
- self.model.addAnnotations.removeAll()
- self.model.removeAnnotations.removeAll()
- for i in 0..<count {
- let page = pdfDocument.page(at: i)
-
- for annotation in page?.annotations ?? [] {
- var newAnnotation: CPDFAnnotation?
- if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
- let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: doc)
- table.border = inkAnnotation.border
- table.color = inkAnnotation.color
- table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
- table.updateAppearanceInk(withIsAdd: false)
- table.contents = annotation.contents
- newAnnotation = table
- }
- if let newAnnotation = newAnnotation {
- self.model.addAnnotations.append(newAnnotation)
- self.model.removeAnnotations.append(annotation)
- }
- }
-
- }
-
- DispatchQueue.main.async {
- for i in 0..<self.model.addAnnotations.count {
- let newAnnotation = self.model.addAnnotations[i]
- let annotation = self.model.removeAnnotations[i]
-
- let page = annotation.page
- // this is only to make sure markup annotations generate the lineRects, for thread safety
- self.listView.addAnnotation(with: newAnnotation, to: page)
- self.listView.remove(annotation)
- if newAnnotation.contents != nil {
- if newAnnotation.contents.count == 0 {
- newAnnotation.autoUpdateString()
- }
- }
- }
-
- self.dismissProgressSheet()
-
- self.listView.undoManager?.removeAllActions()
- self.undoManager?.removeAllActions()
-
- // 清空数据
- self.model.addAnnotations.removeAll()
- self.model.removeAnnotations.removeAll()
-
- callback?()
- }
- }
- }
- }
-
- // MARK: - KMInterfaceThemeChangedProtocol
-
- override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
- super.interfaceThemeDidChanged(appearance)
-
- self.leftSideViewController.interfaceThemeDidChanged(appearance)
-
- self.pdfEditController?.interfaceThemeDidChanged(appearance)
-
- }
- }
- // MARK: - KMToolbarControllerDelegate
- extension KMMainViewController: KMToolbarControllerDelegate {
- func toolbarController(_ viewController: KMToolbarController, heightOffsetChange heightOffset: Float, animated: Bool) {
-
- }
-
- func changeAnnotationModeAction(item: KMToolbarClickButton) {
- if self.listView.toolMode == .formToolMode {
- if !IAPProductsManager.default().isAvailableAllFunction(){
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- if let type = CAnnotationType(rawValue: item.tag) {
- if type == .textField {
- winC?.kEventName = "Reading_TextField_BuyNow"
- } else if type == .checkBox {
- winC?.kEventName = "Reading_CheckBox_BuyNow"
- } else if type == .radioButton {
- winC?.kEventName = "Reading_RadioButton_BuyNow"
- } else if type == .listMenu {
- winC?.kEventName = "Reading_ListBox_BuyNow"
- } else if type == .comboBox {
- winC?.kEventName = "Reading_ComboBox_BuyNow"
- } else if type == .actionButton {
- winC?.kEventName = "Reading_Button_BuyNow"
- }
- }
- winC?.showWindow(nil)
- return
- }
- } else {
- let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
- if type == .signature {
- if !IAPProductsManager.default().isAvailableAllFunction(){
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.kEventName = "Reading_ElectronicSign_BuyNow"
- winC?.showWindow(nil)
- return
- }
- }
- }
-
- if (self.model.rightMouseEventing) {
- self.model.rightMouseEventing = false
- }
-
- var currentType = self.listView.annotationType
-
- let isSelected = (currentType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown) && ((self.listView.toolMode == .noteToolMode) || (self.listView.toolMode == .selfSignMode) || (self.listView.toolMode == .formToolMode))
- var editSelectd = false
- if self.listView.annotationType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown && self.listView.toolMode == .editPDFToolMode {
- editSelectd = true
- }
-
- if isSelected {
- if self.listView.toolMode == .formToolMode {
-
- } else {
- self.listView.toolMode = .textToolMode
- }
- let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
- if type == .signature {
-
- if self.listView.toolMode == .formToolMode {
- self.listView.toolMode = .noteToolMode
- }
- }
- self.listView.annotationType = .unkown
- toggleCloseRightSide()
- } else {
- let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
- self.trackEvent(annotationType: type)
- if type == .signature {
- if self.listView.toolMode != .formToolMode {
- self.listView.toolMode = .formToolMode
-
- }
- }
- self.listView.annotationType = type
-
- if self.listView.currentSelection != nil {
- let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown, selection: self.listView.currentSelection, page: self.listView.currentSelection.page, bounds: self.listView.currentSelection.bounds)
- if((annotation != nil) &&
- !self.listView.activeAnnotations.contains(annotation ?? self.listView.activeAnnotation)) {
- var newAnnonations : [CPDFAnnotation] = []
- newAnnonations.append(annotation!)
- self.listView.updateActiveAnnotations(newAnnonations)
- self.listView.setNeedsDisplayAnnotationViewFor(annotation?.page)
- }
- }
-
- if self.listView.toolMode == .formToolMode {
-
- } else {
- self.refreshListViewToolMode()
- }
- }
-
- if self.listView.toolMode != .editPDFToolMode && self.listView.annotationType != .unkown {
-
- let toolMode = self.listView.toolMode ?? .none
-
- if !viewManager.isPDFReadMode {
- toggleOpenRightSide()
- }
- } else if self.listView.toolMode == .editPDFToolMode {
- if editSelectd {
- self.listView.setShouAddEdit([])
- self.listView.change([.text, .image])
- self.listView.annotationType = .editTextImage
- toggleCloseRightSide()
- } else if self.listView.annotationType == .addText {
- if !viewManager.isPDFReadMode {
- toggleOpenRightSide()
- }
- FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_EditPDF_AddText"])
- self.listView.setShouAddEdit(.text)
- self.listView.change(.text)
- } else if self.listView.annotationType == .addImage {
- if !viewManager.isPDFReadMode {
- toggleOpenRightSide()
- }
- FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_Btn" : "Btn_SubTbr_EditPDF_AddImage"])
-
- self.listView.setShouAddEdit(.image)
- self.listView.change(.image)
- }
- } else {
- toggleCloseRightSide()
- }
- }
-
- func refreshListViewToolMode () {
-
- }
-
- func showPDFLayoutModeAction(show: Bool) {
- self.listView.annotationType = .unkown
- self.showOrHideNotes()
- }
- }
-
- // MARK: - KMLeftSideViewControllerDelegate
- extension KMMainViewController: KMLeftSideViewControllerDelegate {
- func controlStateChange(_ obj: KMLeftSideViewController, show: Bool) {
- if show {
- self.openLeftPane()
- } else {
- self.closeLeftPane()
- }
- }
-
- func enterEditMode(_ obj: KMLeftSideViewController, _ pages: [Int]) {
- }
-
- func searchAction(searchString: String,isCase:Bool) {
- self.search(searchString: searchString,isCase: isCase)
- }
-
- func controller(controller: KMLeftSideViewController, itemClick item: Any?, itemKey: KMItemKey, params: Any?) {
- if (itemKey == .print) { // 打印
- if params is NSImage {
- KMPrintWindowController.cpdf_printImage(image: params as! NSImage)
- return
- }
- var pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [], reversePrintOrder: false)
- if (params != nil) {
- pageRange.type = .custom
- pageRange.selectPages = params as! [Int]
- }
-
- self.showPrintWindow(pageRange: pageRange)
- } else if (itemKey == .cut || itemKey == .paste || itemKey == .delete || itemKey == .leftRotate || itemKey == .rightRotate) {
- self.recordIsPDFDocumentEdited(type: itemKey.toSubscribeWaterMarkType())
- } else if itemKey == .pageEdit {
- } else if itemKey == .demote { // 大纲降级
- guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
- return
- }
- self.listView.demote(outline: currentOutline)
- } else if itemKey == .promote { // 大纲升级
- guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
- return
- }
- self.listView.promote(outline: currentOutline)
- } else if itemKey == .tts {
- FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageDisplay", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageDisplay_TTS"])
- self.listView.annotationType = .unkown
- self.showTTSWindow()
- }
- }
-
- func controller(controller: KMLeftSideViewController, bookMarkDidChange bookMarks: [KMBookMarkItem]) {
- self.needSave = true
- self.recordIsPDFDocumentEdited()
- }
-
- func controller(controller: KMLeftSideViewController, rotateType: KMRotateType) {
- if rotateType == .clockwise {
- self.menuItemAction_rotateRight((Any).self)
- } else if rotateType == .anticlockwise {
- self.menuItemAction_rotateLeft((Any).self)
- }
- }
-
- func controller(controller: KMLeftSideViewController, listViewSelectionDidChange object: Any?, info: [String : Any]?) {
- if controller.thumbnailTableView.isEqual(to: object) {
- let row = controller.thumbnailTableView.selectedRow
- let curIndex = self.listView.currentPageIndex
- if (row != -1 && row != curIndex) {
- self.listView.go(toPageIndex: row, animated: true)
- }
- }
- }
- }
- //MARK: - ReadModel
- extension KMMainViewController {
-
-
- @objc func addOutLineItemAction() {
- let labelString = "\(KMLocalizedString("Page")) \((self.listView.currentPageIndex ?? 0) + 1)"
- let label = self.listView.currentSelection?.string() ?? labelString
- let dest = self.listView.currentDestination
- if let row = self.leftSideViewController.selectedRowIndexes().last {
- if let ol = self.leftSideViewController.tocOutlineView.item(atRow: row) as? CPDFOutline {
- _ = self.listView.addOutline(for: ol.parent, label: label, dest: dest, at: ol.index+1)
- } else {
- let idx = self.listView.document?.outlineRoot()?.numberOfChildren ?? 0
- _ = self.listView.addOutlineForRoot(label: label, dest:dest, at: idx)
- }
- } else {
- let idx = self.listView.document?.outlineRoot()?.numberOfChildren ?? 0
- _ = self.listView.addOutlineForRoot(label: label, dest:dest, at: idx)
- }
- }
-
- @objc func showTTSView() {
- FMTrackEventManager.defaultManager.trackEvent(event: "RightClickBar", withProperties: ["RightClickBar_Btn": "Btn_RightClickBar_TTS"])
-
- self.showTTSWindow()
- }
-
- @objc func showShareAction() {
-
- }
-
- @objc func lookUpAction() {
- let label = self.listView.currentSelection?.string() ?? ""
-
- }
-
- @objc func searchBaiduAction() {
- let label = self.listView.currentSelection?.string() ?? ""
- let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
- if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
- NSWorkspace.shared.open(url)
- }
- }
-
- @objc func showInfoInFinder() {
-
- }
-
- @objc func NextPageAction() {
-
- }
-
- @objc func PreviousPageAction() {
-
- }
-
- @objc func TranslateItemAction() {
- // 获取选中的文本
- if let selection = self.listView.currentSelection?.string() {
- // 进行翻译
- let escapedText = selection.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
- let urlStr = "https://translate.google.com/?sl=auto&tl=zh-CN&text=\(escapedText)"
- if let url = URL(string: urlStr) {
- NSWorkspace.shared.open(url)
- }
- } else {
- print("No text selected.")
- }
- }
- @objc func AutoScrollItemAction() {
- //增加判断,如果是正在滚动,就停止,否则就开始滚动
- self.listView.autoFlow()
- }
-
-
- @objc func cutAction(sender: NSMenuItem) {
-
- }
- @objc func deleteAction(sender: NSMenuItem) {
-
- }
- @objc func ColorsItemAction(sender: NSMenuItem) {
-
- }
- @objc func LinesItemAction(sender: NSMenuItem) {
-
- }
- @objc func EditNoteItemAction(sender: NSMenuItem) {
-
- }
- @objc func AITranslateItemAction(sender: NSMenuItem) {
- self.showAITypeChooseView(aiConfigType: .translate)
- }
- @objc func AIProofreadItemAction(sender: NSMenuItem) {
- self.showAITypeChooseView(aiConfigType: .proofreading)
- }
- @objc func AIRewriteItemAction(sender: NSMenuItem) {
- self.showAITypeChooseView(aiConfigType: .reWriting)
- }
-
- }
-
- // MARK: - KMSnapshotWindowControllerDelegate
- extension KMMainViewController: KMSnapshotWindowControllerDelegate {
- func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
- self.leftSideViewController.snapshotControllerWillClose(controller)
- }
-
- func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
- if isMiniaturize && self.interactionMode != .presentation {
- if self.interactionMode != .legacyFullScreen && self.model.leftPanelOpen == false {
- self.toggleLeftPane()
- } else if self.interactionMode == .legacyFullScreen {
- }
- var row = NSNotFound
- for (i,sp) in self.leftSideViewController.snapshots.enumerated() {
- if controller.isEqual(sp.windowC) {
- row = i
- }
- }
- if (row != NSNotFound) {
- if self.leftSideViewController.type.methodType != .snapshot {
- self.leftSideViewController.leftView.segmentedControl.selectedSegment = 5
- } else {
- self.leftSideViewController.refreshUIOfSnapshotIfNeed()
- }
- self.leftSideViewController.snapshotTableView.scrollRowToVisible(row)
- }
- }
-
- return self.leftSideViewController.snapshotController(controller, miniaturizedRect: isMiniaturize)
- }
-
- func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
- self.leftSideViewController.snapshotControllerDidFinishSetup(controller)
- }
- }
- // MARK: - CPDFDocumentDelegate
- extension KMMainViewController: CPDFDocumentDelegate {
- func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
- self.leftSideViewController.documentDidBeginFind()
- }
-
- func documentDidEndDocumentFind(_ document: CPDFDocument!) {
- self.leftSideViewController.documentDidEndFind()
- }
- }
- // MAKR: - CPDFViewDelegate,CPDFListViewDelegate
|