main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Load settings before everything
  2. let appConfig;
  3. const fs = require("fs-extra");
  4. const path = require('path');
  5. const {app, BrowserWindow, ipcMain, Menu, Tray, Notification} = require('electron');
  6. const os = require('os');
  7. loadSettings();
  8. const theApp = require('./app');
  9. const WindowStateManager = require('electron-window-state-manager');
  10. const url = require('url');
  11. let tray = null;
  12. let mainWindow;
  13. let trayIcon = __dirname + "/icon.png";
  14. let isTray = false;
  15. // Create a new instance of the WindowStateManager
  16. const mainWindowState = new WindowStateManager('mainWindow', {
  17. defaultWidth: 1280,
  18. defaultHeight: 800
  19. });
  20. var shouldQuit = !app.requestSingleInstanceLock()
  21. app.on('second-instance', function (argv, cwd) {
  22. if (mainWindow) {
  23. if (mainWindow.isMinimized()) mainWindow.restore();
  24. if (!mainWindow.isVisible()) mainWindow.show();
  25. mainWindow.focus();
  26. }
  27. })
  28. if (shouldQuit) {
  29. app.quit();
  30. return;
  31. }
  32. app.isQuiting = false;
  33. app.serverMode = (process.argv.indexOf("-s")>-1 || process.argv.indexOf("--server")>-1);
  34. require('electron-context-menu')({
  35. showInspectElement: false
  36. });
  37. function loadSettings(){
  38. var userdata = "";
  39. if(process.platform == "android"){
  40. userdata = os.homedir() + "/storage/shared/Deezloader Remix/";
  41. }else{
  42. userdata = app.getPath("appData")+path.sep+"Deezloader Remix"+path.sep;
  43. }
  44. if(!fs.existsSync(userdata+"config.json")){
  45. fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
  46. }
  47. appConfig = require(userdata+path.sep+"config.json");
  48. if( typeof appConfig.userDefined.numplaylistbyalbum != "boolean" ||
  49. typeof appConfig.userDefined.syncedlyrics != "boolean" ||
  50. typeof appConfig.userDefined.padtrck != "boolean" ||
  51. typeof appConfig.userDefined.albumNameTemplate != "string"
  52. ){
  53. fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
  54. appConfig = require(userdata+path.sep+"config.json");
  55. }
  56. }
  57. function createTray(){
  58. tray = new Tray(trayIcon);
  59. const contextMenu = Menu.buildFromTemplate([
  60. {
  61. label: 'Show App',
  62. click: function() {
  63. if (mainWindow) mainWindow.show();
  64. }
  65. },
  66. {
  67. label: 'Quit',
  68. click: function() {
  69. app.isQuiting = true;
  70. if (mainWindow){mainWindow.close()}else{app.quit()};
  71. }
  72. }
  73. ]);
  74. tray.setToolTip('Deezloader Remix');
  75. tray.setContextMenu(contextMenu);
  76. tray.on('click', function(e){
  77. if (mainWindow){
  78. if (mainWindow.isVisible()) {
  79. mainWindow.hide()
  80. } else {
  81. mainWindow.show()
  82. }
  83. }
  84. });
  85. }
  86. function createWindow () {
  87. // Create the browser window.
  88. mainWindow = new BrowserWindow({
  89. width: mainWindowState.width,
  90. height: mainWindowState.height,
  91. x: mainWindowState.x,
  92. y: mainWindowState.y,
  93. alwaysOnTop: false,
  94. frame: false,
  95. icon: __dirname + "/icon.png",
  96. minWidth: 415,
  97. minHeight: 32,
  98. backgroundColor: "#23232c"
  99. });
  100. mainWindow.setMenu(null);
  101. // and load the index.html of the app.
  102. mainWindow.loadURL('http://localhost:' + appConfig.serverPort);
  103. mainWindow.on('closed', function () {
  104. mainWindow = null;
  105. });
  106. // Check if window was closed maximized and restore it
  107. if (mainWindowState.maximized) {
  108. mainWindow.maximize();
  109. }
  110. // Save current window state
  111. mainWindow.on('close', (event) => {
  112. if(appConfig.userDefined.minimizeToTray && !app.isQuiting){
  113. event.preventDefault()
  114. mainWindow.hide();
  115. } else {
  116. mainWindowState.saveState(mainWindow);
  117. }
  118. });
  119. }
  120. app.on('ready', function(){
  121. if (!app.serverMode){
  122. createWindow();
  123. createTray();
  124. }
  125. });
  126. // Quit when all windows are closed.
  127. app.on('window-all-closed', function () {
  128. if (!appConfig.userDefined.minimizeToTray || app.isQuiting){
  129. app.quit();
  130. }
  131. });
  132. app.on('activate', function () {
  133. if (!app.serverMode && mainWindow === null) {
  134. createWindow();
  135. }
  136. });