mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
support-page extension -- menu-registry / part 1
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
ef66f67b39
commit
b853d1d54b
@ -1,4 +1,5 @@
|
||||
import { LensMainExtension, Registry } from "@lens/extensions";
|
||||
import { supportPageURL } from "./src/support.route";
|
||||
|
||||
export default class SupportPageMainExtension extends LensMainExtension {
|
||||
async onActivate() {
|
||||
@ -6,6 +7,15 @@ export default class SupportPageMainExtension extends LensMainExtension {
|
||||
}
|
||||
|
||||
async registerAppMenus(registry: Registry.MenuRegistry) {
|
||||
// TODO: allow to modify global menu item "Help -> Support"
|
||||
this.disposers.push(
|
||||
registry.add({
|
||||
parentId: "help",
|
||||
label: "Support",
|
||||
async click() {
|
||||
// fixme: require runtime windowManager (ensureMainWindow + navigate for main)
|
||||
console.log(`navigate: ${supportPageURL()}`);
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,23 @@
|
||||
// Extensions API -> Global menu customize
|
||||
|
||||
import { observable } from "mobx";
|
||||
import { MenuItemConstructorOptions } from "electron";
|
||||
import type { MenuTopId } from "../main/menu";
|
||||
|
||||
export interface MenuRegistration {
|
||||
data?: any;
|
||||
export interface MenuRegistration extends MenuItemConstructorOptions {
|
||||
parentId?: MenuTopId;
|
||||
}
|
||||
|
||||
export class MenuRegistry {
|
||||
items = observable<MenuRegistration>([], { deep: false });
|
||||
protected items = observable.array([], { deep: false });
|
||||
|
||||
getItems(): MenuRegistration[] {
|
||||
return this.items.toJS();
|
||||
}
|
||||
|
||||
add(item: MenuRegistration) {
|
||||
this.items.push(item);
|
||||
return () => this.items.remove(item)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -38,12 +38,10 @@ export class PageRegistry {
|
||||
}
|
||||
|
||||
// fixme: validate route paths to avoid collisions
|
||||
add(params: PageRegistration) {
|
||||
this.pages.push(params);
|
||||
add(pageInit: PageRegistration) {
|
||||
this.pages.push(pageInit);
|
||||
return () => {
|
||||
this.pages.replace(
|
||||
this.pages.filter(page => page.components !== params.components)
|
||||
)
|
||||
this.pages.remove(pageInit); // works because of {deep: false}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,8 +6,11 @@ import { addClusterURL } from "../renderer/components/+add-cluster/add-cluster.r
|
||||
import { preferencesURL } from "../renderer/components/+preferences/preferences.route";
|
||||
import { whatsNewURL } from "../renderer/components/+whats-new/whats-new.route";
|
||||
import { clusterSettingsURL } from "../renderer/components/+cluster-settings/cluster-settings.route";
|
||||
import { menuRegistry } from "../extensions/menu-registry";
|
||||
import logger from "./logger";
|
||||
|
||||
export type MenuTopId = "mac" | "file" | "edit" | "view" | "help"
|
||||
|
||||
export function initMenu(windowManager: WindowManager) {
|
||||
autorun(() => buildMenu(windowManager), {
|
||||
delay: 100
|
||||
@ -53,8 +56,6 @@ export function buildMenu(windowManager: WindowManager) {
|
||||
})
|
||||
}
|
||||
|
||||
const mt: MenuItemConstructorOptions[] = [];
|
||||
|
||||
const macAppMenu: MenuItemConstructorOptions = {
|
||||
label: app.getName(),
|
||||
submenu: [
|
||||
@ -83,10 +84,6 @@ export function buildMenu(windowManager: WindowManager) {
|
||||
]
|
||||
};
|
||||
|
||||
if (isMac) {
|
||||
mt.push(macAppMenu);
|
||||
}
|
||||
|
||||
const fileMenu: MenuItemConstructorOptions = {
|
||||
label: "File",
|
||||
submenu: [
|
||||
@ -124,7 +121,6 @@ export function buildMenu(windowManager: WindowManager) {
|
||||
])
|
||||
]
|
||||
};
|
||||
mt.push(fileMenu)
|
||||
|
||||
const editMenu: MenuItemConstructorOptions = {
|
||||
label: 'Edit',
|
||||
@ -140,7 +136,7 @@ export function buildMenu(windowManager: WindowManager) {
|
||||
{ role: 'selectAll' },
|
||||
]
|
||||
};
|
||||
mt.push(editMenu)
|
||||
|
||||
const viewMenu: MenuItemConstructorOptions = {
|
||||
label: 'View',
|
||||
submenu: [
|
||||
@ -174,7 +170,6 @@ export function buildMenu(windowManager: WindowManager) {
|
||||
{ role: 'togglefullscreen' }
|
||||
]
|
||||
};
|
||||
mt.push(viewMenu)
|
||||
|
||||
const helpMenu: MenuItemConstructorOptions = {
|
||||
role: 'help',
|
||||
@ -214,7 +209,29 @@ export function buildMenu(windowManager: WindowManager) {
|
||||
]
|
||||
};
|
||||
|
||||
mt.push(helpMenu)
|
||||
// Prepare menu items order
|
||||
const appMenu: Record<MenuTopId, MenuItemConstructorOptions> = {
|
||||
mac: macAppMenu,
|
||||
file: fileMenu,
|
||||
edit: editMenu,
|
||||
view: viewMenu,
|
||||
help: helpMenu,
|
||||
}
|
||||
|
||||
Menu.setApplicationMenu(Menu.buildFromTemplate(mt));
|
||||
// Modify menu from extensions-api
|
||||
menuRegistry.getItems().forEach(({ parentId, ...menuItem }) => {
|
||||
try {
|
||||
const topMenu = appMenu[parentId].submenu as MenuItemConstructorOptions[];
|
||||
topMenu.push(menuItem);
|
||||
} catch (err) {
|
||||
logger.error(`[MENU]: can't register menu item, parentId=${parentId}`, { menuItem })
|
||||
}
|
||||
})
|
||||
|
||||
if (!isMac) {
|
||||
delete appMenu.mac
|
||||
}
|
||||
|
||||
const menu = Menu.buildFromTemplate(Object.values(appMenu));
|
||||
Menu.setApplicationMenu(menu);
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ export interface ILanguage {
|
||||
|
||||
export const _i18n = setupI18n({
|
||||
missing: (message, id) => {
|
||||
console.warn('Missing localization:', message, id);
|
||||
// console.warn('Missing localization:', message, id);
|
||||
return id;
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user