-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
110 lines (93 loc) · 2.42 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
const {
app,
BrowserWindow,
ipcMain,
Tray,
nativeImage,
nativeTheme
} = require('electron');
const path = require('path');
const axios = require('axios');
const apiUrl = "https://www.haberler.com/son-dakika/";
let tray = undefined;
let window = undefined;
app.dock.hide()
app.on('ready', () => {
createTray()
createWindow()
setInterval(retrieveNews, 2000);
})
app.setName("Bundle");
const createTray = () => {
tray = new Tray(getTrayIcon(false))
tray.on('click', function () {
toggleWindow()
});
}
const getWindowPosition = () => {
const windowBounds = window.getBounds();
const trayBounds = tray.getBounds();
const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2));
const y = Math.round(trayBounds.y + trayBounds.height + 4);
return {
x: x,
y: y
};
}
const createWindow = () => {
window = new BrowserWindow({
width: 400,
height: 450,
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: false,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: true,
contextIsolation: false,
}
})
window.loadURL(`file://${path.join(__dirname, 'index.html')}`)
window.on('blur', () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide()
}
})
}
const toggleWindow = () => {
if (window.isVisible()) {
window.hide();
} else {
showWindow();
app.setBadgeCount(0);
}
}
const showWindow = () => {
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.show();
//window.openDevTools();
}
const retrieveNews = () => {
axios.get(apiUrl).then(res => {
window.webContents.send('news-message', res.data);
})
}
ipcMain.on('show-window', () => {
showWindow()
})
ipcMain.on("quitApp-event", () => {
app.quit();
})
ipcMain.on("badge-count-event", () => {
app.setBadgeCount(app.getBadgeCount() === 0 ? 1 : app.getBadgeCount() + 1)
})
nativeTheme.on("updated", () => tray.setImage(getTrayIcon()));
function getTrayIcon(isDark = nativeTheme.shouldUseDarkColors) {
return nativeImage.createFromPath(`${path.join(__dirname, `/assets/icon/icon${isDark ? '_dark' : ''}.png`)}`).resize({
width: 16,
height: 16
});
}