2023-02-13 13:57:11 -05:00
// ElectronJS
2023-01-24 22:38:54 -05:00
const { app , BrowserWindow , Menu } = require ( 'electron' )
2023-02-13 13:57:11 -05:00
// Dependenies by Korbs Studio
2023-02-01 14:08:59 -05:00
const glasstron = require ( 'glasstron-clarity' )
2023-02-13 13:57:11 -05:00
const TitlebarRespect = require ( 'electron-titlebar-respect' )
// Other dependenies
2023-02-11 15:57:28 -05:00
const isDev = require ( 'electron-is-dev' )
2023-02-13 13:57:11 -05:00
const log = require ( 'electron-log' )
const Pushy = require ( 'pushy-electron' )
// Needed for other stuff
2023-01-25 03:25:56 -05:00
const path = require ( 'path' )
2023-02-11 15:57:28 -05:00
let mainWindow
2023-01-25 03:25:56 -05:00
2023-02-13 13:57:11 -05:00
// Grab .env options
2023-02-12 12:35:24 -05:00
require ( 'dotenv' ) . config ( )
2023-02-13 13:57:11 -05:00
// Override the console.log function with Electron Log
log . transports . console . format = '\x1b[34m[electron]:\x1b[37m'
console . log = log . log
2023-02-12 12:35:24 -05:00
2023-01-25 03:25:56 -05:00
// Use the correct icon depending on the operating system
2023-01-28 06:35:45 -05:00
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' }
2023-01-23 03:55:41 -05:00
function createWindow ( ) {
2023-02-11 15:57:28 -05:00
mainWindow = new glasstron . BrowserWindow ( {
2023-02-13 13:57:11 -05:00
title : process . env . Name ,
2023-01-23 03:55:41 -05:00
minWidth : 400 ,
minHeight : 400 ,
width : 1200 ,
height : 800 ,
autoHideMenuBar : true ,
2023-01-24 22:38:54 -05:00
show : false ,
2023-01-23 03:55:41 -05:00
blur : true ,
frame : global . frame ,
titleBarStyle : global . titleBarStyle ,
2023-01-25 03:25:56 -05:00
icon : path . join ( global . AppIcon ) ,
2023-01-23 03:55:41 -05:00
trafficLightPosition : { x : 25 , y : 25 } , // Position of Traffic Light buttons on macOS
titleBarOverlay : { // Background and Height of Windows titlebar buttons
2023-01-28 06:35:45 -05:00
color : '#232323' , // Background
2023-01-23 03:55:41 -05:00
symbolColor : 'white' , // Icon
2023-01-28 06:35:45 -05:00
height : 44 ,
2023-01-23 03:55:41 -05:00
} ,
webPreferences : {
webviewTag : true
}
} )
2023-02-11 15:57:28 -05:00
2023-01-24 22:38:54 -05:00
// Showing the window gracefuly
// Doc: https://www.electronjs.org/docs/latest/api/browser-window#showing-the-window-gracefully
2023-02-11 15:57:28 -05:00
mainWindow . once ( 'ready-to-show' , ( ) => { mainWindow . show ( ) } )
// Pushy - Pushy Notification System (PNS)
mainWindow . webContents . on ( 'did-finish-load' , ( ) => { Pushy . listen ( ) } )
2023-02-12 12:35:24 -05:00
Pushy . register ( { appId : process . env . PushyAppId } ) . then ( ( deviceToken ) => { } ) . catch ( ( error ) => { console . log ( 'Pushy registration error: ' + error . message ) } )
2023-02-11 15:57:28 -05:00
setTimeout ( ( ) => {
Pushy . setNotificationListener ( ( data ) => {
mainWindow . webContents . executeJavaScript ( `
2023-02-12 12:35:24 -05:00
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>' )
2023-02-11 15:57:28 -05:00
` )
} )
2023-02-13 13:57:11 -05:00
} , 10000 )
2023-02-11 15:57:28 -05:00
if ( Pushy . isRegistered ( ) ) { Pushy . subscribe ( 'AppName' ) . then ( ( ) => { } ) . catch ( ( error ) => { console . error ( error ) } ) }
2023-01-24 22:38:54 -05:00
// Load Content
2023-01-23 03:55:41 -05:00
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
}
2023-02-11 15:57:28 -05:00
2023-01-24 22:38:54 -05:00
// 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' ,
2023-02-13 13:57:11 -05:00
click : async ( ) => { if ( isDev ) { mainWindow . webContents . executeJavaScript ( ` document.querySelector('.active webview').openDevTools() ` ) } else { console . log ( 'Not available in production mode.' ) } }
2023-01-24 22:38:54 -05:00
} ,
{ 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 : [
2023-02-13 13:57:11 -05:00
{
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 )
2023-01-24 22:38:54 -05:00
}
2023-02-13 13:57:11 -05:00
}
2023-01-24 22:38:54 -05:00
]
}
]
const menu = Menu . buildFromTemplate ( template )
Menu . setApplicationMenu ( menu )
2023-01-23 03:55:41 -05:00
}
2023-01-24 22:38:54 -05:00
// App
app . whenReady ( ) . then ( ( ) => {
createWindow ( )
// DNS
2023-02-13 13:57:11 -05:00
// Docs: https://www.electronjs.org/docs/latest/api/app#appconfigurehostresolveroptions
2023-01-24 22:38:54 -05:00
app . configureHostResolver ( {
secureDnsMode : 'secure' ,
secureDnsServers : [
'https://cloudflare-dns.com/dns-query'
]
} )
} )
// app.disableHardwareAcceleration()
app . on ( 'window-all-closed' , ( ) => {
app . quit ( )
} )