tidal-hifi/src/main.js

120 lines
3.1 KiB
JavaScript
Raw Normal View History

const { app, BrowserWindow, globalShortcut, ipcMain } = require("electron");
2019-11-03 18:52:15 +01:00
const {
settings,
store,
createSettingsWindow,
showSettingsWindow,
closeSettingsWindow,
hideSettingsWindow,
} = require("./scripts/settings");
const { addTray, refreshTray } = require("./scripts/tray");
2019-09-16 23:13:12 +02:00
const path = require("path");
const tidalUrl = "https://listen.tidal.com";
const expressModule = require("./scripts/express");
const mediaKeys = require("./constants/mediaKeys");
const mediaInfoModule = require("./scripts/mediaInfo");
2019-11-03 18:52:15 +01:00
const globalEvents = require("./constants/globalEvents");
2019-09-16 23:13:12 +02:00
let mainWindow;
2019-10-30 21:34:26 +01:00
let icon = path.join(__dirname, "../assets/icon.png");
2019-09-16 23:13:12 +02:00
/**
* Enable live reload in development builds
*/
if (!app.isPackaged) {
require("electron-reload")(`${__dirname}`, {
electron: require(`${__dirname}/../node_modules/electron`),
});
}
2019-09-16 23:13:12 +02:00
function createWindow(options = {}) {
// Create the browser window.
mainWindow = new BrowserWindow({
x: options.x,
y: options.y,
width: 1024,
height: 800,
2019-10-30 21:34:26 +01:00
icon,
2019-10-22 21:25:57 +02:00
tray: true,
2019-09-16 23:13:12 +02:00
backgroundColor: options.backgroundColor,
webPreferences: {
affinity: "window",
preload: path.join(__dirname, "preload.js"),
plugins: true,
2019-11-03 18:52:15 +01:00
devTools: !app.isPackaged,
2019-09-16 23:13:12 +02:00
},
});
2019-11-03 18:52:15 +01:00
mainWindow.setMenuBarVisibility(store.get(settings.menuBar));
2019-09-16 23:13:12 +02:00
// load the Tidal website
mainWindow.loadURL(tidalUrl);
// run stuff after first load
mainWindow.webContents.once("did-finish-load", () => {});
// Emitted when the window is closed.
mainWindow.on("closed", function() {
2019-11-03 18:52:15 +01:00
closeSettingsWindow();
app.quit();
2019-09-16 23:13:12 +02:00
});
mainWindow.on("resize", () => {
let { width, height } = mainWindow.getBounds();
store.set(settings.windowBounds.root, { width, height });
});
2019-09-16 23:13:12 +02:00
}
function addGlobalShortcuts() {
Object.keys(mediaKeys).forEach((key) => {
globalShortcut.register(`${key}`, () => {
mainWindow.webContents.send("globalEvent", `${mediaKeys[key]}`);
2019-10-22 21:25:57 +02:00
});
});
2019-09-16 23:13:12 +02:00
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", () => {
createWindow();
2019-11-03 18:52:15 +01:00
createSettingsWindow();
2019-09-16 23:13:12 +02:00
addGlobalShortcuts();
addTray({ icon });
refreshTray();
store.get(settings.api) && expressModule.run(mainWindow);
2019-09-16 23:13:12 +02:00
});
app.on("activate", function() {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
// IPC
2019-11-03 18:52:15 +01:00
ipcMain.on(globalEvents.updateInfo, (event, arg) => {
mediaInfoModule.update(arg);
});
2019-11-03 18:52:15 +01:00
ipcMain.on(globalEvents.hideSettings, (event, arg) => {
hideSettingsWindow();
});
ipcMain.on(globalEvents.showSettings, (event, arg) => {
showSettingsWindow();
});
ipcMain.on(globalEvents.updateStatus, (event, arg) => {
mediaInfoModule.updateStatus(arg);
});
2019-11-03 18:52:15 +01:00
ipcMain.on(globalEvents.storeChanged, (event, arg) => {
mainWindow.setMenuBarVisibility(store.get(settings.menuBar));
});
ipcMain.on(globalEvents.error, (event, arg) => {
console.log(event);
});