This repository has been archived on 2024-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
Nexus-Polestar/electron/index.js
2023-02-17 18:02:24 -05:00

234 lines
No EOL
7.6 KiB
JavaScript

// ElectronJS
const {app, BrowserWindow, Menu, nativeTheme, Notification, crashReporter} = require('electron')
// Dependenies by Korbs Studio
const glasstron = require('glasstron-clarity')
const TitlebarRespect = require('electron-titlebar-respect')
// Other dependenies
const isDev = require('electron-is-dev')
const log = require('electron-log')
const Pushy = require('pushy-electron')
// Needed for other stuff
const path = require('path')
const bugsplat = require("bugsplat");
let mainWindow
// Grab .env options
require('dotenv').config()
// Override the console.log function with Electron Log
log.transports.console.format = '\x1b[34m[electron]:\x1b[37m'
console.log = log.log
// Use the correct icon depending on the operating system
if /* If macOS */ (process.platform === 'darwin') {global.AppIcon = 'public/images/icons/app/macOS.icns' }
else if /* If Windows */ (process.platform === 'win32') {global.AppIcon = 'public/images/icons/app/Windows.png' }
else /* If Linux */ {global.AppIcon = 'public/images/icons/app/Linux.png' }
function createWindow () {
mainWindow = new glasstron.BrowserWindow({
title: process.env.Name,
minWidth: 400,
minHeight: 400,
width: 1200,
height: 800,
autoHideMenuBar: true,
show: false,
blur: true,
frame: global.frame,
titleBarStyle: global.titleBarStyle,
icon: path.join(global.AppIcon),
trafficLightPosition: { x: 25, y: 25 }, // Position of Traffic Light buttons on macOS
titleBarOverlay: { // Background and Height of Windows titlebar buttons
color: '#232323', // Background
symbolColor: 'white', // Icon
height: 44,
},
webPreferences: {
webviewTag: true
}
})
// Showing the window gracefuly
// Doc: https://www.electronjs.org/docs/latest/api/browser-window#showing-the-window-gracefully
mainWindow.once('ready-to-show', () => {mainWindow.show()})
// Set Platform Class
if /* If macOS */ (process.platform === 'darwin') { mainWindow.webContents.executeJavaScript(`document.querySelector('html').setAttribute('os', 'Mac')`) }
else if /* If Windows */ (process.platform === 'win32') { mainWindow.webContents.executeJavaScript(`document.querySelector('html').setAttribute('os', 'Windows')`) }
else /* If Linux */ { mainWindow.webContents.executeJavaScript(`document.querySelector('html').setAttribute('os', 'Linux')`) }
// Pushy - Pushy Notification System (PNS)
mainWindow.webContents.on('did-finish-load', () => {Pushy.listen()})
Pushy.register({ appId: process.env.PushyAppId }).then((deviceToken) => {}).catch((error) => {console.log('Pushy registration error: ' + error.message)})
setTimeout(() => {
Pushy.setNotificationListener((data) => {
new Notification({ title: data.title, body: data.message }).show()
if(process.env.NotificationType === 'Native') {
new Notification({ title: data.title, body: data.message }).show()
}
else if(process.env.NotificationType === 'Custom') {
mainWindow.webContents.executeJavaScript(`
document.querySelector("body > div > div.sidebar > div.sidebar-bottom").insertAdjacentHTML("afterBegin",'<li class="notification"><img class="notification-image" src="${data.image}"><i class="${data.icon}"></i><p class="notification-title">${data.title}</p><p class="notification-message">${data.message}</p><div class="notification-actions"><button id="primary" class="notification-action" onclick="shell.openExternal(${data.action})">Open</button><button id="secondary" class="notification-action">Dismiss</button></div></li>')
`)
}
})
}, 5000) // If this is triggered too soon, the notifications won't work.
if (Pushy.isRegistered()) {Pushy.subscribe('AppName').then(() => {}).catch((error) => {console.error(error)})}
// Load Content
if (isDev) {
mainWindow.loadURL('http://localhost:3000') // Use a URL in development mode
} else {
mainWindow.loadFile('app/index.html') // Do NOT use a URL in production mode, as that can create a security risk for your customers who will use this app
}
// Menu
const isMac = process.platform === 'darwin'
const template = [
...(isMac ? [{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}] : []),
{
label: 'File',
submenu: [
isMac ? { role: 'close' } : { role: 'quit' }
]
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
...(isMac ? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startSpeaking' },
{ role: 'stopSpeaking' }
]
}
] : [
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
])
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{
label: 'Open WebView Developer Tools',
accelerator: 'CmdOrCtrl+Shift+W',
click: async () => {if (isDev) {mainWindow.webContents.executeJavaScript(`document.querySelector('.active webview').openDevTools()`)}else {console.log('Not available in production mode.')}}
},
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac ? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }
])
]
},
{
role: 'help',
submenu: [
{
label: 'Website',
click: async () => {
const { shell } = require('electron')
await shell.openExternal(process.env.Website)
}
},
{
label: 'Support',
click: async () => {
const { shell } = require('electron')
await shell.openExternal(process.env.Support)
}
},
{
label: 'Source Code',
click: async () => {
const { shell } = require('electron')
await shell.openExternal(process.env.SourceCode)
}
}
]
}
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
// Crash Reporter
crashReporter.start({
productName: process.env.Name,
submitURL: process.env.CrashReportServer,
})
const javaScriptErrorHandler = async (error) => {
await bugsplat.post(error);
app.quit();
}
// If you want test the crash reporter, uncomment the code below and wait a few moments.
// setTimeout(() => {process.crash()}, 2400) // Wait a moment, then crash.z
// App
app.whenReady().then(() => {
createWindow()
// DNS
// Docs: https://www.electronjs.org/docs/latest/api/app#appconfigurehostresolveroptions
app.configureHostResolver({
secureDnsMode: 'secure',
secureDnsServers: [
'https://cloudflare-dns.com/dns-query'
]
})
})
// app.disableHardwareAcceleration()
app.on('window-all-closed', () => {
app.quit()
})