Bug 1834014 - Add effects to always- and never-translate menuitems r=gregtatum

Adds functionality to the following menuitems:

always-translate language will immediately translate
the page if not already translated.

never-translate language will hide the translations
button and restore the page if translated.

never-translate site will hide the translations
button and restore the page if translated.

Depends on D177860

Differential Revision: https://phabricator.services.mozilla.com/D178512
This commit is contained in:
Erik Nordin 2023-05-24 01:22:44 +00:00
Родитель ca64f2b0e0
Коммит 5e165ee981
2 изменённых файлов: 65 добавлений и 12 удалений

Просмотреть файл

@ -504,6 +504,28 @@ var TranslationsPanel = new (class {
}).catch(error => this.console.error(error));
}
/**
* Removes the translations button.
*/
#hideTranslationsButton() {
const { button, buttonLocale, buttonCircleArrows } = this.elements;
button.hidden = true;
buttonLocale.hidden = true;
buttonCircleArrows.hidden = true;
button.removeAttribute("translationsactive");
}
/**
* Returns true if translations is currently active, otherwise false.
*
* @returns {boolean}
*/
#isTranslationsActive() {
const { requestedTranslationPair } =
this.#getTranslationsActor().languageState;
return requestedTranslationPair !== null;
}
/**
* Handle the translation button being clicked on the default view.
*/
@ -570,8 +592,20 @@ var TranslationsPanel = new (class {
*/
async onAlwaysTranslateLanguage() {
const docLangTag = await this.#getDocLangTag();
TranslationsParent.toggleAlwaysTranslateLanguagePref(docLangTag);
await this.#updateSettingsMenuLanguageCheckboxStates();
const toggledOn =
TranslationsParent.toggleAlwaysTranslateLanguagePref(docLangTag);
const translationsActive = this.#isTranslationsActive();
this.#updateSettingsMenuLanguageCheckboxStates();
if (toggledOn && !translationsActive) {
await this.onDefaultTranslate();
} else if (!toggledOn && translationsActive) {
await this.onRestore();
}
// If always-translate was activated while translations is active
// If always-translate was deactivated while translations is inactive
// There is no need to change the state of the page.
}
/**
@ -582,7 +616,13 @@ var TranslationsPanel = new (class {
async onNeverTranslateLanguage() {
const docLangTag = await this.#getDocLangTag();
TranslationsParent.toggleNeverTranslateLanguagePref(docLangTag);
await this.#updateSettingsMenuLanguageCheckboxStates();
this.#updateSettingsMenuLanguageCheckboxStates();
if (this.#isTranslationsActive()) {
await this.onRestore();
} else {
this.#hideTranslationsButton();
}
}
/**
@ -592,7 +632,13 @@ var TranslationsPanel = new (class {
*/
async onNeverTranslateSite() {
await this.#getTranslationsActor().toggleNeverTranslateSitePermissions();
await this.#updateSettingsMenuSiteCheckboxStates();
this.#updateSettingsMenuSiteCheckboxStates();
if (this.#isTranslationsActive()) {
await this.onRestore();
} else {
this.#hideTranslationsButton();
}
}
/**
@ -654,8 +700,7 @@ var TranslationsPanel = new (class {
buttonCircleArrows.hidden = true;
}
} else {
button.removeAttribute("translationsactive");
button.hidden = true;
this.#hideTranslationsButton();
}
switch (error) {

Просмотреть файл

@ -1487,14 +1487,19 @@ export class TranslationsParent extends JSWindowActorParent {
* to the pref list if it is not present, or removing it if it is present.
*
* @param {string} langTag - A BCP-47 language tag
* @returns {boolean} Whether the pref was toggled on for the langTag
*/
static toggleAlwaysTranslateLanguagePref(langTag) {
if (TranslationsParent.shouldAlwaysTranslateLanguage(langTag)) {
// The pref was toggled off for this langTag
this.#removeLangTagFromPref(langTag, ALWAYS_TRANSLATE_LANGS_PREF);
} else {
this.#addLangTagToPref(langTag, ALWAYS_TRANSLATE_LANGS_PREF);
this.#removeLangTagFromPref(langTag, NEVER_TRANSLATE_LANGS_PREF);
return false;
}
// The pref was toggled on for this langTag
this.#addLangTagToPref(langTag, ALWAYS_TRANSLATE_LANGS_PREF);
this.#removeLangTagFromPref(langTag, NEVER_TRANSLATE_LANGS_PREF);
return true;
}
/**
@ -1505,11 +1510,14 @@ export class TranslationsParent extends JSWindowActorParent {
*/
static toggleNeverTranslateLanguagePref(langTag) {
if (TranslationsParent.shouldNeverTranslateLanguage(langTag)) {
// The pref was toggled off for this langTag
this.#removeLangTagFromPref(langTag, NEVER_TRANSLATE_LANGS_PREF);
} else {
this.#addLangTagToPref(langTag, NEVER_TRANSLATE_LANGS_PREF);
this.#removeLangTagFromPref(langTag, ALWAYS_TRANSLATE_LANGS_PREF);
return;
}
// The pref was toggled on for this langTag
this.#addLangTagToPref(langTag, NEVER_TRANSLATE_LANGS_PREF);
this.#removeLangTagFromPref(langTag, ALWAYS_TRANSLATE_LANGS_PREF);
}
/**