2018-02-08 21:58:47 +01:00
|
|
|
import { app, Menu } from "electron";
|
2024-03-21 17:09:44 +01:00
|
|
|
import { firstValueFrom } from "rxjs";
|
2018-02-08 21:58:47 +01:00
|
|
|
|
2024-01-24 20:21:50 +01:00
|
|
|
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
|
|
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
|
2018-04-25 21:43:26 +02:00
|
|
|
|
2023-03-10 21:32:26 +01:00
|
|
|
import { UpdaterMain } from "../updater.main";
|
2022-12-02 12:45:09 +01:00
|
|
|
import { WindowMain } from "../window.main";
|
2022-02-24 20:50:19 +01:00
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
import { MenuUpdateRequest } from "./menu.updater";
|
|
|
|
import { Menubar } from "./menubar";
|
2018-02-09 06:21:00 +01:00
|
|
|
|
2022-02-24 20:50:19 +01:00
|
|
|
const cloudWebVaultUrl = "https://vault.bitwarden.com";
|
2018-02-19 14:52:53 +01:00
|
|
|
|
2022-12-02 12:45:09 +01:00
|
|
|
export class MenuMain {
|
2023-03-10 21:32:26 +01:00
|
|
|
constructor(
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private messagingService: MessagingService,
|
2024-01-24 20:21:50 +01:00
|
|
|
private environmentService: EnvironmentService,
|
2023-03-10 21:32:26 +01:00
|
|
|
private windowMain: WindowMain,
|
|
|
|
private updaterMain: UpdaterMain,
|
|
|
|
) {}
|
2021-12-20 15:47:17 +01:00
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
async init() {
|
2018-02-14 05:06:45 +01:00
|
|
|
this.initContextMenu();
|
2021-12-15 23:32:00 +01:00
|
|
|
await this.setMenu();
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
async updateApplicationMenuState(updateRequest: MenuUpdateRequest) {
|
|
|
|
await this.setMenu(updateRequest);
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
private async setMenu(updateRequest?: MenuUpdateRequest) {
|
|
|
|
Menu.setApplicationMenu(
|
|
|
|
new Menubar(
|
2023-03-10 21:32:26 +01:00
|
|
|
this.i18nService,
|
|
|
|
this.messagingService,
|
|
|
|
this.updaterMain,
|
2021-12-15 23:32:00 +01:00
|
|
|
this.windowMain,
|
|
|
|
await this.getWebVaultUrl(),
|
|
|
|
app.getVersion(),
|
|
|
|
updateRequest,
|
2021-12-20 15:47:17 +01:00
|
|
|
).menu,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
private async getWebVaultUrl() {
|
2024-03-21 17:09:44 +01:00
|
|
|
const env = await firstValueFrom(this.environmentService.environment$);
|
|
|
|
return env.getWebVaultUrl() ?? cloudWebVaultUrl;
|
2018-02-19 14:52:53 +01:00
|
|
|
}
|
2022-12-02 12:45:09 +01:00
|
|
|
|
|
|
|
private initContextMenu() {
|
|
|
|
if (this.windowMain.win == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectionMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("copy"),
|
|
|
|
role: "copy",
|
|
|
|
},
|
|
|
|
{ type: "separator" },
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("selectAll"),
|
|
|
|
role: "selectAll",
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const inputMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("undo"),
|
|
|
|
role: "undo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("redo"),
|
|
|
|
role: "redo",
|
|
|
|
},
|
|
|
|
{ type: "separator" },
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("cut"),
|
|
|
|
role: "cut",
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("copy"),
|
|
|
|
role: "copy",
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("paste"),
|
|
|
|
role: "paste",
|
|
|
|
},
|
|
|
|
{ type: "separator" },
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("selectAll"),
|
|
|
|
role: "selectAll",
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const inputSelectionMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("cut"),
|
|
|
|
role: "cut",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("copy"),
|
|
|
|
role: "copy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("paste"),
|
|
|
|
role: "paste",
|
|
|
|
},
|
|
|
|
{ type: "separator" },
|
|
|
|
{
|
|
|
|
label: this.i18nService.t("selectAll"),
|
|
|
|
role: "selectAll",
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
this.windowMain.win.webContents.on("context-menu", (e, props) => {
|
|
|
|
const selected = props.selectionText && props.selectionText.trim() !== "";
|
|
|
|
if (props.isEditable && selected) {
|
|
|
|
inputSelectionMenu.popup({ window: this.windowMain.win });
|
|
|
|
} else if (props.isEditable) {
|
|
|
|
inputMenu.popup({ window: this.windowMain.win });
|
|
|
|
} else if (selected) {
|
|
|
|
selectionMenu.popup({ window: this.windowMain.win });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-02-08 19:10:13 +01:00
|
|
|
}
|