1 Star 0 Fork 0

Boluofan / ieaseMusic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.js 17.28 KB
一键复制 编辑 原始数据 按行查看 历史
小楼楼 提交于 2019-01-24 21:49 . Clean log
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
import path from 'path';
import { app, BrowserWindow, Menu, Tray, globalShortcut, ipcMain, shell, powerMonitor } from 'electron';
import windowStateKeeper from 'electron-window-state';
import storage from 'electron-json-storage';
import axios from 'axios';
import _debug from 'debug';
import pkg from './package.json';
import config from './config';
import api from './server/api';
import usocket from './server/usocket';
import { updater, downloader } from './submodules';
const _PLATFORM = process.platform;
let debug = _debug('dev:main');
let error = _debug('dev:main:error');
let apiServer;
let forceQuit = false;
let quitting = false;
let menu;
let tray;
let mainWindow;
let isOsx = _PLATFORM === 'darwin';
let isLinux = _PLATFORM === 'linux';
let showMenuBarOnLinux = false;
let revertTrayIcon = false;
// Shared data to other applocation via a unix socket file
let shared = {
modes: [],
track: {},
playing: false,
playlist: []
};
let mainMenu = [
{
label: 'ieaseMusic',
submenu: [
{
label: `About ieaseMusic`,
selector: 'orderFrontStandardAboutPanel:',
},
{
label: 'Preferences...',
accelerator: 'Cmd+,',
click() {
mainWindow.webContents.send('show-preferences');
}
},
{
type: 'separator'
},
{
role: 'hide'
},
{
role: 'hideothers'
},
{
role: 'unhide'
},
{
type: 'separator'
},
{
label: 'Check for updates',
accelerator: 'Cmd+U',
click() {
updater.checkForUpdates();
}
},
{
label: 'Quit',
accelerator: 'Command+Q',
selector: 'terminate:',
click() {
goodbye();
}
}
]
},
{
label: 'Controls',
submenu: [
{
label: 'Pause',
accelerator: 'Space',
click() {
mainWindow.show();
mainWindow.webContents.send('player-toggle');
}
},
{
label: 'Next',
accelerator: 'Right',
click() {
mainWindow.show();
mainWindow.webContents.send('player-next');
}
},
{
label: 'Previous',
accelerator: 'Left',
click() {
mainWindow.show();
mainWindow.webContents.send('player-previous');
}
},
{
label: 'Increase Volume',
accelerator: 'Up',
click() {
mainWindow.show();
mainWindow.webContents.send('player-volume-up');
}
},
{
label: 'Decrease Volume',
accelerator: 'Down',
click() {
mainWindow.show();
mainWindow.webContents.send('player-volume-down');
}
},
{
label: 'Like',
accelerator: 'Cmd+L',
click() {
mainWindow.show();
mainWindow.webContents.send('player-like');
}
},
],
},
{
label: 'Recently Played',
submenu: [
{
label: 'Nothing...',
}
],
},
{
label: 'Next Up',
submenu: [
{
label: 'Nothing...',
}
],
},
{
label: 'Edit',
submenu: [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
]
},
{
label: 'View',
submenu: [
{
label: 'Home',
accelerator: 'Cmd+Shift+H',
click() {
mainWindow.webContents.send('show-home');
}
},
{
label: 'Search',
accelerator: 'Cmd+F',
click() {
mainWindow.webContents.send('show-search');
}
},
{
label: 'Top podcasts',
accelerator: 'Cmd+Shift+T',
click() {
mainWindow.webContents.send('show-top');
}
},
{
label: 'Playlist',
accelerator: 'Cmd+Shift+P',
click() {
mainWindow.webContents.send('show-playlist');
}
},
{
label: 'Made For You',
accelerator: 'Cmd+Shift+F',
click() {
mainWindow.webContents.send('show-fm');
}
},
{
label: 'Downloads',
accelerator: 'Cmd+Shift+D',
click() {
downloader.showDownloader();
}
},
{
type: 'separator',
},
{
label: 'Menu',
accelerator: 'Cmd+Shift+L',
click() {
mainWindow.webContents.send('show-menu');
}
},
{
label: 'Next Up',
accelerator: 'Cmd+P',
click() {
mainWindow.webContents.send('show-playing');
}
},
{
type: 'separator'
},
{
role: 'toggledevtools'
},
]
},
{
role: 'window',
submenu: [
{
role: 'minimize'
},
{
role: 'close'
}
]
},
{
role: 'help',
submenu: [
{
label: 'Bug report 🐛',
click() {
shell.openExternal('https://github.com/trazyn/ieaseMusic/issues');
}
},
{
label: 'Fork me on Github 🚀',
click() {
shell.openExternal('https://github.com/trazyn/ieaseMusic');
}
},
{
type: 'separator'
},
{
label: '💕 Follow me on Twitter 👏',
click() {
shell.openExternal('https://twitter.com/var_darling');
}
}
]
}
];
let trayMenu = [
{
label: 'Pause',
click() {
mainWindow.webContents.send('player-toggle');
}
},
{
label: 'Next',
click() {
mainWindow.webContents.send('player-next');
}
},
{
label: 'Previous',
click() {
mainWindow.webContents.send('player-previous');
}
},
{
type: 'separator'
},
{
label: 'Preferences...',
accelerator: 'Cmd+,',
click() {
mainWindow.webContents.send('show-preferences');
}
},
{
type: 'separator'
},
{
label: 'Toggle main window',
click() {
let isVisible = mainWindow.isVisible();
isVisible ? mainWindow.hide() : mainWindow.show();
}
},
{
type: 'separator'
},
{
label: 'Check for updates',
accelerator: 'Cmd+U',
click() {
updater.checkForUpdates();
}
},
{
label: 'Fork me on Github',
click() {
shell.openExternal('https://github.com/trazyn/ieaseMusic');
}
},
{
type: 'separator'
},
{
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click() {
mainWindow.show();
mainWindow.toggleDevTools();
}
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
selector: 'terminate:',
click() {
goodbye();
}
}
];
let dockMenu = [
{
label: 'Toggle Player',
accelerator: 'Space',
click() {
mainWindow.show();
mainWindow.webContents.send('player-toggle');
}
},
{
label: 'Next',
accelerator: 'Right',
click() {
mainWindow.show();
mainWindow.webContents.send('player-next');
}
},
{
label: 'Previous',
accelerator: 'Left',
click() {
mainWindow.show();
mainWindow.webContents.send('player-previous');
}
},
{
label: 'Like',
accelerator: 'Cmd+L',
click() {
mainWindow.show();
mainWindow.webContents.send('player-like');
}
},
];
function updateMenu(playing) {
if (!isOsx && !showMenuBarOnLinux) {
return;
}
mainMenu[1]['submenu'][0]['label'] = playing ? 'Pause' : 'Play';
menu = Menu.buildFromTemplate(mainMenu);
Menu.setApplicationMenu(menu);
}
function updateTray(playing) {
// Update unread mesage count
trayMenu[0].label = playing ? 'Pause' : 'Play';
let contextmenu = Menu.buildFromTemplate(trayMenu);
let icon = playing
? `${__dirname}/src/assets/playing.png`
: `${__dirname}/src/assets/notplaying.png`
;
if (revertTrayIcon) {
icon = playing
? `${__dirname}/src/assets/playing-dark-panel.png`
: `${__dirname}/src/assets/notplaying-dark-panel.png`
;
}
if (!tray) {
// Init tray icon
tray = new Tray(icon);
tray.on('right-click', () => {
tray.popUpContextMenu();
});
}
tray.setImage(icon);
tray.setContextMenu(contextmenu);
}
function registerGlobalShortcut() {
// Play the next song
globalShortcut.register('MediaNextTrack', e => {
mainWindow.webContents.send('player-next');
});
// Play the previous song
globalShortcut.register('MediaPreviousTrack', e => {
mainWindow.webContents.send('player-previous');
});
// Toggle the player
globalShortcut.register('MediaPlayPause', e => {
mainWindow.webContents.send('player-toggle');
});
}
const goodbye = () => {
forceQuit = true;
app.quit();
};
const createMainWindow = () => {
var mainWindowState = windowStateKeeper({
defaultWidth: 800,
defaultHeight: 520,
});
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
show: false,
width: 800,
height: 520,
resizable: false,
maximizable: false,
fullscreenable: false,
backgroundColor: 'none',
titleBarStyle: 'hiddenInset',
});
if (isLinux) {
mainWindow.setIcon(
path.join(__dirname, 'src/assets/dock.png')
);
// Disable default menu bar
if (!showMenuBarOnLinux) {
mainWindow.setMenu(null);
}
}
mainWindowState.manage(mainWindow);
mainWindow.loadURL(`file://${__dirname}/src/index.html`);
mainWindow.webContents.on('did-finish-load', () => {
try {
mainWindow.show();
mainWindow.focus();
} catch (ex) { }
});
mainWindow.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
});
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
mainWindow.on('close', e => {
if (isLinux) {
app.quit();
return;
}
if (forceQuit) {
app.quit();
} else {
e.preventDefault();
mainWindow.hide();
}
});
// Update the history menu
ipcMain.on('update-history', (event, args) => {
var historyMenu = mainMenu.find(e => e.label === 'Recently Played');
var submenu = args.songs.map((e, index) => {
return {
label: e.name,
accelerator: `Cmd+${index}`,
click() {
mainWindow.show();
mainWindow.webContents.send('player-play', {
id: e.id,
});
}
};
});
historyMenu.submenu = submenu;
updateMenu();
});
// Update next up menu
ipcMain.on('update-playing', async(event, args) => {
var playingMenu = mainMenu.find(e => e.label === 'Next Up');
var submenu = args.songs.map((e, index) => {
return {
label: e.name,
click() {
mainWindow.show();
mainWindow.webContents.send('player-play', {
id: e.id,
});
}
};
});
shared.playlist = args.songs;
playingMenu.submenu = submenu;
updateMenu();
});
// Update menu icon image and controls menu
ipcMain.on('update-status', (event, args) => {
var { playing, song, modes } = args;
if (tray) {
updateTray(playing, song);
}
shared.modes = modes;
shared.track = song;
shared.playing = +playing;
updateMenu(playing);
});
// Show/Hide menu icon
ipcMain.on('update-preferences', (event, args = {}) => {
var proxyOptions = { proxyBypassRules: 'localhost' };
var proxy = args.proxy || '';
if (proxy.endsWith('.pac')) {
proxyOptions.pacScript = proxy;
} else {
proxyOptions.proxyRules = proxy;
}
mainWindow.setAlwaysOnTop(!!args.alwaysOnTop);
mainWindow.webContents.session.setProxy(
proxyOptions,
() => debug('Apply proxy: %s', args.proxy)
);
revertTrayIcon = args.revertTrayIcon;
if (!args.showTray) {
if (tray) {
tray.destroy();
tray = null;
}
return;
}
updateTray(args.playing);
});
// Show the main window
ipcMain.on('show', event => {
mainWindow.show();
mainWindow.focus();
});
// Minimize the window
ipcMain.on('minimize', event => {
mainWindow.minimize();
});
// Quit app
ipcMain.on('goodbye', () => goodbye());
// A javaScript error occured in the main process
process.on('uncaughtException', (ex) => {
error(ex);
});
// App has suspend
powerMonitor.on('suspend', () => {
mainWindow.webContents.send('player-pause');
});
if (isOsx) {
// App about
app.setAboutPanelOptions({
applicationName: 'ieaseMusic',
applicationVersion: pkg.version,
copyright: 'Made with 💖 by trazyn. \n https://github.com/trazyn/ieaseMusic',
credits: `With the invaluable help of: \n github.com/Binaryify/NeteaseCloudMusicApi`,
version: pkg.version
});
app.dock.setMenu(Menu.buildFromTemplate(dockMenu));
}
mainWindow.goodbye = () => goodbye();
updateMenu();
registerGlobalShortcut();
usocket(shared, mainWindow);
updater.installAutoUpdater(() => goodbye());
downloader.createDownloader();
mainWindow.webContents.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8');
global.mainWindow = mainWindow;
debug('Create main process success 🍻');
};
app.setName('ieaseMusic');
app.on('activate', e => {
if (!mainWindow.isVisible()) {
mainWindow.show();
}
});
app.on('before-quit', e => {
e.preventDefault();
if (quitting) {
return;
}
apiServer && apiServer.close();
// Fix issues #14
forceQuit = true;
quitting = true;
mainWindow = null;
app.exit(0);
process.exit(0);
});
app.on('ready', async() => {
storage.get('preferences', (err, preferences = {}) => {
if (err) {
throw err;
}
var port = config.api.port || preferences.port;
showMenuBarOnLinux = preferences.showMenuBarOnLinux;
revertTrayIcon = preferences.revertTrayIcon;
createMainWindow();
updater.checkForUpdates(preferences.autoupdate);
axios.defaults.baseURL = `http://localhost:${port}`;
apiServer = api(
port,
preferences.proxy,
err => {
if (err) throw err;
debug(`API server is running with port ${port} 👊`);
}
);
});
});
1
https://gitee.com/Boluofan/ieaseMusic.git
git@gitee.com:Boluofan/ieaseMusic.git
Boluofan
ieaseMusic
ieaseMusic
master

搜索帮助