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 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
@@ -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));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user