Commit ee437463 authored by Katie D's avatar Katie D Committed by Commit Bot

Add system voice as default Select-to-Speak voice option.

The system voice is chosen using the Text-to-Speech settings page
and depends on locale.

Bug: 866550
Change-Id: I249a023d9f018ef8ef1f242f82cf3c9d5b6d788b
Reviewed-on: https://chromium-review.googlesource.com/1150773Reviewed-by: default avatarDavid Tseng <dtseng@chromium.org>
Commit-Queue: Katie Dektar <katie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578668}
parent ff212bcc
......@@ -207,6 +207,9 @@ js_library("select_to_speak") {
}
js_library("select_to_speak_options") {
deps = [
":prefs_manager",
]
externs_list = [
"externs.js",
"$externs_path/accessibility_private.js",
......
......@@ -101,6 +101,7 @@
</div>
</div>
<script src="prefs_manager.js"></script>
<script src="select_to_speak_options.js"></script>
</body>
</html>
......@@ -73,14 +73,20 @@ let PrefsManager = function() {
this.voiceNameFromLocale_ = voices[0].voiceName;
chrome.storage.sync.get(['voice'], (prefs) => {
if (!prefs['voice']) {
chrome.storage.sync.set({'voice': voices[0].voiceName});
}
if (!prefs['voice'])
chrome.storage.sync.set({'voice': PrefsManager.SYSTEM_VOICE});
});
});
};
};
/**
* Constant representing the system TTS voice.
* @type {string}
* @public
*/
PrefsManager.SYSTEM_VOICE = 'select_to_speak_system_voice';
/**
* Loads preferences from chrome.storage, sets default values if
* necessary, and registers a listener to update prefs when they
......@@ -140,6 +146,10 @@ PrefsManager.prototype.speechOptions = function() {
enqueue: true
};
// To use the default (system) voice: don't specify options['voiceName'].
if (this.voiceNameFromPrefs_ === PrefsManager.SYSTEM_VOICE)
return options;
// Pick the voice name from prefs first, or the one that matches
// the locale next, but don't pick a voice that isn't currently
// loaded. If no voices are found, leave the voiceName option
......@@ -206,4 +216,4 @@ PrefsManager.prototype.highlightColor = function() {
*/
PrefsManager.prototype.focusRingColor = function() {
return this.color_;
};
\ No newline at end of file
};
......@@ -20,9 +20,9 @@ SelectToSpeakOptionsPage.prototype = {
window.speechSynthesis.onvoiceschanged = (function() {
this.populateVoiceList_('voice');
}.bind(this));
this.syncSelectControlToPref_('voice', 'voice');
this.syncSelectControlToPref_('voice', 'voice', 'voiceName');
this.syncRangeControlToPref_('rate', 'rate');
this.syncSelectControlToPref_('pitch', 'pitch');
this.syncSelectControlToPref_('pitch', 'pitch', 'value');
this.syncCheckboxControlToPref_(
'wordHighlight', 'wordHighlight', function(checked) {
let elem = document.getElementById('highlightSubOption');
......@@ -74,11 +74,14 @@ SelectToSpeakOptionsPage.prototype = {
*/
populateVoiceList_: function(selectId) {
chrome.tts.getVoices(function(voices) {
var select = document.getElementById(selectId);
let select = document.getElementById(selectId);
select.innerHTML = '';
var option = document.createElement('option');
option.voiceName = null;
option.innerText = option.voiceName;
// Add the system voice.
let option = document.createElement('option');
option.voiceName = PrefsManager.SYSTEM_VOICE;
option.innerText = chrome.i18n.getMessage('select_to_speak_system_voice');
select.add(option);
voices.forEach(function(voice) {
voice.voiceName = voice.voiceName || '';
......@@ -96,7 +99,7 @@ SelectToSpeakOptionsPage.prototype = {
// Required event types for Select-to-Speak.
return;
}
var option = document.createElement('option');
let option = document.createElement('option');
option.voiceName = voice.voiceName;
option.innerText = option.voiceName;
select.add(option);
......@@ -146,10 +149,11 @@ SelectToSpeakOptionsPage.prototype = {
* pref, sync them both ways.
* @param {string} selectId The id of the select element.
* @param {string} pref The name of a chrome.storage pref.
* @param {string} valueKey The key of the option to use as value.
* @param {?function(string): undefined=} opt_onChange Optional change
* listener to call when the setting has been changed.
*/
syncSelectControlToPref_: function(selectId, pref, opt_onChange) {
syncSelectControlToPref_: function(selectId, pref, valueKey, opt_onChange) {
var element = document.getElementById(selectId);
function updateFromPref() {
......@@ -157,7 +161,7 @@ SelectToSpeakOptionsPage.prototype = {
var value = items[pref];
element.selectedIndex = -1;
for (var i = 0; i < element.options.length; ++i) {
if (element.options[i].value == value) {
if (element.options[i][valueKey] == value) {
element.selectedIndex = i;
break;
}
......@@ -169,7 +173,7 @@ SelectToSpeakOptionsPage.prototype = {
}
element.addEventListener('change', function() {
var newValue = element.options[element.selectedIndex].value;
var newValue = element.options[element.selectedIndex][valueKey];
var setParams = {};
setParams[pref] = newValue;
chrome.storage.sync.set(setParams);
......@@ -224,7 +228,8 @@ SelectToSpeakOptionsPage.prototype = {
}
};
this.syncSelectControlToPref_('highlightColor', 'highlightColor', onChange);
this.syncSelectControlToPref_(
'highlightColor', 'highlightColor', 'value', onChange);
}
};
......
......@@ -192,6 +192,9 @@
<message desc="Label for highest synthesized speech pitch in the Select-to-speak options dialog." name="IDS_SELECT_TO_SPEAK_OPTIONS_PITCH_HIGHEST">
Highest
</message>
<message desc="Voice name for the system default Text-to-Speech voice" name="IDS_SELECT_TO_SPEAK_SYSTEM_VOICE">
System Text-to-Speech voice
</message>
</messages>
</release>
</grit>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment