rebuild menu
This commit is contained in:
1 parent
a1adbe69c5
commit
677815572c
3 files changed
+93
-22
No files matched your search
+72
-17
@@ -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 MainEventType from '../common/MainEventType';
|
||||||
import { MainProcess } from './main';
|
import { MainProcess } from './main';
|
||||||
|
import Settings from './Settings';
|
||||||
|
|
||||||
export default class AppMenu {
|
export default class AppMenu {
|
||||||
|
private mainProcess: MainProcess;
|
||||||
|
private mainWindow: BrowserWindow;
|
||||||
|
private webContents: WebContents;
|
||||||
|
private settings: Settings;
|
||||||
|
|
||||||
constructor(mainProcess: MainProcess) {
|
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 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: "行为树",
|
label: "行为树",
|
||||||
submenu: [
|
submenu: [
|
||||||
{
|
{
|
||||||
@@ -20,7 +39,7 @@ export default class AppMenu {
|
|||||||
{ name: "Json", extensions: ['json'] }
|
{ 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']
|
properties: ['openDirectory']
|
||||||
});
|
});
|
||||||
if (res.filePaths.length > 0) {
|
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: "开发工具",
|
label: "开发工具",
|
||||||
submenu: [
|
submenu: [
|
||||||
|
{
|
||||||
|
label: "打开控制台",
|
||||||
|
accelerator: "Ctrl+Shift+I",
|
||||||
|
click: (_, browserWindow) => {
|
||||||
|
browserWindow.webContents.toggleDevTools();
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: "重载",
|
label: "重载",
|
||||||
accelerator: "Ctrl+R",
|
accelerator: "Ctrl+R",
|
||||||
@@ -70,19 +131,13 @@ export default class AppMenu {
|
|||||||
browserWindow.reload();
|
browserWindow.reload();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
label: "打开控制台",
|
|
||||||
accelerator: "Ctrl+Shift+I",
|
|
||||||
click: (_, browserWindow) => {
|
|
||||||
browserWindow.webContents.toggleDevTools();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
menu.append(fileMenu);
|
private createNodeMenu() {
|
||||||
menu.append(toolsMenu);
|
return new MenuItem({
|
||||||
|
label: "新建节点"
|
||||||
Menu.setApplicationMenu(menu);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,9 @@ import * as fs from 'fs';
|
|||||||
const settingPath = 'settings.json';
|
const settingPath = 'settings.json';
|
||||||
|
|
||||||
export interface SettingsModel {
|
export interface SettingsModel {
|
||||||
recentWorkspaces: string[];
|
recentWorkspaces?: string[];
|
||||||
recentFiles: string[];
|
recentFiles?: string[];
|
||||||
nodeConfigPath: string; // 节点配置路径
|
nodeConfigPath?: string; // 节点配置路径
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Settings {
|
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 = {
|
||||||
...this.settings,
|
...this.settings,
|
||||||
...config
|
...config
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ export class MainProcess {
|
|||||||
|
|
||||||
createWindow (){
|
createWindow (){
|
||||||
this.settings = new Settings();
|
this.settings = new Settings();
|
||||||
|
this.appMenu = new AppMenu(this);
|
||||||
this.mainWindow = new BrowserWindow({
|
this.mainWindow = new BrowserWindow({
|
||||||
width: 1280,
|
width: 1280,
|
||||||
height: 800,
|
height: 800,
|
||||||
@@ -39,7 +40,12 @@ export class MainProcess {
|
|||||||
this.mainWindow.on('closed', function () {
|
this.mainWindow.on('closed', function () {
|
||||||
this.mainWindow = null
|
this.mainWindow = null
|
||||||
});
|
});
|
||||||
this.appMenu = new AppMenu(this);
|
this.rebuildMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
rebuildMenu() {
|
||||||
|
console.log("rebuild menu");
|
||||||
|
Menu.setApplicationMenu(this.appMenu.createMenu());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user