Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
QoL features from github.com/vleeuwenmenn.
Browse files Browse the repository at this point in the history
  • Loading branch information
Braasileiro committed Oct 7, 2020
1 parent 28bf362 commit dc70e49
Showing 1 changed file with 118 additions and 10 deletions.
128 changes: 118 additions & 10 deletions source/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,62 @@ import Song from './model/Song';
import Settings from './settings';
import InputManager from './input';
import { Client } from 'discord-rpc';
import { app, BrowserWindow, ipcMain, dialog, globalShortcut } from 'electron';
import { app, BrowserWindow, Tray, Menu, ipcMain, dialog, globalShortcut, nativeImage} from 'electron';
import settings from 'electron-settings';

const RPC = new Client({ transport: 'ipc' });
const APP_VERSION = require('../package.json').version;

var tray: Tray;
var mWindow: BrowserWindow;

// States
var closeToTray: boolean;
var minimizeToTray: boolean;

// Entry
function createMainWindow() {
let userAgent: string;
let splashWindow: BrowserWindow;
const mainWindow = createWindow(false, 'deezer-preload.js');

// Main
// Disable menu (only works on Windows)
mainWindow.setMenu(null);

// User agent
switch (process.platform) {
case 'linux':
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
break;

case 'darwin':
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
break;

// win32
default:
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
break;
}

mainWindow.loadURL(Settings.DeezerUrl, { userAgent: userAgent });

// MainWindow
mainWindow.webContents.once('did-finish-load', () => {
mainWindow.show();
splashWindow.close();
registerShortcuts(mainWindow.webContents);
registerShortcutsAndTray(mainWindow);
});

// Events
mainWindow.on('close', (event) => {
event.preventDefault();
mainWindow.destroy();

if (closeToTray) mainWindow.hide(); else app.exit();
});

mainWindow.on('minimize', () => {
if (minimizeToTray) mainWindow.hide();
});

// Splash
Expand Down Expand Up @@ -75,9 +91,96 @@ function createWindow(visibility: boolean, preload?: string) {
});
}

function registerShortcuts(webContents: Electron.WebContents) {
const input = new InputManager(webContents);
async function registerShortcutsAndTray(mainWindow: BrowserWindow) {
const input = new InputManager(mainWindow.webContents);

closeToTray = await settings.get('closeToTray') == true;
minimizeToTray = await settings.get('minimizeToTray') == true;

// Tray
const icon = nativeImage.createFromPath(`${__dirname}/view/icon.png`);

icon.setTemplateImage(true);

tray = new Tray(icon);

const menu = Menu.buildFromTemplate([
{
type: 'normal',
label: 'Toggle',
click: () => {
if (mainWindow.isVisible()) mainWindow.hide(); else mainWindow.show();
},
},
{ type: 'separator' },
{
type: 'submenu',
label: 'Player',
submenu: [
{
type: 'normal',
label: 'Play/Pause',
click: () => input.space()
},
{
type: 'normal',
label: 'Next',
click: () => input.shiftRight()
},
{
type: 'normal',
label: 'Previous',
click: () => input.shiftLeft()
}
]
},
{ type: 'separator' },
{
type: 'submenu',
label: 'Settings',
submenu: [
{
type: 'checkbox',
label: 'Minimize to tray',
checked: minimizeToTray,
click: async () => {
minimizeToTray = !minimizeToTray;
await settings.set('minimizeToTray', minimizeToTray);
}
},
{
type: 'checkbox',
label: 'Close to tray',
checked: closeToTray,
click: async () => {
closeToTray = !closeToTray;
await settings.set('closeToTray', closeToTray);
}
},
]
},
{
type: 'normal',
label: `DeezerRPC ${APP_VERSION}`,
click: () => Electron.shell.openExternal("https://github.com/Braasileiro/DeezerRPC")
},
{ type: 'separator' },
{
type: 'normal',
label: 'Exit',
click: () => mainWindow.destroy()
}
]);

tray.setContextMenu(menu);
tray.setToolTip('No music played yet.');

// Double clicking hide/show
tray.on('double-click', () => {
if (mainWindow.isVisible()) mainWindow.hide(); else mainWindow.show();
});

// Global Shortcuts
globalShortcut.register('MediaPlayPause', () => {
input.space();
});
Expand All @@ -89,6 +192,9 @@ function registerShortcuts(webContents: Electron.WebContents) {
globalShortcut.register('MediaNextTrack', () => {
input.shiftRight();
});

// Set global MainWindow
mWindow = mainWindow;
}


Expand Down Expand Up @@ -124,6 +230,8 @@ ipcMain.on('song-changed', (event: any, song: Song) => {
instance: false,
});
}

tray.setToolTip(`${song.artist} - ${song.name}`);
});


Expand All @@ -133,5 +241,5 @@ app.on('ready', createMainWindow);

// Initialize RPC
RPC.login({ clientId: Settings.DiscordClientID }).catch(() => {
dialog.showErrorBox("Rich Presence Login Failed", "Please, verify if your discord app is opened/working and reopen this application.");
dialog.showErrorBox("Rich Presence Login Failed", "Please, verify if your discord app is opened/working and relaunch this application.");
});

0 comments on commit dc70e49

Please sign in to comment.