feat: updates

This commit is contained in:
qier222 2023-03-26 02:16:01 +08:00
parent ce757215a3
commit c1cd31840e
No known key found for this signature in database
86 changed files with 1048 additions and 778 deletions

View file

@ -1,13 +1,15 @@
import path from 'path'
import { isProd } from '../env'
import log from '../log'
import appleMusic from './routes/r3play/appleMusic'
import netease from './routes/netease/netease'
import audio from './routes/r3play/audio'
import fastifyCookie from '@fastify/cookie'
import fastifyMultipart from '@fastify/multipart'
import fastifyStatic from '@fastify/static'
import fastify from 'fastify'
import path from 'path'
import { isProd } from '../env'
import log from '../log'
import netease from './routes/netease/netease'
import appleMusic from './routes/r3play/appleMusic'
import audio from './routes/r3play/audio'
log.info('[electron] appServer/appServer.ts')
const initAppServer = async () => {
const server = fastify({

View file

@ -1,9 +1,11 @@
import cache from '../../../cache'
import log from '@/desktop/main/log'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { pathCase, snakeCase } from 'change-case'
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
import NeteaseCloudMusicApi from 'NeteaseCloudMusicApi'
import cache from '../../../cache'
log.info('[electron] appServer/routes/netease.ts')
async function netease(fastify: FastifyInstance) {
const getHandler = (name: string, neteaseApi: (params: any) => any) => {

View file

@ -1,6 +1,9 @@
import { FastifyInstance } from 'fastify'
import proxy from '@fastify/http-proxy'
import { isDev } from '@/desktop/main/env'
import log from '@/desktop/main/log'
log.info('[electron] appServer/routes/r3play/appleMusic.ts')
async function appleMusic(fastify: FastifyInstance) {
fastify.register(proxy, {

View file

@ -11,6 +11,8 @@ import { FetchTracksResponse } from '@/shared/api/Track'
import store from '@/desktop/main/store'
import { db, Tables } from '@/desktop/main/db'
log.info('[electron] appServer/routes/r3play/audio.ts')
const getAudioFromCache = async (id: number) => {
// get from cache
const cache = await db.find(Tables.Audio, id)

View file

@ -8,6 +8,8 @@ import { CacheAPIs, CacheAPIsParams } from '@/shared/CacheAPIs'
import { TablesStructures } from './db'
import { FastifyReply } from 'fastify'
log.info('[electron] cache.ts')
class Cache {
constructor() {
//

View file

@ -9,6 +9,8 @@ import pkg from '../../../package.json'
import { compare, validate } from 'compare-versions'
import os from 'os'
log.info('[electron] db.ts')
export const enum Tables {
Track = 'Track',
Album = 'Album',
@ -108,7 +110,7 @@ class DB {
const prodBinPaths = {
darwin: path.resolve(app.getPath('exe'), `../../Resources/bin/better_sqlite3.node`),
win32: path.resolve(app.getPath('exe'), `../resources/bin/better_sqlite3.node`),
linux: '',
linux: path.resolve(app.getPath('exe'), `../resources/bin/better_sqlite3.node`),
}
return isProd
? prodBinPaths[os.platform as unknown as 'darwin' | 'win32' | 'linux']

View file

@ -13,6 +13,8 @@ import { isDev, isWindows, isLinux, isMac, appName } from './env'
import store from './store'
import initAppServer from './appServer/appServer'
log.info('[electron] index.ts')
class Main {
win: BrowserWindow | null = null
tray: YPMTray | null = null
@ -103,7 +105,7 @@ class Main {
// Make all links open with the browser, not with the application
this.win.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https:')) shell.openExternal(url)
if (url.startsWith('https://')) shell.openExternal(url)
return { action: 'deny' }
})

29
packages/desktop/main/ipcMain.ts Normal file → Executable file
View file

@ -14,6 +14,8 @@ import path from 'path'
import prettyBytes from 'pretty-bytes'
import { db, Tables } from './db'
log.info('[electron] ipcMain.ts')
const on = <T extends keyof IpcChannelsParams>(
channel: T,
listener: (event: Electron.IpcMainEvent, params: IpcChannelsParams[T]) => void
@ -50,9 +52,30 @@ function initWindowIpcMain(win: BrowserWindow | null) {
win?.minimize()
})
let isMaximized = false
let unMaximizeSize: { width: number; height: number } | null = null
let windowPosition: { x: number; y: number } | null = null
on(IpcChannels.MaximizeOrUnmaximize, () => {
if (!win) return
win.isMaximized() ? win.unmaximize() : win.maximize()
if (!win) return false
if (isMaximized) {
if (unMaximizeSize) {
win.setSize(unMaximizeSize.width, unMaximizeSize.width, true)
}
if (windowPosition) {
win.setPosition(windowPosition.x, windowPosition.y, true)
}
win.unmaximize()
} else {
const size = win.getSize()
unMaximizeSize = { width: size[1], height: size[0] }
const position = win.getPosition()
windowPosition = { x: position[0], y: position[1] }
win.maximize()
}
isMaximized = !isMaximized
win.webContents.send(IpcChannels.IsMaximized, isMaximized)
})
on(IpcChannels.Close, () => {
@ -66,7 +89,7 @@ function initWindowIpcMain(win: BrowserWindow | null) {
handle(IpcChannels.IsMaximized, () => {
if (!win) return
return win.isMaximized()
return isMaximized
})
}

View file

@ -1,5 +1,5 @@
/** By default, it writes logs to the following locations:
* on Linux: ~/.config/r3play/logs/main.log
* on Linux: ~/.config/R3PLAY/logs/main.log
* on macOS: ~/Library/Logs/r3play/main.log
* on Windows: %USERPROFILE%\AppData\Roaming\r3play\logs\main.log
* @see https://www.npmjs.com/package/electron-log

View file

@ -1,14 +1,10 @@
import {
app,
BrowserWindow,
Menu,
MenuItem,
MenuItemConstructorOptions,
shell,
} from 'electron'
import { app, BrowserWindow, Menu, MenuItem, MenuItemConstructorOptions, shell } from 'electron'
import { isMac } from './env'
import { logsPath } from './utils'
import { exec } from 'child_process'
import log from './log'
log.info('[electron] menu.ts')
export const createMenu = (win: BrowserWindow) => {
const template: Array<MenuItemConstructorOptions | MenuItem> = [
@ -51,9 +47,7 @@ export const createMenu = (win: BrowserWindow) => {
{
label: '反馈问题',
click: async () => {
await shell.openExternal(
'https://github.com/qier222/YesPlayMusic/issues/new'
)
await shell.openExternal('https://github.com/qier222/YesPlayMusic/issues/new')
},
},
{ type: 'separator' },
@ -66,17 +60,13 @@ export const createMenu = (win: BrowserWindow) => {
{
label: '访问论坛',
click: async () => {
await shell.openExternal(
'https://github.com/qier222/YesPlayMusic/discussions'
)
await shell.openExternal('https://github.com/qier222/YesPlayMusic/discussions')
},
},
{
label: '加入交流群',
click: async () => {
await shell.openExternal(
'https://github.com/qier222/YesPlayMusic/discussions'
)
await shell.openExternal('https://github.com/qier222/YesPlayMusic/discussions')
},
},
],

View file

@ -1,4 +1,7 @@
import Store from 'electron-store'
import log from './log'
log.info('[electron] store.ts')
export interface TypedElectronStore {
window: {

View file

@ -1,15 +1,11 @@
import path from 'path'
import {
app,
BrowserWindow,
Menu,
MenuItemConstructorOptions,
nativeImage,
Tray,
} from 'electron'
import { app, BrowserWindow, Menu, MenuItemConstructorOptions, nativeImage, Tray } from 'electron'
import { IpcChannels } from '@/shared/IpcChannels'
import { RepeatMode } from '@/shared/playerDataTypes'
import { appName } from './env'
import log from './log'
log.info('[electron] tray.ts')
const iconDirRoot =
process.env.NODE_ENV === 'development'

View file

@ -3,6 +3,9 @@ import path from 'path'
import os from 'os'
import pkg from '../../../package.json'
import { appName, isDev } from './env'
import log from './log'
log.info('[electron] utils.ts')
export const dirname = isDev ? process.cwd() : __dirname
export const devUserDataPath = path.resolve(process.cwd(), '../../tmp/userData')

View file

@ -1,6 +1,9 @@
import { IpcChannels } from '@/shared/IpcChannels'
import { BrowserWindow, nativeImage, ThumbarButton } from 'electron'
import path from 'path'
import log from './log'
log.info('[electron] windowsTaskbar.ts')
enum ItemKeys {
Play = 'play',
@ -66,15 +69,11 @@ class ThumbarImpl implements Thumbar {
}
private _updateThumbarButtons(clear: boolean) {
this._win.setThumbarButtons(
clear ? [] : [this._previous, this._playOrPause, this._next]
)
this._win.setThumbarButtons(clear ? [] : [this._previous, this._playOrPause, this._next])
}
setPlayState(isPlaying: boolean) {
this._playOrPause = this._buttons.get(
isPlaying ? ItemKeys.Pause : ItemKeys.Play
)!
this._playOrPause = this._buttons.get(isPlaying ? ItemKeys.Pause : ItemKeys.Play)!
this._updateThumbarButtons(false)
}
}