2019-01-21 21:42:44 +08:00
|
|
|
const { app, BrowserWindow, protocol, session, ipcMain } = require('electron')
|
2018-12-26 23:58:24 +08:00
|
|
|
const path = require('path')
|
|
|
|
const fs = require('iofs')
|
2019-01-20 17:58:28 +08:00
|
|
|
const { exec } = require('child_process')
|
2018-12-26 23:58:24 +08:00
|
|
|
const log = console.log
|
|
|
|
|
2019-01-21 21:42:44 +08:00
|
|
|
const createTray = require('./tools/tray')
|
|
|
|
const createMenu = require('./tools/menu')
|
|
|
|
|
2019-01-20 20:30:09 +08:00
|
|
|
/* ******************************* */
|
|
|
|
/* **********修复环境变量*********** */
|
|
|
|
/* ******************************* */
|
|
|
|
let PATH_SET = new Set()
|
|
|
|
process.env.PATH.split(':').forEach(_ => {
|
|
|
|
PATH_SET.add(_)
|
|
|
|
})
|
|
|
|
PATH_SET.add('/usr/local/bin')
|
|
|
|
PATH_SET.add('/usr/local/sbin')
|
|
|
|
|
|
|
|
process.env.PATH = Array.from(PATH_SET).join(':')
|
|
|
|
PATH_SET = null
|
|
|
|
|
2018-12-26 23:58:24 +08:00
|
|
|
const ROOT = __dirname
|
|
|
|
const HOME = app.getPath('home')
|
|
|
|
const MIME_TYPES = {
|
|
|
|
js: 'application/javascript',
|
|
|
|
html: 'text/html',
|
|
|
|
htm: 'text/html',
|
|
|
|
css: 'text/css',
|
|
|
|
jpg: 'image/jpg',
|
|
|
|
png: 'image/png',
|
|
|
|
gif: 'image/gif'
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:19:55 +08:00
|
|
|
/* ----------------------------------------------------- */
|
2019-01-21 21:42:44 +08:00
|
|
|
app.commandLine.appendSwitch('--lang', 'zh-CN')
|
|
|
|
app.commandLine.appendSwitch('--autoplay-policy', 'no-user-gesture-required')
|
2019-01-19 22:19:55 +08:00
|
|
|
|
2019-01-21 21:42:44 +08:00
|
|
|
app.setPath('appData', path.resolve(HOME, '.sonist/'))
|
|
|
|
protocol.registerStandardSchemes(['app'], { secure: true })
|
2019-01-19 02:53:59 +08:00
|
|
|
|
2019-01-21 21:42:44 +08:00
|
|
|
let appPath = app.getPath('appData')
|
|
|
|
if (!fs.exists(appPath)) {
|
|
|
|
fs.mkdir(appPath)
|
|
|
|
fs.mkdir(path.join(appPath, 'lyrics'))
|
|
|
|
fs.mkdir(path.join(appPath, 'cache'))
|
|
|
|
fs.echo('{}', path.join(appPath, 'app.ini'))
|
|
|
|
fs.echo('[]', path.join(appPath, 'music.db'))
|
2019-01-19 02:53:59 +08:00
|
|
|
}
|
2019-01-19 22:19:55 +08:00
|
|
|
/* ----------------------------------------------------- */
|
2019-01-21 21:42:44 +08:00
|
|
|
let win = null
|
2019-01-19 02:53:59 +08:00
|
|
|
|
2018-12-26 23:58:24 +08:00
|
|
|
function createWindow() {
|
|
|
|
// 创建浏览器窗口
|
|
|
|
win = new BrowserWindow({
|
|
|
|
title: 'sonist',
|
|
|
|
width: 1024,
|
2018-12-29 20:00:19 +08:00
|
|
|
height: 640,
|
2018-12-26 23:58:24 +08:00
|
|
|
frame: false,
|
|
|
|
resizable: false,
|
2019-01-19 04:33:34 +08:00
|
|
|
icon: path.resolve(ROOT, './images/app.png'),
|
2018-12-26 23:58:24 +08:00
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
|
|
|
experimentalFeatures: true
|
2019-01-20 20:30:09 +08:00
|
|
|
},
|
|
|
|
show: false
|
2018-12-26 23:58:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// 然后加载应用的 index.html。
|
|
|
|
win.loadURL('app://sonist/index.html')
|
|
|
|
}
|
2019-01-21 21:42:44 +08:00
|
|
|
/* ****************************************** */
|
|
|
|
/* ************* init ******************* */
|
|
|
|
/* ****************************************** */
|
2018-12-26 23:58:24 +08:00
|
|
|
|
|
|
|
// 创建窗口
|
2019-01-19 23:29:34 +08:00
|
|
|
app.once('ready', () => {
|
2018-12-26 23:58:24 +08:00
|
|
|
protocol.registerBufferProtocol('app', (req, cb) => {
|
|
|
|
let file = req.url.replace(/^app:\/\/sonist\//, '')
|
|
|
|
let ext = path.extname(req.url).slice(1)
|
|
|
|
let buf = fs.cat(path.resolve(ROOT, file))
|
|
|
|
cb({ data: buf, mimeType: MIME_TYPES[ext] })
|
|
|
|
})
|
2018-12-28 18:47:45 +08:00
|
|
|
|
2019-01-20 17:58:28 +08:00
|
|
|
exec('which ffprobe', (err, res) => {
|
|
|
|
if (res) {
|
|
|
|
session.defaultSession.setUserAgent(
|
|
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
|
|
|
|
)
|
|
|
|
|
|
|
|
createWindow()
|
2019-01-20 20:30:09 +08:00
|
|
|
|
2019-01-21 21:42:44 +08:00
|
|
|
createTray(win)
|
|
|
|
createMenu(win)
|
|
|
|
|
2019-01-20 20:30:09 +08:00
|
|
|
win.on('ready-to-show', _ => {
|
|
|
|
win.show()
|
|
|
|
})
|
2019-01-21 21:42:44 +08:00
|
|
|
win.openDevTools()
|
2019-01-20 17:58:28 +08:00
|
|
|
} else {
|
|
|
|
win = new BrowserWindow({
|
|
|
|
width: 600,
|
|
|
|
height: 360,
|
2019-01-20 18:25:53 +08:00
|
|
|
skipTaskbar: true,
|
|
|
|
maximizable: false,
|
|
|
|
minimizable: false,
|
|
|
|
resizable: false,
|
2019-01-20 17:58:28 +08:00
|
|
|
titleBarStyle: 'hiddenInset'
|
|
|
|
})
|
2019-01-20 18:25:53 +08:00
|
|
|
win.setMenuBarVisibility(false)
|
2019-01-20 17:58:28 +08:00
|
|
|
win.loadURL('app://sonist/depends.html')
|
2019-01-20 20:30:09 +08:00
|
|
|
win.on('closed', _ => {
|
|
|
|
app.exit()
|
|
|
|
})
|
2019-01-20 17:58:28 +08:00
|
|
|
}
|
|
|
|
})
|
2018-12-26 23:58:24 +08:00
|
|
|
})
|
2019-01-20 23:03:26 +08:00
|
|
|
|
|
|
|
app.on('activate', _ => {
|
|
|
|
if (win) {
|
|
|
|
win.show()
|
|
|
|
}
|
|
|
|
})
|