-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
122 lines (104 loc) · 2.92 KB
/
main.js
File metadata and controls
122 lines (104 loc) · 2.92 KB
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
const { app, BrowserWindow, screen, ipcMain, Tray, Menu, nativeImage } = require('electron');
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
let win = null;
let tray = null;
const BAR_H = 1;
const DATA_PATH = path.join(app.getPath('userData'), 'notes-data.json');
function getStartupEnabled() {
return app.getLoginItemSettings().openAtLogin;
}
function setStartup(enable) {
app.setLoginItemSettings({
openAtLogin: enable,
path: process.execPath,
args: []
});
}
function loadData() {
try {
if (fs.existsSync(DATA_PATH)) {
return JSON.parse(fs.readFileSync(DATA_PATH, 'utf8'));
}
} catch (e) {}
return { cols: { g: [], b: [], r: [] } };
}
function saveData(data) {
try {
fs.writeFileSync(DATA_PATH, JSON.stringify(data, null, 2), 'utf8');
} catch (e) {}
}
function createWindow() {
const { width } = screen.getPrimaryDisplay().workAreaSize;
win = new BrowserWindow({
x: 0,
y: 0,
width: width,
height: BAR_H,
frame: false,
transparent: true,
alwaysOnTop: true,
skipTaskbar: true,
resizable: false,
movable: false,
focusable: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
win.setAlwaysOnTop(true, 'screen-saver');
win.loadFile('renderer.html');
win.on('blur', () => {
win.webContents.send('window-blur');
});
}
function createTray() {
const img = nativeImage.createEmpty();
tray = new Tray(img);
tray.setToolTip('TopBar Notes');
const updateMenu = () => {
const startupOn = getStartupEnabled();
const menu = Menu.buildFromTemplate([
{ label: 'TopBar Notes', enabled: false },
{ type: 'separator' },
{
label: 'Launch at Windows startup',
type: 'checkbox',
checked: startupOn,
click: () => {
setStartup(!startupOn);
updateMenu();
}
},
{ type: 'separator' },
{ label: 'Quit', click: () => app.quit() }
]);
tray.setContextMenu(menu);
};
updateMenu();
tray.on('click', () => {
if (win) win.show();
});
}
app.whenReady().then(() => {
createWindow();
createTray();
ipcMain.handle('expand-window', () => {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
const expandH = Math.min(480, Math.floor(height * 0.6));
win.setBounds({ x: 0, y: 0, width, height: expandH }, false);
win.setAlwaysOnTop(true, 'screen-saver');
});
ipcMain.handle('collapse-window', () => {
const { width } = screen.getPrimaryDisplay().workAreaSize;
win.setBounds({ x: 0, y: 0, width, height: BAR_H }, false);
win.setAlwaysOnTop(true, 'screen-saver');
});
ipcMain.handle('quit-app', () => app.quit());
ipcMain.handle('load-data', () => loadData());
ipcMain.handle('save-data', (_, data) => saveData(data));
});
app.on('window-all-closed', () => {});