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:
62
apps/desktop/src/services/electron-dialog.service.ts
Normal file
62
apps/desktop/src/services/electron-dialog.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user