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
8 changes: 8 additions & 0 deletions Sources/Runestone/TextView/Core/LayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ final class LayoutManager {

// MARK: - Views
let gutterContainerView = GutterContainerView()
var isFindResultPresentationActive = false
private let gutterDimmingLayer = CALayer()
private let foldingButtonReuseQueue = ViewReuseQueue<Int, FoldingGutterButton>()
private var lineFragmentViewReuseQueue = ViewReuseQueue<LineFragmentID, LineFragmentView>()
private var lineNumberLabelReuseQueue = ViewReuseQueue<DocumentLineNodeID, LineNumberView>()
Expand Down Expand Up @@ -355,6 +357,7 @@ final class LayoutManager {
// Line numbers decorate the text; separate VoiceOver stops obscure the editor's controls.
self.lineNumbersContainerView.accessibilityElementsHidden = true
self.gutterContainerView.isUserInteractionEnabled = true
self.gutterDimmingLayer.backgroundColor = FindResultPresentationView.dimmingColor.cgColor
self.gutterBackgroundView.isUserInteractionEnabled = false
self.gutterSelectionBackgroundView.isUserInteractionEnabled = false
self.lineSelectionBackgroundView.isUserInteractionEnabled = false
Expand Down Expand Up @@ -509,6 +512,11 @@ extension LayoutManager {
gutterContainerView.frame = CGRect(x: viewport.minX, y: 0, width: totalGutterWidth, height: contentSize.height)
gutterBackgroundView.frame = CGRect(x: 0, y: viewport.minY, width: totalGutterWidth, height: viewport.height)
lineNumbersContainerView.frame = CGRect(x: 0, y: 0, width: totalGutterWidth, height: contentSize.height)
// The gutter is above the text input's search overlay. Dim its complete
// viewport separately, including controls added or reused during layout.
gutterDimmingLayer.frame = gutterBackgroundView.frame
gutterDimmingLayer.isHidden = !isFindResultPresentationActive || (!showLineNumbers && !isCodeFoldingEnabled)
gutterContainerView.layer.addSublayer(gutterDimmingLayer)
}

private func layoutLineSelection() {
Expand Down
1 change: 1 addition & 0 deletions Sources/Runestone/TextView/Core/TextInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ final class TextInputView: UIView, UITextInput {
let isActive = isFindResultPresentationEnabled && isFindResultPresentationRequested
findResultPresentationView.isActive = isActive
highlightService.isFindResultPresentationActive = isActive
layoutManager.isFindResultPresentationActive = isActive
layoutManager.setNeedsLayout()
layoutManager.layoutIfNeeded()
guard isActive else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class FindResultPresentationView: UIView {
isHidden = true
isUserInteractionEnabled = false
backgroundColor = .clear
dimmingLayer.fillColor = UIColor.black.withAlphaComponent(0.22).cgColor
dimmingLayer.fillColor = Self.dimmingColor.cgColor
dimmingLayer.fillRule = .evenOdd
layer.addSublayer(dimmingLayer)
selectedResultPlateView.isHidden = true
Expand Down Expand Up @@ -260,6 +260,8 @@ final class FindResultPresentationView: UIView {
range.presentationRole == .selectedSearchResult ? Self.selectedCornerRadius : range.cornerRadius
}

static let dimmingColor = UIColor.black.withAlphaComponent(0.22)

private static let selectedHorizontalPadding: CGFloat = 4
private static let selectedVerticalPadding: CGFloat = 3
private static let selectedCornerRadius: CGFloat = 5
Expand Down
61 changes: 61 additions & 0 deletions Tests/RunestoneTests/CodeFoldingGutterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,67 @@ final class CodeFoldingGutterTests: XCTestCase {
}
}

func testFindDimmingCoversGutterAndPreservesFoldingHitTargets() throws {
let (view, input) = try makeTextView()
let gutter = input.gutterContainerView
view.contentInset.top = 80
view.contentOffset = CGPoint(x: 40, y: -80)
settle(view, input: input)
let sample = CGPoint(x: 2, y: -40)
let normalBrightness = try gutterBrightness(gutter, at: sample)

// Requesting find alone must preserve the opt-in contract.
view.isFindResultPresentationActive = true
XCTAssertEqual(try gutterBrightness(gutter, at: sample), normalBrightness)
view.isFindResultPresentationEnabled = true
settle(view, input: input)
XCTAssertLessThan(try gutterBrightness(gutter, at: sample), normalBrightness - 30)

for showLineNumbers in [true, false, true] {
view.showLineNumbers = showLineNumbers
// Rebuild folding controls while find remains active.
view.isCodeFoldingEnabled = false
view.isCodeFoldingEnabled = true
input.refreshCodeFolding(synchronously: true)
view.contentOffset = CGPoint(x: showLineNumbers ? 80 : 40, y: -80)
settle(view, input: input)
let button = try XCTUnwrap(foldingButtons(in: gutter).first)
assertGutterEdgesHit(button, in: gutter)
let dim = try XCTUnwrap(gutter.layer.sublayers?.last)
XCTAssertEqual(dim.backgroundColor, FindResultPresentationView.dimmingColor.cgColor)
XCTAssertFalse(dim.isHidden)
XCTAssertEqual(dim.frame, CGRect(x: 0, y: input.viewport.minY,
width: gutter.bounds.width, height: input.viewport.height))
XCTAssertTrue(gutter.subviews.allSatisfy { child in
guard let layers = gutter.layer.sublayers,
let childIndex = layers.firstIndex(of: child.layer),
let dimIndex = layers.firstIndex(of: dim) else { return false }
return childIndex < dimIndex
}, "Dimming must cover line numbers and newly created folding controls")
}

view.isFindResultPresentationEnabled = false
settle(view, input: input)
XCTAssertEqual(try gutterBrightness(gutter, at: sample), normalBrightness)
view.isFindResultPresentationEnabled = true
view.isFindResultPresentationActive = false
settle(view, input: input)
XCTAssertEqual(try gutterBrightness(gutter, at: sample), normalBrightness)
}

private func gutterBrightness(_ gutter: UIView, at point: CGPoint) throws -> Int {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
format.preferredRange = .standard
let image = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1), format: format).image { context in
context.cgContext.translateBy(x: -point.x, y: -point.y)
gutter.layer.render(in: context.cgContext)
}
let data = try XCTUnwrap(image.cgImage?.dataProvider?.data)
let bytes = try XCTUnwrap(CFDataGetBytePtr(data))
return Int(bytes[0]) + Int(bytes[1]) + Int(bytes[2])
}

private func assertGutterEdgesHit(_ button: FoldingGutterButton, in gutter: UIView,
file: StaticString = #filePath, line: UInt = #line) {
for x in [gutter.bounds.minX + 0.5, gutter.bounds.maxX - 0.5] {
Expand Down
Loading