summaryrefslogtreecommitdiffstats
path: root/accessible/mac/MOXTextMarkerDelegate.mm
blob: 3e1e451ddd69fef78ed5d345552a98e880dfe796 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* 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 <Cocoa/Cocoa.h>
#include "DocAccessible.h"

#import "MOXTextMarkerDelegate.h"

#include "mozAccessible.h"
#include "mozilla/Preferences.h"
#include "nsISelectionListener.h"

using namespace mozilla::a11y;

#define PREF_ACCESSIBILITY_MAC_DEBUG "accessibility.mac.debug"

static nsTHashMap<nsPtrHashKey<mozilla::a11y::Accessible>,
                  MOXTextMarkerDelegate*>
    sDelegates;

@implementation MOXTextMarkerDelegate

+ (id)getOrCreateForDoc:(mozilla::a11y::Accessible*)aDoc {
  MOZ_ASSERT(aDoc);

  MOXTextMarkerDelegate* delegate = sDelegates.Get(aDoc);
  if (!delegate) {
    delegate = [[MOXTextMarkerDelegate alloc] initWithDoc:aDoc];
    sDelegates.InsertOrUpdate(aDoc, delegate);
    [delegate retain];
  }

  return delegate;
}

+ (void)destroyForDoc:(mozilla::a11y::Accessible*)aDoc {
  MOZ_ASSERT(aDoc);

  MOXTextMarkerDelegate* delegate = sDelegates.Get(aDoc);
  if (delegate) {
    sDelegates.Remove(aDoc);
    [delegate release];
  }
}

- (id)initWithDoc:(Accessible*)aDoc {
  MOZ_ASSERT(aDoc, "Cannot init MOXTextDelegate with null");
  if ((self = [super init])) {
    mGeckoDocAccessible = aDoc;
  }

  mCaretMoveGranularity = nsISelectionListener::NO_AMOUNT;

  return self;
}

- (void)dealloc {
  [self invalidateSelection];
  [super dealloc];
}

- (void)setSelectionFrom:(Accessible*)startContainer
                      at:(int32_t)startOffset
                      to:(Accessible*)endContainer
                      at:(int32_t)endOffset {
  GeckoTextMarkerRange selection(GeckoTextMarker(startContainer, startOffset),
                                 GeckoTextMarker(endContainer, endOffset));

  // We store it as an AXTextMarkerRange because it is a safe
  // way to keep a weak reference - when we need to use the
  // range we can convert it back to a GeckoTextMarkerRange
  // and check that it's valid.
  mSelection = selection.CreateAXTextMarkerRange();
  CFRetain(mSelection);
}

- (void)setCaretOffset:(mozilla::a11y::Accessible*)container
                    at:(int32_t)offset
       moveGranularity:(int32_t)granularity {
  GeckoTextMarker caretMarker(container, offset);

  mPrevCaret = mCaret;
  mCaret = caretMarker.CreateAXTextMarker();
  mCaretMoveGranularity = granularity;

  CFRetain(mCaret);
}

mozAccessible* GetEditableNativeFromGeckoAccessible(Accessible* aAcc) {
  // The gecko accessible may not have a native accessible so we need
  // to walk up the parent chain to find the nearest one.
  // This happens when caching is enabled and the text marker's accessible
  // may be a text leaf that is pruned from the platform.
  for (Accessible* acc = aAcc; acc; acc = acc->Parent()) {
    if (mozAccessible* mozAcc = GetNativeFromGeckoAccessible(acc)) {
      return [mozAcc moxEditableAncestor];
    }
  }

  return nil;
}

// This returns an info object to pass with AX SelectedTextChanged events.
// It uses the current and previous caret position to make decisions
// regarding which attributes to add to the info object.
- (NSDictionary*)selectionChangeInfo {
  GeckoTextMarkerRange selectedGeckoRange =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, mSelection);

  int32_t stateChangeType =
      selectedGeckoRange.Start() == selectedGeckoRange.End()
          ? AXTextStateChangeTypeSelectionMove
          : AXTextStateChangeTypeSelectionExtend;

  // This is the base info object, includes the selected marker range and
  // the change type depending on the collapsed state of the selection.
  NSMutableDictionary* info = [[@{
    @"AXSelectedTextMarkerRange" : selectedGeckoRange.IsValid()
        ? (__bridge id)mSelection
        : [NSNull null],
    @"AXTextStateChangeType" : @(stateChangeType),
  } mutableCopy] autorelease];

  GeckoTextMarker caretMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, mCaret);
  GeckoTextMarker prevCaretMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, mPrevCaret);

  if (!caretMarker.IsValid()) {
    // If the current caret is invalid, stop here and return base info.
    return info;
  }

  mozAccessible* caretEditable =
      GetEditableNativeFromGeckoAccessible(caretMarker.Acc());

  if (!caretEditable && stateChangeType == AXTextStateChangeTypeSelectionMove) {
    // If we are not in an editable, VO expects AXTextStateSync to be present
    // and true.
    info[@"AXTextStateSync"] = @YES;
  }

  if (!prevCaretMarker.IsValid() || caretMarker == prevCaretMarker) {
    // If we have no stored previous marker, stop here.
    return info;
  }

  mozAccessible* prevCaretEditable =
      GetEditableNativeFromGeckoAccessible(prevCaretMarker.Acc());

  if (prevCaretEditable != caretEditable) {
    // If the caret goes in or out of an editable, consider the
    // move direction "discontiguous".
    info[@"AXTextSelectionDirection"] =
        @(AXTextSelectionDirectionDiscontiguous);
    if ([[caretEditable moxFocused] boolValue]) {
      // If the caret is in a new focused editable, VO expects this attribute to
      // be present and to be true.
      info[@"AXTextSelectionChangedFocus"] = @YES;
    }

    return info;
  }

  bool isForward = prevCaretMarker < caretMarker;
  int direction = isForward ? AXTextSelectionDirectionNext
                            : AXTextSelectionDirectionPrevious;

  int32_t granularity = AXTextSelectionGranularityUnknown;
  switch (mCaretMoveGranularity) {
    case nsISelectionListener::CHARACTER_AMOUNT:
    case nsISelectionListener::CLUSTER_AMOUNT:
      granularity = AXTextSelectionGranularityCharacter;
      break;
    case nsISelectionListener::WORD_AMOUNT:
    case nsISelectionListener::WORDNOSPACE_AMOUNT:
      granularity = AXTextSelectionGranularityWord;
      break;
    case nsISelectionListener::LINE_AMOUNT:
      granularity = AXTextSelectionGranularityLine;
      break;
    case nsISelectionListener::BEGINLINE_AMOUNT:
      direction = AXTextSelectionDirectionBeginning;
      granularity = AXTextSelectionGranularityLine;
      break;
    case nsISelectionListener::ENDLINE_AMOUNT:
      direction = AXTextSelectionDirectionEnd;
      granularity = AXTextSelectionGranularityLine;
      break;
    case nsISelectionListener::PARAGRAPH_AMOUNT:
      granularity = AXTextSelectionGranularityParagraph;
      break;
    default:
      break;
  }

  // Determine selection direction with marker comparison.
  // If the delta between the two markers is more than one, consider it
  // a word. Not accurate, but good enough for VO.
  [info addEntriesFromDictionary:@{
    @"AXTextSelectionDirection" : @(direction),
    @"AXTextSelectionGranularity" : @(granularity)
  }];

  return info;
}

- (void)invalidateSelection {
  CFRelease(mSelection);
  CFRelease(mCaret);
  CFRelease(mPrevCaret);
  mSelection = nil;
}

- (mozilla::a11y::GeckoTextMarkerRange)selection {
  return mozilla::a11y::GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
      mGeckoDocAccessible, mSelection);
}

- (AXTextMarkerRef)moxStartTextMarker {
  GeckoTextMarker geckoTextPoint(mGeckoDocAccessible, 0);
  return geckoTextPoint.CreateAXTextMarker();
}

- (AXTextMarkerRef)moxEndTextMarker {
  GeckoTextMarker geckoTextPoint(mGeckoDocAccessible,
                                 nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT);
  return geckoTextPoint.CreateAXTextMarker();
}

- (AXTextMarkerRangeRef)moxSelectedTextMarkerRange {
  return mSelection && GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
                           mGeckoDocAccessible, mSelection)
                           .IsValid()
             ? mSelection
             : nil;
}

- (NSString*)moxStringForTextMarkerRange:(AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);
  if (!range.IsValid()) {
    return @"";
  }

  return range.Text();
}

- (NSNumber*)moxLengthForTextMarkerRange:(AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);
  if (!range.IsValid()) {
    return @0;
  }

  return @(range.Length());
}

- (AXTextMarkerRangeRef)moxTextMarkerRangeForUnorderedTextMarkers:
    (NSArray*)textMarkers {
  if ([textMarkers count] != 2) {
    // Don't allow anything but a two member array.
    return nil;
  }

  GeckoTextMarker p1 = GeckoTextMarker::MarkerFromAXTextMarker(
      mGeckoDocAccessible, (__bridge AXTextMarkerRef)textMarkers[0]);
  GeckoTextMarker p2 = GeckoTextMarker::MarkerFromAXTextMarker(
      mGeckoDocAccessible, (__bridge AXTextMarkerRef)textMarkers[1]);

  if (!p1.IsValid() || !p2.IsValid()) {
    // If either marker is invalid, return nil.
    return nil;
  }

  bool ordered = p1 < p2;
  GeckoTextMarkerRange range(ordered ? p1 : p2, ordered ? p2 : p1);

  return range.CreateAXTextMarkerRange();
}

- (AXTextMarkerRef)moxStartTextMarkerForTextMarkerRange:
    (AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);

  return range.IsValid() ? range.Start().CreateAXTextMarker() : nil;
}

- (AXTextMarkerRef)moxEndTextMarkerForTextMarkerRange:
    (AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);

  return range.IsValid() ? range.End().CreateAXTextMarker() : nil;
}

- (AXTextMarkerRangeRef)moxLeftWordTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.LeftWordRange().CreateAXTextMarkerRange();
}

- (AXTextMarkerRangeRef)moxRightWordTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.RightWordRange().CreateAXTextMarkerRange();
}

- (AXTextMarkerRangeRef)moxLineTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.LineRange().CreateAXTextMarkerRange();
}

- (AXTextMarkerRangeRef)moxLeftLineTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.LeftLineRange().CreateAXTextMarkerRange();
}

- (AXTextMarkerRangeRef)moxRightLineTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.RightLineRange().CreateAXTextMarkerRange();
}

- (AXTextMarkerRangeRef)moxParagraphTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.ParagraphRange().CreateAXTextMarkerRange();
}

// override
- (AXTextMarkerRangeRef)moxStyleTextMarkerRangeForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.StyleRange().CreateAXTextMarkerRange();
}

- (AXTextMarkerRef)moxNextTextMarkerForTextMarker:(AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  if (!geckoTextMarker.Next()) {
    return nil;
  }

  return geckoTextMarker.CreateAXTextMarker();
}

- (AXTextMarkerRef)moxPreviousTextMarkerForTextMarker:
    (AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  if (!geckoTextMarker.Previous()) {
    return nil;
  }

  return geckoTextMarker.CreateAXTextMarker();
}

- (NSAttributedString*)moxAttributedStringForTextMarkerRange:
    (AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);
  if (!range.IsValid()) {
    return nil;
  }

  return range.AttributedText();
}

- (NSValue*)moxBoundsForTextMarkerRange:(AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);
  if (!range.IsValid()) {
    return nil;
  }

  return range.Bounds();
}

- (NSNumber*)moxIndexForTextMarker:(AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  GeckoTextMarkerRange range(GeckoTextMarker(mGeckoDocAccessible, 0),
                             geckoTextMarker);

  return @(range.Length());
}

- (AXTextMarkerRef)moxTextMarkerForIndex:(NSNumber*)index {
  GeckoTextMarker geckoTextMarker = GeckoTextMarker::MarkerFromIndex(
      mGeckoDocAccessible, [index integerValue]);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  return geckoTextMarker.CreateAXTextMarker();
}

- (id)moxUIElementForTextMarker:(AXTextMarkerRef)textMarker {
  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return nil;
  }

  Accessible* leaf = geckoTextMarker.Leaf();
  if (!leaf) {
    return nil;
  }

  return GetNativeFromGeckoAccessible(leaf);
}

- (AXTextMarkerRangeRef)moxTextMarkerRangeForUIElement:(id)element {
  if (![element isKindOfClass:[mozAccessible class]]) {
    return nil;
  }

  GeckoTextMarkerRange range((Accessible*)[element geckoAccessible]);
  return range.CreateAXTextMarkerRange();
}

- (NSString*)moxMozDebugDescriptionForTextMarker:(AXTextMarkerRef)textMarker {
  if (!mozilla::Preferences::GetBool(PREF_ACCESSIBILITY_MAC_DEBUG)) {
    return nil;
  }

  GeckoTextMarker geckoTextMarker =
      GeckoTextMarker::MarkerFromAXTextMarker(mGeckoDocAccessible, textMarker);
  if (!geckoTextMarker.IsValid()) {
    return @"<GeckoTextMarker 0x0 [0]>";
  }

  return [NSString stringWithFormat:@"<GeckoTextMarker %p [%d]>",
                                    geckoTextMarker.Acc(),
                                    geckoTextMarker.Offset()];
}

- (NSString*)moxMozDebugDescriptionForTextMarkerRange:
    (AXTextMarkerRangeRef)textMarkerRange {
  if (!mozilla::Preferences::GetBool(PREF_ACCESSIBILITY_MAC_DEBUG)) {
    return nil;
  }

  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);
  if (!range.IsValid()) {
    return @"<GeckoTextMarkerRange 0x0 [0] - 0x0 [0]>";
  }

  return [NSString stringWithFormat:@"<GeckoTextMarkerRange %p [%d] - %p [%d]>",
                                    range.Start().Acc(), range.Start().Offset(),
                                    range.End().Acc(), range.End().Offset()];
}

- (void)moxSetSelectedTextMarkerRange:(AXTextMarkerRangeRef)textMarkerRange {
  mozilla::a11y::GeckoTextMarkerRange range =
      GeckoTextMarkerRange::MarkerRangeFromAXTextMarkerRange(
          mGeckoDocAccessible, textMarkerRange);
  if (range.IsValid()) {
    range.Select();
  }
}

@end