123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // KMLeftSideViewController+Search.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/12/24.
- //
- import Foundation
- private let _kRelevanceColumnId = NSUserInterfaceItemIdentifier(rawValue: "relevance")
- // MARK: - Methods
- extension KMLeftSideViewController {
- func applySearchTableHeader(_ message: String) {
- self.findTableView.tableColumn(withIdentifier: kResultsColumnId)?.headerCell.stringValue = message
- self.findTableView.headerView?.needsDisplay = true
- self.groupedFindTableView.tableColumn(withIdentifier: _kRelevanceColumnId)?.headerCell.stringValue = message
- self.groupedFindTableView.headerView?.needsDisplay = true
- }
-
- func documentDidBeginFind() {
- // [leftSideController applySearchTableHeader:[NSLocalizedString(@"Searching", @"Message in search table header") stringByAppendingEllipsis]];
- self.applySearchTableHeader(KMLocalizedString("Searching", "Message in search table header"))
- self.searchResults = []
- self.groupSearchResults = []
- }
-
- func documentDidEndFind() {
- self.applySearchTableHeader(KMLocalizedString("\(self.searchResults.count) Results", "Message in search table header"))
- }
-
- func updateFindResultHighlightsForDirection(_ direction: NSWindow.SelectionDirection) {
- var findResults: [KMSearchMode] = []
- if self.findPaneState == .singular && self.findTableView.window != nil {
- findResults = self.searchResults
- } else if self.findPaneState == .grouped && self.groupedFindTableView.window != nil {
- findResults = self.groupSearchResults[self.groupedFindTableView.clickedRow].datas
- }
-
- if (findResults.count == 0) {
- self.listView?.setHighlightedSelections(nil)
- } else {
- if direction == .directSelection {
- self.searchResultIndex = 0
- } else if (direction == .selectingNext) {
- self.searchResultIndex += 1
- if self.searchResultIndex >= findResults.count {
- self.searchResultIndex = 0
- }
- } else if (direction == .selectingPrevious) {
- self.searchResultIndex -= 1
- if self.searchResultIndex < 0 {
- self.searchResultIndex = findResults.count-1
- }
- }
- let currentSel = findResults[self.searchResultIndex].selection
- if currentSel.hasCharacters() {
- let page = currentSel.safeFirstPage()
- var rect = NSZeroRect
- for model in findResults {
- if let data = page, model.selection.pages().contains(data) {
- rect = NSUnionRect(rect, model.selection.bounds(for: data))
- }
- }
- let FIND_RESULT_MARGIN = 50.0
- rect = NSIntersectionRect(NSInsetRect(rect, -FIND_RESULT_MARGIN, -FIND_RESULT_MARGIN), page?.bounds(for: .cropBox) ?? .zero)
- self.listView?.go(to: page)
- self.listView?.go(to: rect, on: page)
- }
-
- if self.findPaneState == .grouped && self.groupedFindTableView.window != nil {
- var sels: [CPDFSelection] = []
- for model in findResults {
- model.selection.setColor(NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5))
- sels.append(model.selection)
- // model.selection.setColor(.yellow)
- }
- self.listView?.setHighlightedSelections(sels)
- }
-
- if currentSel.hasCharacters() {
- let bColor = NSColor(red: 236/255.0, green: 241/255.0, blue: 83/255.0, alpha: 0.5)
- let color = NSColor(red: 219/255.0, green: 220/255.0, blue: 3/255.0, alpha: 0.5)
- self.listView?.setHighlight(currentSel, forBorderColor: .clear, fill: color, animated: true)
- self.listView?.go(to: currentSel, animated: true)
- self.listView?.setCurrentSelection(currentSel, animate: true)
- }
- let mode = self.listView?.toolMode ?? .none
- if mode == .moveToolMode || mode == .magnifyToolMode || mode == .selectToolMode {
- self.listView?.setCurrentSelection(nil, animate: false)
- }
- }
- }
- }
- // MARK: - Actions
- extension KMLeftSideViewController {
- // 显示搜索模块
- func showSearchMode(_ searchString: String = "") {
- if self.leftView.segmentedControl.selectedSegment == 5 {
-
- } else {
- self.leftView.segmentedControl.selectedSegment = 5
- }
-
- self.searchViewController.searchField.stringValue = searchString
- self.findTableView.reloadData()
- }
-
- func search_initSubViews() {
- self.findTableView.delegate = self
- self.findTableView.dataSource = self
- self.findTableView.botaDelegate = self
- self.findTableView.menu?.delegate = self
- self.findTableView.target = self
- self.findTableView.doubleAction = #selector(goToSelectedFindResults)
-
- self.groupedFindTableView.delegate = self
- self.groupedFindTableView.dataSource = self
- self.groupedFindTableView.botaDelegate = self
- self.groupedFindTableView.menu?.delegate = self
-
- self.groupedFindTableView.target = self
- self.groupedFindTableView.doubleAction = #selector(goToSelectedFindResults)
- }
-
- func search_initDefalutValue() {
- self.mwcFlags.wholeWordSearch = KMDataManager.ud_integer(forKey: SKWholeWordSearchKey)
- self.mwcFlags.caseInsensitiveSearch = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
-
- self.findTableView.backgroundColor = KMAppearance.Layout.l0Color()
- self.groupedFindTableView.backgroundColor = KMAppearance.Layout.l0Color()
-
- self.findTableView.tableColumn(withIdentifier: kPageColumnId)?.headerCell.title = KMLocalizedString("Page", "Table header title")
- self.groupedFindTableView.tableColumn(withIdentifier: kPageColumnId)?.headerCell.title = KMLocalizedString("Page", "Table header title")
- (self.groupedFindTableView.tableColumn(withIdentifier: _kRelevanceColumnId)?.dataCell as? NSCell)?.isEnabled = false
- }
-
- public func refreshUIOfSeachListIfNeed() {
- if self.type.methodType != .Search {
- return
- }
- Task { @MainActor in
- if self.searchField.stringValue.isEmpty == false {
- self.search(self.searchField)
- }
- }
- }
- }
- // MARK: - Menu
- extension KMLeftSideViewController {
- @objc func addAnnotationsForSelections(_ sender: NSMenuItem) {
- guard let selections = sender.representedObject as? [CPDFSelection], selections.count > 0 else {
- return
- }
- for selection in selections {
- self.listView?.addAnnotation(with: CAnnotationType(rawValue: sender.tag) ?? .circle, selection: selection, page: selection.page, bounds: selection.bounds)
- }
- }
-
- @objc func selectSelections(_ sender: NSMenuItem) {
- guard let selections = sender.representedObject as? [CPDFSelection], selections.count > 0 else {
- return
- }
- self.listView?.currentSelection = CPDFSelection.selectionByAddingSelections(selections as NSArray)
- }
-
- @objc func toggleWholeWordSearch(_ sender: AnyObject?) {
- if self.mwcFlags.wholeWordSearch == 1 {
- self.mwcFlags.wholeWordSearch = 0
- } else {
- self.mwcFlags.wholeWordSearch = 1
- }
- if self.searchField.stringValue.isEmpty == false {
- self.search(self.searchField)
- }
- KMDataManager.ud_set(self.mwcFlags.wholeWordSearch, forKey: SKWholeWordSearchKey)
- }
-
- @objc func toggleCaseInsensitiveSearch(_ sender: AnyObject?) {
- if self.mwcFlags.caseInsensitiveSearch == 0 {
- self.mwcFlags.caseInsensitiveSearch = 1
- } else {
- self.mwcFlags.caseInsensitiveSearch = 0
- }
-
- if self.searchField.stringValue.isEmpty == false {
- self.search(self.searchField)
- }
- KMDataManager.ud_set(self.mwcFlags.caseInsensitiveSearch, forKey: SKCaseInsensitiveSearchKey)
- }
-
- @objc func goToSelectedFindResults(_ sender: AnyObject?) {
- guard let olView = sender as? NSTableView, olView.clickedRow != -1 else {
- NSSound.beep()
- return
- }
- self.updateFindResultHighlightsForDirection(.directSelection)
- }
- }
|