|
| 1 | +// |
| 2 | +// FindViewController+Delegate.swift |
| 3 | +// CodeEditSourceEditor |
| 4 | +// |
| 5 | +// Created by Austin Condiff on 4/3/25. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | +import CodeEditTextView |
| 10 | + |
| 11 | +extension FindViewController: FindPanelDelegate { |
| 12 | + func findPanelOnSubmit() { |
| 13 | + findPanelNextButtonClicked() |
| 14 | + } |
| 15 | + |
| 16 | + func findPanelOnDismiss() { |
| 17 | + if isShowingFindPanel { |
| 18 | + hideFindPanel() |
| 19 | + // Ensure text view becomes first responder after hiding |
| 20 | + if let textViewController = target as? TextViewController { |
| 21 | + DispatchQueue.main.async { |
| 22 | + _ = textViewController.textView.window?.makeFirstResponder(textViewController.textView) |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + func findPanelDidUpdate(_ text: String) { |
| 29 | + // Check if this update was triggered by a return key without shift |
| 30 | + if let currentEvent = NSApp.currentEvent, |
| 31 | + currentEvent.type == .keyDown, |
| 32 | + currentEvent.keyCode == 36, // Return key |
| 33 | + !currentEvent.modifierFlags.contains(.shift) { |
| 34 | + return // Skip find for regular return key |
| 35 | + } |
| 36 | + |
| 37 | + // Only perform find if we're focusing the text view |
| 38 | + if let textViewController = target as? TextViewController, |
| 39 | + textViewController.textView.window?.firstResponder === textViewController.textView { |
| 40 | + // If the text view has focus, just clear visual emphases but keep matches in memory |
| 41 | + target?.emphasisManager?.removeEmphases(for: "find") |
| 42 | + // Re-add the current active emphasis without visual emphasis |
| 43 | + if let emphases = target?.emphasisManager?.getEmphases(for: "find"), |
| 44 | + let activeEmphasis = emphases.first(where: { !$0.inactive }) { |
| 45 | + target?.emphasisManager?.addEmphasis( |
| 46 | + Emphasis( |
| 47 | + range: activeEmphasis.range, |
| 48 | + style: .standard, |
| 49 | + flash: false, |
| 50 | + inactive: false, |
| 51 | + select: true |
| 52 | + ), |
| 53 | + for: "find" |
| 54 | + ) |
| 55 | + } |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // Clear existing emphases before performing new find |
| 60 | + target?.emphasisManager?.removeEmphases(for: "find") |
| 61 | + find(text: text) |
| 62 | + } |
| 63 | + |
| 64 | + func findPanelPrevButtonClicked() { |
| 65 | + guard let textViewController = target as? TextViewController, |
| 66 | + let emphasisManager = target?.emphasisManager else { return } |
| 67 | + |
| 68 | + // Check if there are any matches |
| 69 | + if findMatches.isEmpty { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // Update to previous match |
| 74 | + let oldIndex = currentFindMatchIndex |
| 75 | + currentFindMatchIndex = (currentFindMatchIndex - 1 + findMatches.count) % findMatches.count |
| 76 | + |
| 77 | + // Show bezel notification if we cycled from first to last match |
| 78 | + if oldIndex == 0 && currentFindMatchIndex == findMatches.count - 1 { |
| 79 | + BezelNotification.show( |
| 80 | + symbolName: "arrow.trianglehead.bottomleft.capsulepath.clockwise", |
| 81 | + over: textViewController.textView |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + // If the text view has focus, show a flash animation for the current match |
| 86 | + if textViewController.textView.window?.firstResponder === textViewController.textView { |
| 87 | + let newActiveRange = findMatches[currentFindMatchIndex] |
| 88 | + |
| 89 | + // Clear existing emphases before adding the flash |
| 90 | + emphasisManager.removeEmphases(for: "find") |
| 91 | + |
| 92 | + emphasisManager.addEmphasis( |
| 93 | + Emphasis( |
| 94 | + range: newActiveRange, |
| 95 | + style: .standard, |
| 96 | + flash: true, |
| 97 | + inactive: false, |
| 98 | + select: true |
| 99 | + ), |
| 100 | + for: "find" |
| 101 | + ) |
| 102 | + |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + // Create updated emphases with new active state |
| 107 | + let updatedEmphases = findMatches.enumerated().map { index, range in |
| 108 | + Emphasis( |
| 109 | + range: range, |
| 110 | + style: .standard, |
| 111 | + flash: false, |
| 112 | + inactive: index != currentFindMatchIndex, |
| 113 | + select: index == currentFindMatchIndex |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + // Replace all emphases to update state |
| 118 | + emphasisManager.replaceEmphases(updatedEmphases, for: "find") |
| 119 | + } |
| 120 | + |
| 121 | + func findPanelNextButtonClicked() { |
| 122 | + guard let textViewController = target as? TextViewController, |
| 123 | + let emphasisManager = target?.emphasisManager else { return } |
| 124 | + |
| 125 | + // Check if there are any matches |
| 126 | + if findMatches.isEmpty { |
| 127 | + // Show "no matches" bezel notification and play beep |
| 128 | + NSSound.beep() |
| 129 | + BezelNotification.show( |
| 130 | + symbolName: "arrow.down.to.line", |
| 131 | + over: textViewController.textView |
| 132 | + ) |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + // Update to next match |
| 137 | + let oldIndex = currentFindMatchIndex |
| 138 | + currentFindMatchIndex = (currentFindMatchIndex + 1) % findMatches.count |
| 139 | + |
| 140 | + // Show bezel notification if we cycled from last to first match |
| 141 | + if oldIndex == findMatches.count - 1 && currentFindMatchIndex == 0 { |
| 142 | + BezelNotification.show( |
| 143 | + symbolName: "arrow.triangle.capsulepath", |
| 144 | + over: textViewController.textView |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + // If the text view has focus, show a flash animation for the current match |
| 149 | + if textViewController.textView.window?.firstResponder === textViewController.textView { |
| 150 | + let newActiveRange = findMatches[currentFindMatchIndex] |
| 151 | + |
| 152 | + // Clear existing emphases before adding the flash |
| 153 | + emphasisManager.removeEmphases(for: "find") |
| 154 | + |
| 155 | + emphasisManager.addEmphasis( |
| 156 | + Emphasis( |
| 157 | + range: newActiveRange, |
| 158 | + style: .standard, |
| 159 | + flash: true, |
| 160 | + inactive: false, |
| 161 | + select: true |
| 162 | + ), |
| 163 | + for: "find" |
| 164 | + ) |
| 165 | + |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + // Create updated emphases with new active state |
| 170 | + let updatedEmphases = findMatches.enumerated().map { index, range in |
| 171 | + Emphasis( |
| 172 | + range: range, |
| 173 | + style: .standard, |
| 174 | + flash: false, |
| 175 | + inactive: index != currentFindMatchIndex, |
| 176 | + select: index == currentFindMatchIndex |
| 177 | + ) |
| 178 | + } |
| 179 | + |
| 180 | + // Replace all emphases to update state |
| 181 | + emphasisManager.replaceEmphases(updatedEmphases, for: "find") |
| 182 | + } |
| 183 | + |
| 184 | + func findPanelUpdateMatchCount(_ count: Int) { |
| 185 | + findPanel.updateMatchCount(count) |
| 186 | + } |
| 187 | + |
| 188 | + func findPanelClearEmphasis() { |
| 189 | + target?.emphasisManager?.removeEmphases(for: "find") |
| 190 | + } |
| 191 | +} |
0 commit comments