typescript is hell
This commit is contained in:
parent
c561bb7589
commit
d15ee4de30
2 changed files with 139 additions and 29 deletions
|
@ -1,15 +1,15 @@
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import {app, ipcMain} from "electron";
|
import {app, ipcMain, shell} from "electron";
|
||||||
import {join} from "path";
|
|
||||||
import * as windowStateKeeper from "electron-window-state";
|
import * as windowStateKeeper from "electron-window-state";
|
||||||
import * as express from "express";
|
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";
|
||||||
|
|
||||||
export default class Win {
|
export default class Win {
|
||||||
public win: Electron.BrowserWindow | undefined;
|
public win: null | undefined;
|
||||||
public app: Electron.App | undefined;
|
public app: Electron.App | undefined;
|
||||||
|
|
||||||
private static port: number = 0;
|
private clientPort: number = 0;
|
||||||
private static envVars: object = {
|
private static envVars: object = {
|
||||||
"env": {
|
"env": {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -19,7 +19,7 @@ export default class Win {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private static options: any = {
|
private static options: any = {
|
||||||
icon: join(__dirname, `../../../resources/icons/icon.ico`),
|
icon: path.join(__dirname, `../../../resources/icons/icon.ico`),
|
||||||
width: 1024,
|
width: 1024,
|
||||||
height: 600,
|
height: 600,
|
||||||
x: undefined,
|
x: undefined,
|
||||||
|
@ -42,7 +42,7 @@ export default class Win {
|
||||||
sandbox: true,
|
sandbox: true,
|
||||||
nativeWindowOpen: true,
|
nativeWindowOpen: true,
|
||||||
contextIsolation: false,
|
contextIsolation: false,
|
||||||
preload: join(__dirname, '../../preload/cider-preload.js')
|
preload: path.join(__dirname, '../../preload/cider-preload.js')
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -53,7 +53,9 @@ export default class Win {
|
||||||
/**
|
/**
|
||||||
* Creates the browser window
|
* Creates the browser window
|
||||||
*/
|
*/
|
||||||
public createWindow(): Electron.BrowserWindow {
|
public async createWindow(): Promise<Electron.BrowserWindow> {
|
||||||
|
this.clientPort = await getPort({port: 9000});
|
||||||
|
|
||||||
let BrowserWindow;
|
let BrowserWindow;
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
BrowserWindow = require("electron-acrylic-window").BrowserWindow;
|
BrowserWindow = require("electron-acrylic-window").BrowserWindow;
|
||||||
|
@ -71,29 +73,23 @@ export default class Win {
|
||||||
|
|
||||||
// Create the browser window.
|
// Create the browser window.
|
||||||
const win = new BrowserWindow(this.options);
|
const win = new BrowserWindow(this.options);
|
||||||
|
console.debug('Browser window created');
|
||||||
|
this.win = win;
|
||||||
|
|
||||||
// and load the renderer.
|
// and load the renderer.
|
||||||
this.startWebServer(win)
|
this.startWebServer(win)
|
||||||
.then((url) => {
|
this.startSession(win);
|
||||||
console.log(url)
|
this.startHandlers(win);
|
||||||
this.startSession(win, url);
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.win = win;
|
|
||||||
return win;
|
return win;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the webserver for the renderer process.
|
* Starts the webserver for the renderer process.
|
||||||
* @param win The window to use
|
* @param win The BrowserWindow
|
||||||
*/
|
*/
|
||||||
private async startWebServer(win: Electron.BrowserWindow): Promise<string> {
|
private startWebServer(win: Electron.BrowserWindow): void {
|
||||||
Win.port = await getPort({port: 9000});
|
|
||||||
|
|
||||||
const webapp = express(),
|
const webapp = express(),
|
||||||
webRemotePath = path.join(__dirname, '../../renderer/');
|
webRemotePath = path.join(__dirname, '../../renderer/');
|
||||||
|
|
||||||
|
@ -113,19 +109,16 @@ export default class Win {
|
||||||
//res.sendFile(path.join(webRemotePath, 'index_old.html'));
|
//res.sendFile(path.join(webRemotePath, 'index_old.html'));
|
||||||
res.render("main", Win.envVars)
|
res.render("main", Win.envVars)
|
||||||
});
|
});
|
||||||
webapp.listen(Win.port, function () {
|
webapp.listen(this.clientPort, () => {
|
||||||
console.debug(`Cider client port: ${Win.port}`);
|
console.debug(`Cider client port: ${this.clientPort}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
return "http://localhost:" + Win.port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the session for the renderer process.
|
* Starts the session for the renderer process.
|
||||||
* @param win The window to use
|
* @param win The BrowserWindow
|
||||||
* @param location The location of the renderer
|
|
||||||
*/
|
*/
|
||||||
private startSession(win: Electron.BrowserWindow, location: string) {
|
private startSession(win: Electron.BrowserWindow): void {
|
||||||
// intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead
|
// intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead
|
||||||
win.webContents.session.webRequest.onBeforeRequest(
|
win.webContents.session.webRequest.onBeforeRequest(
|
||||||
{
|
{
|
||||||
|
@ -134,7 +127,7 @@ export default class Win {
|
||||||
(details, callback) => {
|
(details, callback) => {
|
||||||
if (details.url.includes("hls.js")) {
|
if (details.url.includes("hls.js")) {
|
||||||
callback({
|
callback({
|
||||||
redirectURL: `http://localhost:${Win.port}/apple-hls.js`
|
redirectURL: `http://localhost:${this.clientPort}/apple-hls.js`
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
callback({
|
callback({
|
||||||
|
@ -155,6 +148,122 @@ export default class Win {
|
||||||
callback({requestHeaders: details.requestHeaders})
|
callback({requestHeaders: details.requestHeaders})
|
||||||
})
|
})
|
||||||
|
|
||||||
win.loadURL(location).catch(console.error);
|
const location = `http://localhost:${this.clientPort}/`
|
||||||
|
console.log('yeah')
|
||||||
|
win.loadURL(location)
|
||||||
|
.then(() => {
|
||||||
|
console.debug(`Cider client location: ${location}`);
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the window handlers
|
||||||
|
* @param win The BrowserWindow
|
||||||
|
*/
|
||||||
|
private startHandlers(win: Electron.BrowserWindow): void {
|
||||||
|
win.on('closed', () => {
|
||||||
|
this.win = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (process.platform === "win32") {
|
||||||
|
let WND_STATE = {
|
||||||
|
MINIMIZED: 0,
|
||||||
|
NORMAL: 1,
|
||||||
|
MAXIMIZED: 2,
|
||||||
|
FULL_SCREEN: 3
|
||||||
|
}
|
||||||
|
let wndState = WND_STATE.NORMAL
|
||||||
|
|
||||||
|
win.on("resize", (_: any) => {
|
||||||
|
const isMaximized = win.isMaximized()
|
||||||
|
const isMinimized = win.isMinimized()
|
||||||
|
const isFullScreen = win.isFullScreen()
|
||||||
|
const state = wndState;
|
||||||
|
if (isMinimized && state !== WND_STATE.MINIMIZED) {
|
||||||
|
wndState = WND_STATE.MINIMIZED
|
||||||
|
} else if (isFullScreen && state !== WND_STATE.FULL_SCREEN) {
|
||||||
|
wndState = WND_STATE.FULL_SCREEN
|
||||||
|
} else if (isMaximized && state !== WND_STATE.MAXIMIZED) {
|
||||||
|
wndState = WND_STATE.MAXIMIZED
|
||||||
|
win.webContents.executeJavaScript(`app.chrome.maximized = true`)
|
||||||
|
} else if (state !== WND_STATE.NORMAL) {
|
||||||
|
wndState = WND_STATE.NORMAL
|
||||||
|
win.webContents.executeJavaScript(`app.chrome.maximized = false`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set window Handler
|
||||||
|
win.webContents.setWindowOpenHandler(({url}) => {
|
||||||
|
if (url.includes("apple") || url.includes("localhost")) {
|
||||||
|
return {action: "allow"}
|
||||||
|
}
|
||||||
|
shell.openExternal(url).catch(() => {
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
action: 'deny'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------------
|
||||||
|
// Renderer IPC Listeners
|
||||||
|
//-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ipcMain.on("cider-platform", (event) => {
|
||||||
|
event.returnValue = process.platform
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.on("get-gpu-mode", (event) => {
|
||||||
|
event.returnValue = process.platform
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.on("is-dev", (event) => {
|
||||||
|
event.returnValue = !app.isPackaged
|
||||||
|
})
|
||||||
|
|
||||||
|
// IPC stuff (listeners)
|
||||||
|
ipcMain.on('close', () => { // listen for close event
|
||||||
|
win.close();
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.handle('getYTLyrics', async (event, track, artist) => {
|
||||||
|
const u = track + " " + artist + " official video";
|
||||||
|
const videos = await yt.search(u);
|
||||||
|
return videos
|
||||||
|
})
|
||||||
|
|
||||||
|
// ipcMain.handle('getStoreValue', (event, key, defaultValue) => {
|
||||||
|
// return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key));
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// ipcMain.handle('setStoreValue', (event, key, value) => {
|
||||||
|
// app.cfg.set(key, value);
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// ipcMain.on('getStore', (event) => {
|
||||||
|
// event.returnValue = app.cfg.store
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// ipcMain.on('setStore', (event, store) => {
|
||||||
|
// app.cfg.store = store
|
||||||
|
// })
|
||||||
|
|
||||||
|
ipcMain.on('maximize', () => { // listen for maximize event
|
||||||
|
if (win.isMaximized()) {
|
||||||
|
win.unmaximize()
|
||||||
|
} else {
|
||||||
|
win.maximize()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.on('minimize', () => { // listen for minimize event
|
||||||
|
win.minimize();
|
||||||
|
})
|
||||||
|
|
||||||
|
// Set scale
|
||||||
|
ipcMain.on('setScreenScale', (event, scale) => {
|
||||||
|
win.webContents.setZoomFactor(parseFloat(scale))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,7 +9,8 @@
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"*": ["node_modules/*"]
|
"*": ["node_modules/*"]
|
||||||
}
|
},
|
||||||
|
"allowSyntheticDefaultImports": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src/**/*"
|
"src/**/*"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue