Changed handlers and removed old listeners
Fixed on-close to work correctly on quit
This commit is contained in:
parent
0ab0bd7b77
commit
d80bfc8398
2 changed files with 47 additions and 46 deletions
|
@ -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
|
||||||
|
@ -140,7 +140,7 @@ export class Win {
|
||||||
*/
|
*/
|
||||||
private startWebServer(): void {
|
private startWebServer(): void {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(express.static(path.join(this.paths.srcPath, "./renderer/")));
|
app.use(express.static(path.join(this.paths.srcPath, "./renderer/")));
|
||||||
app.set("views", path.join(this.paths.srcPath, "./renderer/views"));
|
app.set("views", path.join(this.paths.srcPath, "./renderer/views"));
|
||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
|
@ -157,7 +157,7 @@ export class Win {
|
||||||
res.redirect("https://discord.gg/applemusic");
|
res.redirect("https://discord.gg/applemusic");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
res.render("main", this.EnvironmentVariables);
|
res.render("main", this.EnvironmentVariables);
|
||||||
});
|
});
|
||||||
|
@ -199,7 +199,7 @@ export class Win {
|
||||||
remote.set("views", path.join(this.paths.srcPath, "./web-remote/views"));
|
remote.set("views", path.join(this.paths.srcPath, "./web-remote/views"));
|
||||||
remote.set("view engine", "ejs");
|
remote.set("view engine", "ejs");
|
||||||
getPort({port: 6942}).then((port) => {
|
getPort({port: 6942}).then((port) => {
|
||||||
this.remotePort = port;
|
this.remotePort = port;
|
||||||
// Start Remote Discovery
|
// Start Remote Discovery
|
||||||
this.broadcastRemote()
|
this.broadcastRemote()
|
||||||
remote.listen(this.remotePort, () => {
|
remote.listen(this.remotePort, () => {
|
||||||
|
@ -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", () => {
|
||||||
|
@ -429,7 +424,7 @@ export class Win {
|
||||||
});
|
});
|
||||||
electron.ipcMain.on("unmaximize", () => {
|
electron.ipcMain.on("unmaximize", () => {
|
||||||
// listen for maximize event
|
// listen for maximize event
|
||||||
this.win.unmaximize();
|
this.win.unmaximize();
|
||||||
});
|
});
|
||||||
|
|
||||||
electron.ipcMain.on("minimize", () => {
|
electron.ipcMain.on("minimize", () => {
|
||||||
|
@ -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) => {
|
||||||
|
@ -451,7 +446,7 @@ export class Win {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set scale
|
// Set scale
|
||||||
electron.ipcMain.on("windowresize", (event, width, height, lock = false) => {
|
electron.ipcMain.on("windowresize", (event, width, height, lock = false) => {
|
||||||
this.win.setContentSize(width, height);
|
this.win.setContentSize(width, height);
|
||||||
this.win.setResizable(!lock);
|
this.win.setResizable(!lock);
|
||||||
});
|
});
|
||||||
|
@ -462,9 +457,9 @@ 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'});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
electron.ipcMain.on('play', (event, type, id) => {
|
electron.ipcMain.on('play', (event, type, id) => {
|
||||||
this.win.webContents.executeJavaScript(`
|
this.win.webContents.executeJavaScript(`
|
||||||
|
@ -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,14 +601,15 @@ 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');
|
||||||
var x = mdns.tcp('cider-remote');
|
var x = mdns.tcp('cider-remote');
|
||||||
var txt_record = {
|
var txt_record = {
|
||||||
"Ver": "131077",
|
"Ver": "131077",
|
||||||
'DvSv': '3689',
|
'DvSv': '3689',
|
||||||
|
@ -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')
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue