Skip to content

Commit

Permalink
Improve initial accessibility focus behavior (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankeller authored Dec 4, 2023
1 parent 8d3f745 commit 66dd26d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Sources/Public/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ public final class CalendarView: UIView {
guard isReadyForLayout else { return }

// Layout with an extended bounds if Voice Over is running, reducing the likelihood of a
// Voice Over user experiencing "No heading found" when navigating by heading.
_layoutSubviews(extendLayoutRegion: UIAccessibility.isVoiceOverRunning)
// Voice Over user experiencing "No heading found" when navigating by heading. We also check to
// make sure an accessibility element has already been focused, otherwise the first
// accessibility element will be off-screen when a user first focuses into the calendar view.
let extendLayoutRegion = UIAccessibility.isVoiceOverRunning &&
itemTypeOfFocusedAccessibilityElement != nil
_layoutSubviews(extendLayoutRegion: extendLayoutRegion)
}

/// Sets the content of the `CalendarView`, causing it to re-render, with no animation.
Expand Down Expand Up @@ -517,8 +521,6 @@ public final class CalendarView: UIView {
private weak var autoScrollDisplayLink: CADisplayLink?
private var autoScrollOffset: CGFloat?

private var itemTypeOfFocusedAccessibilityElement: VisibleItem.ItemType?

private var lastMultiDaySelectionDay: Day?

private lazy var scrollViewDelegate = ScrollViewDelegate(calendarView: self)
Expand All @@ -536,6 +538,18 @@ public final class CalendarView: UIView {
return false
}()

private var itemTypeOfFocusedAccessibilityElement: VisibleItem.ItemType? {
didSet {
switch (oldValue, itemTypeOfFocusedAccessibilityElement) {
case (.none, .some), (.some, .none):
setNeedsLayout()
layoutIfNeeded()
default:
break
}
}
}

private var isReadyForLayout: Bool {
// There's no reason to attempt layout unless we have a non-zero `bounds.size`. We'll have a
// non-zero size once the `frame` is set to something non-zero, either manually or via the
Expand Down Expand Up @@ -1226,6 +1240,7 @@ extension CalendarView {
let element = notification.userInfo?[UIAccessibility.focusedElementUserInfoKey] as? UIResponder,
let itemView = element.nextItemView()
else {
itemTypeOfFocusedAccessibilityElement = nil
return
}

Expand Down

0 comments on commit 66dd26d

Please sign in to comment.