Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -57,15 +58,14 @@ 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
|| currentRanges.map(\.range) != searchRanges.map(\.range)
currentBounds = sourceView.bounds
currentRanges = searchRanges
frame = sourceView.bounds
dimmingLayer.frame = bounds
if geometryChanged {
cachedPresentations = presentations(for: searchRanges,
sourceView: sourceView,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down
39 changes: 39 additions & 0 deletions Tests/RunestoneTests/FindResultPresentationViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
Loading