From 23f4795562c3e26b399471372b39d685a7ec95dc Mon Sep 17 00:00:00 2001 From: zhandouxiaojiji Date: Thu, 15 Oct 2020 16:59:03 +0800 Subject: [PATCH] MainProcess add global val settings --- main-process/AppMenu.ts | 2 +- main-process/{main.tsx => MainProcess.tsx} | 20 ++++++++++------ main-process/Settings.ts | 28 ++++++++++++---------- render-process/Editor/Properties.tsx | 7 +++++- webpack.config.js | 2 +- 5 files changed, 37 insertions(+), 22 deletions(-) rename main-process/{main.tsx => MainProcess.tsx} (78%) diff --git a/main-process/AppMenu.ts b/main-process/AppMenu.ts index 83e78b2..82acc47 100644 --- a/main-process/AppMenu.ts +++ b/main-process/AppMenu.ts @@ -1,6 +1,6 @@ import { Menu, app, dialog, BrowserWindow, MenuItem, WebContents, MenuItemConstructorOptions } from 'electron'; import MainEventType from '../common/MainEventType'; -import { MainProcess } from './main'; +import { MainProcess } from './MainProcess'; import Settings from './Settings'; export default class AppMenu { diff --git a/main-process/main.tsx b/main-process/MainProcess.tsx similarity index 78% rename from main-process/main.tsx rename to main-process/MainProcess.tsx index 56a285c..814a6ae 100644 --- a/main-process/main.tsx +++ b/main-process/MainProcess.tsx @@ -3,6 +3,12 @@ import AppMenu from './AppMenu' import Settings from './Settings'; import MainEventType from '../common/MainEventType'; +// 一些暴露给render-process的全局变量 +export interface Global { + settings: Settings +} +declare var global: Global; + export class MainProcess { mainWindow: BrowserWindow; appMenu: AppMenu; @@ -26,11 +32,14 @@ export class MainProcess { createWindow() { this.settings = new Settings(); + global.settings = this.settings; + this.mainWindow = new BrowserWindow({ width: 1280, height: 800, webPreferences: { - nodeIntegration: true + nodeIntegration: true, + enableRemoteModule: true, } // fullscreenable:false, // maximizable:false @@ -42,11 +51,6 @@ export class MainProcess { }); this.appMenu = new AppMenu(this); this.rebuildMenu(); - - const lastWorkspace = this.settings.recentWorkspaces[0]; - if(lastWorkspace) { - this.mainWindow.webContents.send(MainEventType.OPEN_WORKSPACE, lastWorkspace); - } } rebuildMenu() { @@ -54,4 +58,6 @@ export class MainProcess { } } -new MainProcess(); \ No newline at end of file + +export default new MainProcess(); + diff --git a/main-process/Settings.ts b/main-process/Settings.ts index 74edbc0..eac19b2 100644 --- a/main-process/Settings.ts +++ b/main-process/Settings.ts @@ -25,18 +25,7 @@ const sampleNodeClassify: BehaviorNodeClassify[] = [ export default class Settings { private settings: SettingsModel; constructor() { - if (fs.existsSync(settingPath)) { - const str = fs.readFileSync(settingPath, 'utf8'); - this.settings = JSON.parse(str); - } else { - this.settings = { - recentWorkspaces: [], - recentFiles: [], - nodeConfigPath: sampleNodeConfig, - nodeClassify: sampleNodeClassify, - }; - this.save(); - } + this.load(); } get nodeConfigPath() { @@ -65,6 +54,21 @@ export default class Settings { this.save(); } + load() { + if (fs.existsSync(settingPath)) { + const str = fs.readFileSync(settingPath, 'utf8'); + this.settings = JSON.parse(str); + } else { + this.settings = { + recentWorkspaces: [], + recentFiles: [], + nodeConfigPath: sampleNodeConfig, + nodeClassify: sampleNodeClassify, + }; + this.save(); + } + } + save() { fs.writeFileSync(settingPath, JSON.stringify(this.settings, null, 2)); } diff --git a/render-process/Editor/Properties.tsx b/render-process/Editor/Properties.tsx index 7460d12..9a13251 100644 --- a/render-process/Editor/Properties.tsx +++ b/render-process/Editor/Properties.tsx @@ -2,6 +2,9 @@ import React, { Component } from "react"; import { Menu, Input } from "antd"; import * as fs from 'fs'; import { BehaviorTreeModel } from "../../common/BehaviorTreeModel"; +import { remote } from 'electron'; +import { MainProcess } from "../../main-process/MainProcess"; +import Settings from "../../main-process/Settings"; const { Search } = Input; @@ -21,7 +24,9 @@ export default class Properties extends Component { }; componentDidMount() { - + const settings: Settings = remote.getGlobal("settings"); + const lastWorkspace = settings.recentWorkspaces[0]; + console.log("&&&&&& last workspace", lastWorkspace); } render() { diff --git a/webpack.config.js b/webpack.config.js index c227081..9a0b60c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -2,7 +2,7 @@ module.exports = { mode: "development", entry: { "bundle": ["./render-process/index.tsx"], - "main": ["./main-process/main.tsx"] + "main": ["./main-process/MainProcess.tsx"] }, output: { filename: "[name].js",