123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870 |
- //
- // KMLeftSideViewController+Action.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/12/22.
- //
- import Foundation
- // MARK: - Action
- extension KMLeftSideViewController {
- @IBAction func leftSideViewMoreButtonAction(_ sender: AnyObject?) {
- guard let button = sender as? NSButton else {
- NSSound.beep()
- return
- }
- let tag = button.tag
- if (tag == 300) {
- var selectedRow = 0
- if (self.snapshotTableView.selectedRow >= 0) {
- selectedRow = self.snapshotTableView.selectedRow;
- } else {
- return
- }
- let model = self.snapshots[selectedRow]
- let controller = model.windowC
- let menu = NSMenu()
- let itemExport = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
- let subMenu = NSMenu()
- var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
- t?.representedObject = controller
- t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
- t?.representedObject = controller
- t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
- t?.representedObject = controller
- itemExport?.submenu = subMenu
- let itemPrint = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
- itemPrint?.representedObject = controller
- menu.addItem(.separator())
- let itemSelectAll = menu.addItem(title: KMLocalizedString("Select All", "Menu item title"), action: #selector(menuItemClick_SelectAll), target: self)
- itemSelectAll?.representedObject = controller
- menu.addItem(.separator())
- let itemDeleteAllSnapshot = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
- itemDeleteAllSnapshot?.representedObject = controller
- if let data = NSApp.currentEvent {
- NSMenu.popUpContextMenu(menu, with: data, for: button)
- }
- } else if (tag == 302) {
- let menu = NSMenu()
- let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(toc_expandAllComments), target: self)
- expandAllCommentsItem?.representedObject = self.tocOutlineView
- let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(toc_foldAllComments), target: self)
- expandAllCommentsItem?.representedObject = self.tocOutlineView
- let item = self.outlineRoot()
- var num = 0
- for i in 0 ..< Int(item?.numberOfChildren ?? 0) {
- let outline = item?.child(at: UInt(i))
- if self.tocOutlineView.isItemExpanded(outline) {
- num += 1
- }
- }
- if let cnt = item?.numberOfChildren, cnt > 0 && num == 0 {
- self.tocType = .fold
- } else if let cnt = item?.numberOfChildren, cnt > 0 && num == cnt {
- self.tocType = .unfold
- } else {
- self.tocType = .none
- }
- if (self.tocType == .unfold) {
- expandAllCommentsItem?.state = .on
- foldAllCommentsItem?.state = .off
- } else if (self.tocType == .fold) {
- expandAllCommentsItem?.state = .off
- foldAllCommentsItem?.state = .on
- } else {
- expandAllCommentsItem?.state = .off
- foldAllCommentsItem?.state = .off
- }
- let removeEntryItem = menu.addItem(title: KMLocalizedString("Remove All Outlines", nil), action: #selector(leftSideEmptyAnnotationClick_DeleteOutline), target: self)
- removeEntryItem?.representedObject = self.tocOutlineView
- if let data = NSApp.currentEvent {
- NSMenu.popUpContextMenu(menu, with: data, for: button)
- }
- } else if (tag == 304) {
- let menu = NSMenu()
- let object = KMPopupMenuObject()
- object.menuTag = 1001
- menu.delegate = object
- object.enterControllerCallback = { [weak self] isEnter in
- if (isEnter) {
- self?.moreButtonLayer?.isHidden = false
- } else {
- self?.moreButtonLayer?.isHidden = true
- }
- }
- let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
- expandAllCommentsItem?.representedObject = self.noteOutlineView
- let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
- foldAllCommentsItem?.representedObject = self.noteOutlineView
- if (self.foldType == .unfold) {
- expandAllCommentsItem?.state = .on
- foldAllCommentsItem?.state = .off
- } else if (self.foldType == .fold) {
- expandAllCommentsItem?.state = .off
- foldAllCommentsItem?.state = .on
- } else {
- expandAllCommentsItem?.state = .off
- foldAllCommentsItem?.state = .off
- }
- let showAnnotationItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
- let subMenu = NSMenu()
- var t = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
- let pageKey = self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false
- if pageKey {
- t?.state = .off
- } else {
- t?.state = .on
- }
- t?.representedObject = self.noteOutlineView
- t?.tag = 101
- t = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
- let timeKey = self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false
- if timeKey {
- t?.state = .off
- } else {
- t?.state = .on
- }
- t?.representedObject = self.noteOutlineView
- t?.tag = 102
- t = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
- let authorKey = self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false
- if authorKey {
- t?.state = .off
- } else {
- t?.state = .on
- }
- t?.representedObject = self.noteOutlineView
- t?.tag = 103
- showAnnotationItem?.submenu = subMenu
- menu.addItem(.separator())
- let exportAnnotationsItem = menu.addItem(title: NSLocalizedString("Export Annotations…", comment: ""), action: nil, target: self)
- let subMenu2 = NSMenu()
- var t2 = subMenu2.addItem(title: NSLocalizedString("PDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 0
- t2 = subMenu2.addItem(title: NSLocalizedString("PDF Bundle", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 1
- t2 = subMenu2.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 2
- t2 = subMenu2.addItem(title: NSLocalizedString("Notes as Text", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 3
- t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 4
- t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTFD", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 5
- t2 = subMenu2.addItem(title: NSLocalizedString("Notes as FDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- t2?.representedObject = self.noteOutlineView
- t2?.tag = 6
- exportAnnotationsItem?.submenu = subMenu2
- menu.addItem(.separator())
- let removeAllAnnotationsItem = menu.addItem(title: NSLocalizedString("Remove All Annotations", comment: ""), action: #selector(leftSideEmptyAnnotationClick_DeleteAnnotation), target: self)
- removeAllAnnotationsItem?.representedObject = self.noteOutlineView
- if let data = NSApp.currentEvent {
- NSMenu.popUpContextMenu(menu, with: data, for: button, with: nil)
- }
- }
- }
-
- func searchFieldChangeAction(_ editContent: String) {
- if self.findState == .note {
- self.updateNoteFilterPredicate()
- } else {
- self.updateSnapshotFilterPredicate()
- }
- if editContent.count > 0 {
- let findPboard = NSPasteboard(name: .find)
- findPboard.clearContents()
- findPboard.writeObjects([editContent as NSPasteboardWriting])
- }
- }
-
- @IBAction func search(_ sender: NSSearchField) {
- if sender.stringValue.isEmpty {
- self.applySearchTableHeader("")
- }
- self.delegate?.searchAction?(searchString: sender.stringValue, isCase: self.mwcFlags.caseInsensitiveSearch == 1)
- }
-
- // RefreshUI
-
- public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
- self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
- self.note_reloadDataIfNeed()
- }
-
- public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
- if let data = anno {
- self.note_reloadDataForAnnoIfNeed(anno: data)
- }
- }
-
- public func refreshUIForDocumentChanged() {
- if self.type.methodType == .Thumbnail {
- self.resetThumbnails(ks: false)
- } else if self.type.methodType == .Outline {
- Task { @MainActor in
- self.tocOutlineView.reloadData()
- }
- } else if self.type.methodType == .Annotation {
- self.note_reloadDataIfNeed()
- } else if self.type.methodType == .snapshot {
- self.reloadSnapshotDataIfNeed()
- } else if self.type.methodType == .Search {
- if self.searchField.stringValue.isEmpty == false {
- self.search(self.searchField)
- }
- }
- }
- }
- // MARK: - Double Action
- extension KMLeftSideViewController {
- @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
- let indexs = self.snapshotTableView.selectedRowIndexes
- if indexs.isEmpty {
- return
- }
- let model = self.snapshots[indexs.last!]
- let windowC = model.windowC
- if let data = windowC?.window?.isVisible, data {
- windowC?.miniaturize()
- } else {
- windowC?.deminiaturize()
- }
-
- var rowIndexSet = IndexSet()
- let row = self.snapshotTableView.selectedRow
- if row >= 0 && row < self.snapshots.count {
- rowIndexSet.insert(row)
- }
- var columnIndexSet = IndexSet()
- columnIndexSet.insert(0)
- self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
- }
-
- // MARK: - KMInterfaceThemeChangedProtocol
-
- override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
- super.interfaceThemeDidChanged(appearance)
-
- self.updateViewColor()
- self.leftView.interfaceThemeDidChanged(appearance)
- }
- }
- // MARK: - NSMenuDelegate
- extension KMLeftSideViewController: NSMenuDelegate {
- func menuNeedsUpdate(_ menu: NSMenu) {
- menu.removeAllItems()
- if menu.isEqual(to: self.tocOutlineView.menu) {
- self.outlineListMenu(menu)
- return
- }
- var item: NSMenuItem?
- if menu.isEqual(to: self.thumbnailTableView.menu) {
- let row = self.thumbnailTableView.clickedRow
- if self.pdfDocument()?.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
- return
- }
- let isLocked = self.isLocked()
- let pageCount = self.pageCount()
- if (row != -1 && isLocked == false) {
- if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
- item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
- } else {
- item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
- }
- item?.representedObject = IndexSet(integer: row)
-
-
- item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
- item?.representedObject = IndexSet(integer: row)
-
- if (self.copyPages.count > 0) {
- item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
- }else{
- item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
- }
- item?.representedObject = IndexSet(integer: row)
- menu.addItem(.separator())
- if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
- item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
- } else {
- item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
- }
- item?.representedObject = IndexSet(integer: row)
- menu.addItem(.separator())
- item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
- item?.representedObject = IndexSet(integer: row)
- item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
- let subMenu = NSMenu()
- _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
- _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
- _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
- item?.submenu = subMenu
- item?.representedObject = IndexSet(integer: row)
- item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
- item?.representedObject = IndexSet(integer: row)
- item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
- menu.addItem(.separator())
- var displayStr = ""
- if (self.isDisplayPageSize) {
- displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
- } else {
- displayStr = KMLocalizedString("Display Page Size", "Menu item title")
- }
- item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
- item?.representedObject = IndexSet(integer: row)
-
- if let doct = self.pdfDocument() {
- item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
-
- var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
- if (tName.count > 50) {
- tName = tName.substring(to: 50)
- }
- item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
- }
- }
- } else if menu.isEqual(to: self.findTableView.menu) {
- var rowIndexes = self.findTableView.selectedRowIndexes
- let row = self.findTableView.clickedRow
- if (row != -1) {
- if rowIndexes.contains(row) == false {
- rowIndexes = IndexSet(integer: row)
- }
-
- var selections: [CPDFSelection] = []
- for (i, data) in self.searchResults.enumerated() {
- if rowIndexes.contains(i) {
- selections.append(data.selection)
- }
- }
- let hideNotes = self.hideNotes()
- let allowsNotes = self.allowsNotes()
- if hideNotes == false && allowsNotes {
- item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
- item?.representedObject = selections
- }
- }
- } else if menu.isEqual(to: self.groupedFindTableView.menu) {
- var rowIndexes = self.groupedFindTableView.selectedRowIndexes
- let row = self.groupedFindTableView.clickedRow
- if (row != -1) {
- if rowIndexes.contains(row) == false {
- rowIndexes = IndexSet(integer: row)
- }
- // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
- var selections: [CPDFSelection] = []
- for (i, data) in self.groupSearchResults.enumerated() {
- if rowIndexes.contains(i) {
- for searchM in data.datas {
- selections.append(searchM.selection)
- }
- }
- }
- item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
- item?.representedObject = selections
- menu.addItem(.separator())
- let hideNotes = self.hideNotes()
- let allowsNotes = self.allowsNotes()
- if hideNotes == false && allowsNotes {
- item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
- item?.representedObject = selections
- item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
- item?.representedObject = selections
- }
- }
- } else if menu.isEqual(to: self.snapshotTableView.menu) {
- let row = self.snapshotTableView.clickedRow
- if (row != -1) {
- let model = self.snapshots[row]
- let controller = model.windowC
- if let data = controller?.window?.isVisible, data {
- item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
- item?.representedObject = controller
- } else {
- item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
- item?.representedObject = controller
- }
- menu.addItem(.separator())
- item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
- let subMenu = NSMenu()
- var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
- t?.representedObject = controller
- t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
- t?.representedObject = controller
-
- t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
- t?.representedObject = controller
- item?.submenu = subMenu
- item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
- item?.representedObject = controller
- menu.addItem(.separator())
-
- item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
- item?.representedObject = controller
- menu.addItem(.separator())
- item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
- item?.representedObject = controller
- item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
- item?.representedObject = controller
- }
- } else if menu.isEqual(to: self.noteOutlineView.menu) {
- var items: NSArray?
- var rowIndexes = self.noteOutlineView.selectedRowIndexes
- let row = self.noteOutlineView.clickedRow
- if (row != -1) {
- if rowIndexes.contains(row) == false {
- rowIndexes = IndexSet(integer: row)
- }
- items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
-
- // PDFAnnotation *foldNote = (PDFAnnotation *)notes[row];
- // let foldNote = self.allAnnotations[row]
- guard let foldNote = self.fetchNote(for: row) else {
- return
- }
- var isFold = true
- if self.allFoldNotes.contains(foldNote) {
- isFold = false
- }
- item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
- if (isFold) {
- item?.state = .off
- } else {
- item?.state = .on
- }
- item?.representedObject = items
- item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
- if (isFold) {
- item?.state = .on
- } else {
- item?.state = .off
- }
- item?.representedObject = items
-
- menu.addItem(.separator())
- let hideNotes = self.hideNotes()
- if hideNotes == false && (items?.count ?? 0) == 1 {
- let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
- if let data = annotation?.isEditable(), data {
- if annotation?.type == nil {
- let isNote = annotation?.isNote() ?? false
- if isNote {
- // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
- item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
- item?.representedObject = annotation
- }
- } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
- // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
- item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
- item?.representedObject = annotation
- } else {
- item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
- item?.representedObject = annotation
- item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
- item?.representedObject = annotation
- item?.keyEquivalentModifierMask = [.option]
- item?.isAlternate = true
- }
- }
- }
- if menu.numberOfItems > 0 {
- item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
-
- let subMenu = NSMenu()
- item?.submenu = subMenu
- item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 0
- item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 1
- item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 2
- item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 3
- item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 4
- item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 5
- item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 6
-
- menu.addItem(.separator())
- if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
- item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
- item?.representedObject = items
- }
- item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
- }
- } else {
- let subMenu = NSMenu()
- item?.submenu = subMenu
- item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 0
- item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 1
- item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 2
- item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 3
- item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 4
- item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 5
- item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
- item?.tag = 6
-
- item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
- }
- }
- }
- }
- // MARK: - NSMenuItemValidation
- extension KMLeftSideViewController: NSMenuItemValidation {
- func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
- let isLocked = self.isLocked()
- if isLocked {
- return false
- }
- let action = menuItem.action
- if (action == #selector(toggleCaseInsensitiveSearch)) {
- let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
- menuItem.state = state == 1 ? .on : .off
- return true
- } else if (action == #selector(toggleWholeWordSearch)) {
- menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
- return true
- } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
- menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
- return true
- }
- if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
- action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
- action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
- action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
- action == #selector(outlineContextMenuItemClicked_Edit) ||
- action == #selector(outlineContextMenuItemClicked_SetDestination) ||
- action == #selector(outlineContextMenuItemClicked_Rename) ||
- action == #selector(outlineContextMenuItemClicked_Promote) ||
- action == #selector(outlineContextMenuItemClicked_Demote)) {
- return self.outlineListValidateMenuItem(menuItem)
- } else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
- menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
- return true
- }
- // } else if (action == @selector(note_expandAllComments:) ||
- // action == @selector(note_foldAllComments:) ||
- // action == @selector(exportAnnotationNotes:) ||
- // action == @selector(leftSideEmptyAnnotationClick_DeleteAnnotation:) ||
- // action == @selector(removeAllAnnotations:)) {
- // if (@available(macOS 10.13, *)) {
- // if (notes.count == 0) {
- // return NO;
- // } else {
- // return YES;
- // }
- // } else {
- // if (action == @selector(note_expandAllComments:) ||
- // action == @selector(note_foldAllComments:)) {
- // return NO;
- // }
- // }
- // } else
- if (action == #selector(unfoldNoteAction) ||
- action == #selector(foldNoteAction)) {
- let row = self.noteOutlineView.clickedRow
- // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
- // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
- // let foldNote = self.allAnnotations[row]
- let foldNote = self.fetchNote(for: row)
- // SKNPDFAnnotationNote
- if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
- return true
- } else {
- return false
- }
- } else if (action == #selector(menuItemClick_ExportPNG) ||
- action == #selector(menuItemClick_ExportJPG) ||
- action == #selector(menuItemClick_ExportPDF)) {
- if (self.snapshotTableView.selectedRow == -1 ) {
- return false
- } else {
- return true
- }
- }
- // else if (action == @selector(editNoteFromTable:)) {
- // NSInteger row = [rightSideController.noteOutlineView clickedRow];
- // NSArray * noteArr = [rightSideController.noteArrayController arrangedObjects];
- // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
- // if (@available(macOS 10.13, *)) {
- // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
- // return NO;
- // }
- // }
- // if ([foldNote isKindOfClass:[PDFAnnotationStamp class]] ||
- // [foldNote isKindOfClass:[KMAnnotationStamp class]]) {
- // return NO;
- // } else {
- // return YES;
- // }
- // }
- else if (action == #selector(menuItemClick_SelectAll)) {
- menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
- return true
- }
- // else if (action == @selector(leftSideEmptyAnnotationClick_DeleteOutline:)) {
- // PDFOutline * item = [[pdfView document] outlineRoot];
- // if (self.isSearchOutlineMode) {
- // NSInteger num = 0;
- // for (NSUInteger i = 0; i < [item numberOfChildren]; i++) {
- // PDFOutline *outline = [item childAtIndex:i];
- // if ([self hasContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:outline]) {
- // num ++;
- // }
- // }
- // if (num > 0)
- // return YES;
- // else
- // return NO;
- // } else {
- // if ([item numberOfChildren] > 0)
- // return YES;
- // else
- // return NO;
- // }
- // }
- return true
- }
- }
- // MARK: - NSPopoverDelegate
- extension KMLeftSideViewController: NSPopoverDelegate {
- func popoverWillClose(_ notification: Notification) {
- guard let popover = notification.object as? NSPopover else {
- return
- }
- if let vc = popover.contentViewController as? KMOutlineEditViewController {
- self.editOutlineUI(vc)
- self.tocOutlineView.reloadData()
- }
- }
- }
- // MARK: - NSSearchFieldDelegate
- extension KMLeftSideViewController: NSSearchFieldDelegate {
- func controlTextDidChange(_ obj: Notification) {
- if self.outlineSearchField.isEqual(to: obj.object) {
- if (self.outlineSearchField.stringValue.isEmpty == false) {
- self.isSearchOutlineMode = true
-
- self.tocOutlineView.reloadData()
- self.tocOutlineView.expandItem(nil, expandChildren: true)
- self.tocType = .unfold
- } else {
- self.isSearchOutlineMode = false
- self.showSearchOutlineBlankState(false)
- self.tocOutlineView.reloadData()
- }
- self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
- } else if self.snapshotSearchField.isEqual(to: obj.object) {
- let searchString = self.snapshotSearchField.stringValue
- // NSPredicate *filterPredicate = nil;
- // if ([searchString length] > 0) {
- // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
- // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
- // NSUInteger options = NSDiacriticInsensitivePredicateOption;
- // if (mwcFlags.caseInsensitiveNoteSearch)
- // options |= NSCaseInsensitivePredicateOption;
- // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
- // }
- // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
- // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
- self.searchSnapshots.removeAll()
- if searchString.isEmpty {
- self.isSearchSnapshotMode = false
- self.searchSnapshots = self.snapshots.filter({ model in
- let data = model.windowC?.string.contains(searchString) ?? false
- return data
- })
- } else {
- self.isSearchSnapshotMode = true
- }
- var snapshots = self.searchSnapshots
- if self.isSearchSnapshotMode == false {
- snapshots = self.snapshots
- }
- Task { @MainActor in
- self.updataLeftSideSnapView()
- self.snapshotTableView.reloadData()
- }
- if (snapshots.count > 0) {
- self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
- } else {
- let view = self.snapshotTableView.enclosingScrollView
- let viewFrmae = view?.frame ?? .zero
- let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
- self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
- self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
- self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
- }
- } else if self.noteSearchField.isEqual(to: obj.object) {
- if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
- self.noteSearchMode = !isEmpty
- } else {
- self.noteSearchMode = false
- }
- }
- }
- }
- // MARK: - NSTextFieldDelegate
- extension KMLeftSideViewController: NSTextFieldDelegate {
- func controlTextDidBeginEditing(_ obj: Notification) {
- if self.noteOutlineView.isEqual(to: obj.object) {
- // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
- // [[self document] objectDidBeginEditing:self];
- // mwcFlags.isEditingTable = YES;
- }
- }
-
- func controlTextDidEndEditing(_ obj: Notification) {
- if self.noteOutlineView.isEqual(to: obj.object) {
- // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
- // [[self document] objectDidEndEditing:self];
- // mwcFlags.isEditingTable = NO;
- } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
- let tf = obj.object as! NSTextField
- if let ol = self.renamePDFOutline {
- if self.isRenameNoteOutline == false {
- if tf.stringValue == ol.label {
- return
- }
- }
- self.renamePDFOutline(ol, label: tf.stringValue)
- self.updateSelectRowHeight()
- self.tocOutlineView.reloadData()
- }
- }
- }
- }
- // MARK: - Menu Item Actions
- extension KMLeftSideViewController {
- @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
- self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
- if self.searchField.stringValue.isEmpty == false {
- self.searchNotes(self.searchField)
- }
- KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
- }
-
- @objc func searchNotes(_ sender: AnyObject?) {
- self.noteSearchMode = false
- if self.findState == .note {
- if self.noteSearchField.isEqual(to: sender) {
- self.noteSearchMode = true
- }
- self.updateNoteFilterPredicate()
- } else {
- self.updateSnapshotFilterPredicate()
- }
- let textfield = sender as? NSSearchField
- if let data = textfield?.stringValue.isEmpty, data == false {
- let findPboard = NSPasteboard(name: .find)
- findPboard.clearContents()
- // findPboard.writeObjects([textfield!.stringValue])
- }
- }
-
- func updateSnapshotFilterPredicate() {
- let searchString = self.snapshotSearchField.stringValue
- self.searchSnapshots.removeAll()
- if self.findState == .snapshot && searchString.isEmpty == false {
- self.searchSnapshots = self.snapshots.filter({ model in
- let data = model.windowC?.string.contains(searchString) ?? false
- return data
- })
- }
- Task { @MainActor in
- self.updataLeftSideSnapView()
- self.snapshotTableView.reloadData()
- }
- }
-
- func snapshotListIsAllSelected() -> Bool {
- if self.snapshots.isEmpty {
- return false
- }
- for model in self.snapshots {
- if model.isSelected == false {
- return false
- }
- }
- return true
- }
- }
|