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 @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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),
Expand Down
53 changes: 52 additions & 1 deletion Tests/MarkdownViewTests/DiffFilesViewedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading