2024-06-14 12:57:34 +00:00
import fs from "fs" ;
2023-06-10 21:04:27 +00:00
import { Menu , MessageBoxOptions , Tray , app , dialog , nativeImage } from "electron" ;
2024-06-15 08:20:44 +00:00
import { createInviteWindow , mainWindows } from "./discord/window.js" ;
2024-06-14 12:57:34 +00:00
import path from "path" ;
2024-06-01 09:56:19 +00:00
import { createSettingsWindow } from "./settings/main.js" ;
import { getConfig , getConfigLocation , setConfig } from "./common/config.js" ;
import { getDisplayVersion } from "./common/version.js" ;
2024-06-14 12:57:34 +00:00
export let tray : Tray ;
2024-06-15 09:41:35 +00:00
export let isQuitting = false ;
2023-06-10 18:06:00 +00:00
let trayIcon = "ac_plug_colored" ;
2024-06-14 12:57:34 +00:00
void app . whenReady ( ) . then ( async ( ) = > {
// REVIEW - app will hang at startup if line above is awaited.
const finishedSetup = getConfig ( "doneSetup" ) ;
if ( getConfig ( "trayIcon" ) != "default" ) {
trayIcon = getConfig ( "trayIcon" ) ;
2023-06-10 18:06:00 +00:00
}
2024-05-15 18:14:18 +00:00
let trayPath = nativeImage . createFromPath ( path . join ( import . meta . dirname , "../" , ` /assets/ ${ trayIcon } .png ` ) ) ;
2024-06-14 12:57:34 +00:00
const trayVerIcon = function ( ) {
2022-09-25 18:30:09 +00:00
if ( process . platform == "win32" ) {
return trayPath . resize ( { height : 16 } ) ;
} else if ( process . platform == "darwin" ) {
return trayPath . resize ( { height : 18 } ) ;
} else if ( process . platform == "linux" ) {
return trayPath . resize ( { height : 24 } ) ;
}
2023-05-08 19:24:30 +00:00
return undefined ;
2022-09-25 18:30:09 +00:00
} ;
2022-10-08 15:43:08 +00:00
if ( process . platform == "darwin" && trayPath . getSize ( ) . height > 22 ) trayPath = trayPath . resize ( { height : 22 } ) ;
2024-06-14 12:57:34 +00:00
if ( getConfig ( "tray" ) ) {
const clientName = getConfig ( "clientName" ) ? ? "ArmCord" ;
2023-06-11 15:18:00 +00:00
tray = new Tray ( trayPath ) ;
if ( finishedSetup == false ) {
const contextMenu = Menu . buildFromTemplate ( [
{
label : ` Finish the setup first! ` ,
enabled : false
} ,
{
label : ` Quit ${ clientName } ` ,
2024-06-14 12:57:34 +00:00
click() {
fs . unlink ( getConfigLocation ( ) , ( err ) = > {
2023-06-11 15:18:00 +00:00
if ( err ) throw err ;
2022-10-08 15:43:08 +00:00
2023-06-11 15:18:00 +00:00
console . log ( 'Closed during setup. "settings.json" was deleted' ) ;
app . quit ( ) ;
} ) ;
}
2023-06-10 20:54:46 +00:00
}
2023-06-11 15:18:00 +00:00
] ) ;
2023-06-10 20:54:46 +00:00
tray . setContextMenu ( contextMenu ) ;
} else {
2023-06-11 15:18:00 +00:00
const contextMenu = Menu . buildFromTemplate ( [
2024-06-14 12:57:34 +00:00
// REVIEW - Awaiting any window creation will fail silently
2023-06-11 15:18:00 +00:00
{
label : ` ${ clientName } ${ getDisplayVersion ( ) } ` ,
icon : trayVerIcon ( ) ,
enabled : false
} ,
{
type : "separator"
} ,
{
label : ` Open ${ clientName } ` ,
click() {
2024-06-15 08:20:44 +00:00
mainWindows . forEach ( ( mainWindow ) = > {
mainWindow . show ( ) ;
} ) ;
2022-07-04 14:39:22 +00:00
}
2023-06-11 15:18:00 +00:00
} ,
{
label : "Open Settings" ,
click() {
2024-06-14 12:57:34 +00:00
void createSettingsWindow ( ) ;
2022-07-04 14:39:22 +00:00
}
2023-06-11 15:18:00 +00:00
} ,
{
label : "Support Discord Server" ,
click() {
2024-06-14 12:57:34 +00:00
void createInviteWindow ( "TnhxcqynZ2" ) ;
2023-06-11 15:18:00 +00:00
}
} ,
{
type : "separator"
} ,
{
label : ` Quit ${ clientName } ` ,
click() {
2024-06-15 09:41:35 +00:00
//NOTE - It would be better to unify isQuitting with forceQuit in window.ts that's used on macOS
isQuitting = true ;
app . quit ( ) ;
2023-06-11 15:18:00 +00:00
}
}
] ) ;
tray . setContextMenu ( contextMenu ) ;
2023-05-08 19:24:30 +00:00
}
2022-07-04 14:39:22 +00:00
tray . setToolTip ( clientName ) ;
2023-06-10 20:54:46 +00:00
tray . on ( "click" , function ( ) {
2024-06-15 08:20:44 +00:00
mainWindows . forEach ( ( mainWindow ) = > {
mainWindow . show ( ) ;
} ) ;
2023-06-10 20:54:46 +00:00
} ) ;
2022-04-20 19:50:23 +00:00
} else {
2024-06-14 12:57:34 +00:00
if ( getConfig ( "tray" ) == undefined ) {
2023-06-10 21:05:39 +00:00
if ( process . platform == "linux" ) {
2023-06-10 21:04:27 +00:00
const options : MessageBoxOptions = {
2023-06-10 20:54:46 +00:00
type : "question" ,
buttons : [ "Yes, please" , "No, I don't" ] ,
defaultId : 1 ,
title : "Tray icon choice" ,
message : ` Do you want to use tray icons? ` ,
detail : "Linux may not work well with tray icons. Depending on your system configuration, you may not be able to see the tray icon. Enable at your own risk. Can be changed later."
} ;
2022-10-08 15:43:08 +00:00
2024-06-15 08:20:44 +00:00
await dialog . showMessageBox ( mainWindows [ 0 ] , options ) . then ( ( { response } ) = > {
2023-06-10 20:54:46 +00:00
if ( response == 0 ) {
setConfig ( "tray" , true ) ;
} else {
setConfig ( "tray" , false ) ;
2022-08-22 09:24:55 +00:00
}
2023-06-10 20:54:46 +00:00
app . relaunch ( ) ;
app . exit ( ) ;
} ) ;
} else {
setConfig ( "tray" , true ) ;
app . relaunch ( ) ;
app . exit ( ) ;
}
2022-07-04 14:39:22 +00:00
}
2022-10-08 15:43:08 +00:00
}
2022-08-22 09:24:55 +00:00
} ) ;