-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (121 loc) · 3.38 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
141
142
143
144
145
146
147
148
149
150
const electron = require('electron');
const path = require('path');
const fs = require('fs');
const Store = require('electron-store');
const store = new Store();
// const debug = require('electron-debug');
// debug();
let tray;
let window;
const {
app,
BrowserWindow,
ipcMain,
Tray,
shell,
} = electron;
// Don't show the app in the doc
app.dock.hide();
const getWindowPosition = () => {
const windowBounds = window.getBounds();
const trayBounds = tray.getBounds();
// Center window horizontally below the tray icon
const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2));
// Position window 4 pixels vertically below the tray icon
const y = Math.round(trayBounds.y + trayBounds.height + 4);
return { x, y };
};
const showWindow = () => {
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.setVisibleOnAllWorkspaces(true); // put the window on all screens
window.focus(); // focus the window up front on the active screen
window.setVisibleOnAllWorkspaces(false); // disable all screen behavior
window.show();
};
const toggleWindow = () => {
if (window.isVisible()) {
window.hide();
} else {
showWindow();
}
};
const createWindow = () => {
window = new BrowserWindow({
width: 400,
height: 600,
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: true,
},
name: 'Pippets',
vibrancy: 'dark',
acceptFirstMouse: true,
backgroundColor: '#AA000000',
});
window.loadURL(`file://${path.join(__dirname, 'public/index.html')}`);
// Hide the window when it loses focus
window.on('blur', () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide();
}
});
window.webContents.on('did-finish-load', () => {
window.webContents.send('receive-store', store.store);
});
ipcMain.on('drag-file', (_, file) => {
window.webContents.startDrag({
file,
icon: path.join(__dirname, './src/assets/img.png'),
});
});
};
const addFiles = async (files) => {
const images = await Promise.all(files.map(async (file) => ({
path: file,
ext: path.extname(file).substr(1),
directory: fs.lstatSync(file).isDirectory(),
image: await (await app.getFileIcon(file, { size: 'normal' })).toDataURL(),
})));
store.set('files', images);
tray.setTitle(`${images.length}`);
tray.setToolTip('Click to open palette');
window.webContents.send('receive-store', store.store);
};
const createTray = () => {
tray = new Tray(path.join(__dirname, './src/assets/sunTemplate.png'));
tray.setToolTip('Drag files to hold');
tray.setTitle(`${(store.get('files') || []).length || ''}`);
tray.on('drop-files', async (_, files) => {
await addFiles(files);
showWindow();
});
tray.on('right-click', () => {
tray.setTitle('');
store.set('files', []);
tray.setToolTip('Drag files into icon to hold');
window.webContents.send('receive-store', store.store);
window.hide();
});
tray.on('click', () => {
toggleWindow();
});
};
app.on('ready', () => {
createTray();
createWindow();
});
app.on('open-file', (_, file) => {
addFiles([file]);
});
ipcMain.on('show-window', () => {
showWindow();
});
ipcMain.on('open-file', (_, file) => {
shell.openItem(file);
});