From c1e58f6c67a03712b57d477929c03aee4fd46030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=B8vring?= Date: Wed, 4 Nov 2026 20:02:49 +0100 Subject: [PATCH] Extend find result dimming across scroll view insets --- .../FindResultPresentationView.swift | 24 ++++++++---- .../FindResultPresentationViewTests.swift | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/Sources/Runestone/TextView/SearchAndReplace/FindResultPresentationView.swift b/Sources/Runestone/TextView/SearchAndReplace/FindResultPresentationView.swift index 8072bdfdd..16dfdaf79 100644 --- a/Sources/Runestone/TextView/SearchAndReplace/FindResultPresentationView.swift +++ b/Sources/Runestone/TextView/SearchAndReplace/FindResultPresentationView.swift @@ -24,6 +24,7 @@ final class FindResultPresentationView: UIView { private weak var sourceView: UIView? private let dimmingLayer = CAShapeLayer() private var selectedRange: NSRange? + private var currentDimmingBounds = CGRect.null private var currentBounds = CGRect.null private var currentRanges: [HighlightedRange] = [] private var cachedPresentations: [RangePresentation] = [] @@ -57,7 +58,7 @@ final class FindResultPresentationView: UIView { let shouldAnimate = selectedRange != nil && selected?.range != selectedRange let boundsChanged = currentBounds != sourceView.bounds let rangesChanged = currentRanges != searchRanges - guard boundsChanged || rangesChanged else { + guard boundsChanged || rangesChanged || dimmingBounds != currentDimmingBounds else { return } let geometryChanged = currentBounds.width != sourceView.bounds.width @@ -65,7 +66,6 @@ final class FindResultPresentationView: UIView { currentBounds = sourceView.bounds currentRanges = searchRanges frame = sourceView.bounds - dimmingLayer.frame = bounds if geometryChanged { cachedPresentations = presentations(for: searchRanges, sourceView: sourceView, @@ -84,12 +84,12 @@ final class FindResultPresentationView: UIView { guard let sourceView else { return } + let dimmingBoundsChanged = dimmingBounds != currentDimmingBounds let boundsChanged = sourceView.bounds != currentBounds let geometryChanged = sourceView.bounds.width != currentBounds.width if boundsChanged { currentBounds = sourceView.bounds frame = sourceView.bounds - dimmingLayer.frame = bounds if geometryChanged { cachedPresentations = presentations(for: currentRanges, sourceView: sourceView, @@ -99,7 +99,7 @@ final class FindResultPresentationView: UIView { guard let selectedIndex = cachedPresentations.firstIndex(where: { presentation in presentation.range.presentationRole == .selectedSearchResult }) else { - if boundsChanged { + if boundsChanged || dimmingBoundsChanged { updateDimming() } return @@ -116,7 +116,7 @@ final class FindResultPresentationView: UIView { if let refreshedPresentation, selectedGeometryChanged { cachedPresentations[selectedIndex] = refreshedPresentation } - if boundsChanged || selectedGeometryChanged { + if boundsChanged || dimmingBoundsChanged || selectedGeometryChanged { updateDimming() } if geometryChanged || selectedGeometryChanged || segmentSignature != selectedTextSegmentSignature { @@ -155,17 +155,27 @@ final class FindResultPresentationView: UIView { return RangePresentation(range: range, rects: selectionRects(textRange).map(\.rect)) } + private var dimmingBounds: CGRect { + // The viewport includes adjusted content insets and scroll bounce regions + // outside the text content, including the area behind navigation bars. + let viewport = (sourceView as? TextInputView)?.viewport ?? bounds + return bounds.union(viewport) + } + private func updateDimming() { - let path = UIBezierPath(rect: bounds) + let coverage = dimmingBounds + let path = UIBezierPath(rect: CGRect(origin: .zero, size: coverage.size)) for presentation in cachedPresentations { for rect in displayRects(for: presentation) where !rect.isEmpty { - path.append(UIBezierPath(roundedRect: rect, + path.append(UIBezierPath(roundedRect: rect.offsetBy(dx: -coverage.minX, dy: -coverage.minY), cornerRadius: cornerRadius(for: presentation.range))) } } CATransaction.begin() CATransaction.setDisableActions(true) + dimmingLayer.frame = coverage dimmingLayer.path = path.cgPath + currentDimmingBounds = coverage CATransaction.commit() } diff --git a/Tests/RunestoneTests/FindResultPresentationViewTests.swift b/Tests/RunestoneTests/FindResultPresentationViewTests.swift index bfec74d44..5073f98cd 100644 --- a/Tests/RunestoneTests/FindResultPresentationViewTests.swift +++ b/Tests/RunestoneTests/FindResultPresentationViewTests.swift @@ -106,6 +106,45 @@ final class FindResultPresentationViewTests: XCTestCase { )) } + func testDimmingCoversViewportOutsideContentAndTracksInsetChanges() throws { + let sourceView = TextInputView(theme: DefaultTheme()) + sourceView.frame = CGRect(x: 0, y: 0, width: 320, height: 480) + sourceView.string = "x" + sourceView.viewport = CGRect(x: -20, y: -100, width: 360, height: 700) + let presentationView = FindResultPresentationView(sourceView: sourceView) + sourceView.addSubview(presentationView) + presentationView.isActive = true + let resultRect = CGRect(x: 10, y: 10, width: 10, height: 18) + let selectionRects: (UITextRange) -> [UITextSelectionRect] = { _ in + [TextSelectionRect(rect: resultRect, writingDirection: .natural, + containsStart: true, containsEnd: true)] + } + presentationView.update(highlightedRanges: ranges(count: 1, selectedIndex: 0), + selectionRects: selectionRects) + let dimmingLayer = try XCTUnwrap(presentationView.layer.sublayers?.first as? CAShapeLayer) + func isDimmed(_ point: CGPoint) throws -> Bool { + let path = try XCTUnwrap(dimmingLayer.path) + let localPoint = CGPoint(x: point.x - dimmingLayer.frame.minX, + y: point.y - dimmingLayer.frame.minY) + return path.contains(localPoint, using: .evenOdd) + } + XCTAssertTrue(try isDimmed(CGPoint(x: 100, y: -80)), "Cover the navigation inset") + XCTAssertTrue(try isDimmed(CGPoint(x: -10, y: 100)), "Cover horizontal insets") + XCTAssertTrue(try isDimmed(CGPoint(x: 100, y: 580)), "Cover below short content") + XCTAssertFalse(try isDimmed(CGPoint(x: 15, y: 15)), "Keep result cutouts aligned") + + sourceView.viewport = CGRect(x: 0, y: -160, width: 320, height: 640) + presentationView.updateLayoutIfNeeded(selectionRects: selectionRects) + XCTAssertTrue(try isDimmed(CGPoint(x: 100, y: -140)), "Track viewport-only changes") + XCTAssertFalse(try isDimmed(CGPoint(x: 15, y: 15))) + + presentationView.update(highlightedRanges: [], selectionRects: selectionRects) + sourceView.viewport = CGRect(x: 0, y: -200, width: 320, height: 680) + presentationView.updateLayoutIfNeeded(selectionRects: selectionRects) + XCTAssertTrue(try isDimmed(CGPoint(x: 100, y: -180)), "Dim even without matches") + XCTAssertTrue(try isDimmed(CGPoint(x: 15, y: 15))) + } + private func ranges(count: Int, selectedIndex: Int) -> [HighlightedRange] { (0 ..< count).map { index in HighlightedRange(id: "result-\(index)",