summaryrefslogtreecommitdiffstats
path: root/accessible/mac/mozSelectableElements.mm
blob: fdfeda198e3b368a083cda146c4841cab64e69ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* clang-format off */
/* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* clang-format on */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#import "mozSelectableElements.h"
#import "MOXWebAreaAccessible.h"
#import "MacUtils.h"
#include "Accessible-inl.h"
#include "nsCocoaUtils.h"

using namespace mozilla::a11y;

@implementation mozSelectableAccessible

/**
 * Return the mozAccessibles that are selectable.
 */
- (NSArray*)selectableChildren {
  NSArray* toFilter;
  if ([self isKindOfClass:[mozMenuAccessible class]]) {
    // If we are a menu, our children are only selectable if they are visible
    // so we filter this array instead of our unignored children list, which may
    // contain invisible items.
    toFilter = [static_cast<mozMenuAccessible*>(self) moxVisibleChildren];
  } else {
    toFilter = [self moxUnignoredChildren];
  }
  return [toFilter
      filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(
                                                   mozAccessible* child,
                                                   NSDictionary* bindings) {
        return [child isKindOfClass:[mozSelectableChildAccessible class]];
      }]];
}

- (void)moxSetSelectedChildren:(NSArray*)selectedChildren {
  for (id child in [self selectableChildren]) {
    BOOL selected =
        [selectedChildren indexOfObjectIdenticalTo:child] != NSNotFound;
    [child moxSetSelected:@(selected)];
  }
}

/**
 * Return the mozAccessibles that are actually selected.
 */
- (NSArray*)moxSelectedChildren {
  return [[self selectableChildren]
      filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(
                                                   mozAccessible* child,
                                                   NSDictionary* bindings) {
        // Return mozSelectableChildAccessibles that have are selected (truthy
        // value).
        return [[(mozSelectableChildAccessible*)child moxSelected] boolValue];
      }]];
}

@end

@implementation mozSelectableChildAccessible

- (NSNumber*)moxSelected {
  return @([self stateWithMask:states::SELECTED] != 0);
}

- (void)moxSetSelected:(NSNumber*)selected {
  // Get SELECTABLE and UNAVAILABLE state.
  uint64_t state =
      [self stateWithMask:(states::SELECTABLE | states::UNAVAILABLE)];
  if ((state & states::SELECTABLE) == 0 || (state & states::UNAVAILABLE) != 0) {
    // The object is either not selectable or is unavailable. Don't do anything.
    return;
  }

  if (Accessible* acc = mGeckoAccessible.AsAccessible()) {
    acc->SetSelected([selected boolValue]);
  } else {
    mGeckoAccessible.AsProxy()->SetSelected([selected boolValue]);
  }

  // We need to invalidate the state because the accessibility service
  // may check the selected attribute synchornously and not wait for
  // selection events.
  [self invalidateState];
}

@end

@implementation mozTabGroupAccessible

- (NSArray*)moxTabs {
  return [self selectableChildren];
}

- (NSArray*)moxContents {
  return [self moxUnignoredChildren];
}

- (id)moxValue {
  // The value of a tab group is its selected child. In the case
  // of multiple selections this will return the first one.
  return [[self moxSelectedChildren] firstObject];
}

@end

@implementation mozTabAccessible

- (NSString*)moxRoleDescription {
  return utils::LocalizedString(u"tab"_ns);
}

- (id)moxValue {
  // Retuens 1 if item is selected, 0 if not.
  return [self moxSelected];
}

@end

@implementation mozListboxAccessible

- (BOOL)moxIgnoreChild:(mozAccessible*)child {
  if (!child || child->mRole == roles::GROUPING) {
    return YES;
  }

  return [super moxIgnoreChild:child];
}

- (BOOL)disableChild:(mozAccessible*)child {
  return ![child isKindOfClass:[mozSelectableChildAccessible class]];
}

- (NSString*)moxOrientation {
  return NSAccessibilityUnknownOrientationValue;
}

@end

@implementation mozOptionAccessible

- (NSString*)moxTitle {
  return @"";
}

- (id)moxValue {
  // Swap title and value of option so it behaves more like a AXStaticText.
  return [super moxTitle];
}

@end

@implementation mozMenuAccessible

- (NSString*)moxTitle {
  return @"";
}

- (NSString*)moxLabel {
  return @"";
}

- (BOOL)moxIgnoreWithParent:(mozAccessible*)parent {
  // This helps us generate the correct moxChildren array for
  // a sub menu -- that returned array should contain all
  // menu items, regardless of if they are visible or not.
  // Because moxChildren does ignore filtering, and because
  // our base ignore method filters out invisible accessibles,
  // we override this method.
  if ([parent isKindOfClass:[MOXWebAreaAccessible class]] ||
      [parent isKindOfClass:[MOXRootGroup class]]) {
    // We are a top level menu. Check our visibility the normal way
    return [super moxIgnoreWithParent:parent];
  }

  if ([parent isKindOfClass:[mozMenuItemAccessible class]] &&
      [parent geckoAccessible].Role() == roles::PARENT_MENUITEM) {
    // We are a submenu. If our parent menu item is in an open menu
    // we should not be ignored
    id grandparent = [parent moxParent];
    if ([grandparent isKindOfClass:[mozMenuAccessible class]]) {
      mozMenuAccessible* parentMenu =
          static_cast<mozMenuAccessible*>(grandparent);
      return ![parentMenu isOpened];
    }
  }

  // Otherwise, we call into our superclass's ignore method
  // to handle menus that are not submenus
  return [super moxIgnoreWithParent:parent];
}

- (NSArray*)moxVisibleChildren {
  // VO expects us to expose two lists of children on menus: all children
  // (done in moxUnignoredChildren), and children which are visible (here).
  // We implement ignoreWithParent for both menus and menu items
  // to ensure moxUnignoredChildren returns a complete list of children
  // regardless of visibility, see comments in those methods for additional
  // info.
  return [[self moxChildren]
      filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(
                                                   mozAccessible* child,
                                                   NSDictionary* bindings) {
        if (Accessible* acc = [child geckoAccessible].AsAccessible()) {
          if (acc->IsContent() && acc->GetContent()->IsXULElement()) {
            return ((acc->VisibilityState() & states::INVISIBLE) == 0);
          }
        }
        return true;
      }]];
}

- (id)moxTitleUIElement {
  id parent = [self moxUnignoredParent];
  if (parent && [parent isKindOfClass:[mozAccessible class]]) {
    return parent;
  }

  return nil;
}

- (void)moxPostNotification:(NSString*)notification {
  if ([notification isEqualToString:@"AXMenuOpened"]) {
    mIsOpened = YES;
  } else if ([notification isEqualToString:@"AXMenuClosed"]) {
    mIsOpened = NO;
  }

  [super moxPostNotification:notification];
}

- (void)expire {
  if (mIsOpened) {
    // VO needs to receive a menu closed event when the menu goes away.
    // If the menu is being destroyed, send a menu closed event first.
    [self moxPostNotification:@"AXMenuClosed"];
  }

  [super expire];
}

- (BOOL)isOpened {
  return mIsOpened;
}

@end

@implementation mozMenuItemAccessible

- (NSString*)moxLabel {
  return @"";
}

- (BOOL)moxIgnoreWithParent:(mozAccessible*)parent {
  // This helps us generate the correct moxChildren array for
  // a mozMenuAccessible; the returned array should contain all
  // menu items, regardless of if they are visible or not.
  // Because moxChildren does ignore filtering, and because
  // our base ignore method filters out invisible accessibles,
  // we override this method.
  AccessibleOrProxy parentAcc = [parent geckoAccessible];
  if (!parentAcc.IsNull()) {
    AccessibleOrProxy grandparentAcc = parentAcc.Parent();
    if (mozAccessible* directGrandparent =
            GetNativeFromGeckoAccessible(grandparentAcc)) {
      if ([directGrandparent isKindOfClass:[MOXWebAreaAccessible class]]) {
        return [parent moxIgnoreWithParent:directGrandparent];
      }
    }
  }

  id grandparent = [parent moxParent];
  if ([grandparent isKindOfClass:[mozMenuItemAccessible class]]) {
    mozMenuItemAccessible* acc =
        static_cast<mozMenuItemAccessible*>(grandparent);
    if ([acc geckoAccessible].Role() == roles::PARENT_MENUITEM) {
      mozMenuAccessible* parentMenu = static_cast<mozMenuAccessible*>(parent);
      // if we are a menu item in a submenu, display only when
      // parent menu item is open
      return ![parentMenu isOpened];
    }
  }

  // Otherwise, we call into our superclass's method to handle
  // menuitems that are not within submenus
  return [super moxIgnoreWithParent:parent];
}

- (NSString*)moxMenuItemMarkChar {
  Accessible* acc = mGeckoAccessible.AsAccessible();
  if (acc && acc->IsContent() &&
      acc->GetContent()->IsXULElement(nsGkAtoms::menuitem)) {
    // We need to provide a marker character. This is the visible "√" you see
    // on dropdown menus. In our a11y tree this is a single child text node
    // of the menu item.
    // We do this only with XUL menuitems that conform to the native theme, and
    // not with aria menu items that might have a pseudo element or something.
    if (acc->ChildCount() == 1 &&
        acc->FirstChild()->Role() == roles::STATICTEXT) {
      nsAutoString marker;
      acc->FirstChild()->Name(marker);
      if (marker.Length() == 1) {
        return nsCocoaUtils::ToNSString(marker);
      }
    }
  }

  return nil;
}

- (NSNumber*)moxSelected {
  // Our focused state is equivelent to native selected states for menus.
  return @([self stateWithMask:states::FOCUSED] != 0);
}

- (void)handleAccessibleEvent:(uint32_t)eventType {
  switch (eventType) {
    case nsIAccessibleEvent::EVENT_FOCUS:
      // Our focused state is equivelent to native selected states for menus.
      mozAccessible* parent = (mozAccessible*)[self moxUnignoredParent];
      [parent moxPostNotification:
                  NSAccessibilitySelectedChildrenChangedNotification];
      break;
  }

  [super handleAccessibleEvent:eventType];
}

@end