[Editor] Add a new dialog for alt-text settings (bug 1909604)

This patch adds a new entry in the secondary menu in order to open a dialog to let the user:
 - disables the alt-text generation thanks to a ML model;
 - deletes the alt-text model downloaded in Firefox;
 - disabled the new alt-text flow.
This commit is contained in:
Calixte Denizet 2024-08-01 15:29:01 +02:00
parent d562e0525d
commit 32d09276f0
17 changed files with 627 additions and 54 deletions

View file

@ -59,12 +59,15 @@ import {
import { AppOptions, OptionKind } from "./app_options.js";
import { EventBus, FirefoxEventBus } from "./event_utils.js";
import { ExternalServices, initCom, MLManager } from "web-external_services";
import {
ImageAltTextSettings,
NewAltTextManager,
} from "web-new_alt_text_manager";
import { LinkTarget, PDFLinkService } from "./pdf_link_service.js";
import { AltTextManager } from "web-alt_text_manager";
import { AnnotationEditorParams } from "web-annotation_editor_params";
import { CaretBrowsingMode } from "./caret_browsing.js";
import { DownloadManager } from "web-download_manager";
import { NewAltTextManager } from "web-new_alt_text_manager";
import { OverlayManager } from "./overlay_manager.js";
import { PasswordPrompt } from "./password_prompt.js";
import { PDFAttachmentViewer } from "web-pdf_attachment_viewer";
@ -151,6 +154,8 @@ const PDFViewerApplication = {
l10n: null,
/** @type {AnnotationEditorParams} */
annotationEditorParams: null,
/** @type {ImageAltTextSettings} */
imageAltTextSettings: null,
isInitialViewSet: false,
isViewerEmbedded: window.parent !== window,
url: "",
@ -211,6 +216,9 @@ const PDFViewerApplication = {
this.mlManager =
MLManager.getFakeMLManager?.({
enableGuessAltText: AppOptions.get("enableGuessAltText"),
enableAltTextModelDownload: AppOptions.get(
"enableAltTextModelDownload"
),
}) || null;
}
}
@ -218,6 +226,9 @@ const PDFViewerApplication = {
// We want to load the image-to-text AI engine as soon as possible.
this.mlManager = new MLManager({
enableGuessAltText: AppOptions.get("enableGuessAltText"),
enableAltTextModelDownload: AppOptions.get(
"enableAltTextModelDownload"
),
altTextLearnMoreUrl: AppOptions.get("altTextLearnMoreUrl"),
});
}
@ -390,12 +401,12 @@ const PDFViewerApplication = {
externalServices,
AppOptions.get("isInAutomation")
);
if (this.mlManager) {
this.mlManager.eventBus = eventBus;
}
} else {
eventBus = new EventBus();
}
if (this.mlManager) {
this.mlManager.eventBus = eventBus;
}
this.eventBus = eventBus;
this.overlayManager = new OverlayManager();
@ -445,7 +456,11 @@ const PDFViewerApplication = {
let altTextManager;
if (AppOptions.get("enableUpdatedAddImage")) {
altTextManager = appConfig.newAltTextDialog
? new NewAltTextManager(appConfig.newAltTextDialog, this.overlayManager)
? new NewAltTextManager(
appConfig.newAltTextDialog,
this.overlayManager,
eventBus
)
: null;
} else {
altTextManager = appConfig.altTextDialog
@ -479,6 +494,9 @@ const PDFViewerApplication = {
"enableHighlightFloatingButton"
),
enableUpdatedAddImage: AppOptions.get("enableUpdatedAddImage"),
enableNewAltTextWhenAddingImage: AppOptions.get(
"enableNewAltTextWhenAddingImage"
),
imageResourcesPath: AppOptions.get("imageResourcesPath"),
enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"),
maxCanvasPixels: AppOptions.get("maxCanvasPixels"),
@ -539,6 +557,15 @@ const PDFViewerApplication = {
}
}
if (appConfig.secondaryToolbar?.imageAltTextSettingsButton) {
this.imageAltTextSettings = new ImageAltTextSettings(
appConfig.altTextSettingsDialog,
this.overlayManager,
eventBus,
this.mlManager
);
}
if (appConfig.documentProperties) {
this.pdfDocumentProperties = new PDFDocumentProperties(
appConfig.documentProperties,
@ -579,6 +606,15 @@ const PDFViewerApplication = {
}
if (appConfig.secondaryToolbar) {
if (AppOptions.get("enableAltText")) {
appConfig.secondaryToolbar.imageAltTextSettingsButton?.classList.remove(
"hidden"
);
appConfig.secondaryToolbar.imageAltTextSettingsSeparator?.classList.remove(
"hidden"
);
}
this.secondaryToolbar = new SecondaryToolbar(
appConfig.secondaryToolbar,
eventBus
@ -1914,6 +1950,9 @@ const PDFViewerApplication = {
eventBus._on("scrollmodechanged", webViewerScrollModeChanged, { signal });
eventBus._on("switchspreadmode", webViewerSwitchSpreadMode, { signal });
eventBus._on("spreadmodechanged", webViewerSpreadModeChanged, { signal });
eventBus._on("imagealttextsettings", webViewerImageAltTextSettings, {
signal,
});
eventBus._on("documentproperties", webViewerDocumentProperties, { signal });
eventBus._on("findfromurlhash", webViewerFindFromUrlHash, { signal });
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount, {
@ -1934,6 +1973,11 @@ const PDFViewerApplication = {
{ signal }
);
eventBus._on("reporttelemetry", webViewerReportTelemetry, { signal });
}
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("TESTING || MOZCENTRAL")
) {
eventBus._on("setpreference", webViewerSetPreference, { signal });
}
},
@ -2470,6 +2514,15 @@ function webViewerDocumentProperties() {
PDFViewerApplication.pdfDocumentProperties?.open();
}
function webViewerImageAltTextSettings() {
PDFViewerApplication.imageAltTextSettings?.open({
enableGuessAltText: AppOptions.get("enableGuessAltText"),
enableNewAltTextWhenAddingImage: AppOptions.get(
"enableNewAltTextWhenAddingImage"
),
});
}
function webViewerFindFromUrlHash(evt) {
PDFViewerApplication.eventBus.dispatch("find", {
source: evt.source,