-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
129 lines (107 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
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
const {app, BrowserWindow, globalShortcut, ipcMain, clipboard, Menu} = require('electron');
const electron = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const robot = require("robotjs");
const ClipboardWatcher = require('./util/clipboard-watcher');
const db = require('./util/db');
const tray = require('./util/tray');
let mainWindow
if (app.makeSingleInstance(() => {})) {
app.quit();
}
function init() {
globalShortcut.register('CommandOrControl+Shift+V', show);
globalShortcut.register('CommandOrControl+;', show);
createWindow();
whatchForClipboard();
tray.buildTrayMenu({
show,
clearHistory
});
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 300,
height: 400,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true
});
mainWindow.setVisibleOnAllWorkspaces(true);
hide();
mainWindow.on('blur', () => hide());
app.dock.hide()
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'app/index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
//mainWindow.webContents.openDevTools({mode: 'undocked'});
// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null
});
}
app.on('ready', init)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
ipcMain.on('hide', () => mainWindow.hide());
ipcMain.on('pageready', () => {
db.findAll().then(rows => mainWindow.webContents.send('data', rows));
});
ipcMain.on('filter', (event, data) => {
db.filter(data.input).then(rows => mainWindow.webContents.send('data-filter', {rows: rows, filter: data.input}));
});
ipcMain.on('select', (event, data) => {
hide();
if (data.file) {
clipboard.writeText(fs.readFileSync(data.file).toString());
} else {
clipboard.writeText(data.text);
}
setTimeout(() => {
robot.keyTap('v', 'command');
}, 100)
});
ipcMain.on('delete', (event, data) => db.remove(data));
function show() {
const currentDisplay = electron.screen.getDisplayNearestPoint(electron.screen.getCursorScreenPoint());
const x = currentDisplay.bounds.x + currentDisplay.bounds.width / 2 - 150;
const y = currentDisplay.bounds.y + currentDisplay.bounds.height / 2 - 200;
mainWindow.setBounds({
width: 300,
height: 400,
x: x,
y: y
});
mainWindow.show();
mainWindow.webContents.send('page-show');
}
function hide() {
Menu.sendActionToFirstResponder('hide:');
mainWindow.hide();
}
function clearHistory() {
db.clear();
db.findAll().then(rows => mainWindow.webContents.send('data', rows));
}
function whatchForClipboard() {
new ClipboardWatcher().watchForClipboard((item) => {
db.insertEntry(item).then((rows) => mainWindow.webContents.send('data', rows));
});
}