IPC: Move channel ids to their own file and make them constants

This commit is contained in:
Svallinn 2021-08-29 17:05:06 +01:00
parent 36b2885198
commit 672cffdc6b
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
9 changed files with 68 additions and 35 deletions

20
src/constants.js Normal file
View File

@ -0,0 +1,20 @@
// IPC Channels
const IpcChannels = {
ENABLE_PROXY: 'enable-proxy',
DISABLE_PROXY: 'disable-proxy',
OPEN_EXTERNAL_LINK: 'open-external-link',
GET_SYSTEM_LOCALE: 'get-system-locale',
GET_USER_DATA_PATH: 'get-user-data-path',
GET_USER_DATA_PATH_SYNC: 'get-user-data-path-sync',
SHOW_OPEN_DIALOG: 'show-open-dialog',
SHOW_SAVE_DIALOG: 'show-save-dialog',
STOP_POWER_SAVE_BLOCKER: 'stop-power-save-blocker',
START_POWER_SAVE_BLOCKER: 'start-power-save-blocker',
CREATE_NEW_WINDOW: 'create-new-window',
SYNC_WINDOWS: 'sync-windows',
OPEN_IN_EXTERNAL_PLAYER: 'open-in-external-player'
}
export {
IpcChannels
}

View File

@ -6,6 +6,8 @@ import Datastore from 'nedb-promises'
import path from 'path'
import cp from 'child_process'
import { IpcChannels } from '../constants'
if (process.argv.includes('--version')) {
console.log(`v${app.getVersion()}`)
app.exit()
@ -319,54 +321,54 @@ function runApp() {
app.quit()
})
ipcMain.on('enableProxy', (_, url) => {
ipcMain.on(IpcChannels.ENABLE_PROXY, (_, url) => {
console.log(url)
session.defaultSession.setProxy({
proxyRules: url
})
})
ipcMain.on('disableProxy', () => {
ipcMain.on(IpcChannels.DISABLE_PROXY, () => {
session.defaultSession.setProxy({})
})
ipcMain.on('openExternalLink', (_, url) => {
ipcMain.on(IpcChannels.OPEN_EXTERNAL_LINK, (_, url) => {
if (typeof url === 'string') shell.openExternal(url)
})
ipcMain.handle('getSystemLocale', () => {
ipcMain.handle(IpcChannels.GET_SYSTEM_LOCALE, () => {
return app.getLocale()
})
ipcMain.handle('getUserDataPath', () => {
ipcMain.handle(IpcChannels.GET_USER_DATA_PATH, () => {
return app.getPath('userData')
})
ipcMain.on('getUserDataPathSync', (event) => {
ipcMain.on(IpcChannels.GET_USER_DATA_PATH_SYNC, (event) => {
event.returnValue = app.getPath('userData')
})
ipcMain.handle('showOpenDialog', async (_, options) => {
ipcMain.handle(IpcChannels.SHOW_OPEN_DIALOG, async (_, options) => {
return await dialog.showOpenDialog(options)
})
ipcMain.handle('showSaveDialog', async (_, options) => {
ipcMain.handle(IpcChannels.SHOW_SAVE_DIALOG, async (_, options) => {
return await dialog.showSaveDialog(options)
})
ipcMain.on('stopPowerSaveBlocker', (_, id) => {
ipcMain.on(IpcChannels.STOP_POWER_SAVE_BLOCKER, (_, id) => {
powerSaveBlocker.stop(id)
})
ipcMain.handle('startPowerSaveBlocker', (_, type) => {
return powerSaveBlocker.start(type)
ipcMain.handle(IpcChannels.START_POWER_SAVE_BLOCKER, (_) => {
return powerSaveBlocker.start('prevent-display-sleep')
})
ipcMain.on('createNewWindow', () => {
ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, () => {
createWindow(false)
})
ipcMain.on('syncWindows', (event, payload) => {
ipcMain.on(IpcChannels.SYNC_WINDOWS, (event, payload) => {
const otherWindows = BrowserWindow.getAllWindows().filter(
(window) => {
return window.webContents.id !== event.sender.id
@ -374,11 +376,11 @@ function runApp() {
)
for (const window of otherWindows) {
window.webContents.send('syncWindows', payload)
window.webContents.send(IpcChannels.SYNC_WINDOWS, payload)
}
})
ipcMain.on('openInExternalPlayer', (_, payload) => {
ipcMain.on(IpcChannels.OPEN_IN_EXTERNAL_PLAYER, (_, payload) => {
const child = cp.spawn(payload.executable, payload.args, { detached: true, stdio: 'ignore' })
child.unref()
})

View File

@ -12,6 +12,8 @@ import 'videojs-vtt-thumbnails-freetube'
import 'videojs-contrib-quality-levels'
import 'videojs-http-source-selector'
import { IpcChannels } from '../../../constants'
export default Vue.extend({
name: 'FtVideoPlayer',
components: {
@ -31,7 +33,7 @@ export default Vue.extend({
if (this.usingElectron && this.powerSaveBlocker !== null) {
const { ipcRenderer } = require('electron')
ipcRenderer.send('stopPowerSaveBlocker', this.powerSaveBlocker)
ipcRenderer.send(IpcChannels.STOP_POWER_SAVE_BLOCKER, this.powerSaveBlocker)
}
},
props: {
@ -217,7 +219,7 @@ export default Vue.extend({
if (this.usingElectron && this.powerSaveBlocker !== null) {
const { ipcRenderer } = require('electron')
ipcRenderer.send('stopPowerSaveBlocker', this.powerSaveBlocker)
ipcRenderer.send(IpcChannels.STOP_POWER_SAVE_BLOCKER, this.powerSaveBlocker)
}
},
methods: {
@ -318,14 +320,14 @@ export default Vue.extend({
if (this.usingElectron) {
const { ipcRenderer } = require('electron')
this.powerSaveBlocker =
await ipcRenderer.invoke('startPowerSaveBlocker', 'prevent-display-sleep')
await ipcRenderer.invoke(IpcChannels.START_POWER_SAVE_BLOCKER)
}
})
this.player.on('pause', function () {
if (this.usingElectron && this.powerSaveBlocker !== null) {
const { ipcRenderer } = require('electron')
ipcRenderer.send('stopPowerSaveBlocker', this.powerSaveBlocker)
ipcRenderer.send(IpcChannels.STOP_POWER_SAVE_BLOCKER, this.powerSaveBlocker)
this.powerSaveBlocker = null
}
})

View File

@ -14,6 +14,8 @@ import FtFlexBox from '../ft-flex-box/ft-flex-box.vue'
import { ipcRenderer } from 'electron'
import debounce from 'lodash.debounce'
import { IpcChannels } from '../../../constants'
export default Vue.extend({
name: 'ProxySettings',
components: {
@ -111,11 +113,11 @@ export default Vue.extend({
},
enableProxy: function () {
ipcRenderer.send('enableProxy', this.proxyUrl)
ipcRenderer.send(IpcChannels.ENABLE_PROXY, this.proxyUrl)
},
disableProxy: function () {
ipcRenderer.send('disableProxy')
ipcRenderer.send(IpcChannels.DISABLE_PROXY)
},
testProxy: function () {

View File

@ -7,6 +7,8 @@ import $ from 'jquery'
import debounce from 'lodash.debounce'
import ytSuggest from 'youtube-suggest'
import { IpcChannels } from '../../../constants'
export default Vue.extend({
name: 'TopNav',
components: {
@ -303,7 +305,7 @@ export default Vue.extend({
createNewWindow: function () {
if (this.usingElectron) {
const { ipcRenderer } = require('electron')
ipcRenderer.send('createNewWindow')
ipcRenderer.send(IpcChannels.CREATE_NEW_WINDOW)
} else {
// Web placeholder
}

View File

@ -1,5 +1,7 @@
import Datastore from 'nedb-promises'
import { IpcChannels } from '../../constants'
// Initialize all datastores and export their references
// Current dbs:
// `settings.db`
@ -13,7 +15,7 @@ let buildFileName = null
const usingElectron = window?.process?.type === 'renderer'
if (usingElectron) {
const { ipcRenderer } = require('electron')
const userDataPath = ipcRenderer.sendSync('getUserDataPathSync')
const userDataPath = ipcRenderer.sendSync(IpcChannels.GET_USER_DATA_PATH_SYNC)
buildFileName = (dbName) => userDataPath + '/' + dbName + '.db'
} else {
buildFileName = (dbName) => dbName + '.db'

View File

@ -1,5 +1,7 @@
import { historyDb } from '../datastores'
import { IpcChannels } from '../../../constants'
const state = {
historyCache: []
}
@ -48,7 +50,7 @@ const actions = {
propagateHistory({ getters: { getUsingElectron: usingElectron } }) {
if (usingElectron) {
const { ipcRenderer } = require('electron')
ipcRenderer.send('syncWindows', {
ipcRenderer.send(IpcChannels.SYNC_WINDOWS, {
type: 'history',
data: state.historyCache
})

View File

@ -1,5 +1,6 @@
import { settingsDb } from '../datastores'
import i18n from '../../i18n/index'
import { IpcChannels } from '../../../constants'
/*
* Due to the complexity of the settings module in FreeTube, a more
@ -329,7 +330,7 @@ const customActions = {
setupListenerToSyncWindows: ({ commit, dispatch, getters }) => {
// Already known to be Electron, no need to check
const { ipcRenderer } = require('electron')
ipcRenderer.on('syncWindows', (_, payload) => {
ipcRenderer.on(IpcChannels.SYNC_WINDOWS, (_, payload) => {
const { type, data } = payload
switch (type) {
case 'setting':
@ -417,7 +418,7 @@ for (const settingId of Object.keys(state)) {
const { ipcRenderer } = require('electron')
// Propagate settings to all other existing windows
ipcRenderer.send('syncWindows', {
ipcRenderer.send(IpcChannels.SYNC_WINDOWS, {
type: 'setting',
data: { _id: settingId, value: value }
})

View File

@ -1,6 +1,9 @@
import IsEqual from 'lodash.isequal'
import FtToastEvents from '../../components/ft-toast/ft-toast-events'
import fs from 'fs'
import { IpcChannels } from '../../../constants'
const state = {
isSideNavOpen: false,
sessionSearchHistory: [],
@ -152,7 +155,7 @@ const actions = {
const usingElectron = rootState.settings.usingElectron
if (usingElectron) {
const ipcRenderer = require('electron').ipcRenderer
ipcRenderer.send('openExternalLink', url)
ipcRenderer.send(IpcChannels.OPEN_EXTERNAL_LINK, url)
} else {
// Web placeholder
}
@ -165,25 +168,25 @@ const actions = {
}
}
return (await invokeIRC(context, 'getSystemLocale', webCbk)) || 'en-US'
return (await invokeIRC(context, IpcChannels.GET_SYSTEM_LOCALE, webCbk)) || 'en-US'
},
async showOpenDialog (context, options) {
// TODO: implement showOpenDialog web compatible callback
const webCbk = () => null
return await invokeIRC(context, 'showOpenDialog', webCbk, options)
return await invokeIRC(context, IpcChannels.SHOW_OPEN_DIALOG, webCbk, options)
},
async showSaveDialog (context, options) {
// TODO: implement showSaveDialog web compatible callback
const webCbk = () => null
return await invokeIRC(context, 'showSaveDialog', webCbk, options)
return await invokeIRC(context, IpcChannels.SHOW_SAVE_DIALOG, webCbk, options)
},
async getUserDataPath (context) {
// TODO: implement getUserDataPath web compatible callback
const webCbk = () => null
return await invokeIRC(context, 'getUserDataPath', webCbk)
return await invokeIRC(context, IpcChannels.GET_USER_DATA_PATH, webCbk)
},
updateShowProgressBar ({ commit }, value) {
@ -798,10 +801,7 @@ const actions = {
console.log(executable, args)
const { ipcRenderer } = require('electron')
ipcRenderer.send('openInExternalPlayer', {
executable,
args
})
ipcRenderer.send(IpcChannels.OPEN_IN_EXTERNAL_PLAYER, { executable, args })
}
}