Fixed right click not working
[DeezloaderRemix.git] / app / main.js
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
9 const captcha = require('./public/js/captcha');
10 captcha.registerScheme();
11
12 const theApp = require('./app');
13 const WindowStateManager = require('electron-window-state-manager');
14 const url = require('url');
15
16 let tray = null;
17 let mainWindow;
18 let trayIcon = __dirname + "/icon.png";
19 let isTray = false;
20 // Create a new instance of the WindowStateManager
21 const mainWindowState = new WindowStateManager('mainWindow', {
22         defaultWidth: 1280,
23         defaultHeight: 800
24 });
25
26 var shouldQuit = !app.requestSingleInstanceLock()
27 app.on('second-instance', function (argv, cwd) {
28         if (mainWindow) {
29     if (mainWindow.isMinimized()) mainWindow.restore();
30                 if (!mainWindow.isVisible()) mainWindow.show();
31     mainWindow.focus();
32   }
33 })
34
35 if (shouldQuit) {
36   app.quit();
37   return;
38 }
39
40 app.isQuiting = false;
41
42 app.serverMode = (process.argv.indexOf("-s")>-1 || process.argv.indexOf("--server")>-1);
43
44 require('electron-context-menu')()
45
46 function loadSettings(){
47         var userdata = "";
48         if(process.platform == "android"){
49                 userdata = os.homedir() + "/storage/shared/Deezloader Remix/";
50         }else{
51                 userdata = app.getPath("appData")+path.sep+"Deezloader Remix"+path.sep;
52         }
53
54         if(!fs.existsSync(userdata+"config.json")){
55                 fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
56         }
57
58         appConfig = require(userdata+path.sep+"config.json");
59
60         if( typeof appConfig.userDefined.numplaylistbyalbum != "boolean" ||
61                         typeof appConfig.userDefined.syncedlyrics != "boolean" ||
62                         typeof appConfig.userDefined.padtrck != "boolean" ||
63                         typeof appConfig.userDefined.albumNameTemplate != "string"
64                 ){
65                 fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
66                 appConfig = require(userdata+path.sep+"config.json");
67         }
68 }
69
70 function createTray(){
71         tray = new Tray(trayIcon);
72         const contextMenu = Menu.buildFromTemplate([
73                 {
74                         label: 'Show App',
75                         click: function() {
76                                 if (mainWindow) mainWindow.show();
77                         }
78                 },
79                 {
80                         label: 'Quit',
81                         click: function() {
82                                 app.isQuiting = true;
83                                 if (mainWindow){mainWindow.close()}else{app.quit()};
84                         }
85                 }
86         ]);
87         tray.setToolTip('Deezloader Remix');
88         tray.setContextMenu(contextMenu);
89
90         tray.on('click', function(e){
91                 if (mainWindow){
92                         if (mainWindow.isVisible()) {
93                                 mainWindow.hide()
94                         } else {
95                                 mainWindow.show()
96                         }
97                 }
98         });
99 }
100
101 function createWindow () {
102         // Create the browser window.
103         mainWindow = new BrowserWindow({
104                 width: mainWindowState.width,
105                 height: mainWindowState.height,
106                 x: mainWindowState.x,
107                 y: mainWindowState.y,
108                 alwaysOnTop: false,
109                 frame: false,
110                 icon: __dirname + "/icon.png",
111                 minWidth: 415,
112                 minHeight: 32,
113                 backgroundColor: "#23232c"
114         });
115
116         mainWindow.setMenu(null);
117         var template = [{
118             label: "Application",
119             submenu: [
120                 { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }}
121             ]}, {
122             label: "Edit",
123             submenu: [
124                 { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
125                 { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
126                 { type: "separator" },
127                 { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
128                 { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
129                 { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
130                 { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
131             ]}
132         ]
133
134         Menu.setApplicationMenu(Menu.buildFromTemplate(template))
135
136         // and load the index.html of the app.
137         mainWindow.loadURL('http://localhost:' + appConfig.serverPort);
138
139         mainWindow.on('closed', function () {
140                 mainWindow = null;
141         });
142
143         // Check if window was closed maximized and restore it
144         if (mainWindowState.maximized) {
145                 mainWindow.maximize();
146         }
147
148         // Save current window state
149         mainWindow.on('close', (event) => {
150                 if(appConfig.userDefined.minimizeToTray && !app.isQuiting){
151                         event.preventDefault()
152                         mainWindow.hide();
153                 } else {
154                         mainWindowState.saveState(mainWindow);
155                 }
156         });
157 }
158
159 app.on('ready', function(){
160         if (!app.serverMode){
161                 createWindow();
162                 createTray();
163                 captcha.registerProtocol();
164         }
165 });
166
167 // Quit when all windows are closed.
168 app.on('window-all-closed', function () {
169         if (!appConfig.userDefined.minimizeToTray || app.isQuiting){
170                 app.quit();
171         }
172 });
173
174 app.on('activate', function () {
175         if (!app.serverMode && mainWindow === null) {
176                 createWindow();
177         }
178 });