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

Commit

Permalink
[ios] Refactored MGLAnnotationContext
Browse files Browse the repository at this point in the history
Fixed an issue where the “return to map” accessibility element lacked a label. Always update the title and subtitle of an annotation accessibility element, in case the title or subtitle changes from one showing to the next.
  • Loading branch information
1ec5 committed Apr 21, 2016
1 parent 397c0b2 commit 46e0bb1
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,22 @@ @interface MGLAnnotationAccessibilityElement : UIAccessibilityElement

@property (nonatomic) MGLAnnotationTag tag;

- (instancetype)initWithAccessibilityContainer:(id)container tag:(MGLAnnotationTag)identifier NS_DESIGNATED_INITIALIZER;

@end

@implementation MGLAnnotationAccessibilityElement

- (instancetype)initWithAccessibilityContainer:(id)container tag:(MGLAnnotationTag)tag
{
if (self = [super initWithAccessibilityContainer:container])
{
_tag = tag;
self.accessibilityTraits = UIAccessibilityTraitButton;
}
return self;
}

@end

/// Lightweight container for metadata about an annotation, including the annotation itself.
Expand All @@ -204,7 +216,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container
if (self = [super initWithAccessibilityContainer:container])
{
self.accessibilityTraits = UIAccessibilityTraitButton;
self.accessibilityLabel = self.accessibilityLabel;
self.accessibilityLabel = [self.accessibilityContainer accessibilityLabel];
self.accessibilityHint = @"Returns to the map";
}
return self;
Expand Down Expand Up @@ -251,7 +263,7 @@ @interface MGLMapView () <UIGestureRecognizerDelegate,
@property (nonatomic) CGFloat quickZoomStart;
@property (nonatomic, getter=isDormant) BOOL dormant;
@property (nonatomic, readonly, getter=isRotationAllowed) BOOL rotationAllowed;
@property (nonatomic) UIAccessibilityElement *mapViewProxyAccessibilityElement;
@property (nonatomic) MGLMapViewProxyAccessibilityElement *mapViewProxyAccessibilityElement;

@end

Expand Down Expand Up @@ -1925,32 +1937,29 @@ - (id)accessibilityElementAtIndex:(NSInteger)index
NSAssert(_annotationContextsByAnnotationTag.count(annotationTag), @"Missing annotation for tag %u.", annotationTag);
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
id <MGLAnnotation> annotation = annotationContext.annotation;
MGLAnnotationAccessibilityElement *element = annotationContext.accessibilityElement;

// Lazily create an accessibility element for the found annotation.
if ( ! element)
if ( ! annotationContext.accessibilityElement)
{
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;
annotationContext.accessibilityElement = [[MGLAnnotationAccessibilityElement alloc] initWithAccessibilityContainer:self tag:annotationTag];
}

// Update the accessibility element’s frame.
// Update the accessibility element.
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag];
CGRect annotationFrame = [self frameOfImage:annotationImage.image centeredAtCoordinate:annotation.coordinate];
CGRect screenRect = UIAccessibilityConvertFrameToScreenCoordinates(annotationFrame, self);
element.accessibilityFrame = screenRect;
annotationContext.accessibilityElement.accessibilityFrame = screenRect;

if ([annotation respondsToSelector:@selector(title)])
{
annotationContext.accessibilityElement.accessibilityLabel = annotation.title;
}
if ([annotation respondsToSelector:@selector(subtitle)])
{
annotationContext.accessibilityElement.accessibilityValue = annotation.subtitle;
}

return element;
return annotationContext.accessibilityElement;
}

- (NSInteger)indexOfAccessibilityElement:(id)element
Expand Down Expand Up @@ -1986,11 +1995,11 @@ - (NSInteger)indexOfAccessibilityElement:(id)element
else return std::distance(visibleAnnotations.begin(), foundElement) + 2 /* compass, userLocationAnnotationView */;
}

- (UIAccessibilityElement *)mapViewProxyAccessibilityElement
- (MGLMapViewProxyAccessibilityElement *)mapViewProxyAccessibilityElement
{
if ( ! _mapViewProxyAccessibilityElement)
{
_mapViewProxyAccessibilityElement = [[MGLAnnotationAccessibilityElement alloc] initWithAccessibilityContainer:self];
_mapViewProxyAccessibilityElement = [[MGLMapViewProxyAccessibilityElement alloc] initWithAccessibilityContainer:self];
}
return _mapViewProxyAccessibilityElement;
}
Expand Down

0 comments on commit 46e0bb1

Please sign in to comment.