// // 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")) self.searchResults = [] self.groupSearchResults = [] } func documentDidEndFind() { self.applySearchTableHeader(KMLocalizedString("\(self.searchResults.count) Results")) } 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 == 0 { } else { self.leftView.segmentedControl.selectedSegment = 0 } 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") self.groupedFindTableView.tableColumn(withIdentifier: kPageColumnId)?.headerCell.title = KMLocalizedString("Page") (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) } }