MainProcess add global val settings
This commit is contained in:
1 parent
d0690f0398
commit
23f4795562
5 files changed
+37
-22
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
import { Menu, app, dialog, BrowserWindow, MenuItem, WebContents, MenuItemConstructorOptions } from 'electron';
|
import { Menu, app, dialog, BrowserWindow, MenuItem, WebContents, MenuItemConstructorOptions } from 'electron';
|
||||||
import MainEventType from '../common/MainEventType';
|
import MainEventType from '../common/MainEventType';
|
||||||
import { MainProcess } from './main';
|
import { MainProcess } from './MainProcess';
|
||||||
import Settings from './Settings';
|
import Settings from './Settings';
|
||||||
|
|
||||||
export default class AppMenu {
|
export default class AppMenu {
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ import AppMenu from './AppMenu'
|
|||||||
import Settings from './Settings';
|
import Settings from './Settings';
|
||||||
import MainEventType from '../common/MainEventType';
|
import MainEventType from '../common/MainEventType';
|
||||||
|
|
||||||
|
// 一些暴露给render-process的全局变量
|
||||||
|
export interface Global {
|
||||||
|
settings: Settings
|
||||||
|
}
|
||||||
|
declare var global: Global;
|
||||||
|
|
||||||
export class MainProcess {
|
export class MainProcess {
|
||||||
mainWindow: BrowserWindow;
|
mainWindow: BrowserWindow;
|
||||||
appMenu: AppMenu;
|
appMenu: AppMenu;
|
||||||
@@ -26,11 +32,14 @@ export class MainProcess {
|
|||||||
|
|
||||||
createWindow() {
|
createWindow() {
|
||||||
this.settings = new Settings();
|
this.settings = new Settings();
|
||||||
|
global.settings = this.settings;
|
||||||
|
|
||||||
this.mainWindow = new BrowserWindow({
|
this.mainWindow = new BrowserWindow({
|
||||||
width: 1280,
|
width: 1280,
|
||||||
height: 800,
|
height: 800,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
nodeIntegration: true
|
nodeIntegration: true,
|
||||||
|
enableRemoteModule: true,
|
||||||
}
|
}
|
||||||
// fullscreenable:false,
|
// fullscreenable:false,
|
||||||
// maximizable:false
|
// maximizable:false
|
||||||
@@ -42,11 +51,6 @@ export class MainProcess {
|
|||||||
});
|
});
|
||||||
this.appMenu = new AppMenu(this);
|
this.appMenu = new AppMenu(this);
|
||||||
this.rebuildMenu();
|
this.rebuildMenu();
|
||||||
|
|
||||||
const lastWorkspace = this.settings.recentWorkspaces[0];
|
|
||||||
if(lastWorkspace) {
|
|
||||||
this.mainWindow.webContents.send(MainEventType.OPEN_WORKSPACE, lastWorkspace);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rebuildMenu() {
|
rebuildMenu() {
|
||||||
@@ -54,4 +58,6 @@ export class MainProcess {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new MainProcess();
|
|
||||||
|
export default new MainProcess();
|
||||||
|
|
||||||
+16
-12
@@ -25,18 +25,7 @@ const sampleNodeClassify: BehaviorNodeClassify[] = [
|
|||||||
export default class Settings {
|
export default class Settings {
|
||||||
private settings: SettingsModel;
|
private settings: SettingsModel;
|
||||||
constructor() {
|
constructor() {
|
||||||
if (fs.existsSync(settingPath)) {
|
this.load();
|
||||||
const str = fs.readFileSync(settingPath, 'utf8');
|
|
||||||
this.settings = JSON.parse(str);
|
|
||||||
} else {
|
|
||||||
this.settings = {
|
|
||||||
recentWorkspaces: [],
|
|
||||||
recentFiles: [],
|
|
||||||
nodeConfigPath: sampleNodeConfig,
|
|
||||||
nodeClassify: sampleNodeClassify,
|
|
||||||
};
|
|
||||||
this.save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get nodeConfigPath() {
|
get nodeConfigPath() {
|
||||||
@@ -65,6 +54,21 @@ export default class Settings {
|
|||||||
this.save();
|
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() {
|
save() {
|
||||||
fs.writeFileSync(settingPath, JSON.stringify(this.settings, null, 2));
|
fs.writeFileSync(settingPath, JSON.stringify(this.settings, null, 2));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import React, { Component } from "react";
|
|||||||
import { Menu, Input } from "antd";
|
import { Menu, Input } from "antd";
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { BehaviorTreeModel } from "../../common/BehaviorTreeModel";
|
import { BehaviorTreeModel } from "../../common/BehaviorTreeModel";
|
||||||
|
import { remote } from 'electron';
|
||||||
|
import { MainProcess } from "../../main-process/MainProcess";
|
||||||
|
import Settings from "../../main-process/Settings";
|
||||||
|
|
||||||
const { Search } = Input;
|
const { Search } = Input;
|
||||||
|
|
||||||
@@ -21,7 +24,9 @@ export default class Properties extends Component<PropertiesProps> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
const settings: Settings = remote.getGlobal("settings");
|
||||||
|
const lastWorkspace = settings.recentWorkspaces[0];
|
||||||
|
console.log("&&&&&& last workspace", lastWorkspace);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ module.exports = {
|
|||||||
mode: "development",
|
mode: "development",
|
||||||
entry: {
|
entry: {
|
||||||
"bundle": ["./render-process/index.tsx"],
|
"bundle": ["./render-process/index.tsx"],
|
||||||
"main": ["./main-process/main.tsx"]
|
"main": ["./main-process/MainProcess.tsx"]
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
filename: "[name].js",
|
filename: "[name].js",
|
||||||
|
|||||||
Reference in new issue
Block a user