Migrate some application menu items to injectables

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-tursoandJanne Savolainen authored and Janne Savolainen committed 2022-10-20 08:36:19 +03:00
1 parent 18c2cdf851
commit 88ea157352
28 files changed
+805 -106

No files matched your search

@@ -0,0 +1,399 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`application-menu given enough time passes populates application menu janne 1`] = `
Array [
Array [
Array [
Object {
"id": "root",
"label": "some-product-name",
"submenu": Array [
Object {
"id": "primary",
"label": "some-product-name",
"parentId": null,
},
Object {
"click": [Function],
"id": "about",
"label": "About some-product-name",
"parentId": "primary",
},
Object {
"type": "separator",
},
Object {
"accelerator": "CmdOrCtrl+,",
"click": [Function],
"id": "preferences",
"label": "Preferences",
},
Object {
"accelerator": "CmdOrCtrl+Shift+E",
"click": [Function],
"id": "extensions",
"label": "Extensions",
},
Object {
"type": "separator",
},
Object {
"role": "services",
},
Object {
"type": "separator",
},
Object {
"role": "hide",
},
Object {
"role": "hideOthers",
},
Object {
"role": "unhide",
},
Object {
"type": "separator",
},
Object {
"accelerator": "Cmd+Q",
"click": [Function],
"id": "quit",
"label": "Quit",
},
],
},
Object {
"id": "file",
"label": "File",
"submenu": Array [
Object {
"accelerator": "CmdOrCtrl+Shift+A",
"click": [Function],
"id": "add-cluster",
"label": "Add Cluster",
},
Object {
"type": "separator",
},
Object {
"accelerator": "Shift+Cmd+W",
"label": "Close Window",
"role": "close",
},
],
},
Object {
"id": "edit",
"label": "Edit",
"submenu": Array [
Object {
"role": "undo",
},
Object {
"role": "redo",
},
Object {
"type": "separator",
},
Object {
"role": "cut",
},
Object {
"role": "copy",
},
Object {
"role": "paste",
},
Object {
"role": "delete",
},
Object {
"type": "separator",
},
Object {
"role": "selectAll",
},
],
},
Object {
"id": "view",
"label": "View",
"submenu": Array [
Object {
"accelerator": "Shift+CmdOrCtrl+C",
"click": [Function],
"id": "catalog",
"label": "Catalog",
},
Object {
"accelerator": "Shift+CmdOrCtrl+P",
"click": [Function],
"id": "command-palette",
"label": "Command Palette...",
},
Object {
"type": "separator",
},
Object {
"accelerator": "CmdOrCtrl+[",
"click": [Function],
"id": "go-back",
"label": "Back",
},
Object {
"accelerator": "CmdOrCtrl+]",
"click": [Function],
"id": "go-forward",
"label": "Forward",
},
Object {
"accelerator": "CmdOrCtrl+R",
"click": [Function],
"id": "reload",
"label": "Reload",
},
Object {
"role": "toggleDevTools",
},
Object {
"type": "separator",
},
Object {
"role": "resetZoom",
},
Object {
"role": "zoomIn",
},
Object {
"role": "zoomOut",
},
Object {
"type": "separator",
},
Object {
"role": "togglefullscreen",
},
],
},
Object {
"id": "help",
"role": "help",
"submenu": Array [
Object {
"click": [Function],
"id": "welcome",
"label": "Welcome",
},
Object {
"click": [Function],
"id": "documentation",
"label": "Documentation",
},
Object {
"click": [Function],
"id": "support",
"label": "Support",
},
],
},
],
],
]
`;
exports[`application-menu given enough time passes populates application menu lol 1`] = `
Array [
Array [
Array [
Object {
"id": "root",
"label": "some-product-name",
"submenu": Array [
Object {
"click": [Function],
"id": "about",
"label": "About some-product-name",
},
Object {
"type": "separator",
},
Object {
"accelerator": "CmdOrCtrl+,",
"click": [Function],
"id": "preferences",
"label": "Preferences",
},
Object {
"accelerator": "CmdOrCtrl+Shift+E",
"click": [Function],
"id": "extensions",
"label": "Extensions",
},
Object {
"type": "separator",
},
Object {
"role": "services",
},
Object {
"type": "separator",
},
Object {
"role": "hide",
},
Object {
"role": "hideOthers",
},
Object {
"role": "unhide",
},
Object {
"type": "separator",
},
Object {
"accelerator": "Cmd+Q",
"click": [Function],
"id": "quit",
"label": "Quit",
},
],
},
Object {
"id": "file",
"label": "File",
"submenu": Array [
Object {
"accelerator": "CmdOrCtrl+Shift+A",
"click": [Function],
"id": "add-cluster",
"label": "Add Cluster",
},
Object {
"type": "separator",
},
Object {
"accelerator": "Shift+Cmd+W",
"label": "Close Window",
"role": "close",
},
],
},
Object {
"id": "edit",
"label": "Edit",
"submenu": Array [
Object {
"role": "undo",
},
Object {
"role": "redo",
},
Object {
"type": "separator",
},
Object {
"role": "cut",
},
Object {
"role": "copy",
},
Object {
"role": "paste",
},
Object {
"role": "delete",
},
Object {
"type": "separator",
},
Object {
"role": "selectAll",
},
],
},
Object {
"id": "view",
"label": "View",
"submenu": Array [
Object {
"accelerator": "Shift+CmdOrCtrl+C",
"click": [Function],
"id": "catalog",
"label": "Catalog",
},
Object {
"accelerator": "Shift+CmdOrCtrl+P",
"click": [Function],
"id": "command-palette",
"label": "Command Palette...",
},
Object {
"type": "separator",
},
Object {
"accelerator": "CmdOrCtrl+[",
"click": [Function],
"id": "go-back",
"label": "Back",
},
Object {
"accelerator": "CmdOrCtrl+]",
"click": [Function],
"id": "go-forward",
"label": "Forward",
},
Object {
"accelerator": "CmdOrCtrl+R",
"click": [Function],
"id": "reload",
"label": "Reload",
},
Object {
"role": "toggleDevTools",
},
Object {
"type": "separator",
},
Object {
"role": "resetZoom",
},
Object {
"role": "zoomIn",
},
Object {
"role": "zoomOut",
},
Object {
"type": "separator",
},
Object {
"role": "togglefullscreen",
},
],
},
Object {
"id": "help",
"role": "help",
"submenu": Array [
Object {
"click": [Function],
"id": "welcome",
"label": "Welcome",
},
Object {
"click": [Function],
"id": "documentation",
"label": "Documentation",
},
Object {
"click": [Function],
"id": "support",
"label": "Support",
},
],
},
],
],
]
`;
@@ -0,0 +1,52 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import populateApplicationMenuInjectable from "./main/populate-application-menu.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
describe("application-menu", () => {
let builder: ApplicationBuilder;
let mainDi: DiContainer;
let populateApplicationMenuMock: jest.Mock;
beforeEach(async () => {
useFakeTime();
populateApplicationMenuMock = jest.fn();
builder = getApplicationBuilder();
builder.beforeApplicationStart((mainDi) => {
mainDi.override(populateApplicationMenuInjectable, () => populateApplicationMenuMock);
});
// await builder.render();
await builder.startHidden();
mainDi = builder.mainDi;
});
it("when insufficient time passes, does not populate menu items yet", () => {
advanceFakeTime(99);
expect(populateApplicationMenuMock).not.toHaveBeenCalled();
});
describe("given enough time passes", () => {
beforeEach(() => {
advanceFakeTime(100);
});
it("populates application menu", () => {
expect(populateApplicationMenuMock).toHaveBeenCalledWith(expect.any(Array));
});
it("populates application menu lol", () => {
expect(populateApplicationMenuMock.mock.calls).toMatchSnapshot();
});
});
});
@@ -0,0 +1,316 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { docsUrl, supportUrl } from "../../../common/vars";
import { broadcastMessage } from "../../../common/ipc";
import type { MenuItemConstructorOptions } from "electron";
import { webContents } from "electron";
import loggerInjectable from "../../../common/logger.injectable";
import electronMenuItemsInjectable from "./electron-menu-items.injectable";
import updatingIsEnabledInjectable from "../../application-update/main/updating-is-enabled/updating-is-enabled.injectable";
import navigateToPreferencesInjectable from "../../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
import navigateToExtensionsInjectable from "../../../common/front-end-routing/routes/extensions/navigate-to-extensions.injectable";
import navigateToCatalogInjectable from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import navigateToWelcomeInjectable from "../../../common/front-end-routing/routes/welcome/navigate-to-welcome.injectable";
import navigateToAddClusterInjectable from "../../../common/front-end-routing/routes/add-cluster/navigate-to-add-cluster.injectable";
import stopServicesAndExitAppInjectable from "../../../main/stop-services-and-exit-app.injectable";
import isMacInjectable from "../../../common/vars/is-mac.injectable";
import { computed } from "mobx";
import showAboutInjectable from "./menu-items/primary-for-mac/show-about-application/show-about.injectable";
import reloadCurrentApplicationWindowInjectable from "../../../main/start-main-application/lens-window/reload-current-application-window.injectable";
import showApplicationWindowInjectable from "../../../main/start-main-application/lens-window/show-application-window.injectable";
import processCheckingForUpdatesInjectable from "../../application-update/main/process-checking-for-updates.injectable";
import openLinkInBrowserInjectable from "../../../common/utils/open-link-in-browser.injectable";
import appNameInjectable from "../../../common/vars/app-name.injectable";
import productNameInjectable from "../../../common/vars/product-name.injectable";
import applicationMenuItemInjectionToken from "./menu-items/application-menu-item-injection-token";
function ignoreIf(check: boolean, menuItems: MenuItemOpts[]) {
return check ? [] : menuItems;
}
export interface MenuItemOpts extends MenuItemConstructorOptions {
submenu?: MenuItemConstructorOptions[];
}
const applicationMenuItemsInjectable = getInjectable({
id: "application-menu-items",
instantiate: (di) => {
const logger = di.inject(loggerInjectable);
const appName = di.inject(appNameInjectable);
const productName = di.inject(productNameInjectable);
const isMac = di.inject(isMacInjectable);
const updatingIsEnabled = di.inject(updatingIsEnabledInjectable);
const electronMenuItems = di.inject(electronMenuItemsInjectable);
const showAbout = di.inject(showAboutInjectable);
const showApplicationWindow = di.inject(showApplicationWindowInjectable);
const reloadApplicationWindow = di.inject(
reloadCurrentApplicationWindowInjectable,
);
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
const navigateToExtensions = di.inject(navigateToExtensionsInjectable);
const navigateToCatalog = di.inject(navigateToCatalogInjectable);
const navigateToWelcome = di.inject(navigateToWelcomeInjectable);
const navigateToAddCluster = di.inject(navigateToAddClusterInjectable);
const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable);
const processCheckingForUpdates = di.inject(
processCheckingForUpdatesInjectable,
);
const openLinkInBrowser = di.inject(openLinkInBrowserInjectable);
logger.info(`[MENU]: autoUpdateEnabled=${updatingIsEnabled}`);
const menuItems = di
.injectMany(applicationMenuItemInjectionToken)
.filter(x => x.isShown !== false);
return computed((): MenuItemOpts[] => {
const macAppMenu: MenuItemOpts = {
label: appName,
id: "root",
submenu: [
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ role: "unhide" },
{ type: "separator" },
{
label: "Quit",
accelerator: "Cmd+Q",
id: "quit",
click() {
stopServicesAndExitApp();
},
},
],
};
const fileMenu: MenuItemOpts = {
label: "File",
id: "file",
submenu: [
{
label: "Add Cluster",
accelerator: "CmdOrCtrl+Shift+A",
id: "add-cluster",
click() {
navigateToAddCluster();
},
},
...ignoreIf(isMac, [
{ type: "separator" },
{
label: "Preferences",
id: "preferences",
accelerator: "Ctrl+,",
click() {
navigateToPreferences();
},
},
{
label: "Extensions",
accelerator: "Ctrl+Shift+E",
click() {
navigateToExtensions();
},
},
]),
{ type: "separator" },
...ignoreIf(!isMac, [
{
role: "close",
label: "Close Window",
accelerator: "Shift+Cmd+W",
},
]),
...ignoreIf(isMac, [
{
label: "Exit",
accelerator: "Alt+F4",
id: "quit",
click() {
stopServicesAndExitApp();
},
},
]),
],
};
const editMenu: MenuItemOpts = {
label: "Edit",
id: "edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
],
};
const viewMenu: MenuItemOpts = {
label: "View",
id: "view",
submenu: [
{
label: "Catalog",
accelerator: "Shift+CmdOrCtrl+C",
id: "catalog",
click() {
navigateToCatalog();
},
},
{
label: "Command Palette...",
accelerator: "Shift+CmdOrCtrl+P",
id: "command-palette",
click(_m, _b, event) {
/**
* Don't broadcast unless it was triggered by menu iteration so that
* there aren't double events in renderer
*
* NOTE: this `?` is required because of a bug in playwright. https://github.com/microsoft/playwright/issues/10554
*/
if (!event?.triggeredByAccelerator) {
broadcastMessage("command-palette:open");
}
},
},
{ type: "separator" },
{
label: "Back",
accelerator: "CmdOrCtrl+[",
id: "go-back",
click() {
webContents
.getAllWebContents()
.filter((wc) => wc.getType() === "window")
.forEach((wc) => wc.goBack());
},
},
{
label: "Forward",
accelerator: "CmdOrCtrl+]",
id: "go-forward",
click() {
webContents
.getAllWebContents()
.filter((wc) => wc.getType() === "window")
.forEach((wc) => wc.goForward());
},
},
{
label: "Reload",
accelerator: "CmdOrCtrl+R",
id: "reload",
click() {
reloadApplicationWindow();
},
},
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetZoom" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
};
const helpMenu: MenuItemOpts = {
role: "help",
id: "help",
submenu: [
{
label: "Welcome",
id: "welcome",
click() {
navigateToWelcome();
},
},
{
label: "Documentation",
id: "documentation",
click: async () => {
openLinkInBrowser(docsUrl).catch((error) => {
logger.error("[MENU]: failed to open browser", { error });
});
},
},
{
label: "Support",
id: "support",
click: async () => {
openLinkInBrowser(supportUrl).catch((error) => {
logger.error("[MENU]: failed to open browser", { error });
});
},
},
...ignoreIf(isMac, [
{
label: `About ${productName}`,
id: "about",
click() {
showAbout();
},
},
...ignoreIf(!updatingIsEnabled, [
{
label: "Check for updates",
click() {
processCheckingForUpdates("periodic").then(() =>
showApplicationWindow(),
);
},
},
]),
]),
],
};
// Prepare menu items order
const appMenu = new Map([
["mac", macAppMenu],
["file", fileMenu],
["edit", editMenu],
["view", viewMenu],
["help", helpMenu],
]);
// Modify menu from extensions-api
for (const menuItem of electronMenuItems.get()) {
const parentMenu = appMenu.get(menuItem.parentId);
if (!parentMenu) {
logger.error(
`[MENU]: cannot register menu item for parentId=${menuItem.parentId}, parent item doesn't exist`,
{ menuItem },
);
continue;
}
(parentMenu.submenu ??= []).push(menuItem);
}
if (!isMac) {
appMenu.delete("mac");
}
return [...menuItems, ...appMenu.values()];
});
},
});
export default applicationMenuItemsInjectable;
@@ -0,0 +1,26 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { autorun } from "mobx";
import applicationMenuItemsInjectable from "./application-menu-items.injectable";
import { getStartableStoppable } from "../../../common/utils/get-startable-stoppable";
import populateApplicationMenuInjectable from "./populate-application-menu.injectable";
const applicationMenuReactivityInjectable = getInjectable({
id: "application-menu-reactivity",
instantiate: (di) => {
const applicationMenuItems = di.inject(applicationMenuItemsInjectable);
const populateApplicationMenu = di.inject(populateApplicationMenuInjectable);
return getStartableStoppable("application-menu-reactivity", () =>
autorun(() => populateApplicationMenu(applicationMenuItems.get()), {
delay: 100,
}),
);
},
});
export default applicationMenuReactivityInjectable;
@@ -0,0 +1,20 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import mainExtensionsInjectable from "../../../extensions/main-extensions.injectable";
const electronMenuItemsInjectable = getInjectable({
id: "electron-menu-items",
instantiate: (di) => {
const extensions = di.inject(mainExtensionsInjectable);
return computed(() =>
extensions.get().flatMap((extension) => extension.appMenus));
},
});
export default electronMenuItemsInjectable;
@@ -0,0 +1,116 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import { LensMainExtension } from "../../../extensions/lens-main-extension";
import electronMenuItemsInjectable from "./electron-menu-items.injectable";
import type { IComputedValue } from "mobx";
import { computed, ObservableMap, runInAction } from "mobx";
import type { MenuRegistration } from "./menu-registration";
import { getDiForUnitTesting } from "../../../main/getDiForUnitTesting";
import mainExtensionsInjectable from "../../../extensions/main-extensions.injectable";
describe("electron-menu-items", () => {
let di: DiContainer;
let electronMenuItems: IComputedValue<MenuRegistration[]>;
let extensionsStub: ObservableMap<string, LensMainExtension>;
beforeEach(() => {
di = getDiForUnitTesting({ doGeneralOverrides: true });
extensionsStub = new ObservableMap();
di.override(
mainExtensionsInjectable,
() => computed(() => [...extensionsStub.values()]),
);
electronMenuItems = di.inject(electronMenuItemsInjectable);
});
it("does not have any items yet", () => {
expect(electronMenuItems.get()).toHaveLength(0);
});
describe("when extension is enabled", () => {
beforeEach(() => {
const someExtension = new SomeTestExtension({
id: "some-extension-id",
appMenus: [{ parentId: "some-parent-id-from-first-extension" }],
});
runInAction(() => {
extensionsStub.set("some-extension-id", someExtension);
});
});
it("has menu items", () => {
expect(electronMenuItems.get()).toEqual([
{
parentId: "some-parent-id-from-first-extension",
},
]);
});
it("when disabling extension, does not have menu items", () => {
extensionsStub.delete("some-extension-id");
expect(electronMenuItems.get()).toHaveLength(0);
});
describe("when other extension is enabled", () => {
beforeEach(() => {
const someOtherExtension = new SomeTestExtension({
id: "some-extension-id",
appMenus: [{ parentId: "some-parent-id-from-second-extension" }],
});
extensionsStub.set("some-other-extension-id", someOtherExtension);
});
it("has menu items for both extensions", () => {
expect(electronMenuItems.get()).toEqual([
{
parentId: "some-parent-id-from-first-extension",
},
{
parentId: "some-parent-id-from-second-extension",
},
]);
});
it("when extension is disabled, still returns menu items for extensions that are enabled", () => {
runInAction(() => {
extensionsStub.delete("some-other-extension-id");
});
expect(electronMenuItems.get()).toEqual([
{
parentId: "some-parent-id-from-first-extension",
},
]);
});
});
});
});
class SomeTestExtension extends LensMainExtension {
constructor({ id, appMenus }: {
id: string;
appMenus: MenuRegistration[];
}) {
super({
id,
absolutePath: "irrelevant",
isBundled: false,
isCompatible: false,
isEnabled: false,
manifest: { name: id, version: "some-version", engines: { lens: "^5.5.0" }},
manifestPath: "irrelevant",
});
this.appMenus = appMenus;
}
}
@@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
interface Shared {
parentId: string | null;
orderNumber: number;
isShown?: boolean;
}
export interface ApplicationMenuItem extends Shared {
id: string;
label: string;
click?: () => void;
accelerator?: string;
}
export interface Separator extends Shared {
type: "separator";
}
const applicationMenuItemInjectionToken = getInjectionToken<ApplicationMenuItem | Separator>({
id: "application-menu-item-injection-token",
});
export default applicationMenuItemInjectionToken;
@@ -0,0 +1,43 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemInjectionToken from "../../application-menu-item-injection-token";
import processCheckingForUpdatesInjectable from "../../../../../application-update/main/process-checking-for-updates.injectable";
import showApplicationWindowInjectable from "../../../../../../main/start-main-application/lens-window/show-application-window.injectable";
import updatingIsEnabledInjectable from "../../../../../application-update/main/updating-is-enabled/updating-is-enabled.injectable";
const checkForUpdatesMenuItemInjectable = getInjectable({
id: "check-for-updates-menu-item",
instantiate: (di) => {
const processCheckingForUpdates = di.inject(
processCheckingForUpdatesInjectable,
);
const showApplicationWindow = di.inject(showApplicationWindowInjectable);
const updatingIsEnabled = di.inject(updatingIsEnabledInjectable);
return {
parentId: "primary-for-mac",
id: "check-for-updates",
orderNumber: 20,
label: "Check for updates",
isShown: updatingIsEnabled,
click: () => {
// Todo: implement using async/await
processCheckingForUpdates("application-menu").then(() =>
showApplicationWindow(),
);
},
};
},
injectionToken: applicationMenuItemInjectionToken,
});
export default checkForUpdatesMenuItemInjectable;
@@ -0,0 +1,31 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemInjectionToken from "../../application-menu-item-injection-token";
import navigateToExtensionsInjectable from "../../../../../../common/front-end-routing/routes/extensions/navigate-to-extensions.injectable";
const navigateToExtensionsMenuItem = getInjectable({
id: "navigate-to-extensions-menu-item",
instantiate: (di) => {
const navigateToExtensions = di.inject(navigateToExtensionsInjectable);
return {
parentId: "primary-for-mac",
id: "navigate-to-extensions",
orderNumber: 50,
label: "Extensions",
accelerator: "CmdOrCtrl+Shift+E",
click: () => {
navigateToExtensions();
},
};
},
injectionToken: applicationMenuItemInjectionToken,
});
export default navigateToExtensionsMenuItem;
@@ -0,0 +1,31 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemInjectionToken from "../../application-menu-item-injection-token";
import navigateToPreferencesInjectable from "../../../../../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
const navigateToPreferencesMenuItem = getInjectable({
id: "navigate-to-preferences-menu-item",
instantiate: (di) => {
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
return {
parentId: "primary-for-mac",
id: "navigate-to-preferences",
orderNumber: 40,
label: "Preferences",
accelerator: "CmdOrCtrl+,",
click: () => {
navigateToPreferences();
},
};
},
injectionToken: applicationMenuItemInjectionToken,
});
export default navigateToPreferencesMenuItem;
@@ -0,0 +1,29 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemInjectionToken from "../application-menu-item-injection-token";
import appNameInjectable from "../../../../../common/vars/app-name.injectable";
import isMacInjectable from "../../../../../common/vars/is-mac.injectable";
const primaryMenuItemInjectable = getInjectable({
id: "primary-application-menu-item",
instantiate: (di) => {
const appName = di.inject(appNameInjectable);
const isMac = di.inject(isMacInjectable);
return {
parentId: null,
id: "primary-for-mac",
orderNumber: 10,
label: appName,
isShown: isMac,
};
},
injectionToken: applicationMenuItemInjectionToken,
});
export default primaryMenuItemInjectable;
@@ -0,0 +1,22 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemInjectionToken from "../../application-menu-item-injection-token";
const separatorAfterCheckForUpdatesInjectable = getInjectable({
id: "separator-after-check-for-updates",
instantiate: () => ({
id: "separator-after-check-for-updates",
parentId: "primary-for-mac",
type: "separator" as const,
orderNumber: 30,
}),
injectionToken: applicationMenuItemInjectionToken,
});
export default separatorAfterCheckForUpdatesInjectable;
@@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuItemInjectionToken from "../../application-menu-item-injection-token";
const id = "separator-after-extensions";
const separatorAfterExtensionsInjectable = getInjectable({
id,
instantiate: () => ({
id,
parentId: "primary-for-mac",
type: "separator" as const,
orderNumber: 70,
}),
injectionToken: applicationMenuItemInjectionToken,
});
export default separatorAfterExtensionsInjectable;
@@ -0,0 +1,32 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import productNameInjectable from "../../../../../../common/vars/product-name.injectable";
import showAboutInjectable from "./show-about.injectable";
import applicationMenuItemInjectionToken from "../../application-menu-item-injection-token";
const aboutMenuItemInjectable = getInjectable({
id: "about-menu-item",
instantiate: (di) => {
const productName = di.inject(productNameInjectable);
const showAbout = di.inject(showAboutInjectable);
return {
parentId: "primary-for-mac",
id: "about",
orderNumber: 10,
label: `About ${productName}`,
click() {
showAbout();
},
};
},
injectionToken: applicationMenuItemInjectionToken,
});
export default aboutMenuItemInjectable;
@@ -0,0 +1,45 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import showMessagePopupInjectable from "../../../../../../main/electron-app/features/show-message-popup.injectable";
import isWindowsInjectable from "../../../../../../common/vars/is-windows.injectable";
import appNameInjectable from "../../../../../../common/vars/app-name.injectable";
import productNameInjectable from "../../../../../../common/vars/product-name.injectable";
import buildVersionInjectable from "../../../../../../main/vars/build-version/build-version.injectable";
import extensionApiVersionInjectable from "../../../../../../common/vars/extension-api-version.injectable";
import applicationCopyrightInjectable from "../../../../../../common/vars/application-copyright.injectable";
const showAboutInjectable = getInjectable({
id: "show-about",
instantiate: (di) => {
const buildVersion = di.inject(buildVersionInjectable);
const extensionApiVersion = di.inject(extensionApiVersionInjectable);
const showMessagePopup = di.inject(showMessagePopupInjectable);
const isWindows = di.inject(isWindowsInjectable);
const appName = di.inject(appNameInjectable);
const productName = di.inject(productNameInjectable);
const applicationCopyright = di.inject(applicationCopyrightInjectable);
return () => {
const appInfo = [
`${appName}: ${buildVersion.get()}`,
`Extension API: ${extensionApiVersion}`,
`Electron: ${process.versions.electron}`,
`Chrome: ${process.versions.chrome}`,
`Node: ${process.versions.node}`,
applicationCopyright,
];
showMessagePopup(
`${isWindows ? " ".repeat(2) : ""}${appName}`,
productName,
appInfo.join("\r\n"),
);
};
},
});
export default showAboutInjectable;
@@ -0,0 +1,9 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { MenuItemConstructorOptions } from "electron";
export interface MenuRegistration extends MenuItemConstructorOptions {
parentId: string;
}
@@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { Menu } from "electron";
import type { MenuItemOpts } from "./application-menu-items.injectable";
export type MenuTopId = "mac" | "file" | "edit" | "view" | "help";
export function buildMenu(applicationMenuItems: MenuItemOpts[]) {
Menu.setApplicationMenu(
Menu.buildFromTemplate(applicationMenuItems),
);
}
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverrideForFunction } from "../../../common/test-utils/get-global-override-for-function";
import populateApplicationMenuInjectable from "./populate-application-menu.injectable";
export default getGlobalOverrideForFunction(populateApplicationMenuInjectable);
@@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { Menu } from "electron";
import type { MenuItemOpts } from "./application-menu-items.injectable";
const populateApplicationMenuInjectable = getInjectable({
id: "populate-application-menu",
instantiate: () => (applicationMenuItems: MenuItemOpts[]) => {
Menu.setApplicationMenu(Menu.buildFromTemplate(applicationMenuItems));
},
causesSideEffects: true,
});
export default populateApplicationMenuInjectable;
@@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuReactivityInjectable from "./application-menu-reactivity.injectable";
import { onLoadOfApplicationInjectionToken } from "../../../main/start-main-application/runnable-tokens/on-load-of-application-injection-token";
const startApplicationMenuInjectable = getInjectable({
id: "start-application-menu",
instantiate: (di) => {
const applicationMenu = di.inject(
applicationMenuReactivityInjectable,
);
return {
id: "start-application-menu",
run: async () => {
await applicationMenu.start();
},
};
},
injectionToken: onLoadOfApplicationInjectionToken,
});
export default startApplicationMenuInjectable;
@@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import applicationMenuReactivityInjectable from "./application-menu-reactivity.injectable";
import { beforeQuitOfBackEndInjectionToken } from "../../../main/start-main-application/runnable-tokens/before-quit-of-back-end-injection-token";
const stopApplicationMenuInjectable = getInjectable({
id: "stop-application-menu",
instantiate: (di) => {
const applicationMenu = di.inject(
applicationMenuReactivityInjectable,
);
return {
id: "stop-application-menu",
run: async () => {
await applicationMenu.stop();
},
};
},
injectionToken: beforeQuitOfBackEndInjectionToken,
});
export default stopApplicationMenuInjectable;