2018-12-28 18:47:45 +08:00
|
|
|
const { app, BrowserWindow, protocol, Tray } = require('electron')
|
2018-12-26 23:58:24 +08:00
|
|
|
const path = require('path')
|
|
|
|
const fs = require('iofs')
|
|
|
|
const log = console.log
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
|
|
|
let win = null
|
2018-12-28 18:47:45 +08:00
|
|
|
let tray = null
|
2018-12-26 23:58:24 +08:00
|
|
|
|
|
|
|
function createWindow() {
|
|
|
|
// 创建浏览器窗口
|
|
|
|
win = new BrowserWindow({
|
|
|
|
title: 'sonist',
|
|
|
|
width: 1024,
|
|
|
|
height: 600,
|
|
|
|
frame: false,
|
|
|
|
resizable: false,
|
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
|
|
|
experimentalFeatures: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// 然后加载应用的 index.html。
|
|
|
|
win.loadURL('app://sonist/index.html')
|
|
|
|
}
|
|
|
|
app.commandLine.appendSwitch('--autoplay-policy', 'no-user-gesture-required')
|
|
|
|
app.setPath('appData', path.resolve(HOME, '.sonist/'))
|
|
|
|
protocol.registerStandardSchemes(['app'], { secure: true })
|
|
|
|
|
|
|
|
let appPath = app.getPath('appData')
|
|
|
|
if (!fs.exists(appPath)) {
|
|
|
|
fs.mkdir(appPath)
|
2018-12-28 22:19:29 +08:00
|
|
|
fs.mkdir(path.join(appPath, 'lyrics'))
|
2018-12-26 23:58:24 +08:00
|
|
|
fs.echo('{}', path.join(appPath, 'app.ini'))
|
|
|
|
fs.echo('[]', path.join(appPath, 'music.db'))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建窗口
|
|
|
|
app.on('ready', () => {
|
|
|
|
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
|
|
|
|
|
|
|
tray = new Tray('./images/trays/trayTemplate.png')
|
|
|
|
|
|
|
|
tray.on('click', _ => {
|
|
|
|
win.show()
|
|
|
|
})
|
|
|
|
|
2018-12-26 23:58:24 +08:00
|
|
|
createWindow()
|
2018-12-28 18:47:45 +08:00
|
|
|
win.tray = tray
|
2018-12-26 23:58:24 +08:00
|
|
|
win.webContents.openDevTools()
|
|
|
|
})
|