/
app.js
106 lines (95 loc) · 2.59 KB
/
app.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
const { ipcMain, BrowserWindow } = require('electron');
// 主进程业务代码
// 事件前缀规范
// global- 全局通用方法
// project- 项目相关
// sdk- 跟 SDK 操作相关
// generate- 跟模板、脚手架生成器相关
const { globalShortcut } = require('electron');
const log = require('./logger');
const { isWin, isMac } = require('./shared');
const dau = require('./dau');
const sessions = require('./services/sessions');
if (isMac) {
// 执行环境附带 /usr/local/bin
process.env.PATH = ['/usr/local/bin', process.env.PATH].join(':');
}
/**
* 注册 app 交互事件,ipcMain 等事件
* @param {*} app electron.app
* @param {Object} windows window 对象合集
*/
/* eslint no-console:off */
exports.registerApp = (app, windows) => {
app
.on('will-quit', () => {
console.log('will-quit');
})
.on('window-all-closed', () => {
console.log('window-all-closed');
// 等待进程都关闭后退出程序
if (isWin) {
sessions.manager
.destory()
.then(() => {
app.quit();
})
.catch(() => {
app.quit();
});
} else {
sessions.manager
.destory()
.then(() => {})
.catch(() => {});
// not run quit in darwiwn
// https://github.com/alibaba/ice/issues/56
// app.quit();
}
})
.on('before-quit', () => {
log.report('app', { action: 'before-quit' });
console.log('before-quit');
if (isMac) {
BrowserWindow.getAllWindows().forEach((currentWindow) => {
currentWindow.destroy();
});
}
})
.on('activate', () => {
windows.home.show();
windows.home.focus();
});
ipcMain.on('setTitle', (event, title) => {
windows.home.setTitle(title);
});
windows.home.on('focus', ({ sender }) => {
// DAU 激活统计
dau.record();
sender.webContents.send('focus');
});
};
/**
* 注册全局快捷键
* @param {*} app electron.app
* @param {Object} windows window 实例合集
*/
exports.registerShortcut = (app, windows) => {
// TODO 支持自定义快捷键
const shortcutKey = 'CommandOrControl+alt+P';
// settings.get('userconfig.shortcutKey') || 'CommandOrControl+alt+I';
const ret = globalShortcut.register(shortcutKey, () => {
windows.home.show();
});
if (ret) {
log.info('shortcutKey register success', shortcutKey);
log.report('app', {
action: 'shortcut-key',
shortcutKey,
});
}
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll();
});
};