Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/browser/resources/chromeos/chromevox/cvox2/background/live_regions.js

Issue 1457683009: Complete live region support in ChromeVox Next. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 /**
6 * @fileoverview Implements support for live regions in ChromeVox Next.
7 */
8
9 goog.provide('LiveRegions');
10
11 goog.require('ChromeVoxState');
12
13 /**
14 * ChromeVox2 live region handler.
15 * @param {ChromeVoxState} chromeVoxState The ChromeVox state object,
16 * keeping track of the current mode and current range.
17 * @constructor
18 */
19 LiveRegions = function(chromeVoxState) {
20 /**
21 * @type {ChromeVoxState}
22 * @private
23 */
24 this.chromeVoxState_ = chromeVoxState;
25
26 /**
27 * The time the last live region event was output.
28 * @type {Date}
29 * @private
30 */
31 this.lastLiveRegionTime_ = new Date(0);
32
33 /**
34 * Set of nodes that have been announced as part of a live region since
35 * |this.lastLiveRegionTime_|, to prevent duplicate announcements.
36 * @type {WeakSet<chrome.automation.AutomationNode>}
37 * @private
38 */
39 this.liveRegionNodeSet_ = new WeakSet();
40
41 chrome.automation.setTreeChangeObserverMask('liveRegionTreeChanges');
42 chrome.automation.addTreeChangeObserver(this.onTreeChange.bind(this));
43 };
44
45 /**
46 * Live region events received in fewer than this many milliseconds will
47 * queue, otherwise they'll be output with a category flush.
48 * @type {number}
49 * @const
50 */
51 LiveRegions.LIVE_REGION_QUEUE_TIME_MS = 500;
52
53 LiveRegions.prototype = {
54 /**
55 * Called when the automation tree is changed.
56 * @param {chrome.automation.TreeChange} treeChange
Peter Lundblad 2015/11/24 11:04:31 Suggest using goog.scope like in some other files
dmazzoni 2015/11/30 22:00:47 Done.
57 */
58 onTreeChange: function(treeChange) {
59 var node = treeChange.target;
60 if (!node.containerLiveStatus)
61 return;
62
63 var mode = this.chromeVoxState_.getMode();
64 var currentRange = this.chromeVoxState_.getCurrentRange();
65
66 if (mode === ChromeVoxMode.CLASSIC || !cvox.ChromeVox.isActive)
67 return;
68
69 if (!currentRange)
70 return;
71
72 if (!AutomationUtil.isInSameWebpage(node, currentRange.start.node))
Peter Lundblad 2015/11/24 11:04:31 Doesn't classic announce live region updates from
dmazzoni 2015/11/30 22:00:47 Good catch. I switched it but made it an option s
73 return;
74
75 var type = treeChange.type;
76 var relevant = node.containerLiveRelevant;
77 if (relevant.indexOf('additions') >= 0 &&
78 (type == 'nodeCreated' || type == 'subtreeCreated')) {
79 this.outputLiveRegionChange_(node, null);
80 }
81
82 if (relevant.indexOf('text') >= 0 && type == 'nodeChanged')
83 this.outputLiveRegionChange_(node, null);
84
85 if (relevant.indexOf('removals') >= 0 && type == 'nodeRemoved')
86 this.outputLiveRegionChange_(node, '@live_regions_removed');
87 },
88
89 /**
90 * Given a node that needs to be spoken as part of a live region
91 * change and an additional optional format string, output the
92 * live region description.
93 * @param {!chrome.automation.AutomationNode} node The changed node.
94 * @param {?string} opt_prependFormatStr If set, a format string for
Peter Lundblad 2015/11/24 11:04:31 Optional parameters should have an = suffix.
dmazzoni 2015/11/30 22:00:47 Done.
95 * cvox2.Output to prepend to the output.
96 * @private
97 */
98 outputLiveRegionChange_: function(node, opt_prependFormatStr) {
99 if (node.containerLiveBusy)
100 return;
101
102 if (node.containerLiveAtomic && !node.liveAtomic) {
103 if (node.parent)
104 this.outputLiveRegionChange_(node.parent, opt_prependFormatStr);
105 return;
106 }
107
108 var range = cursors.Range.fromNode(node);
109 var output = new Output();
110 if (opt_prependFormatStr) {
Peter Lundblad 2015/11/24 11:04:31 nit: bracket inconsistency.
dmazzoni 2015/11/30 22:00:47 Done.
111 output.format(opt_prependFormatStr);
112 }
113 output.withSpeech(range, range, Output.EventType.NAVIGATE);
114
115 if (!output.hasSpeech && node.liveAtomic) {
Peter Lundblad 2015/11/24 11:04:31 Ditto.
dmazzoni 2015/11/30 22:00:47 Done.
116 output.format('$descendants', node);
117 }
118
119 output.withSpeechCategory(cvox.TtsCategory.LIVE);
120
121 if (!output.hasSpeech)
122 return;
123
124 // Enqueue live region updates that were received at approximately
125 // the same time, otherwise flush previous live region updates.
126 var currentTime = new Date();
127 var queueTime = LiveRegions.LIVE_REGION_QUEUE_TIME_MS;
128 if (currentTime - this.lastLiveRegionTime_ > queueTime) {
129 this.liveRegionNodeSet_ = new WeakSet();
130 output.withQueueMode(cvox.QueueMode.CATEGORY_FLUSH);
131 this.lastLiveRegionTime_ = currentTime;
132 } else {
133 output.withQueueMode(cvox.QueueMode.QUEUE);
134 }
135
136 var parent = node;
137 while (parent) {
138 if (this.liveRegionNodeSet_.has(parent))
139 return;
140 parent = parent.parent;
141 }
142
143 this.liveRegionNodeSet_.add(node);
144 output.go();
145 },
146 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698