-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
140 lines (113 loc) · 3.77 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// npm init -y 用来构造一个package.json
// npm i electron -D 加载electron依赖
// npm i electron -g 全局安装electron依赖
// 全局安装 nodemon
// npm i nodemon -g
// 使用如下命令设置为淘宝的镜像源:
// npm config set registry https://registry.npm.taobao.org
// 使用如下命令检验是否成功:
// npm config get registry
// 默认安装package.json中的所有模块。
// npm install
// 如果只想安装dependencies中的内容,可以使用--dependencies字段:
// npm install --dependencies
// 同样只想安装devDependencies中的内容,可以使用--devDependencies字段:
// npm install --devDependencies
// 修改js 可以使app重启
// "start": "nodemon --exec electron ."
// common.js 包规范导入
const {app,BrowserWindow,ipcMain ,Menu,dialog,desktopCapturer} = require('electron')
const path= require('path')
const WinState = require('electron-win-state').default
const mainMenu = require('./mainMenu')
const createTray = require('./tray')
// 窗口状态保存 x、y在屏幕中的偏移量 以及窗口大小
const winState = new WinState({
defaultWidth:600,
defaultHeight:400,
})
// 设置菜单
let menu = Menu.buildFromTemplate(mainMenu('传给menu的参数', (params) => {
console.log(params);
}) )
const createWindow = () => {
let win = new BrowserWindow({
...winState.winOptions,
// width:1000,height:800,
webPreferences:{
// // 开启沙盒则preload脚本被禁用,所以得设为false
sandbox: false,
// nodeIntegration:true,
// contextIsolation:false,
preload: path.join(__dirname, './preload.js')
}})
win.loadFile('index.html')
// win.loadURL('https://github.com')
// 打开开发者工具
win.webContents.openDevTools()
console.log('文件目录='+__dirname);
winState.manage(win)
// webContents 相关事件
// did-finish-load 页面加载完毕
win.webContents.on('did-finish-load',() => {
console.log('did-finish-load');
})
// dom 加载完毕
win.webContents.on('dom-ready', () => {
console.log('dom-ready');
})
// 右键 上下文菜单
win.webContents.on('context-menu',(event,params) => {
console.log(params);
console.log('右键信息=' + params.selectionText);
// 注入js执行
// win.webContents.executeJavaScript(`alert('${params.selectionText}')`)
// 右键菜单
menu.popup()
})
// 设置menu 可以设置menu点击回调
Menu.setApplicationMenu(menu)
// 托盘功能
createTray(app,win)
// desktopCapturer 捕获音视频信息 只在主进程(main process)可⽤
ipcMain.handle('mediaCaptrue',async(event) => {
console.log('ipcMain mediaCaptrue');
return desktopCapturer.getSources({
types: ['window', 'screen'],
thumbnailSize: {
width: 1728,
height: 1117
}
}).then(async(sources) => {
for(const source of sources) {
if (source.name == '整个屏幕') {
return source
}
}
})
})
}
app.whenReady().then(() => {
createWindow()
})
app.on('window-all-closed',() => {
if (process.platform !== 'darwin') {
console.log('当前平台='+process.platform);
app.quit()
}
})
app.on('quit',() => {
console.log('app quit');
})
// 主进程接收数据
const eventObserver = async(event,data) => {
// 主进程中log打印 会显示在控制终端中
console.log('eventObserver',data);
return 'ok ' + JSON.stringify(data)
}
ipcMain.handle('sendEvent', eventObserver)
const xlog = (data) => {
const electronDialog = window.require('electron').remote;
const con = electronDialog.getGlobal('console')
con.log(data);
}