-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
216 lines (188 loc) · 4.53 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const Config = require("./config.js");
const {app, BrowserWindow, clipboard, globalShortcut, ipcMain, Tray, Menu} = require('electron');
/**
* 这个是保存复制的文本
* @type {Array}
*/
const PASTE_TEXT = [];
/**
* 全局的主窗口
* @type {object}
*/
let mainWindow = null;
/**
* 托盘
* @type {object}
*/
let tray = null;
/**
* 注册函数的原子方法
* @param cmd
* @param callback
*/
function doRegister(cmd, callback) {
globalShortcut.register(cmd, callback);
}
/**
* 注册快捷菜单
*/
function registerShortcut() {
//注册开发F12快捷键
// doRegister(Config.shortcut.dev, () => {
// let win = BrowserWindow.getFocusedWindow();
// if (!win) {
// return;
// }
// win.webContents.toggleDevTools();
// console.log("toggleDevTools F12");
// });
//注册Command + 1~9
for (let i = 1; i < 10; i++) {
doRegister(`${Config.shortcut.paste}+${i}`, () => {
if (i > PASTE_TEXT.length) {
return;
}
if (mainWindow.isVisible()) {
mainWindow.hide();
}
clipboard.writeText(PASTE_TEXT[i - 1]);
});
}
doRegister(Config.shortcut.show, () => {
if (!mainWindow.isVisible()) {
showMainWindow();
}
});
}
/**
* 监听剪切板中的内容
*/
function listenClipboard() {
// 先读取当前剪切板中的文字内容
let currentText = clipboard.readText();
if (!currentText || currentText === "\n") {
return;
}
insertPasteText(currentText);
}
/**
* 把文字内容插入剪切板
* @param text
*/
function insertPasteText(text) {
let pos = PASTE_TEXT.indexOf(text);
if (pos === -1) {
PASTE_TEXT.unshift(text);
if (PASTE_TEXT.length > Config.maxLength) {
PASTE_TEXT.pop();
}
} else {
PASTE_TEXT.splice(pos, 1);
PASTE_TEXT.unshift(text);
}
}
function createMainWindow() {
let win = new BrowserWindow({
width: Config.width,
height: Config.height,
frame: false,
transparent: true,
resizable: true,
title: Config.title,
show: false
});
app.dock.hide();
win.setAlwaysOnTop(true, "floating");
win.setVisibleOnAllWorkspaces(true);
win.setFullScreenable(false);
win.loadURL(`file://${__dirname}/index.html`);
//给窗口绑定各种事件
bindWindowEvent(win);
return win;
}
function createTray() {
let tray = process.platform === 'darwin' ? (new Tray(`${__dirname}/tray.png`)) : (new Tray(`${__dirname}/tray-win.png`));
let trayMenuTemplate = [
{
label: '查看',
click: function () {
if (!mainWindow) {
mainWindow = createMainWindow();
} else {
showMainWindow();
}
}
},
{
label: '退出',
click: function () {
app.quit();
}
}
];
let trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
tray.setContextMenu(trayMenu);
tray.on("click", () => {
if (!mainWindow) {
mainWindow = createMainWindow();
} else {
showMainWindow();
}
});
return tray;
}
/**
* 往渲染ui发送消息
*/
function sendPasteTextToUI() {
//给渲染进程发送剪切板信息
mainWindow.webContents.send(Config.messageFlag.clipboardMessage, PASTE_TEXT);
}
/**
* 给窗口绑定事件
* @param win
*/
function bindWindowEvent(win) {
win.on('blur', () => {
// globalShortcut.unregisterAll();
win.hide();
});
win.on('focus', function () {
// registerShortcut(win);
});
win.once('ready-to-show', () => {
showMainWindow();
})
}
function showMainWindow() {
mainWindow.show();
sendPasteTextToUI();
}
app.on('window-all-closed', () => {
app.quit();
});
// app.on('activate', () => {
// if (!mainWindow) {
// mainWindow = createMainWindow();
// } else {
// showMainWindow();
// }
// });
ipcMain.on(Config.messageFlag.clipboardMessage, (event, arg) => {
mainWindow.hide();
if (arg === 0) {
return;
}
clipboard.writeText(PASTE_TEXT[arg - 1]);
});
/**
* 入口函数
*/
app.on('ready', () => {
mainWindow = createMainWindow();
tray = createTray();
listenClipboard();
setInterval(listenClipboard, Config.listenClipboardInterval);
//注册键盘快捷键
registerShortcut();
});