import AppKit import HelderCore /// The pane's own paint: change marks, the read-only caret row, the jump flash, the ⌘ underline and the gutter. extension CodeEditorController { /// The caret row is only tinted in the read-only panes; the editable buffer shows the caret alone. private var caretLine: Int? { guard !textView.isEditable, let doc = document else { return nil } let selection = textView.selectedRange() guard selection.length == 0 else { return nil } return doc.lineIndex.line(containing: selection.location) } /// Behind the text, from the code edge: Actual's tint and teal rule, gap rules, the caret row, the flash. func drawRowBackgrounds(in rect: NSRect) { let width = max(textView.bounds.width, rect.maxX) let caret = caretLine let flash = flashStrength forEachRow(in: rect) { row in let band = NSRect(x: 0, y: row.rect.minY, width: width, height: row.rect.height) let mark = marks[row.line] if let mark, mark.tint, let tone = mark.tone { EditorColors.fill(tone).setFill() band.fill() EditorColors.tone(tone).setFill() NSRect(x: 0, y: band.minY, width: Metrics.accentRule, height: band.height).fill() } else if mark == nil, row.line == caret { EditorColors.activeLine.setFill() band.fill() } if row.line == flashLine, flash > 0 { EditorColors.flash.withAlphaComponent(EditorColors.flash.alphaComponent * flash).setFill() band.fill(using: .sourceOver) EditorColors.amber.withAlphaComponent(flash).setFill() NSRect(x: 0, y: band.minY, width: Metrics.accentRule, height: band.height).fill(using: .sourceOver) } drawGapRule(mark, row: row, width: width) } } private func drawGapRule(_ mark: CodeLineMark?, row: Row, width: CGFloat) { guard let gap = mark?.gap else { return } let y: CGFloat switch gap { case .above: guard row.isFirst else { return } y = row.rect.minY case .below: guard row.isLast else { return } y = row.rect.maxY - Metrics.accentRule } EditorColors.removedText.setFill() NSRect(x: 0, y: y, width: width, height: Metrics.accentRule).fill() } /// Over the text while ⌘ is held: an amber underline 3pt under the baseline of each clickable class name. func drawSymbolUnderlines(in rect: NSRect) { guard symbolsUnderlined, configuration?.knownSymbols.isEmpty == false, let doc = document else { return } let origin = textView.textContainerOrigin let visible = layout.glyphRange(forBoundingRect: rect.offsetBy(dx: -origin.x, dy: -origin.y), in: textContainer) let characters = layout.characterRange(forGlyphRange: visible, actualGlyphRange: nil) let baseline = EditorFonts.baseline(scale: fontScale) EditorColors.amber.setFill() for range in doc.symbolRanges where NSMaxRange(range) <= doc.length && NSIntersectionRange(range, characters).length > 0 { let glyphs = layout.glyphRange(forCharacterRange: range, actualCharacterRange: nil) layout.enumerateEnclosingRects(forGlyphRange: glyphs, withinSelectedGlyphRange: NSRange(location: NSNotFound, length: 0), in: textContainer) { box, _ in let fragment = self.layout.lineFragmentRect(forGlyphAt: glyphs.location, effectiveRange: nil) let y = fragment.minY + baseline + 3 + origin.y NSRect(x: box.minX + origin.x, y: y, width: box.width, height: 1).fill() } } } /// Numbers only: a changed line in Actual lifts its number to teal; the read-only caret row lifts its number. func drawGutter(in rect: NSRect, of ruler: CodeGutterView) { EditorColors.background.setFill() rect.fill() let font = EditorFonts.code(scale: fontScale) let lineHeight = EditorFonts.lineHeight(scale: fontScale) let caret = caretLine let thickness = ruler.ruleThickness forEachRow(in: textView.visibleRect) { row in let y = ruler.convert(NSPoint(x: 0, y: row.rect.minY), from: textView).y let band = NSRect(x: 0, y: y, width: thickness, height: row.rect.height) guard band.intersects(rect) else { return } let mark = marks[row.line] var color = mark == nil && row.line == caret ? EditorColors.lineNumberActive : EditorColors.lineNumber if let mark, mark.tint, let tone = mark.tone { color = EditorColors.tone(tone) } guard row.isFirst else { return } let label = String(row.line) as NSString let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color] let size = label.size(withAttributes: attributes) let baseline = (lineHeight - (font.ascender - font.descender)) / 2 label.draw(at: NSPoint(x: thickness - 14 - size.width, y: band.minY + baseline), withAttributes: attributes) } } // MARK: The jump flash /// Full strength for the first 65% of the flash, then fading out. private var flashStrength: CGFloat { guard flashLine != nil, let start = flashStart else { return 0 } let elapsed = Date().timeIntervalSince(start) / EditorFonts.flashDuration if elapsed < 0.65 { return 1 } return max(0, 1 - (elapsed - 0.65) / 0.35) } func startFlash(on line: Int) { flashLine = line flashStart = Date() flashTimer?.invalidate() flashTimer = Timer.scheduledTimer(withTimeInterval: 1.0 / 30, repeats: true) { [weak self] timer in MainActor.assumeIsolated { guard let self else { return timer.invalidate() } self.textView.needsDisplay = true if let start = self.flashStart, Date().timeIntervalSince(start) >= EditorFonts.flashDuration { self.endFlash() } } } textView.needsDisplay = true } func endFlash() { flashTimer?.invalidate() flashTimer = nil flashLine = nil flashStart = nil textView.needsDisplay = true } }