2022-07-04 14:39:22 +00:00
import * as fs from "fs" ;
2023-06-10 20:54:46 +00:00
import { Menu , Tray , app , dialog , nativeImage } from "electron" ;
2022-11-22 14:34:24 +00:00
import { createInviteWindow , mainWindow } from "./window" ;
2023-06-10 20:54:46 +00:00
import { getConfig , getConfigLocation , getDisplayVersion , setConfig , setWindowState } from "./utils" ;
2022-03-04 17:53:18 +00:00
import * as path from "path" ;
2022-08-22 09:24:55 +00:00
import { createSettingsWindow } from "./settings/main" ;
2022-08-25 12:57:28 +00:00
export let tray : any = null ;
2023-06-10 18:06:00 +00:00
let trayIcon = "ac_plug_colored" ;
2022-04-20 19:50:23 +00:00
app . whenReady ( ) . then ( async ( ) = > {
2022-08-22 09:24:55 +00:00
let finishedSetup = await getConfig ( "doneSetup" ) ;
2023-06-10 18:06:00 +00:00
if ( ( await getConfig ( "trayIcon" ) ) != "default" ) {
trayIcon = await getConfig ( "trayIcon" ) ;
}
2022-07-11 17:13:52 +00:00
let trayPath = nativeImage . createFromPath ( path . join ( __dirname , "../" , ` /assets/ ${ trayIcon } .png ` ) ) ;
2022-09-25 18:30:09 +00:00
let trayVerIcon ;
trayVerIcon = function ( ) {
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 } ) ;
2023-06-10 20:54:46 +00:00
if ( await getConfig ( "tray" ) ) {
let clientName = ( await getConfig ( "clientName" ) ) ? ? "ArmCord" ;
if ( ( await getConfig ( "windowStyle" ) ) == "basic" ) {
tray = new Tray ( trayPath ) ;
function contextMenu ( ) : Electron . Menu {
if ( finishedSetup == false ) {
return Menu . buildFromTemplate ( [
{
label : ` Finish the setup first! ` ,
enabled : false
} ,
{
label : ` Quit ${ clientName } ` ,
async click() {
fs . unlink ( await getConfigLocation ( ) , ( err ) = > {
if ( err ) throw err ;
2022-10-08 15:43:08 +00:00
2023-06-10 20:54:46 +00:00
console . log ( 'Closed during setup. "settings.json" was deleted' ) ;
app . quit ( ) ;
} ) ;
}
}
] ) ;
} else {
return Menu . buildFromTemplate ( [
{
label : ` Open ${ clientName } ` ,
click() {
mainWindow . show ( ) ;
}
} ,
{
label : ` Quit ${ clientName } ` ,
click() {
let [ width , height ] = mainWindow . getSize ( ) ;
setWindowState ( {
width ,
height ,
isMaximized : mainWindow.isMaximized ( ) ,
x : mainWindow.getPosition ( ) [ 0 ] ,
y : mainWindow.getPosition ( ) [ 1 ]
} ) ;
app . quit ( ) ;
}
}
] ) ;
}
}
tray . setToolTip ( clientName ) ;
tray . setContextMenu ( contextMenu ) ;
} else {
tray = new Tray ( trayPath ) ;
2022-07-04 14:39:22 +00:00
if ( finishedSetup == false ) {
2023-06-10 20:54:46 +00:00
const contextMenu = Menu . buildFromTemplate ( [
2022-07-04 14:39:22 +00:00
{
label : ` Finish the setup first! ` ,
enabled : false
2022-10-08 15:43:08 +00:00
} ,
{
2022-07-04 14:39:22 +00:00
label : ` Quit ${ clientName } ` ,
2023-05-08 19:24:30 +00:00
async click() {
2022-07-04 14:39:22 +00:00
fs . unlink ( await getConfigLocation ( ) , ( err ) = > {
2022-10-08 15:43:08 +00:00
if ( err ) throw err ;
2022-07-04 14:39:22 +00:00
console . log ( 'Closed during setup. "settings.json" was deleted' ) ;
app . quit ( ) ;
2022-08-22 09:24:55 +00:00
} ) ;
2022-07-04 14:39:22 +00:00
}
}
] ) ;
2023-06-10 20:54:46 +00:00
tray . setContextMenu ( contextMenu ) ;
2022-07-04 14:39:22 +00:00
} else {
2023-06-10 20:54:46 +00:00
const contextMenu = Menu . buildFromTemplate ( [
{
label : ` ${ clientName } ${ getDisplayVersion ( ) } ` ,
icon : trayVerIcon ( ) ,
enabled : false
} ,
{
type : "separator"
} ,
2022-07-04 14:39:22 +00:00
{
label : ` Open ${ clientName } ` ,
2023-05-08 19:24:30 +00:00
click() {
2022-07-04 14:39:22 +00:00
mainWindow . show ( ) ;
}
2022-10-08 15:43:08 +00:00
} ,
2023-06-10 20:54:46 +00:00
{
label : "Open Settings" ,
click() {
createSettingsWindow ( ) ;
}
} ,
{
label : "Support Discord Server" ,
click() {
createInviteWindow ( "TnhxcqynZ2" ) ;
}
} ,
{
type : "separator"
} ,
2022-10-08 15:43:08 +00:00
{
2022-07-04 14:39:22 +00:00
label : ` Quit ${ clientName } ` ,
2023-05-08 19:24:30 +00:00
click() {
2022-07-04 14:39:22 +00:00
app . quit ( ) ;
}
}
] ) ;
2023-06-10 20:54:46 +00:00
tray . setContextMenu ( contextMenu ) ;
2022-03-04 17:53:18 +00:00
}
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 ( ) {
mainWindow . show ( ) ;
} ) ;
2022-04-20 19:50:23 +00:00
} else {
2023-06-10 20:54:46 +00:00
if ( ( await getConfig ( "tray" ) ) == undefined ) {
if ( process . platform == "win32" ) {
const options = {
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
2023-06-10 20:54:46 +00:00
dialog . showMessageBox ( mainWindow , options ) . then ( ( { response } ) = > {
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
} ) ;