ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / accessibility / platform / ax_platform_node_mac.mm
blob89444bf0c6fc94c35e48945a9e872c6ee53a89c0
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/accessibility/platform/ax_platform_node_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/strings/sys_string_conversions.h"
10 #import "ui/accessibility/ax_node_data.h"
11 #import "ui/accessibility/platform/ax_platform_node_delegate.h"
12 #import "ui/gfx/mac/coordinate_conversion.h"
14 namespace {
16 struct MapEntry {
17   ui::AXRole value;
18   NSString* nativeValue;
21 typedef std::map<ui::AXRole, NSString*> RoleMap;
23 RoleMap BuildRoleMap() {
24   const MapEntry roles[] = {
25       {ui::AX_ROLE_ALERT, NSAccessibilityGroupRole},
26       {ui::AX_ROLE_ALERT_DIALOG, NSAccessibilityGroupRole},
27       {ui::AX_ROLE_ANNOTATION, NSAccessibilityUnknownRole},
28       {ui::AX_ROLE_APPLICATION, NSAccessibilityGroupRole},
29       {ui::AX_ROLE_ARTICLE, NSAccessibilityGroupRole},
30       {ui::AX_ROLE_BANNER, NSAccessibilityGroupRole},
31       {ui::AX_ROLE_BLOCKQUOTE, NSAccessibilityGroupRole},
32       {ui::AX_ROLE_BUSY_INDICATOR, NSAccessibilityBusyIndicatorRole},
33       {ui::AX_ROLE_BUTTON, NSAccessibilityButtonRole},
34       {ui::AX_ROLE_CANVAS, NSAccessibilityImageRole},
35       {ui::AX_ROLE_CAPTION, NSAccessibilityGroupRole},
36       {ui::AX_ROLE_CELL, @"AXCell"},
37       {ui::AX_ROLE_CHECK_BOX, NSAccessibilityCheckBoxRole},
38       {ui::AX_ROLE_COLOR_WELL, NSAccessibilityColorWellRole},
39       {ui::AX_ROLE_COLUMN, NSAccessibilityColumnRole},
40       {ui::AX_ROLE_COLUMN_HEADER, @"AXCell"},
41       {ui::AX_ROLE_COMBO_BOX, NSAccessibilityComboBoxRole},
42       {ui::AX_ROLE_COMPLEMENTARY, NSAccessibilityGroupRole},
43       {ui::AX_ROLE_CONTENT_INFO, NSAccessibilityGroupRole},
44       {ui::AX_ROLE_DATE, @"AXDateField"},
45       {ui::AX_ROLE_DATE_TIME, NSAccessibilityTextFieldRole},
46       {ui::AX_ROLE_DEFINITION, NSAccessibilityGroupRole},
47       {ui::AX_ROLE_DESCRIPTION_LIST_DETAIL, NSAccessibilityGroupRole},
48       {ui::AX_ROLE_DESCRIPTION_LIST, NSAccessibilityListRole},
49       {ui::AX_ROLE_DESCRIPTION_LIST_TERM, NSAccessibilityGroupRole},
50       {ui::AX_ROLE_DIALOG, NSAccessibilityGroupRole},
51       {ui::AX_ROLE_DETAILS, NSAccessibilityGroupRole},
52       {ui::AX_ROLE_DIRECTORY, NSAccessibilityListRole},
53       {ui::AX_ROLE_DISCLOSURE_TRIANGLE, NSAccessibilityDisclosureTriangleRole},
54       {ui::AX_ROLE_DIV, NSAccessibilityGroupRole},
55       {ui::AX_ROLE_DOCUMENT, NSAccessibilityGroupRole},
56       {ui::AX_ROLE_FIGCAPTION, NSAccessibilityGroupRole},
57       {ui::AX_ROLE_FIGURE, NSAccessibilityGroupRole},
58       {ui::AX_ROLE_FOOTER, NSAccessibilityGroupRole},
59       {ui::AX_ROLE_FORM, NSAccessibilityGroupRole},
60       {ui::AX_ROLE_GRID, NSAccessibilityGridRole},
61       {ui::AX_ROLE_GROUP, NSAccessibilityGroupRole},
62       {ui::AX_ROLE_HEADING, @"AXHeading"},
63       {ui::AX_ROLE_IFRAME, NSAccessibilityGroupRole},
64       {ui::AX_ROLE_IFRAME_PRESENTATIONAL, NSAccessibilityGroupRole},
65       {ui::AX_ROLE_IGNORED, NSAccessibilityUnknownRole},
66       {ui::AX_ROLE_IMAGE, NSAccessibilityImageRole},
67       {ui::AX_ROLE_IMAGE_MAP, NSAccessibilityGroupRole},
68       {ui::AX_ROLE_IMAGE_MAP_LINK, NSAccessibilityLinkRole},
69       {ui::AX_ROLE_LABEL_TEXT, NSAccessibilityGroupRole},
70       {ui::AX_ROLE_LEGEND, NSAccessibilityGroupRole},
71       {ui::AX_ROLE_LINK, NSAccessibilityLinkRole},
72       {ui::AX_ROLE_LIST, NSAccessibilityListRole},
73       {ui::AX_ROLE_LIST_BOX, NSAccessibilityListRole},
74       {ui::AX_ROLE_LIST_BOX_OPTION, NSAccessibilityStaticTextRole},
75       {ui::AX_ROLE_LIST_ITEM, NSAccessibilityGroupRole},
76       {ui::AX_ROLE_LIST_MARKER, @"AXListMarker"},
77       {ui::AX_ROLE_LOG, NSAccessibilityGroupRole},
78       {ui::AX_ROLE_MAIN, NSAccessibilityGroupRole},
79       {ui::AX_ROLE_MARQUEE, NSAccessibilityGroupRole},
80       {ui::AX_ROLE_MATH, NSAccessibilityGroupRole},
81       {ui::AX_ROLE_MENU, NSAccessibilityMenuRole},
82       {ui::AX_ROLE_MENU_BAR, NSAccessibilityMenuBarRole},
83       {ui::AX_ROLE_MENU_BUTTON, NSAccessibilityButtonRole},
84       {ui::AX_ROLE_MENU_ITEM, NSAccessibilityMenuItemRole},
85       {ui::AX_ROLE_MENU_ITEM_CHECK_BOX, NSAccessibilityMenuItemRole},
86       {ui::AX_ROLE_MENU_ITEM_RADIO, NSAccessibilityMenuItemRole},
87       {ui::AX_ROLE_MENU_LIST_OPTION, NSAccessibilityMenuItemRole},
88       {ui::AX_ROLE_MENU_LIST_POPUP, NSAccessibilityUnknownRole},
89       {ui::AX_ROLE_METER, NSAccessibilityProgressIndicatorRole},
90       {ui::AX_ROLE_NAVIGATION, NSAccessibilityGroupRole},
91       {ui::AX_ROLE_NONE, NSAccessibilityGroupRole},
92       {ui::AX_ROLE_NOTE, NSAccessibilityGroupRole},
93       {ui::AX_ROLE_OUTLINE, NSAccessibilityOutlineRole},
94       {ui::AX_ROLE_PARAGRAPH, NSAccessibilityGroupRole},
95       {ui::AX_ROLE_POP_UP_BUTTON, NSAccessibilityPopUpButtonRole},
96       {ui::AX_ROLE_PRE, NSAccessibilityGroupRole},
97       {ui::AX_ROLE_PRESENTATIONAL, NSAccessibilityGroupRole},
98       {ui::AX_ROLE_PROGRESS_INDICATOR, NSAccessibilityProgressIndicatorRole},
99       {ui::AX_ROLE_RADIO_BUTTON, NSAccessibilityRadioButtonRole},
100       {ui::AX_ROLE_RADIO_GROUP, NSAccessibilityRadioGroupRole},
101       {ui::AX_ROLE_REGION, NSAccessibilityGroupRole},
102       {ui::AX_ROLE_ROOT_WEB_AREA, @"AXWebArea"},
103       {ui::AX_ROLE_ROW, NSAccessibilityRowRole},
104       {ui::AX_ROLE_ROW_HEADER, @"AXCell"},
105       {ui::AX_ROLE_RULER, NSAccessibilityRulerRole},
106       {ui::AX_ROLE_SCROLL_BAR, NSAccessibilityScrollBarRole},
107       {ui::AX_ROLE_SEARCH, NSAccessibilityGroupRole},
108       {ui::AX_ROLE_SLIDER, NSAccessibilitySliderRole},
109       {ui::AX_ROLE_SLIDER_THUMB, NSAccessibilityValueIndicatorRole},
110       {ui::AX_ROLE_SPIN_BUTTON, NSAccessibilityIncrementorRole},
111       {ui::AX_ROLE_SPLITTER, NSAccessibilitySplitterRole},
112       {ui::AX_ROLE_STATIC_TEXT, NSAccessibilityStaticTextRole},
113       {ui::AX_ROLE_STATUS, NSAccessibilityGroupRole},
114       {ui::AX_ROLE_SVG_ROOT, NSAccessibilityGroupRole},
115       {ui::AX_ROLE_TAB, NSAccessibilityRadioButtonRole},
116       {ui::AX_ROLE_TABLE, NSAccessibilityTableRole},
117       {ui::AX_ROLE_TABLE_HEADER_CONTAINER, NSAccessibilityGroupRole},
118       {ui::AX_ROLE_TAB_LIST, NSAccessibilityTabGroupRole},
119       {ui::AX_ROLE_TAB_PANEL, NSAccessibilityGroupRole},
120       {ui::AX_ROLE_TEXT_AREA, NSAccessibilityTextAreaRole},
121       {ui::AX_ROLE_TEXT_FIELD, NSAccessibilityTextFieldRole},
122       {ui::AX_ROLE_TIME, @"AXTimeField"},
123       {ui::AX_ROLE_TIMER, NSAccessibilityGroupRole},
124       {ui::AX_ROLE_TOGGLE_BUTTON, NSAccessibilityCheckBoxRole},
125       {ui::AX_ROLE_TOOLBAR, NSAccessibilityToolbarRole},
126       {ui::AX_ROLE_TOOLTIP, NSAccessibilityGroupRole},
127       {ui::AX_ROLE_TREE, NSAccessibilityOutlineRole},
128       {ui::AX_ROLE_TREE_GRID, NSAccessibilityTableRole},
129       {ui::AX_ROLE_TREE_ITEM, NSAccessibilityRowRole},
130       {ui::AX_ROLE_WEB_AREA, @"AXWebArea"},
131       {ui::AX_ROLE_WINDOW, NSAccessibilityWindowRole},
133       // TODO(dtseng): we don't correctly support the attributes for these
134       // roles.
135       // { ui::AX_ROLE_SCROLL_AREA, NSAccessibilityScrollAreaRole },
136   };
138   RoleMap role_map;
139   for (size_t i = 0; i < arraysize(roles); ++i)
140     role_map[roles[i].value] = roles[i].nativeValue;
141   return role_map;
144 RoleMap BuildSubroleMap() {
145   const MapEntry subroles[] = {
146       {ui::AX_ROLE_ALERT, @"AXApplicationAlert"},
147       {ui::AX_ROLE_ALERT_DIALOG, @"AXApplicationAlertDialog"},
148       {ui::AX_ROLE_ARTICLE, @"AXDocumentArticle"},
149       {ui::AX_ROLE_DEFINITION, @"AXDefinition"},
150       {ui::AX_ROLE_DESCRIPTION_LIST_DETAIL, @"AXDefinition"},
151       {ui::AX_ROLE_DESCRIPTION_LIST_TERM, @"AXTerm"},
152       {ui::AX_ROLE_DIALOG, @"AXApplicationDialog"},
153       {ui::AX_ROLE_DOCUMENT, @"AXDocument"},
154       {ui::AX_ROLE_FOOTER, @"AXLandmarkContentInfo"},
155       {ui::AX_ROLE_APPLICATION, @"AXLandmarkApplication"},
156       {ui::AX_ROLE_BANNER, @"AXLandmarkBanner"},
157       {ui::AX_ROLE_COMPLEMENTARY, @"AXLandmarkComplementary"},
158       {ui::AX_ROLE_CONTENT_INFO, @"AXLandmarkContentInfo"},
159       {ui::AX_ROLE_MAIN, @"AXLandmarkMain"},
160       {ui::AX_ROLE_NAVIGATION, @"AXLandmarkNavigation"},
161       {ui::AX_ROLE_SEARCH, @"AXLandmarkSearch"},
162       {ui::AX_ROLE_FORM, @"AXLandmarkForm"},
163       {ui::AX_ROLE_LOG, @"AXApplicationLog"},
164       {ui::AX_ROLE_MARQUEE, @"AXApplicationMarquee"},
165       {ui::AX_ROLE_MATH, @"AXDocumentMath"},
166       {ui::AX_ROLE_NOTE, @"AXDocumentNote"},
167       {ui::AX_ROLE_REGION, @"AXDocumentRegion"},
168       {ui::AX_ROLE_STATUS, @"AXApplicationStatus"},
169       {ui::AX_ROLE_TAB_PANEL, @"AXTabPanel"},
170       {ui::AX_ROLE_TIMER, @"AXApplicationTimer"},
171       {ui::AX_ROLE_TOGGLE_BUTTON, @"AXToggleButton"},
172       {ui::AX_ROLE_TOOLTIP, @"AXUserInterfaceTooltip"},
173       {ui::AX_ROLE_TREE_ITEM, NSAccessibilityOutlineRowSubrole},
174   };
176   RoleMap subrole_map;
177   for (size_t i = 0; i < arraysize(subroles); ++i)
178     subrole_map[subroles[i].value] = subroles[i].nativeValue;
179   return subrole_map;
182 }  // namespace
184 @implementation AXPlatformNodeCocoa
186 // A mapping of AX roles to native roles.
187 + (NSString*)nativeRoleFromAXRole:(ui::AXRole)role {
188   CR_DEFINE_STATIC_LOCAL(RoleMap, role_map, (BuildRoleMap()));
189   RoleMap::iterator it = role_map.find(role);
190   return it != role_map.end() ? it->second : NSAccessibilityUnknownRole;
193 // A mapping of AX roles to native subroles.
194 + (NSString*)nativeSubroleFromAXRole:(ui::AXRole)role {
195   CR_DEFINE_STATIC_LOCAL(RoleMap, subrole_map, (BuildSubroleMap()));
196   RoleMap::iterator it = subrole_map.find(role);
197   return it != subrole_map.end() ? it->second : nil;
200 - (instancetype)initWithNode:(ui::AXPlatformNodeBase*)node {
201   if ((self = [super init])) {
202     node_ = node;
203   }
204   return self;
207 - (void)detach {
208   node_ = nil;
211 - (NSRect)boundsInScreen {
212   if (!node_)
213     return NSZeroRect;
214   return gfx::ScreenRectToNSRect(node_->GetBoundsInScreen());
217 - (NSArray*)AXChildren {
218   if (!node_)
219     return nil;
220   int count = node_->GetChildCount();
221   NSMutableArray* children = [NSMutableArray arrayWithCapacity:count];
222   for (int i = 0; i < count; ++i)
223     [children addObject:node_->ChildAtIndex(i)];
224   return NSAccessibilityUnignoredChildren(children);
227 - (id)AXParent {
228   if (!node_)
229     return nil;
230   return NSAccessibilityUnignoredAncestor(node_->GetParent());
233 - (NSValue*)AXPosition {
234   return [NSValue valueWithPoint:self.boundsInScreen.origin];
237 - (NSString*)AXRole {
238   if (!node_)
239     return nil;
240   return [[self class] nativeRoleFromAXRole:node_->GetData().role];
243 - (NSValue*)AXSize {
244   return [NSValue valueWithSize:self.boundsInScreen.size];
247 // NSAccessibility informal protocol implementation.
249 - (BOOL)accessibilityIsIgnored {
250   return [[self AXRole] isEqualToString:NSAccessibilityUnknownRole];
253 - (id)accessibilityHitTest:(NSPoint)point {
254   for (AXPlatformNodeCocoa* child in [self AXChildren]) {
255     if (NSPointInRect(point, child.boundsInScreen))
256       return [child accessibilityHitTest:point];
257   }
258   return NSAccessibilityUnignoredAncestor(self);
261 - (NSArray*)accessibilityActionNames {
262   return nil;
265 - (NSArray*)accessibilityAttributeNames {
266   return @[
267     NSAccessibilityChildrenAttribute,
268     NSAccessibilityParentAttribute,
269     NSAccessibilityPositionAttribute,
270     NSAccessibilityRoleAttribute,
271     NSAccessibilitySizeAttribute,
272   ];
275 - (BOOL)accessibilityIsAttributeSettable:(NSString*)attribute {
276   return NO;
279 - (id)accessibilityAttributeValue:(NSString*)attribute {
280   SEL selector = NSSelectorFromString(attribute);
281   if ([self respondsToSelector:selector])
282     return [self performSelector:selector];
283   return nil;
286 @end
288 namespace ui {
290 // static
291 AXPlatformNode* AXPlatformNode::Create(AXPlatformNodeDelegate* delegate) {
292   AXPlatformNodeBase* node = new AXPlatformNodeMac();
293   node->Init(delegate);
294   return node;
297 AXPlatformNodeMac::AXPlatformNodeMac() {
300 AXPlatformNodeMac::~AXPlatformNodeMac() {
303 void AXPlatformNodeMac::Destroy() {
304   if (native_node_)
305     [native_node_ detach];
306   delegate_ = nullptr;
307   delete this;
310 gfx::NativeViewAccessible AXPlatformNodeMac::GetNativeViewAccessible() {
311   if (!native_node_)
312     native_node_.reset([[AXPlatformNodeCocoa alloc] initWithNode:this]);
313   return native_node_.get();
316 void AXPlatformNodeMac::NotifyAccessibilityEvent(ui::AXEvent event_type) {
317   // TODO(dmazzoni): implement this.  http://crbug.com/396137
320 int AXPlatformNodeMac::GetIndexInParent() {
321   // TODO(dmazzoni): implement this.  http://crbug.com/396137
322   return -1;
325 }  // namespace ui