MainProcess add global val settings

This commit is contained in:
zhandouxiaojiji committed 2020-10-15 16:59:03 +08:00
1 parent d0690f0398
commit 23f4795562
5 files changed
+37 -22

No files matched your search

+1 -1
View File
@@ -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 {
@@ -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();
export default new MainProcess();
+16 -12
View File
@@ -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));
}
+6 -1
View File
@@ -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<PropertiesProps> {
};
componentDidMount() {
const settings: Settings = remote.getGlobal("settings");
const lastWorkspace = settings.recentWorkspaces[0];
console.log("&&&&&& last workspace", lastWorkspace);
}
render() {
+1 -1
View File
@@ -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",