Changed handlers and removed old listeners

Fixed on-close to work correctly on quit
This commit is contained in:
Core 2022-01-30 16:17:29 +00:00
parent 0ab0bd7b77
commit d80bfc8398
No known key found for this signature in database
GPG key ID: FE9BF1B547F8F3C6
2 changed files with 47 additions and 46 deletions

View file

@ -6,7 +6,7 @@ import * as express from "express";
import * as getPort from "get-port"; import * as getPort from "get-port";
import * as yt from "youtube-search-without-api-key"; import * as yt from "youtube-search-without-api-key";
import * as fs from "fs"; import * as fs from "fs";
import { Stream } from "stream"; import {Stream} from "stream";
import * as qrcode from "qrcode-terminal"; import * as qrcode from "qrcode-terminal";
import * as os from "os"; import * as os from "os";
import * as mm from 'music-metadata'; import * as mm from 'music-metadata';
@ -76,7 +76,7 @@ export class Win {
* Creates the browser window * Creates the browser window
*/ */
async createWindow(): Promise<Electron.BrowserWindow> { async createWindow(): Promise<Electron.BrowserWindow> {
this.clientPort = await getPort({ port: 9000 }); this.clientPort = await getPort({port: 9000});
this.verifyFiles(); this.verifyFiles();
// Load the previous state with fallback to defaults // Load the previous state with fallback to defaults
@ -262,7 +262,7 @@ export class Win {
if (itspod != null) if (itspod != null)
details.requestHeaders["Cookie"] = `itspod=${itspod}`; details.requestHeaders["Cookie"] = `itspod=${itspod}`;
} }
callback({ requestHeaders: details.requestHeaders }); callback({requestHeaders: details.requestHeaders});
} }
); );
@ -295,7 +295,7 @@ export class Win {
let i18n = fs.readFileSync(path.join(__dirname, `../../src/i18n/${key}.jsonc`), "utf8"); let i18n = fs.readFileSync(path.join(__dirname, `../../src/i18n/${key}.jsonc`), "utf8");
i18n = jsonc.parse(i18n) i18n = jsonc.parse(i18n)
Object.assign(i18nBase, i18n) Object.assign(i18nBase, i18n)
}catch(e) { } catch (e) {
console.error(e); console.error(e);
event.returnValue = e; event.returnValue = e;
} }
@ -330,11 +330,6 @@ export class Win {
event.returnValue = this.devMode; event.returnValue = this.devMode;
}); });
electron.ipcMain.on("close", () => {
// listen for close event
this.win.close();
});
electron.ipcMain.on("put-library-songs", (event, arg) => { electron.ipcMain.on("put-library-songs", (event, arg) => {
fs.writeFileSync( fs.writeFileSync(
path.join(this.paths.ciderCache, "library-songs.json"), path.join(this.paths.ciderCache, "library-songs.json"),
@ -415,8 +410,8 @@ export class Win {
return await yt.search(u); return await yt.search(u);
}); });
electron.ipcMain.handle("setVibrancy", (event, key, value) => { electron.ipcMain.on("close", () => {
this.win.setVibrancy(value); this.win.close();
}); });
electron.ipcMain.on("maximize", () => { electron.ipcMain.on("maximize", () => {
@ -443,7 +438,7 @@ export class Win {
}); });
electron.ipcMain.on("windowmin", (event, width, height) => { electron.ipcMain.on("windowmin", (event, width, height) => {
this.win.setMinimumSize(width,height); this.win.setMinimumSize(width, height);
}) })
electron.ipcMain.on("windowontop", (event, ontop) => { electron.ipcMain.on("windowontop", (event, ontop) => {
@ -462,7 +457,7 @@ export class Win {
}) })
//Fullscreen //Fullscreen
electron.ipcMain.on('detachDT', (event, _) => { electron.ipcMain.on('detachDT', (event, _) => {
this.win.webContents.openDevTools({ mode: 'detach' }); this.win.webContents.openDevTools({mode: 'detach'});
}) })
@ -497,7 +492,7 @@ export class Win {
} }
//QR Code //QR Code
electron.ipcMain.handle('showQR', async (event , _) => { electron.ipcMain.handle('showQR', async (event, _) => {
let url = `http://${getIp()}:${this.remotePort}`; let url = `http://${getIp()}:${this.remotePort}`;
electron.shell.openExternal(`https://cider.sh/pair-remote?url=${btoa(encodeURI(url))}`); electron.shell.openExternal(`https://cider.sh/pair-remote?url=${btoa(encodeURI(url))}`);
/* /*
@ -510,11 +505,11 @@ export class Win {
'get url' 'get url'
fetch(url) fetch(url)
.then(res => res.buffer()) .then(res => res.buffer())
.then(async(buffer) => { .then(async (buffer) => {
try { try {
const metadata = await mm.parseBuffer(buffer, 'audio/x-m4a'); const metadata = await mm.parseBuffer(buffer, 'audio/x-m4a');
let SoundCheckTag = metadata.native.iTunes[1].value let SoundCheckTag = metadata.native.iTunes[1].value
console.log('sc',SoundCheckTag) console.log('sc', SoundCheckTag)
this.win.webContents.send('SoundCheckTag', SoundCheckTag) this.win.webContents.send('SoundCheckTag', SoundCheckTag)
} catch (error) { } catch (error) {
console.error(error.message); console.error(error.message);
@ -555,6 +550,25 @@ export class Win {
}); });
} }
let isQuiting = false
this.win.on("close", (event: Event) => {
if ((this.store.general.close_button_hide || process.platform === "darwin" )&& !isQuiting) {
event.preventDefault();
this.win.hide();
} else {
this.win.destroy();
}
})
electron.app.on('before-quit', () => {
isQuiting = true
});
electron.app.on('window-all-closed', () => {
electron.app.quit()
})
this.win.on("closed", () => { this.win.on("closed", () => {
this.win = null; this.win = null;
}); });
@ -562,19 +576,20 @@ export class Win {
// Set window Handler // Set window Handler
this.win.webContents.setWindowOpenHandler((x: any) => { this.win.webContents.setWindowOpenHandler((x: any) => {
if (x.url.includes("apple") || x.url.includes("localhost")) { if (x.url.includes("apple") || x.url.includes("localhost")) {
return { action: "allow" }; return {action: "allow"};
} }
electron.shell.openExternal(x.url).catch(console.error); electron.shell.openExternal(x.url).catch(console.error);
return { action: "deny" }; return {action: "deny"};
}); });
} }
private async broadcastRemote() { private async broadcastRemote() {
function getIp() { function getIp() {
let ip :any = false; let ip: any = false;
let alias = 0; let alias = 0;
const ifaces: any = os.networkInterfaces() ; const ifaces: any = os.networkInterfaces();
for (var dev in ifaces) { for (var dev in ifaces) {
ifaces[dev].forEach( (details: any) => { ifaces[dev].forEach((details: any) => {
if (details.family === 'IPv4') { if (details.family === 'IPv4') {
if (!/(loopback|vmware|internal|hamachi|vboxnet|virtualbox)/gi.test(dev + (alias ? ':' + alias : ''))) { if (!/(loopback|vmware|internal|hamachi|vboxnet|virtualbox)/gi.test(dev + (alias ? ':' + alias : ''))) {
if (details.address.substring(0, 8) === '192.168.' || if (details.address.substring(0, 8) === '192.168.' ||
@ -586,10 +601,11 @@ export class Win {
} }
} }
} }
}) ; });
} }
return ip; return ip;
} }
const myString = `http://${getIp()}:${this.remotePort}`; const myString = `http://${getIp()}:${this.remotePort}`;
let mdns = require('mdns-js'); let mdns = require('mdns-js');
const encoded = new Buffer(myString).toString('base64'); const encoded = new Buffer(myString).toString('base64');
@ -604,7 +620,7 @@ export class Win {
"CtlN": "Cider", "CtlN": "Cider",
"iV": "196623" "iV": "196623"
} }
let server2 = mdns.createAdvertisement(x, `${await getPort({port: 3839})}`, { name: encoded, txt: txt_record }); let server2 = mdns.createAdvertisement(x, `${await getPort({port: 3839})}`, {name: encoded, txt: txt_record});
server2.start(); server2.start();
console.log('remote broadcasted') console.log('remote broadcasted')
} }

View file

@ -3473,22 +3473,7 @@ const app = new Vue({
} }
}, },
closeWindow(){ closeWindow(){
switch (app.cfg.general.close_behavior) { ipcRenderer.send('close');
case 0:
case '0':
// the minimizeToTray plugin will handle this
window.close();
break;
case 1:
case '1':
ipcRenderer.send('minimize');
break;
case 2:
case '2':
ipcRenderer.send('minimizeTray');
break;
}
} }
} }