From 677815572c125694d647f0b0c76ef31f227d46ca Mon Sep 17 00:00:00 2001 From: zhandouxiaojiji Date: Wed, 14 Oct 2020 20:25:31 +0800 Subject: [PATCH] rebuild menu --- main-process/AppMenu.ts | 89 ++++++++++++++++++++++++++++++++-------- main-process/Settings.ts | 18 ++++++-- main-process/main.tsx | 8 +++- 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/main-process/AppMenu.ts b/main-process/AppMenu.ts index 3498e35..4957986 100644 --- a/main-process/AppMenu.ts +++ b/main-process/AppMenu.ts @@ -1,12 +1,31 @@ -import { BrowserView, Menu, app, shell, dialog, BrowserWindow, MenuItem } from 'electron'; +import { BrowserView, Menu, app, shell, dialog, BrowserWindow, MenuItem, WebContents } from 'electron'; import MainEventType from '../common/MainEventType'; import { MainProcess } from './main'; +import Settings from './Settings'; export default class AppMenu { + private mainProcess: MainProcess; + private mainWindow: BrowserWindow; + private webContents: WebContents; + private settings: Settings; + constructor(mainProcess: MainProcess) { - const mainWindow = mainProcess.mainWindow; + this.mainProcess = mainProcess; + this.mainWindow = mainProcess.mainWindow; + this.settings = mainProcess.settings; + } + + createMenu() { const menu: Menu = new Menu(); - const fileMenu: MenuItem = new MenuItem({ + menu.append(this.createFileMenu()); + menu.append(this.createNodeMenu()); + menu.append(this.createSettingsMenu()); + menu.append(this.createToolsMenu()); + return menu; + } + + private createFileMenu() { + return new MenuItem({ label: "行为树", submenu: [ { @@ -20,7 +39,7 @@ export default class AppMenu { { name: "Json", extensions: ['json'] } ] }); - mainWindow.webContents.send(MainEventType.OPEN_FILE, res.filePaths[0]); + this.webContents.send(MainEventType.OPEN_FILE, res.filePaths[0]); })(); } }, @@ -33,7 +52,7 @@ export default class AppMenu { properties: ['openDirectory'] }); if (res.filePaths.length > 0) { - mainWindow.webContents.send(MainEventType.OPEN_WORKSPACE, res.filePaths[0]); + this.webContents.send(MainEventType.OPEN_WORKSPACE, res.filePaths[0]); } })(); } @@ -59,10 +78,52 @@ export default class AppMenu { } ] }); + } - const toolsMenu: MenuItem = new MenuItem({ + private createSettingsMenu() { + return new MenuItem({ + label: "设置", + submenu: [ + { + label: "节点定义", + submenu: [ + { + label: "选择文件", + click: () => { + (async () => { + const res = await dialog.showOpenDialog({ + properties: ['openFile'], + filters: [ + { name: "Json", extensions: ['json'] } + ] + }); + if (res.filePaths.length > 0) { + const nodeConfigPath = res.filePaths[0]; + this.settings.set({ nodeConfigPath }); + this.mainProcess.rebuildMenu(); + } + })(); + } + }, + { type: 'separator' }, + { label: this.settings.nodeConfigPath, } + ] + } + ] + }); + } + + private createToolsMenu() { + return new MenuItem({ label: "开发工具", submenu: [ + { + label: "打开控制台", + accelerator: "Ctrl+Shift+I", + click: (_, browserWindow) => { + browserWindow.webContents.toggleDevTools(); + } + }, { label: "重载", accelerator: "Ctrl+R", @@ -70,19 +131,13 @@ export default class AppMenu { browserWindow.reload(); } }, - { - label: "打开控制台", - accelerator: "Ctrl+Shift+I", - click: (_, browserWindow) => { - browserWindow.webContents.toggleDevTools(); - } - } ] }); + } - menu.append(fileMenu); - menu.append(toolsMenu); - - Menu.setApplicationMenu(menu); + private createNodeMenu() { + return new MenuItem({ + label: "新建节点" + }); } } \ No newline at end of file diff --git a/main-process/Settings.ts b/main-process/Settings.ts index 2359667..5b042e7 100644 --- a/main-process/Settings.ts +++ b/main-process/Settings.ts @@ -3,9 +3,9 @@ import * as fs from 'fs'; const settingPath = 'settings.json'; export interface SettingsModel { - recentWorkspaces: string[]; - recentFiles: string[]; - nodeConfigPath: string; // 节点配置路径 + recentWorkspaces?: string[]; + recentFiles?: string[]; + nodeConfigPath?: string; // 节点配置路径 } export default class Settings { @@ -24,7 +24,17 @@ export default class Settings { } } - set(config: any) { + get nodeConfigPath() { + return this.settings.nodeConfigPath; + } + get recentWorkspaces() { + return this.settings.recentWorkspaces; + } + get recentFiles() { + return this.settings.recentFiles; + } + + set(config: SettingsModel) { this.settings = { ...this.settings, ...config diff --git a/main-process/main.tsx b/main-process/main.tsx index 5ac486f..dd2c34d 100644 --- a/main-process/main.tsx +++ b/main-process/main.tsx @@ -25,6 +25,7 @@ export class MainProcess { createWindow (){ this.settings = new Settings(); + this.appMenu = new AppMenu(this); this.mainWindow = new BrowserWindow({ width: 1280, height: 800, @@ -39,7 +40,12 @@ export class MainProcess { this.mainWindow.on('closed', function () { this.mainWindow = null }); - this.appMenu = new AppMenu(this); + this.rebuildMenu(); + } + + rebuildMenu() { + console.log("rebuild menu"); + Menu.setApplicationMenu(this.appMenu.createMenu()); } }