-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
98 lines (77 loc) · 3.02 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const os = require('os');
var platform = os.platform();
const electron = require("electron");
const app = electron.app;
const ipcMain = electron.ipcMain;
const BrowserWindow = electron.BrowserWindow;
const nativeImage = electron.nativeImage;
var hello = require("hello");
var mainWindow = null;
var onlineStatusWindow = null;
app.disableHardwareAcceleration();
//初始化并准备创建主窗口
app.on("ready", function ()
{
//创建一个宽800px 高700px的窗口
mainWindow = new BrowserWindow({width:800,height:500,frame:false});
//console.log("dirname:%s", __dirname);
//mainWindow.loadURL("https://www.baidu.com");
mainWindow.loadURL("file://" + __dirname + "/index.html");//载入应用的inde.html
//mainWindow.openDevTools(); //打开浏览器调试工具
var iconImage = null;
var isInWin32 = (platform=="win32");
if(isInWin32)
{
iconImage = nativeImage.createFromPath(__dirname + "/logo.png");
mainWindow.setIcon(iconImage);
}
//窗口关闭时触发
mainWindow.on('closed', function(){
//想要取消窗口对象的引用, 如果你的应用支持多窗口,
//你需要将所有的窗口对象存储到一个数组中,然后在这里删除想对应的元素
console.log("mainWindow closed...");
mainWindow = null
});
onlineStatusWindow = new BrowserWindow({width:300,height:300,frame:true,show:true});
onlineStatusWindow.loadURL("file://" + __dirname + "/online-status.html");
if(isInWin32)
{
onlineStatusWindow.setIcon(iconImage);
}
onlineStatusWindow.on('closed', function(){
console.log("onlineStatusWindow closed...");
onlineStatusWindow = null
});
});
// Quit when all windows are closed.
app.on('window-all-closed', function() {
app.quit();
});
//ipcMain对同步消息的处理
ipcMain.on('sync-message', function (event, arg){
console.log(arg); // prints "ping"
event.returnValue = "sync-message-reply";
});
//ipcMain对异步消息的处理
ipcMain.on("invoke-cpp-module", function(event, originStr) {
var retStr = hello.f1();
var curProcessId = hello.getCurrentProcessId();
console.log("current process id :" + curProcessId); //在main process中会输出到黑框console,在renderer process中会输出到devtool的console
event.sender.send("invoke-cpp-module-reply", originStr + " " + retStr + " [Main Process Id : " + curProcessId + "]");
//从hello中执行同步回调函数
hello.runSyncCallback(function(param){
console.log(param);
});
////从hello中执行异步回调函数
hello.runAsyncCallback("<<<<origin string>>>>", function(error, param){
if(error) {
console.log(error);
} else {
console.log(param);
}
});
});
ipcMain.on("online-status-changed", function(event, status){
console.log(status)
event.sender.send("online-status-changed-reply","[async msg received by ipcMain]" + " " + status);
});