Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios] Made annotations accessible
Browse files Browse the repository at this point in the history
Lazily create and cache accessibility elements for annotations as UIAccessibility asks about them.

Fixes #1493.
  • Loading branch information
1ec5 committed Apr 18, 2016
1 parent 1dd7fa8 commit 240e0e1
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,24 @@ typedef NS_ENUM(NSUInteger, MGLUserTrackingState) {
return descriptions[cardinalPoint];
}

@interface MGLAnnotationAccessibilityElement : UIAccessibilityElement

@property (nonatomic) MGLAnnotationTag tag;

@end

@implementation MGLAnnotationAccessibilityElement

@end

/// Lightweight container for metadata about an annotation, including the annotation itself.
class MGLAnnotationContext {
public:
id <MGLAnnotation> annotation;
/// mbgl-given identifier for the annotation image used by this annotation.
/// Based on the annotation image’s reusable identifier.
NSString *symbolIdentifier;
MGLAnnotationAccessibilityElement *accessibilityElement;
};

#pragma mark - Private -
Expand Down Expand Up @@ -1784,6 +1795,64 @@ - (CGRect)accessibilityFrame
return frame;
}

- (NSInteger)accessibilityElementCount
{
std::vector<MGLAnnotationTag> visibleAnnotations = [self annotationTagsInRect:self.bounds];
return visibleAnnotations.size();
}

- (id)accessibilityElementAtIndex:(NSInteger)index
{
NSAssert(index < MGLAnnotationTagNotFound, @"Too many accessibility elements to associate annotation tags with.");
MGLAnnotationTag annotationTag = (MGLAnnotationTag)index;
NSAssert(_annotationContextsByAnnotationTag.count(annotationTag), @"Can’t get accessibility element for nonexistent annotation.");
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag);
id <MGLAnnotation> annotation = annotationContext.annotation;
MGLAnnotationAccessibilityElement *element = annotationContext.accessibilityElement;

// Lazily create an accessibility element for the found annotation.
if ( ! element)
{
element = [[MGLAnnotationAccessibilityElement alloc] initWithAccessibilityContainer:self];
element.tag = annotationTag;
element.accessibilityTraits = UIAccessibilityTraitButton;
if ([annotation respondsToSelector:@selector(title)])
{
element.accessibilityLabel = annotation.title;
}
if ([annotation respondsToSelector:@selector(subtitle)])
{
element.accessibilityValue = annotation.subtitle;
}
annotationContext.accessibilityElement = element;
}

// Update the accessibility element’s frame.
CGPoint mapViewPoint = [self convertCoordinate:annotation.coordinate toPointToView:self];
CGRect mapViewRect = CGRectMake(mapViewPoint.x - 20, mapViewPoint.y - 30, 40, 60);
CGRect screenRect = UIAccessibilityConvertFrameToScreenCoordinates(mapViewRect, self);
element.accessibilityFrame = screenRect;

return element;
}

- (NSInteger)indexOfAccessibilityElement:(id)element
{
if (![element isKindOfClass:[MGLAnnotationAccessibilityElement class]])
{
return NSNotFound;
}

for (auto &pair : _annotationContextsByAnnotationTag)
{
if (pair.second.accessibilityElement == element)
{
return pair.first;
}
}
return NSNotFound;
}

#pragma mark - Geography -

+ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingCenterCoordinate
Expand Down Expand Up @@ -2514,6 +2583,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
}

[self didChangeValueForKey:@"annotations"];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}

- (double)alphaForShapeAnnotation:(MGLShape *)annotation
Expand Down Expand Up @@ -2626,6 +2696,7 @@ - (void)removeAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
[self willChangeValueForKey:@"annotations"];
_mbglMap->removeAnnotations(annotationTagsToRemove);
[self didChangeValueForKey:@"annotations"];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
}

Expand Down

0 comments on commit 240e0e1

Please sign in to comment.