|
| 1 | +const ChainEventEmitter = require('./src/ChainEventEmitter') |
| 2 | +const EventEmitter = require('events') |
| 3 | +const { app, BrowserWindow, ipcMain } = require('electron') |
| 4 | +const path = require('path'); |
| 5 | +const ChildProcess = require('child_process'); |
| 6 | +const reLivePlayer = require('./src/reLivePlayer') |
| 7 | +const isDev = require('electron-is-dev'); |
| 8 | + |
| 9 | +const uiEventEmitter = (webContents) => { |
| 10 | + return { |
| 11 | + send: (channel, args) => { |
| 12 | + if ((typeof webContents.send) === 'function') { |
| 13 | + webContents.send(channel, args) |
| 14 | + // console.log('event send ', channel, args) |
| 15 | + } else { |
| 16 | + console.log('can not send event') |
| 17 | + } |
| 18 | + }, |
| 19 | + on: (channel, callable) => { |
| 20 | + ipcMain.on(channel, function (event, args) { |
| 21 | + // console.log('event received ', channel) |
| 22 | + callable(args, event) |
| 23 | + }) |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const stdEventEmitter = () => { |
| 29 | + const eventEmitter = new EventEmitter() |
| 30 | + return { |
| 31 | + send: eventEmitter.emit, |
| 32 | + on: eventEmitter.on, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const start = (webContents) => { |
| 37 | + const eventEmitter = new ChainEventEmitter() |
| 38 | + eventEmitter.add(stdEventEmitter()) |
| 39 | + eventEmitter.add(uiEventEmitter(webContents)) |
| 40 | + |
| 41 | + |
| 42 | + const player = new reLivePlayer( |
| 43 | + __dirname, |
| 44 | + eventEmitter |
| 45 | + ) |
| 46 | + player.start() |
| 47 | +} |
| 48 | + |
| 49 | +const createWindow = () => { |
| 50 | + const mainWindow = new BrowserWindow({ |
| 51 | + minWidth:600, |
| 52 | + minHeight: 500, |
| 53 | + maxHeight:900, |
| 54 | + maxWidth: 730, |
| 55 | + width: 730, |
| 56 | + height: 900, |
| 57 | + autoHideMenuBar: true, |
| 58 | + webPreferences: { |
| 59 | + preload: path.join(__dirname, 'ui', 'preload.js') |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + mainWindow.loadFile('ui/index.html').then(() => { |
| 64 | + isDev && mainWindow.webContents.openDevTools() |
| 65 | + start(mainWindow.webContents) |
| 66 | + }) |
| 67 | + .catch((err) => console.error(err)) |
| 68 | +} |
| 69 | + |
| 70 | +app.whenReady().then(() => { |
| 71 | + createWindow() |
| 72 | + // to do config loader |
| 73 | + app.on('activate', () => { |
| 74 | + if (BrowserWindow.getAllWindows().length === 0) createWindow() |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +app.on('window-all-closed', () => { |
| 79 | + if (process.platform !== 'darwin') { |
| 80 | + stdEventEmitter().send('app.kill') |
| 81 | + app.quit() |
| 82 | + } |
| 83 | +}) |
| 84 | + |
0 commit comments