Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Commit

Permalink
播放器置顶
Browse files Browse the repository at this point in the history
  • Loading branch information
KinoriN committed Oct 15, 2019
1 parent f53e0ba commit 8c95dc1
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 176 deletions.
235 changes: 73 additions & 162 deletions electron/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,167 +1,37 @@
import { app, BrowserWindow, ipcMain, nativeImage, Menu, dialog } from 'electron';
import * as request from 'request';
import * as fs from 'fs';
import { join } from 'path';
import { autoUpdater } from 'electron-updater';
import { app, BrowserWindow, dialog, ipcMain } from 'electron';
import { VtbInfoService, FollowListService } from './services';
import { FollowList, VtbInfo } from '../../interfaces';
import { PlayerObj } from '../../interfaces';
import { createMainWinMenu } from './mainWinMenu';
const tempPath = app.getPath('temp');
let playerObjMap = new Map<number, PlayerObj>();
let win: BrowserWindow = null;
let vtbInfosService: VtbInfoService;
const mainWindowInit = new Promise<BrowserWindow>((resolve) => {
app.on('ready', () => {
autoUpdater.setFeedURL('https://dd.center/api/update/ddmonitor/');
// autoUpdater.setFeedURL('http://localhost:2333/api/update/ddmonitor/');
autoUpdater.checkForUpdates();
autoUpdater.autoDownload = false;
autoUpdater.on('update-available', () => {
autoUpdater.downloadUpdate();
})
autoUpdater.on('update-downloaded', (info) => {
dialog.showMessageBox(win, <any>{
type: 'info',
title: '发现新版本:' + info.version,
message: '发现新版本:' + info.version,
detail: '更新内容:' + info.releaseNotes,
buttons: ['退出并更新', '暂不更新'],
cancelId: 1
}).then(value => {
if (value.response == 0) {
autoUpdater.quitAndInstall();
}
})
})
resolve(createMainWindow());
});
});
const vtbInfosInit = new Promise<VtbInfoService>((resolve) => {
const vtbInfosService = new VtbInfoService();
setInterval(() => {
if (vtbInfosService.getVtbInfos().length > 0) {
resolve(vtbInfosService);
}
}, 50);
});
const createMainWindow = (): BrowserWindow => {
const win = new BrowserWindow({
width: 1250,
height: 850,
maximizable: false,
fullscreen: false,
fullscreenable: false,
resizable: false,
icon: 'public/icon.ico',
title: 'DD监控室',
webPreferences: {
nodeIntegration: true,
},
});
// win.loadURL('http://localhost:4200');
// win.webContents.openDevTools();
win.loadURL(`file://${__dirname}/../../app/index.html`);
const menu = createMainWinMenu(app, playerObjMap);
if (process.platform === 'darwin') {
Menu.setApplicationMenu(menu);
} else {
win.setMenu(menu);
}
win.on('close', () => {
playerObjMap.forEach((playerObj: PlayerObj) => {
playerObj.playerWin.close();
})
playerObjMap.clear();
app.quit();
});
return win;
}
const createPlayer = (cid: number): PlayerObj => {
const win = new BrowserWindow({
width: 640,
height: 360,
enableLargerThanScreen: true,
useContentSize: true,
icon: 'public/icon.ico',
title: vtbInfosService.getVtbInfos().find((vtbInfo: VtbInfo) => vtbInfo.roomid === cid).title,
webPreferences: {
nodeIntegration: true,
},
});

if (fs.existsSync(join(tempPath, `./faces/${cid}.jpg`))) {
win.setIcon(nativeImage.createFromPath(join(tempPath, `./faces/${cid}.jpg`)));
} else {
if (fs.existsSync(join(tempPath, './faces'))) {
request(vtbInfosService.getVtbInfos().find((vtbInfo: VtbInfo) => vtbInfo.roomid === cid).face).pipe(fs.createWriteStream(join(tempPath, `./faces/${cid}.jpg`))).on('close', () => {
win.setIcon(nativeImage.createFromPath(join(tempPath, `./faces/${cid}.jpg`)));
})
} else {
fs.mkdir(join(tempPath, './faces'), () => {
request(vtbInfosService.getVtbInfos().find((vtbInfo: VtbInfo) => vtbInfo.roomid === cid).face).pipe(fs.createWriteStream(join(tempPath, `./faces/${cid}.jpg`))).on('close', () => {
win.setIcon(nativeImage.createFromPath(join(tempPath, `./faces/${cid}.jpg`)));
})
})
}
}
import { autoUpdater } from 'electron-updater';
import { FollowList, VtbInfo } from '../../interfaces';
import { createMainWin } from './mainWindow';
import { createPlayerWin } from './playerWindow';

win.on('page-title-updated', (event: Electron.Event) => {
event.preventDefault();
let vtbInfosService: VtbInfoService;
let mainWin: BrowserWindow;
let playerObjMap = new Map<number, PlayerObj>();
const updateInit = () => {
autoUpdater.setFeedURL('https://dd.center/api/update/ddmonitor/');
autoUpdater.checkForUpdates();
autoUpdater.autoDownload = false;
autoUpdater.on('update-available', () => {
autoUpdater.downloadUpdate();
})
win.loadURL(`https://www.bilibili.com/blackboard/live/live-activity-player.html?enterTheRoom=0&cid=${cid}`).then(() => {
win.webContents.insertCSS('.bilibili-live-player-video-logo{display:none}')
});
win.setMenu(null);
win.on('close', () => {
if (playerObjMap.get(cid)) {
if (playerObjMap.get(cid).danmakuWin) {
playerObjMap.get(cid).danmakuWin.close();
}
}
playerObjMap.delete(cid);
});
return {
cid,
playerWin: win,
danmakuWin: null
}
}
(async () => {
vtbInfosService = await vtbInfosInit;
win = await mainWindowInit;
FollowListService.initFollowList();
let lastLiveVtbs: number[] = [];
vtbInfosService.onUpdate((vtbInfos) => {
const followVtbs = FollowListService.getFollowLists().map((followList: FollowList) => ([...followList.mids]))[0];
let nowLiveFollowedVtbs = vtbInfos.filter((vtbInfo: VtbInfo) => (followVtbs.includes(vtbInfo.mid) && vtbInfo.liveStatus)).map((vtbInfo: VtbInfo) => vtbInfo.mid);
let upLiveFollowedVtbs: number[] = [];
let downLiveFollowedVtbs: number[] = [];
nowLiveFollowedVtbs.forEach(nowLiveFollowedVtb => {
if (!lastLiveVtbs.includes(nowLiveFollowedVtb)) {
upLiveFollowedVtbs.push(nowLiveFollowedVtb)
}
});
lastLiveVtbs.forEach(lastLiveVtb => {
if (!nowLiveFollowedVtbs.includes(lastLiveVtb)) {
downLiveFollowedVtbs.push(lastLiveVtb);
autoUpdater.on('update-downloaded', (info) => {
dialog.showMessageBox(<any>{
type: 'info',
title: '发现新版本:' + info.version,
message: '发现新版本:' + info.version,
detail: '更新内容:' + info.releaseNotes,
buttons: ['退出并更新', '暂不更新'],
cancelId: 1
}).then(value => {
if (value.response == 0) {
autoUpdater.quitAndInstall();
}
})
upLiveFollowedVtbs.forEach((mid: number) => {
win.webContents.send('liveNotice', vtbInfos.find((vtbInfo: VtbInfo) => vtbInfo.mid == mid), "上播提醒")
})
downLiveFollowedVtbs.forEach((mid: number) => {
win.webContents.send('liveNotice', vtbInfos.find((vtbInfo: VtbInfo) => vtbInfo.mid == mid), "下播提醒")
})
lastLiveVtbs = nowLiveFollowedVtbs;
})
ipcMain.on('showPlayer', (event: Electron.IpcMainEvent, cid: number) => {
if (playerObjMap.has(cid)) {
playerObjMap.get(cid).playerWin.focus();
} else {
playerObjMap.set(cid, createPlayer(cid));
}
})
}
const ipcInit = () => {
ipcMain.on('getVtbInfos', (event: Electron.IpcMainEvent) => {
event.reply('getVtbInfosReply', vtbInfosService.getVtbInfos());
});
Expand Down Expand Up @@ -194,8 +64,49 @@ const createPlayer = (cid: number): PlayerObj => {
FollowListService.setFollowList(mids, listId)
event.reply('setFollowListReply', FollowListService.getFollowLists());;
});
})();




ipcMain.on('showPlayer', (event: Electron.IpcMainEvent, cid: number) => {
if (playerObjMap.has(cid)) {
playerObjMap.get(cid).playerWin.focus();
} else {
playerObjMap.set(cid, createPlayerWin(app, vtbInfosService.getVtbInfos().find((vtbInfo: VtbInfo) => vtbInfo.roomid == cid), playerObjMap, vtbInfosService));
}
})
}
const appInit = () => {
updateInit();
vtbInfosService = new VtbInfoService();
FollowListService.initFollowList();
let lastLiveVtbs: number[] = [];
vtbInfosService.onUpdate((vtbInfos) => {
if (mainWin) {
const followVtbs = FollowListService.getFollowLists().map((followList: FollowList) => ([...followList.mids]))[0];
let nowLiveFollowedVtbs = vtbInfos.filter((vtbInfo: VtbInfo) => (followVtbs.includes(vtbInfo.mid) && vtbInfo.liveStatus)).map((vtbInfo: VtbInfo) => vtbInfo.mid);
let upLiveFollowedVtbs: number[] = [];
let downLiveFollowedVtbs: number[] = [];
nowLiveFollowedVtbs.forEach(nowLiveFollowedVtb => {
if (!lastLiveVtbs.includes(nowLiveFollowedVtb)) {
upLiveFollowedVtbs.push(nowLiveFollowedVtb)
}
});
lastLiveVtbs.forEach(lastLiveVtb => {
if (!nowLiveFollowedVtbs.includes(lastLiveVtb)) {
downLiveFollowedVtbs.push(lastLiveVtb);
}
})
upLiveFollowedVtbs.forEach((mid: number) => {
mainWin.webContents.send('liveNotice', vtbInfos.find((vtbInfo: VtbInfo) => vtbInfo.mid == mid), "上播提醒")
})
downLiveFollowedVtbs.forEach((mid: number) => {
mainWin.webContents.send('liveNotice', vtbInfos.find((vtbInfo: VtbInfo) => vtbInfo.mid == mid), "下播提醒")
})
lastLiveVtbs = nowLiveFollowedVtbs;
}
})
ipcInit();
}
app.once('ready', () => {
appInit();
vtbInfosService.onceUpdate(() => {
createMainWin(app, playerObjMap)
})
})
19 changes: 12 additions & 7 deletions electron/src/mainWinMenu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Menu, screen } from 'electron';
import { PlayerObj } from '../../interfaces';
const createMainWinMenu = (app: Electron.App, players: Map<number, PlayerObj>) => {
export const createMainWinMenu = (app: Electron.App, players: Map<number, PlayerObj>) => {
const primaryDisplays: Electron.Display[] = screen.getAllDisplays();
const autoSetPlayerBounds = (display: Electron.Display) => {
const displayProportion = display.size.height / display.size.width;
Expand Down Expand Up @@ -35,7 +35,7 @@ const createMainWinMenu = (app: Electron.App, players: Map<number, PlayerObj>) =
})
}
}
const template = [// { role: 'appMenu' }
const template = [
...(process.platform === 'darwin' ? [{
label: app.getName(),
submenu: [
Expand All @@ -53,10 +53,18 @@ const createMainWinMenu = (app: Electron.App, players: Map<number, PlayerObj>) =
label: '播放器',
submenu: [
{
label: '置顶显示所有播放器',
label: '置顶显示当前所有播放器',
click: () => {
players.forEach((player: PlayerObj) => {
player.playerWin.focus();
player.playerWin.setAlwaysOnTop(true);
})
}
},
{
label: '取消置顶',
click: () => {
players.forEach((player: PlayerObj) => {
player.playerWin.setAlwaysOnTop(false);
})
}
},
Expand All @@ -82,7 +90,4 @@ const createMainWinMenu = (app: Electron.App, players: Map<number, PlayerObj>) =

];
return Menu.buildFromTemplate(<any>template);
}
export {
createMainWinMenu
}
34 changes: 34 additions & 0 deletions electron/src/mainWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { BrowserWindow, Menu } from "electron";
import { createMainWinMenu } from './mainWinMenu';
import { PlayerObj } from "../../interfaces";

export const createMainWin = (app: Electron.App, playerObjMap: Map<number, PlayerObj>): BrowserWindow => {
const win = new BrowserWindow({
width: 1250,
height: 850,
maximizable: false,
fullscreen: false,
fullscreenable: false,
resizable: false,
icon: 'public/icon.ico',
title: 'DD监控室',
webPreferences: {
nodeIntegration: true,
},
});
win.loadURL(`file://${__dirname}/../../app/index.html`);
const menu = createMainWinMenu(app, playerObjMap);
if (process.platform === 'darwin') {
Menu.setApplicationMenu(menu);
} else {
win.setMenu(menu);
}
win.on('close', () => {
playerObjMap.forEach((playerObj: PlayerObj) => {
playerObj.playerWin.close();
})
playerObjMap.clear();
app.quit();
});
return win;
}
57 changes: 57 additions & 0 deletions electron/src/playerWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { BrowserWindow, nativeImage } from 'electron';
import { PlayerObj } from '../../interfaces';
import * as request from 'request';
import * as fs from 'fs';
import { join } from 'path';
import { VtbInfoService } from './services';
import { VtbInfo } from '../../interfaces';

export const createPlayerWin = (app: Electron.App, playerInfo: VtbInfo, playerObjMap: Map<number, PlayerObj>, vtbInfosService: VtbInfoService): PlayerObj => {
const tempPath = app.getPath('temp');
const win = new BrowserWindow({
width: 640,
height: 360,
enableLargerThanScreen: true,
useContentSize: true,
icon: 'public/icon.ico',
title: playerInfo.title,
webPreferences: {
nodeIntegration: true,
},
});
if (fs.existsSync(join(tempPath, `./faces/${playerInfo.roomid}.jpg`))) {
win.setIcon(nativeImage.createFromPath(join(tempPath, `./faces/${playerInfo.roomid}.jpg`)));
} else {
if (fs.existsSync(join(tempPath, './faces'))) {
request(playerInfo.face).pipe(fs.createWriteStream(join(tempPath, `./faces/${playerInfo.roomid}.jpg`))).on('close', () => {
win.setIcon(nativeImage.createFromPath(join(tempPath, `./faces/${playerInfo.roomid}.jpg`)));
})
} else {
fs.mkdir(join(tempPath, './faces'), () => {
request(playerInfo.face).pipe(fs.createWriteStream(join(tempPath, `./faces/${playerInfo.roomid}.jpg`))).on('close', () => {
win.setIcon(nativeImage.createFromPath(join(tempPath, `./faces/${playerInfo.roomid}.jpg`)));
})
})
}
}
win.on('page-title-updated', (event: Electron.Event) => {
event.preventDefault();
})
win.loadURL(`https://www.bilibili.com/blackboard/live/live-activity-player.html?enterTheRoom=0&cid=${playerInfo.roomid}`).then(() => {
win.webContents.insertCSS('.bilibili-live-player-video-logo{display:none}')
});
win.setMenu(null);
win.on('close', () => {
if (playerObjMap.get(playerInfo.roomid)) {
if (playerObjMap.get(playerInfo.roomid).danmakuWin) {
playerObjMap.get(playerInfo.roomid).danmakuWin.close();
}
}
playerObjMap.delete(playerInfo.roomid);
});
return {
cid: playerInfo.roomid,
playerWin: win,
danmakuWin: null
}
}
Loading

0 comments on commit 8c95dc1

Please sign in to comment.