Revert "[Editor] Dispatch changes in prefs enableAltTextModelDownload and enableGuessAltText to the viewer (bug 1912024)"

This commit is contained in:
calixteman 2024-08-07 23:11:31 +02:00 committed by GitHub
parent fef2853263
commit 6c005eabb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 51 additions and 162 deletions

View file

@ -141,11 +141,9 @@ class StampEditor extends AnnotationEditor {
this._uiManager.useNewAltTextFlow && this._uiManager.useNewAltTextFlow &&
this.#bitmap this.#bitmap
) { ) {
try { // The alt-text dialog isn't opened but we still want to guess the alt
// The alt-text dialog isn't opened but we still want to guess the alt // text.
// text. this.mlGuessAltText();
this.mlGuessAltText();
} catch {}
} }
this.div.focus(); this.div.focus();
@ -157,11 +155,8 @@ class StampEditor extends AnnotationEditor {
} }
const { mlManager } = this._uiManager; const { mlManager } = this._uiManager;
if (!mlManager) { if (!mlManager || !(await mlManager.isEnabledFor("altText"))) {
throw new Error("No ML."); return null;
}
if (!(await mlManager.isEnabledFor("altText"))) {
throw new Error("ML isn't enabled for alt text.");
} }
const { data, width, height } = const { data, width, height } =
imageData || imageData ||
@ -175,18 +170,9 @@ class StampEditor extends AnnotationEditor {
channels: data.length / (width * height), channels: data.length / (width * height),
}, },
}); });
if (!response) { if (!response || response.error || !response.output) {
throw new Error("No response from the AI service.");
}
if (response.error) {
throw new Error("Error from the AI service.");
}
if (response.cancel) {
return null; return null;
} }
if (!response.output) {
throw new Error("No valid response from the AI service.");
}
const altText = response.output; const altText = response.output;
await this.setGuessedAltText(altText); await this.setGuessedAltText(altText);
if (updateAltTextData && !this.hasAltTextData()) { if (updateAltTextData && !this.hasAltTextData()) {

View file

@ -404,7 +404,9 @@ const PDFViewerApplication = {
} else { } else {
eventBus = new EventBus(); eventBus = new EventBus();
} }
this.mlManager?.setEventBus(eventBus, this._globalAbortController.signal); if (this.mlManager) {
this.mlManager.eventBus = eventBus;
}
this.eventBus = eventBus; this.eventBus = eventBus;
this.overlayManager = new OverlayManager(); this.overlayManager = new OverlayManager();

View file

@ -193,12 +193,12 @@ const defaultOptions = {
enableAltTextModelDownload: { enableAltTextModelDownload: {
/** @type {boolean} */ /** @type {boolean} */
value: true, value: true,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE + OptionKind.EVENT_DISPATCH, kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
}, },
enableGuessAltText: { enableGuessAltText: {
/** @type {boolean} */ /** @type {boolean} */
value: true, value: true,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE + OptionKind.EVENT_DISPATCH, kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
}, },
enableHighlightEditor: { enableHighlightEditor: {
// We'll probably want to make some experiments before enabling this // We'll probably want to make some experiments before enabling this

View file

@ -307,15 +307,11 @@ class FirefoxScripting {
} }
class MLManager { class MLManager {
#abortSignal = null;
#enabled = null; #enabled = null;
#eventBus = null;
#ready = null; #ready = null;
#requestResolvers = null; eventBus = null;
hasProgress = false; hasProgress = false;
@ -334,32 +330,6 @@ class MLManager {
this.enableGuessAltText = enableGuessAltText; this.enableGuessAltText = enableGuessAltText;
} }
setEventBus(eventBus, abortSignal) {
this.#eventBus = eventBus;
this.#abortSignal = abortSignal;
eventBus._on(
"enablealttextmodeldownload",
({ value }) => {
if (this.enableAltTextModelDownload === value) {
return;
}
if (value) {
this.downloadModel("altText");
} else {
this.deleteModel("altText");
}
},
abortSignal
);
eventBus._on(
"enableguessalttext",
({ value }) => {
this.toggleService("altText", value);
},
abortSignal
);
}
async isEnabledFor(name) { async isEnabledFor(name) {
return this.enableGuessAltText && !!(await this.#enabled?.get(name)); return this.enableGuessAltText && !!(await this.#enabled?.get(name));
} }
@ -369,17 +339,16 @@ class MLManager {
} }
async deleteModel(name) { async deleteModel(name) {
if (name !== "altText" || !this.enableAltTextModelDownload) { if (name !== "altText") {
return; return;
} }
this.enableAltTextModelDownload = false; this.enableAltTextModelDownload = false;
this.#ready?.delete(name); this.#ready?.delete(name);
this.#enabled?.delete(name); this.#enabled?.delete(name);
await this.toggleService("altText", false); await Promise.all([
await FirefoxCom.requestAsync( this.toggleService("altText", false),
"mlDelete", FirefoxCom.requestAsync("mlDelete", MLManager.#AI_ALT_TEXT_MODEL_NAME),
MLManager.#AI_ALT_TEXT_MODEL_NAME ]);
);
} }
async loadModel(name) { async loadModel(name) {
@ -389,7 +358,7 @@ class MLManager {
} }
async downloadModel(name) { async downloadModel(name) {
if (name !== "altText" || this.enableAltTextModelDownload) { if (name !== "altText") {
return null; return null;
} }
this.enableAltTextModelDownload = true; this.enableAltTextModelDownload = true;
@ -400,53 +369,18 @@ class MLManager {
if (data?.name !== "altText") { if (data?.name !== "altText") {
return null; return null;
} }
const resolvers = (this.#requestResolvers ||= new Set());
const resolver = Promise.withResolvers();
resolvers.add(resolver);
data.service = MLManager.#AI_ALT_TEXT_MODEL_NAME; data.service = MLManager.#AI_ALT_TEXT_MODEL_NAME;
const requestPromise = FirefoxCom.requestAsync("mlGuess", data); return FirefoxCom.requestAsync("mlGuess", data);
requestPromise
.then(response => {
if (resolvers.has(resolver)) {
resolver.resolve(response);
resolvers.delete(resolver);
}
})
.catch(reason => {
if (resolvers.has(resolver)) {
resolver.reject(reason);
resolvers.delete(resolver);
}
});
return resolver.promise;
}
async #cancelAllRequests() {
if (!this.#requestResolvers) {
return;
}
for (const resolver of this.#requestResolvers) {
resolver.resolve({ cancel: true });
}
this.#requestResolvers.clear();
this.#requestResolvers = null;
} }
async toggleService(name, enabled) { async toggleService(name, enabled) {
if (name !== "altText" || this.enableGuessAltText === enabled) { if (name !== "altText") {
return; return;
} }
this.enableGuessAltText = enabled; this.enableGuessAltText = enabled;
if (enabled) { if (enabled && this.enableAltTextModelDownload) {
if (this.enableAltTextModelDownload) { await this.#loadAltTextEngine(false);
await this.#loadAltTextEngine(false);
}
} else {
this.#cancelAllRequests();
} }
} }
@ -469,7 +403,7 @@ class MLManager {
if (listenToProgress) { if (listenToProgress) {
this.hasProgress = true; this.hasProgress = true;
const callback = ({ detail }) => { const callback = ({ detail }) => {
this.#eventBus.dispatch("loadaiengineprogress", { this.eventBus.dispatch("loadaiengineprogress", {
source: this, source: this,
detail, detail,
}); });
@ -478,9 +412,7 @@ class MLManager {
window.removeEventListener("loadAIEngineProgress", callback); window.removeEventListener("loadAIEngineProgress", callback);
} }
}; };
window.addEventListener("loadAIEngineProgress", callback, { window.addEventListener("loadAIEngineProgress", callback);
signal: this.#abortSignal,
});
promise.then(ok => { promise.then(ok => {
if (!ok) { if (!ok) {
this.hasProgress = false; this.hasProgress = false;

View file

@ -79,10 +79,6 @@ class FakeMLManager {
this.enableAltTextModelDownload = enableAltTextModelDownload; this.enableAltTextModelDownload = enableAltTextModelDownload;
} }
setEventBus(eventBus, abortSignal) {
this.eventBus = eventBus;
}
async isEnabledFor(_name) { async isEnabledFor(_name) {
return this.enableGuessAltText; return this.enableGuessAltText;
} }

View file

@ -137,10 +137,6 @@ class NewAltTextManager {
this.#toggleDisclaimer(); this.#toggleDisclaimer();
}); });
eventBus._on("enableguessalttext", ({ value }) => {
this.#toggleGuessAltText(value, /* isInitial = */ false);
});
this.#overlayManager.register(dialog); this.#overlayManager.register(dialog);
} }
@ -251,12 +247,13 @@ class NewAltTextManager {
this.#imageData, this.#imageData,
/* updateAltTextData = */ false /* updateAltTextData = */ false
); );
if (altText) { if (altText === null) {
this.#guessedAltText = altText; throw new Error("No valid response from the AI service.");
this.#wasAILoading = this.#isAILoading; }
if (this.#isAILoading) { this.#guessedAltText = altText;
this.#addAltText(altText); this.#wasAILoading = this.#isAILoading;
} if (this.#isAILoading) {
this.#addAltText(altText);
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -461,8 +458,6 @@ class ImageAltTextSettings {
#createModelButton; #createModelButton;
#downloadModelButton;
#dialog; #dialog;
#eventBus; #eventBus;
@ -491,7 +486,6 @@ class ImageAltTextSettings {
this.#dialog = dialog; this.#dialog = dialog;
this.#aiModelSettings = aiModelSettings; this.#aiModelSettings = aiModelSettings;
this.#createModelButton = createModelButton; this.#createModelButton = createModelButton;
this.#downloadModelButton = downloadModelButton;
this.#showAltTextDialogButton = showAltTextDialogButton; this.#showAltTextDialogButton = showAltTextDialogButton;
this.#overlayManager = overlayManager; this.#overlayManager = overlayManager;
this.#eventBus = eventBus; this.#eventBus = eventBus;
@ -514,61 +508,40 @@ class ImageAltTextSettings {
this.#togglePref.bind(this, "enableNewAltTextWhenAddingImage") this.#togglePref.bind(this, "enableNewAltTextWhenAddingImage")
); );
deleteModelButton.addEventListener("click", this.#delete.bind(this, true)); deleteModelButton.addEventListener("click", async () => {
downloadModelButton.addEventListener( await mlManager.deleteModel("altText");
"click",
this.#download.bind(this, true)
);
closeButton.addEventListener("click", this.#finish.bind(this)); aiModelSettings.classList.toggle("download", true);
createModelButton.disabled = true;
eventBus._on("enablealttextmodeldownload", ({ value }) => { createModelButton.setAttribute("aria-pressed", false);
if (value) { this.#setPref("enableGuessAltText", false);
this.#download(false); this.#setPref("enableAltTextModelDownload", false);
} else {
this.#delete(false);
}
}); });
this.#overlayManager.register(dialog); downloadModelButton.addEventListener("click", async () => {
} downloadModelButton.disabled = true;
downloadModelButton.firstChild.setAttribute(
async #download(isFromUI = false) {
if (isFromUI) {
this.#downloadModelButton.disabled = true;
this.#downloadModelButton.firstChild.setAttribute(
"data-l10n-id", "data-l10n-id",
"pdfjs-editor-alt-text-settings-downloading-model-button" "pdfjs-editor-alt-text-settings-downloading-model-button"
); );
await this.#mlManager.downloadModel("altText"); await mlManager.downloadModel("altText");
this.#downloadModelButton.firstChild.setAttribute( aiModelSettings.classList.toggle("download", false);
downloadModelButton.firstChild.setAttribute(
"data-l10n-id", "data-l10n-id",
"pdfjs-editor-alt-text-settings-download-model-button" "pdfjs-editor-alt-text-settings-download-model-button"
); );
createModelButton.disabled = false;
this.#createModelButton.disabled = false; createModelButton.setAttribute("aria-pressed", true);
this.#setPref("enableGuessAltText", true); this.#setPref("enableGuessAltText", true);
this.#mlManager.toggleService("altText", true); mlManager.toggleService("altText", true);
this.#setPref("enableAltTextModelDownload", true); this.#setPref("enableAltTextModelDownload", true);
this.#downloadModelButton.disabled = false; downloadModelButton.disabled = false;
} });
this.#aiModelSettings.classList.toggle("download", false); closeButton.addEventListener("click", this.#finish.bind(this));
this.#createModelButton.setAttribute("aria-pressed", true); this.#overlayManager.register(dialog);
}
async #delete(isFromUI = false) {
if (isFromUI) {
await this.#mlManager.deleteModel("altText");
this.#setPref("enableGuessAltText", false);
this.#setPref("enableAltTextModelDownload", false);
}
this.#aiModelSettings.classList.toggle("download", true);
this.#createModelButton.disabled = true;
this.#createModelButton.setAttribute("aria-pressed", false);
} }
async open({ enableGuessAltText, enableNewAltTextWhenAddingImage }) { async open({ enableGuessAltText, enableNewAltTextWhenAddingImage }) {