-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
168 lines (142 loc) · 4.97 KB
/
main.js
File metadata and controls
168 lines (142 loc) · 4.97 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
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
const { app, BrowserWindow, session, Notification, ipcMain, Tray, Menu, shell, desktopCapturer } = require('electron');
const path = require('path');
let mainWindow;
let tray = null;
app.isQuitting = false;
const gotTheLock = app.requestSingleInstanceLock();
const getIconPath = () => {
return app.isPackaged
? path.join(process.resourcesPath, 'favicon.png')
: path.join(__dirname, 'favicon.png');
};
const getTrayIconPath = () => {
return app.isPackaged
? path.join(process.resourcesPath, 'tray-icon.png')
: path.join(__dirname, 'tray-icon.png');
};
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.show();
mainWindow.focus();
}
});
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
title: "Sable Client",
icon: path.join(__dirname, 'favicon.png'),
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
mainWindow.removeMenu();
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.executeJavaScript('localStorage.setItem("notificationsEnabled", "true");');
});
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
if (!url.includes('sable.moe')) {
shell.openExternal(url);
return { action: 'deny' };
}
return { action: 'allow' };
});
mainWindow.webContents.on('will-navigate', (event, url) => {
if (!url.includes('sable.moe')) {
event.preventDefault();
shell.openExternal(url);
}
});
session.defaultSession.setPermissionCheckHandler((webContents, permission) => {
return (permission === 'media' || permission === 'notifications' || permission === 'display-capture');
});
session.defaultSession.setPermissionRequestHandler((webContents, permission, callback) => {
const url = webContents.getURL();
const isSable = url.includes('sable.moe');
const isAllowedPermission = (permission === 'media' || permission === 'notifications' || permission === 'display-capture');
callback(isSable && isAllowedPermission);
});
// TODO: window picker
session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
if (sources && sources.length > 0) {
callback({ video: sources[0], audio: 'loopback' });
}
}).catch(err => {
console.error('Error getting desktop sources:', err);
});
});
mainWindow.loadURL('https://app.sable.moe');
mainWindow.on('close', (event) => {
if (!app.isQuitting) {
event.preventDefault();
mainWindow.hide();
}
return false;
});
}
function createTray() {
const iconPath = getTrayIconPath();
try {
tray = new Tray(iconPath);
const contextMenu = Menu.buildFromTemplate([
{
label: 'Open Sable Client', click: () => {
mainWindow.show();
mainWindow.focus();
}
},
{ type: 'separator' },
{
label: 'Quit', click: () => {
app.isQuitting = true;
app.quit();
}
}
]);
tray.setToolTip('Sable Client');
tray.setContextMenu(contextMenu);
tray.on('click', () => {
mainWindow.show();
mainWindow.focus();
});
} catch (error) {
console.error("FAILED to create tray:", error);
}
}
ipcMain.on('notify', (event, { title, body }) => {
const toast = new Notification({
title: title,
body: body,
icon: getIconPath,
silent: false
});
toast.on('click', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.show();
mainWindow.focus();
}
});
toast.show();
});
app.whenReady().then(() => {
createWindow();
createTray();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', () => {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
});
}