diff --git a/Sources/MarkdownView/Components/DiffFilesView/DiffFileHeaderView.swift b/Sources/MarkdownView/Components/DiffFilesView/DiffFileHeaderView.swift index e6a26b9..658e631 100644 --- a/Sources/MarkdownView/Components/DiffFilesView/DiffFileHeaderView.swift +++ b/Sources/MarkdownView/Components/DiffFilesView/DiffFileHeaderView.swift @@ -22,6 +22,7 @@ import Foundation ) private var tapHandler: (() -> Void)? private let collapseButton = UIButton(type: .custom) + private var controlsLeadingConstraint: NSLayoutConstraint? private let viewedControl = SymbolActionView( systemName: "circle", config: UIImage.SymbolConfiguration(pointSize: 22, weight: .regular) @@ -50,8 +51,21 @@ import Foundation ) { tapHandler = onToggle viewedControl.isHidden = !showsViewedControl + // The centered symbol already provides an inset inside its 44-point tap target. + controlsLeadingConstraint?.constant = showsViewedControl ? 0 : DiffFilesViewConfiguration.headerPadding viewedControl.onTap(onToggleViewed) - viewedControl.tintColor = isViewed ? theme.diff.addedIndicatorText : theme.diff.fileMetadataText + let checkedColor = theme.diff.addedIndicatorText + viewedControl.tintColor = isViewed ? UIColor { traits in + let resolved = checkedColor.resolvedColor(with: traits) + var red: CGFloat = 0 + var green: CGFloat = 0 + var blue: CGFloat = 0 + var alpha: CGFloat = 0 + guard resolved.getRed(&red, green: &green, blue: &blue, alpha: &alpha) else { + return resolved + } + return UIColor(displayP3Red: red * 0.8, green: green * 0.8, blue: blue * 0.8, alpha: alpha) + } : theme.diff.fileMetadataText if representedPath != file.displayPath || wasViewed != isViewed { viewedControl.replaceSymbol( systemName: isViewed ? "checkmark.circle.fill" : "circle", @@ -145,9 +159,11 @@ import Foundation addSubview(controls) let viewedWidth = viewedControl.widthAnchor.constraint(equalToConstant: 44) viewedWidth.priority = .init(999) + let controlsLeading = controls.leadingAnchor.constraint(equalTo: leadingAnchor) + controlsLeadingConstraint = controlsLeading NSLayoutConstraint.activate([ viewedWidth, - controls.leadingAnchor.constraint(equalTo: leadingAnchor, constant: DiffFilesViewConfiguration.headerPadding), + controlsLeading, controls.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -DiffFilesViewConfiguration.headerPadding), controls.topAnchor.constraint(equalTo: topAnchor), controls.bottomAnchor.constraint(equalTo: bottomAnchor), diff --git a/Tests/MarkdownViewTests/DiffFilesViewedTests.swift b/Tests/MarkdownViewTests/DiffFilesViewedTests.swift index 12c26a3..85d3f24 100644 --- a/Tests/MarkdownViewTests/DiffFilesViewedTests.swift +++ b/Tests/MarkdownViewTests/DiffFilesViewedTests.swift @@ -62,6 +62,39 @@ XCTAssertTrue(controller.viewedFilePaths.isEmpty) } + func testHeaderLeadingInsetPreservesViewedTapTargetAndHiddenControlPadding() throws { + let document = try XCTUnwrap(DiffPatchDocument(patch: patch, language: "swift")) + let file = try XCTUnwrap(document.files.first) + for direction in [UISemanticContentAttribute.forceLeftToRight, .forceRightToLeft] { + let header = DiffFileHeaderView(frame: CGRect(x: 0, y: 0, width: 402, height: 48)) + header.semanticContentAttribute = direction + let controls = try XCTUnwrap(header.subviews.compactMap { $0 as? UIStackView }.first) + controls.semanticContentAttribute = direction + let viewed = try XCTUnwrap(controls.arrangedSubviews.first as? SymbolActionView) + for visible in [true, false, true] { + header.configure( + file: file, + theme: .default, + isCollapsed: true, + isViewed: true, + showsViewedControl: visible, + onToggle: {}, + onToggleViewed: {} + ) + header.layoutIfNeeded() + let isRTL = direction == .forceRightToLeft + let leadingInset = isRTL ? header.bounds.width - controls.frame.maxX : controls.frame.minX + let trailingInset = isRTL ? controls.frame.minX : header.bounds.width - controls.frame.maxX + XCTAssertEqual(leadingInset, visible ? 0 : DiffFilesViewConfiguration.headerPadding, accuracy: 0.5) + XCTAssertEqual(trailingInset, DiffFilesViewConfiguration.headerPadding, accuracy: 0.5) + if visible { + XCTAssertEqual(viewed.bounds.width, 44, accuracy: 0.5) + XCTAssertGreaterThanOrEqual(viewed.bounds.height, 44) + } + } + } + } + func testHeaderExposesIndependentAccessibleActions() throws { let document = try XCTUnwrap(DiffPatchDocument(patch: patch, language: "swift")) let file = try XCTUnwrap(document.files.first) @@ -104,7 +137,25 @@ XCTAssertEqual(collapseTaps, 1) XCTAssertEqual(viewedTaps, 1) - configure(viewed: true) + for style in [UIUserInterfaceStyle.light, .dark] { + let traits = UITraitCollection(userInterfaceStyle: style) + configure(viewed: false) + XCTAssertEqual( + viewed.tintColor.resolvedColor(with: traits), + MarkdownTheme.default.diff.fileMetadataText.resolvedColor(with: traits) + ) + configure(viewed: true) + var red: CGFloat = 0 + var green: CGFloat = 0 + var blue: CGFloat = 0 + var alpha: CGFloat = 0 + XCTAssertTrue(MarkdownTheme.default.diff.addedIndicatorText.resolvedColor(with: traits) + .getRed(&red, green: &green, blue: &blue, alpha: &alpha)) + let expected = UIColor(displayP3Red: red * 0.8, green: green * 0.8, blue: blue * 0.8, alpha: alpha) + let resolved = viewed.tintColor.resolvedColor(with: traits) + XCTAssertEqual(resolved, expected) + XCTAssertEqual(resolved.cgColor.colorSpace?.name, CGColorSpace.displayP3) + } XCTAssertEqual(viewed.accessibilityValue, "Viewed") XCTAssertTrue(viewed.accessibilityTraits.contains(.selected)) configure(viewed: true, visible: false)