This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
appcat
/
sonist
Archived
1
0
Fork 0
sonist/js/app.js

377 lines
8.6 KiB
JavaScript
Raw Normal View History

2018-12-26 23:58:24 +08:00
/**
* {sonist app}
* @author yutent<yutent@doui.cc>
* @date 2018/12/16 17:15:57
*/
import '/dist/anot.next.js'
import layer from '/dist/layer/index.js'
import store from '/dist/store/index.js'
import AudioPlayer from '/dist/audio/index.js'
import Api from '/js/api.js'
import Artist from '/js/modules/artist.js'
import Local from '/js/modules/local.js'
2018-12-27 03:02:37 +08:00
import Profile from '/js/modules/profile.js'
2018-12-26 23:58:24 +08:00
const log = console.log
const fs = require('iofs')
const path = require('path')
2018-12-28 18:47:45 +08:00
const { remote } = require('electron')
const WIN = remote.getCurrentWindow()
const HOME_PATH = remote.app.getPath('appData')
2018-12-26 23:58:24 +08:00
const APP_INI_PATH = path.join(HOME_PATH, 'app.ini')
2018-12-28 22:19:29 +08:00
const LYRICS_PATH = path.join(HOME_PATH, 'lyrics')
2018-12-26 23:58:24 +08:00
const PLAY_MODE = {
0: 'all',
1: 'single',
2: 'random'
}
2018-12-29 20:00:19 +08:00
const COLORS = [
{
title: '#62778d',
lrc: '#98acae',
bar1: '#dae1e9',
bar2: '#3fc2a7'
},
{
title: '#fff',
lrc: '#d7d8db',
bar1: '#454545',
bar2: '#fff'
}
]
2018-12-26 23:58:24 +08:00
const FONTS_NAME =
' Helvetica, Arial,"WenQuanYi Micro Hei","PingFang SC","Hiragino Sans GB","Segoe UI", "Microsoft Yahei", sans-serif'
// 本地音乐和试用音乐列表
window.LS = store.collection('local')
window.TS = store.collection('temp')
// 音乐播放器
window.SONIST = new AudioPlayer()
let appInit = fs.cat(APP_INI_PATH)
2018-12-28 18:47:45 +08:00
Anot.ss('app-init', appInit + '')
2018-12-26 23:58:24 +08:00
appInit = JSON.parse(appInit)
Anot({
$id: 'app',
state: {
2018-12-28 22:19:29 +08:00
theme: appInit.theme || 1, // 1:macos, 2: deepin
2018-12-28 18:47:45 +08:00
winFocus: false,
2018-12-26 23:58:24 +08:00
mod: 'local',
playMode: Anot.ls('play-mode') >>> 0, // 0:all | 1:single | 2:random
2018-12-29 20:00:19 +08:00
ktvMode: 0,
2018-12-26 23:58:24 +08:00
isPlaying: false,
2018-12-28 18:47:45 +08:00
optBoxShow: false,
2018-12-28 22:19:29 +08:00
volumeCtrlShow: false,
volume: Anot.ls('volume') || 70,
2018-12-26 23:58:24 +08:00
curr: {
id: '',
title: '',
artist: '',
album: '',
time: 0,
duration: 0
2018-12-28 22:19:29 +08:00
}
2018-12-26 23:58:24 +08:00
},
skip: [],
computed: {
views() {
if (!this.mod) {
return
}
return '/views/' + this.mod + '.htm'
2018-12-29 20:00:19 +08:00
},
coverBG() {
if (this.curr.cover) {
return `url(${this.curr.cover})`
} else {
return 'none'
}
2018-12-26 23:58:24 +08:00
}
},
watch: {
mod(val) {
this.activeModule(val)
}
},
mounted() {
let canvas = this.$refs.player
// 画布放大4倍, 以解决模糊的问题
this.__WIDTH__ = canvas.clientWidth * 4
this.__HEIGHT__ = canvas.clientHeight * 4
canvas.width = this.__WIDTH__
canvas.height = this.__HEIGHT__
this.__CTX__ = canvas.getContext('2d')
2018-12-28 18:47:45 +08:00
this.draw()
2018-12-26 23:58:24 +08:00
// 修改歌曲进度
canvas.addEventListener(
'click',
ev => {
if (!this.curr.id) {
return
}
2018-12-26 23:58:24 +08:00
let rect = canvas.getBoundingClientRect()
let aw = rect.width
let ax = ev.pageX - rect.left
let ay = ev.pageY - rect.top
log(aw, ax, ay)
if (ax < 80) {
2018-12-29 20:00:19 +08:00
this.ktvMode = this.ktvMode ^ 1
return
}
2018-12-26 23:58:24 +08:00
if (ax > 124 && ay > 55 && ay < 64) {
let pp = (ax - 124) / (aw - 124)
this.curr.time = pp * this.curr.duration
SONIST.seek(this.curr.time)
if (!this.isPlaying) {
this.draw()
}
2018-12-26 23:58:24 +08:00
}
},
true
)
// 设置循环模式
SONIST.mode = PLAY_MODE[this.playMode]
2018-12-29 02:17:46 +08:00
SONIST.volume = this.volume
2018-12-26 23:58:24 +08:00
SONIST.on('play', time => {
this.curr.time = time
})
SONIST.on('end', time => {
this.nextSong(1)
})
this.activeModule(this.mod)
2018-12-28 18:47:45 +08:00
remote.app.on('browser-window-focus', _ => {
this.winFocus = true
})
remote.app.on('browser-window-blur', _ => {
this.winFocus = false
})
2018-12-26 23:58:24 +08:00
},
methods: {
2018-12-28 18:47:45 +08:00
quit(force) {
if (force) {
remote.app.exit()
} else {
if (appInit.allowPlayOnBack) {
WIN.hide()
} else {
remote.app.exit()
}
}
},
2018-12-26 23:58:24 +08:00
minimize() {},
maximize() {},
activeModule(mod) {
switch (mod) {
case 'artist':
Artist.__init__()
break
case 'local':
Local.__init__()
break
2018-12-27 03:02:37 +08:00
case 'profile':
Profile.__init__()
break
2018-12-26 23:58:24 +08:00
default:
break
}
},
2018-12-28 18:47:45 +08:00
toggleOptBox() {
this.optBoxShow = !this.optBoxShow
},
2018-12-26 23:58:24 +08:00
toggleModule(mod) {
2018-12-29 02:14:00 +08:00
if ('mv' === mod) {
2018-12-26 23:58:24 +08:00
return
}
2018-12-28 18:47:45 +08:00
this.optBoxShow = false
2018-12-26 23:58:24 +08:00
this.mod = mod
},
togglePlayMode() {
let mod = this.playMode
mod++
if (mod > 2) {
mod = 0
}
this.playMode = mod
SONIST.mode = PLAY_MODE[mod]
Anot.ls('play-mode', mod)
},
2018-12-28 22:19:29 +08:00
// 修改音量
changeValume(ev) {
let volume = 535 - ev.pageY
if (volume < 0) {
volume = 0
}
if (volume > 100) {
volume = 100
}
this.volume = volume
SONIST.volume = volume
Anot.ls('volume', volume)
},
2018-12-26 23:58:24 +08:00
draw() {
let img1 = new Image()
let img2 = new Image()
let p1 = Promise.defer()
let p2 = Promise.defer()
let { title, artist, cover } = this.curr
let play = this.isPlaying
img1.onload = p1.resolve
img2.onload = p2.resolve
img1.src = '/images/disk.png'
img2.src = cover || '/images/album.png'
let rx = (play ? 112 : 40) + this.__HEIGHT__ / 2 // 旋转唱片的圆心坐标X
let ry = this.__HEIGHT__ / 2 // 旋转唱片的圆心坐标Y
let pw = this.__WIDTH__ - this.__HEIGHT__ - 180 // 进度条总长度
let wl = this.__HEIGHT__ + 180 // 文字的坐标X
2018-12-29 20:00:19 +08:00
2018-12-26 23:58:24 +08:00
const draw = () => {
let { time, duration } = this.curr
let pp = time / duration // 进度百分比
time = Anot.filters.time(time)
duration = Anot.filters.time(duration)
this.__CTX__.clearRect(0, 0, this.__WIDTH__, this.__HEIGHT__)
this.__CTX__.save()
// 将原点移到唱片圆心, 旋转完再回到初始值
this.__CTX__.translate(rx, ry)
this.__CTX__.rotate(this.__DEG__ * Math.PI)
this.__CTX__.translate(-rx, -ry)
this.__CTX__.drawImage(
img1,
play ? 112 : 40,
0,
this.__HEIGHT__,
this.__HEIGHT__
)
this.__CTX__.restore()
this.__CTX__.drawImage(img2, 0, 0, this.__HEIGHT__, this.__HEIGHT__)
// 歌曲标题和歌手
2018-12-29 20:00:19 +08:00
this.__CTX__.fillStyle = COLORS[this.ktvMode].title
2018-12-26 23:58:24 +08:00
this.__CTX__.font = '56px' + FONTS_NAME
this.__CTX__.fillText(`${title} - ${artist}`, wl, 100)
// 时间
2018-12-29 20:00:19 +08:00
this.__CTX__.fillStyle = COLORS[this.ktvMode].lrc
2018-12-26 23:58:24 +08:00
this.__CTX__.font = '48px' + FONTS_NAME
this.__CTX__.fillText(
`${time} / ${duration}`,
this.__WIDTH__ - 280,
100
)
// 歌词
2018-12-29 20:00:19 +08:00
this.__CTX__.fillStyle = COLORS[this.ktvMode].lrc
2018-12-26 23:58:24 +08:00
this.__CTX__.font = '48px' + FONTS_NAME
this.__CTX__.fillText(`暂无歌词...`, wl, 180)
// 进度条
2018-12-29 20:00:19 +08:00
this.__CTX__.fillStyle = COLORS[this.ktvMode].bar1
2018-12-26 23:58:24 +08:00
this.__CTX__.fillRect(wl, 230, pw, 16)
2018-12-29 20:00:19 +08:00
this.__CTX__.fillStyle = COLORS[this.ktvMode].bar2
2018-12-26 23:58:24 +08:00
this.__CTX__.fillRect(wl, 230, pw * pp, 16)
this.__DEG__ += 0.01
}
Promise.all([p1.promise, p2.promise]).then(_ => {
clearInterval(this.timer)
2018-12-28 18:47:45 +08:00
this.__DEG__ = 0.01
2018-12-26 23:58:24 +08:00
if (play) {
this.timer = setInterval(() => {
draw(img1, img2, play, rx, ry)
}, 20)
} else {
draw(img1, img2, play, rx, ry)
}
})
},
nextSong(step) {
let _p = null
if (step > 0) {
_p = SONIST.next()
} else {
_p = SONIST.prev()
}
this.isPlaying = false
_p.then(it => {
2018-12-28 22:19:29 +08:00
if (this.mod === 'local') {
Local.__updateSong__(it)
2018-12-26 23:58:24 +08:00
}
// 通知子模块歌曲已经改变
this.$fire('child!curr', it.id)
2018-12-28 22:19:29 +08:00
this.play(it)
2018-12-26 23:58:24 +08:00
})
},
pause() {
this.isPlaying = false
},
2018-12-28 22:19:29 +08:00
updateCurr(obj) {
let old = this.curr.$model
this.curr = Object.assign(old, obj)
},
2018-12-26 23:58:24 +08:00
play(song) {
// 有参数的,说明是播放回调通知
// 此时仅更新播放控制条的信息即可
if (song) {
2018-12-28 22:19:29 +08:00
song.time = 0
this.updateCurr(song)
2018-12-26 23:58:24 +08:00
this.isPlaying = true
} else {
if (SONIST.stat === 'ready') {
2018-12-29 20:43:49 +08:00
if (this.curr.id) {
if (this.isPlaying) {
SONIST.pause()
} else {
SONIST.play()
}
2018-12-26 23:58:24 +08:00
} else {
2018-12-29 20:43:49 +08:00
let lastPlay = Anot.ls('last-play') || 0
SONIST.play(lastPlay).then(it => {
it.time = 0
this.updateCurr(it)
this.draw()
})
2018-12-26 23:58:24 +08:00
}
this.isPlaying = !this.isPlaying
}
}
this.draw()
}
}
})