diff --git a/main-process/AppMenu.ts b/main-process/AppMenu.ts index 4957986..3e28e83 100644 --- a/main-process/AppMenu.ts +++ b/main-process/AppMenu.ts @@ -1,4 +1,4 @@ -import { BrowserView, Menu, app, shell, dialog, BrowserWindow, MenuItem, WebContents } from 'electron'; +import { BrowserView, Menu, app, shell, dialog, BrowserWindow, MenuItem, WebContents, MenuItemConstructorOptions } from 'electron'; import MainEventType from '../common/MainEventType'; import { MainProcess } from './main'; import Settings from './Settings'; @@ -12,6 +12,7 @@ export default class AppMenu { constructor(mainProcess: MainProcess) { this.mainProcess = mainProcess; this.mainWindow = mainProcess.mainWindow; + this.webContents = mainProcess.mainWindow.webContents; this.settings = mainProcess.settings; } @@ -25,6 +26,25 @@ export default class AppMenu { } private createFileMenu() { + const fileItems: MenuItemConstructorOptions[] = []; + for (let path of this.settings.recentFiles) { + fileItems.push({ + label: path, + click: () => { + console.log("open recent file", path); + } + }) + } + const workspaceItems: MenuItemConstructorOptions[] = []; + for (let path of this.settings.recentWorkspaces) { + workspaceItems.push({ + label: path, + click: () => { + console.log("open recent workspace", path); + } + }) + } + return new MenuItem({ label: "行为树", submenu: [ @@ -39,7 +59,15 @@ export default class AppMenu { { name: "Json", extensions: ['json'] } ] }); - this.webContents.send(MainEventType.OPEN_FILE, res.filePaths[0]); + if (res.filePaths.length > 0) { + const path = res.filePaths[0]; + if (this.settings.recentFiles.indexOf(path) < 0) { + this.settings.recentFiles.unshift(path); + this.settings.save(); + this.mainProcess.rebuildMenu(); + } + this.webContents.send(MainEventType.OPEN_FILE, path); + } })(); } }, @@ -52,7 +80,13 @@ export default class AppMenu { properties: ['openDirectory'] }); if (res.filePaths.length > 0) { - this.webContents.send(MainEventType.OPEN_WORKSPACE, res.filePaths[0]); + const path = res.filePaths[0]; + if (this.settings.recentWorkspaces.indexOf(path) < 0) { + this.settings.recentWorkspaces.unshift(path); + this.settings.save(); + this.mainProcess.rebuildMenu(); + } + this.webContents.send(MainEventType.OPEN_WORKSPACE, path); } })(); } @@ -60,14 +94,11 @@ export default class AppMenu { { type: 'separator' }, { label: "最近打开", - submenu: [] + submenu: fileItems, }, { label: "最近目录", - submenu: [ - { label: "master" }, - { label: "分支0908" }, - ] + submenu: workspaceItems, }, { type: 'separator' }, { diff --git a/main-process/main.tsx b/main-process/main.tsx index dd2c34d..7c24c6d 100644 --- a/main-process/main.tsx +++ b/main-process/main.tsx @@ -25,7 +25,6 @@ export class MainProcess { createWindow (){ this.settings = new Settings(); - this.appMenu = new AppMenu(this); this.mainWindow = new BrowserWindow({ width: 1280, height: 800, @@ -40,11 +39,11 @@ 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()); } }