1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-26 05:03:33 +00:00

[PM-1504] Migrate Dialogs to DialogService (#5013)

This PR introduces a generic `DialogService` which can be used by all the clients. This allows us to decouple dialogs from the `PlatformUtilsHelper`.

The `DialogService` provides a new method, `openSimpleDialog` which is the new interface for that type of dialogs.

This gives us 3 different implementations: 
- Web: DialogService modern dialogs
- Browser: SweetAlert
- Desktop: Native electron based
This commit is contained in:
Oscar Hinton
2023-05-02 18:46:03 +02:00
committed by GitHub
parent 7c4b2c04b9
commit 4e1867682f
144 changed files with 1514 additions and 1212 deletions

View File

@@ -0,0 +1,62 @@
import { ipcRenderer } from "electron";
import {
DialogService,
SimpleDialogOptions,
SimpleDialogType,
} from "@bitwarden/angular/services/dialog";
// Electron supports a limited set of dialog types
// https://www.electronjs.org/docs/latest/api/dialog#dialogshowmessageboxbrowserwindow-options
const electronTypeMap: Record<SimpleDialogType, string> = {
[SimpleDialogType.PRIMARY]: "info",
[SimpleDialogType.SUCCESS]: "info",
[SimpleDialogType.INFO]: "info",
[SimpleDialogType.WARNING]: "warning",
[SimpleDialogType.DANGER]: "error",
};
export class ElectronDialogService extends DialogService {
async openSimpleDialog(options: SimpleDialogOptions) {
const defaultCancel =
options.cancelButtonText === undefined
? options.acceptButtonText == null
? "no"
: "cancel"
: null;
return this.legacyShowDialog(
this.translate(options.content),
this.translate(options.title),
this.translate(options.acceptButtonText, "yes"),
this.translate(options.cancelButtonText, defaultCancel),
options.type
);
}
private async legacyShowDialog(
body: string,
title?: string,
confirmText?: string,
cancelText?: string,
type?: SimpleDialogType
) {
const buttons = [confirmText == null ? this.i18nService.t("ok") : confirmText];
if (cancelText != null) {
buttons.push(cancelText);
}
const result = await ipcRenderer.invoke("showMessageBox", {
type: electronTypeMap[type] ?? "none",
title: title,
message: title,
detail: body,
buttons: buttons,
cancelId: buttons.length === 2 ? 1 : null,
defaultId: 0,
noLink: true,
});
return Promise.resolve(result.response === 0);
}
}

View File

@@ -115,32 +115,6 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService {
});
}
async showDialog(
text: string,
title?: string,
confirmText?: string,
cancelText?: string,
type?: string
): Promise<boolean> {
const buttons = [confirmText == null ? this.i18nService.t("ok") : confirmText];
if (cancelText != null) {
buttons.push(cancelText);
}
const result = await ipcRenderer.invoke("showMessageBox", {
type: type,
title: title,
message: title,
detail: text,
buttons: buttons,
cancelId: buttons.length === 2 ? 1 : null,
defaultId: 0,
noLink: true,
});
return Promise.resolve(result.response === 0);
}
isDev(): boolean {
return isDev();
}