forked from ShivamJoker/Keymote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (158 loc) · 4.19 KB
/
index.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
const {
app,
BrowserWindow,
ipcMain,
Tray,
win,
Menu,
remote,
nativeImage
} = require("electron");
const path = require("path");
const robot = require("robotjs");
// const remote = require("electron").remote;
let isWindows = false;
let tray = undefined;
let window = undefined;
let isQuiting = undefined;
let browserWindow = undefined;
if (process.platform === "win32") {
isWindows = true;
}
app.on("before-quit", function () {
isQuiting = true;
});
app.on("ready", () => {
createTray();
createWindow();
});
global.status = { isRemoteConnected: false };
const createTray = () => {
//if its windows we will use cwd and show an icon
const iconPath = path.join(__dirname, "build/icons/iconTemplate.png")
if (isWindows) {
tray = new Tray(nativeImage.createFromPath(iconPath));
} else {
tray = new Tray(path.join(__dirname, "build/icons/iconTemplate.png"));
}
// Don't show the app in the mac doc
if (app.dock) app.dock.hide();
tray.on("click", event => {
console.log(remote);
console.log("status: ", global.status.isRemoteConnected);
// const isRemoteConnected = global.status.isRemoteConnected;
// if (!isRemoteConnected) {
// showWindow(remote.getGlobal("status"));
// // return 0;
// }
if (isWindows) {
tray.on("click", tray.popUpContextMenu);
}
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: "Show App",
click: function () {
showWindow();
}
},
{
label: "Quit",
click: function () {
isQuiting = true;
app.quit();
}
}
])
);
});
tray.setToolTip("Keymote");
};
const getWindowPosition = () => {
const windowBounds = window.getBounds();
const trayBounds = tray.getBounds();
//different position on mac and windows
if (isWindows) {
// Center window horizontally abpve the tray icon
const x = Math.round(
trayBounds.x + trayBounds.width / 2 - windowBounds.width / 2
);
// Position window 4 pixels vertically above the tray icon
const y = Math.round(trayBounds.y + trayBounds.height - 475);
return { x: x, y: y };
} else {
// 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: x, y: y };
}
};
const createWindow = () => {
window = new BrowserWindow({
width: 320,
height: 430,
// width: 1000,
// height: 750,
skipTaskbar: true,
show: true,
frame: false,
fullscreenable: true,
resizable: true,
transparent: true,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: true
}
});
// browserWindow = new BrowserWindow({
// show: false,
// webPreferences: {
// backgroundThrottling: false,
// nodeIntegration: true
// }
// });
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.on("close", function (event) {
if (!isQuiting) {
event.preventDefault();
window.hide();
event.returnValue = false;
}
});
window.loadURL(
`file://${path.join(
__dirname,
"/app/index.html"
)}`
);
// browserWindow.loadURL(`file://${path.join(
// isWindows ? process.cwd() : __dirname,
// "/app/notification.html"
// )}`);
// Hide the window when it loses focus
window.on("blur", () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide();
}
});
};
const toggleWindow = () => {
window.isVisible() ? window.hide() : showWindow();
};
const showWindow = () => {
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.show();
// window.webContents.send(
// 'show-notification',
// 'Keymote',
// 'App is running...'
// );
};
ipcMain.on("show-window", () => {
showWindow();
});