Skip to content

Commit 222a24c

Browse files
authored
Merge pull request #20622 from calixteman/bug2014167
Let the toggle button in the alt-text dialog downloading (resp. delete) the model and enabling (resp. disabling) alt-text guessing (bug 2014167)
2 parents 1e0ba4d + ea993bf commit 222a24c

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

test/integration/stamp_editor_spec.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,9 @@ describe("Stamp Editor", () => {
11741174
// Run sequentially to avoid clipboard issues.
11751175
for (const [, page] of pages) {
11761176
await page.evaluate(() => {
1177-
window.PDFViewerApplication.mlManager.enableAltTextModelDownload = false;
1177+
const { mlManager } = window.PDFViewerApplication;
1178+
mlManager.enableGuessAltText =
1179+
mlManager.enableAltTextModelDownload = false;
11781180
});
11791181

11801182
await switchToStamp(page);
@@ -1197,7 +1199,9 @@ describe("Stamp Editor", () => {
11971199
// Run sequentially to avoid clipboard issues.
11981200
for (const [browserName, page] of pages) {
11991201
await page.evaluate(() => {
1200-
window.PDFViewerApplication.mlManager.enableAltTextModelDownload = true;
1202+
const { mlManager } = window.PDFViewerApplication;
1203+
mlManager.enableGuessAltText =
1204+
mlManager.enableAltTextModelDownload = true;
12011205
});
12021206
await switchToStamp(page);
12031207

web/new_alt_text_manager.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,21 @@ class NewAltTextManager {
122122
});
123123

124124
if (this.#uiManager) {
125-
this.#uiManager.setPreference("enableGuessAltText", checked);
125+
const isAltTextEnabled =
126+
await this.#uiManager.mlManager.isEnabledFor("altText");
127+
this.#createAutomaticallyButton.disabled = true;
128+
if (checked && !isAltTextEnabled) {
129+
this.#textarea.value = "";
130+
this.#setProgress();
131+
this.#uiManager.setPreference("enableGuessAltText", true);
132+
await this.#uiManager.mlManager.downloadModel("altText");
133+
this.#setPref("enableAltTextModelDownload", true);
134+
} else if (!checked && isAltTextEnabled) {
135+
this.#uiManager.setPreference("enableGuessAltText", false);
136+
await this.#uiManager.mlManager.deleteModel("altText");
137+
this.#setPref("enableAltTextModelDownload", false);
138+
}
139+
this.#createAutomaticallyButton.disabled = false;
126140
await this.#uiManager.mlManager.toggleService("altText", checked);
127141
}
128142
this.#toggleGuessAltText(checked, /* isInitial = */ false);
@@ -165,6 +179,14 @@ class NewAltTextManager {
165179
});
166180
}
167181

182+
#setPref(name, value) {
183+
this.#eventBus.dispatch("setpreference", {
184+
source: this,
185+
name,
186+
value,
187+
});
188+
}
189+
168190
#toggleLoading(value) {
169191
if (!this.#uiManager || this.#isAILoading === value) {
170192
return;
@@ -180,7 +202,7 @@ class NewAltTextManager {
180202
this.#dialog.classList.toggle("error", value);
181203
}
182204

183-
async #toggleGuessAltText(value, isInitial = false) {
205+
async #toggleGuessAltText(value, isInitial) {
184206
if (!this.#uiManager) {
185207
return;
186208
}
@@ -353,16 +375,15 @@ class NewAltTextManager {
353375
}
354376

355377
this.#firstTime = firstTime;
356-
let { mlManager } = uiManager;
357-
let hasAI = !!mlManager;
378+
const { mlManager } = uiManager;
379+
const hasAI = !!mlManager;
358380
this.#toggleTitleAndDisclaimer();
359381

360382
if (mlManager && !mlManager.isReady("altText")) {
361-
hasAI = false;
362383
if (mlManager.hasProgress) {
363384
this.#setProgress();
364385
} else {
365-
mlManager = null;
386+
this.#createAutomaticallyButton.setAttribute("aria-pressed", false);
366387
}
367388
} else {
368389
this.#downloadModel.classList.toggle("hidden", true);

0 commit comments

Comments
 (0)