Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Refactor IPC message names into a separate file for maintainability #67

Merged
merged 3 commits into from
Dec 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const BrowserWindow = electron.BrowserWindow
const Menu = require('./menu')
const LocalShortcuts = require('./localShortcuts')
const Updater = require('./updater')
const messages = require('../js/constants/messages')

// Report crashes
electron.crashReporter.start()
Expand Down Expand Up @@ -57,23 +58,23 @@ const spawnWindow = () => {
app.on('ready', function () {
windows.push(spawnWindow())

ipcMain.on('quit-application', () => {
ipcMain.on(messages.QUIT_APPLICATION, () => {
app.quit()
})

ipcMain.on('context-menu-opened', (e, nodeName) => {
BrowserWindow.getFocusedWindow().webContents.send('context-menu-opened', nodeName)
ipcMain.on(messages.CONTEXT_MENU_OPENED, (e, nodeName) => {
BrowserWindow.getFocusedWindow().webContents.send(messages.CONTEXT_MENU_OPENED, nodeName)
})

ipcMain.on('new-window', () => windows.push(spawnWindow()))
process.on('new-window', () => windows.push(spawnWindow()))
ipcMain.on(messages.NEW_WINDOW, () => windows.push(spawnWindow()))
process.on(messages.NEW_WINDOW, () => windows.push(spawnWindow()))

ipcMain.on('close-window', () => BrowserWindow.getFocusedWindow().close())
process.on('close-window', () => BrowserWindow.getFocusedWindow().close())
ipcMain.on(messages.CLOSE_WINDOW, () => BrowserWindow.getFocusedWindow().close())
process.on(messages.CLOSE_WINDOW, () => BrowserWindow.getFocusedWindow().close())

Menu.init()

ipcMain.on('update-requested', () => {
ipcMain.on(messages.UPDATE_REQUESTED, () => {
Updater.update()
})

Expand All @@ -82,8 +83,8 @@ app.on('ready', function () {
Updater.init(process.platform)

// this is fired by the menu entry
process.on('check-for-update', () => Updater.checkForUpdate())
process.on(messages.CHECK_FOR_UPDATE, () => Updater.checkForUpdate())
} else {
process.on('check-for-update', () => Updater.fakeCheckForUpdate())
process.on(messages.CHECK_FOR_UPDATE, () => Updater.fakeCheckForUpdate())
}
})
29 changes: 15 additions & 14 deletions app/localShortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const electronLocalshortcut = require('electron-localshortcut')
const messages = require('../js/constants/messages')

module.exports.register = (win) => {
// Most of these events will simply be listened to by the app store and acted
// upon. However sometimes there are no state changes, for example with focusing
// the URL bar. In those cases it's acceptable for the individual components to
// listen to the events.
const simpleWebContentEvents = [
['CmdOrCtrl+L', 'shortcut-focus-url'],
['Ctrl+Tab', 'shortcut-next-tab'],
['Ctrl+Shift+Tab', 'shortcut-prev-tab'],
['CmdOrCtrl+Shift+]', 'shortcut-next-tab'],
['CmdOrCtrl+Shift+[', 'shortcut-prev-tab'],
['CmdOrCtrl+9', 'shortcut-set-active-frame-to-last'],
['CmdOrCtrl+Shift+T', 'shortcut-undo-closed-frame'],
['Escape', 'shortcut-active-frame-stop'],
['CmdOrCtrl+R', 'shortcut-active-frame-reload'],
['CmdOrCtrl+=', 'shortcut-active-frame-zoom-in'],
['CmdOrCtrl+-', 'shortcut-active-frame-zoom-out'],
['CmdOrCtrl+0', 'shortcut-active-frame-zoom-reset'],
['CmdOrCtrl+Alt+I', 'shortcut-active-frame-toggle-dev-tools']
['CmdOrCtrl+L', messages.SHORTCUT_FOCUS_URL],
['Ctrl+Tab', messages.SHORTCUT_NEXT_TAB],
['Ctrl+Shift+Tab', messages.SHORTCUT_PREV_TAB],
['CmdOrCtrl+Shift+]', messages.SHORTCUT_NEXT_TAB],
['CmdOrCtrl+Shift+[', messages.SHORTCUT_PREV_TAB],
['CmdOrCtrl+9', messages.SHORTCUT_SET_ACTIVE_FRAME_TO_LAST],
['CmdOrCtrl+Shift+T', messages.SHORTCUT_UNDO_CLOSED_FRAME],
['Escape', messages.SHORTCUT_ACTIVE_FRAME_STOP],
['CmdOrCtrl+R', messages.SHORTCUT_ACTIVE_FRAME_RELOAD],
['CmdOrCtrl+=', messages.SHORTCUT_ACTIVE_FRAME_ZOOM_IN],
['CmdOrCtrl+-', messages.SHORTCUT_ACTIVE_FRAME_ZOOM_OUT],
['CmdOrCtrl+0', messages.SHORTCUT_ACTIVE_FRAME_ZOOM_RESET],
['CmdOrCtrl+Alt+I', messages.SHORTCUT_ACTIVE_FRAME_TOGGLE_DEV_TOOLS]
]

// Tab ordering shortcuts
Array.from(new Array(8), (x, i) => i).reduce((list, i) => {
list.push(['CmdOrCtrl+' + String(i + 1), 'shortcut-set-active-frame-by-index', i])
list.push(['CmdOrCtrl+' + String(i + 1), messages.SHORTCUT_SET_ACTIVE_FRAME_BY_INDEX, i])
return list
}, simpleWebContentEvents)

Expand Down
27 changes: 14 additions & 13 deletions app/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const electron = require('electron')
const app = electron.app
const Menu = require('menu')
const messages = require('../js/constants/messages')

const init = () => {
var template = [
Expand All @@ -14,34 +15,34 @@ const init = () => {
{
label: 'Check for updates ...',
click: function (item, focusedWindow) {
process.emit('check-for-update')
process.emit(messages.CHECK_FOR_UPDATE)
}
},
{
label: 'New Tab',
accelerator: 'CmdOrCtrl+T',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send('shortcut-new-frame')
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME)
} else {
// no active windows
process.emit('new-window')
process.emit(messages.NEW_WINDOW)
}
}
}, {
label: 'New Private Tab',
accelerator: 'CmdOrCtrl+Alt+T',
click: function (item, focusedWindow) {
focusedWindow.webContents.send('shortcut-new-frame')
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME)
}
}, {
label: 'New Window',
accelerator: 'CmdOrCtrl+N',
click: () => process.emit('new-window')
click: () => process.emit(messages.NEW_WINDOW)
}, {
label: 'New Private Window',
accelerator: 'CmdOrCtrl+Alt+N',
click: () => process.emit('new-window')
click: () => process.emit(messages.NEW_WINDOW)
}, {
type: 'separator'
}, {
Expand All @@ -68,7 +69,7 @@ const init = () => {
accelerator: 'CmdOrCtrl+W',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send('shortcut-close-frame')
focusedWindow.webContents.send(messages.SHORTCUT_CLOSE_FRAME)
}
}
}, {
Expand All @@ -78,7 +79,7 @@ const init = () => {
accelerator: 'CmdOrCtrl+Shift+W',
click: function (item, focusedWindow) {
if (focusedWindow) {
process.emit('close-window')
process.emit(messages.CLOSE_WINDOW)
}
}
}, {
Expand Down Expand Up @@ -225,7 +226,7 @@ const init = () => {
accelerator: 'CmdOrCtrl+Alt+I',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send('shortcut-active-frame-toggle-dev-tools')
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_TOGGLE_DEV_TOOLS)
}
}
}, {
Expand Down Expand Up @@ -404,21 +405,21 @@ const init = () => {
{
label: 'Brave Help',
click: function (item, focusedWindow) {
focusedWindow.webContents.send('shortcut-new-frame',
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
}
}, {
type: 'separator'
}, {
label: 'Submit Feedback...',
click: function (item, focusedWindow) {
focusedWindow.webContents.send('shortcut-new-frame',
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
}
}, {
label: 'Spread the word about Brave...',
click: function (item, focusedWindow) {
focusedWindow.webContents.send('shortcut-new-frame',
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
}
}
Expand All @@ -444,7 +445,7 @@ const init = () => {
}, {
label: 'Send us Feedback...',
click: function (item, focusedWindow) {
focusedWindow.webContents.send('shortcut-new-frame',
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
}
}, {
Expand Down
11 changes: 6 additions & 5 deletions app/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const path = require('path')
const fs = require('fs')
const autoUpdater = require('auto-updater')
const config = require('./appConfig')
const messages = require('../js/constants/messages')

// this maps the result of a call to process.platform to an update API identifier
var platforms = {
Expand Down Expand Up @@ -37,22 +38,22 @@ exports.checkForUpdate = () => {

// development version only
exports.fakeCheckForUpdate = () => {
BrowserWindow.getFocusedWindow().webContents.send('update-available')
BrowserWindow.getFocusedWindow().webContents.send(messages.UPDATE_AVAILABLE)
}

exports.update = () => {
console.log('update requested in updater')
autoUpdater.quitAndInstall()
}

autoUpdater.on('update-downloaded', (evt) => {
BrowserWindow.getFocusedWindow().webContents.send('update-available')
autoUpdater.on(messages.UPDATE_DOWNLOADED, (evt) => {
BrowserWindow.getFocusedWindow().webContents.send(messages.UPDATE_AVAILABLE)
})

autoUpdater.on('update-available', (evt) => {
autoUpdater.on(messages.UPDATE_AVAILABLE, (evt) => {
console.log('update downloading')
})

autoUpdater.on('update-not-available', (evt) => {
autoUpdater.on(messages.UPDATE_NOT_AVAILABLE, (evt) => {
console.log('update is not available')
})
9 changes: 5 additions & 4 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Config = require('../constants/config')
const UrlUtil = require('../../node_modules/urlutil.js/dist/node-urlutil.js')
const AppStore = require('../stores/appStore')
const ipc = global.require('electron').ipcRenderer
const messages = require('../constants/messages')

const AppActions = {
/**
Expand Down Expand Up @@ -135,7 +136,7 @@ const AppActions = {
* Dispatches an event to the main process to create a new window
*/
newWindow: function () {
ipc.send('new-window')
ipc.send(messages.NEW_WINDOW)
},

closeFrame: function (frameProps) {
Expand All @@ -153,15 +154,15 @@ const AppActions = {
* Dispatches an event to the main process to close the current window
*/
closeWindow: function () {
ipc.send('close-window')
ipc.send(messages.CLOSE_WINDOW)
},

/**
* Dispatches an event to the main process to update the browser
*/
updateRequested: function () {
console.log('appActions updateRequested')
ipc.send('update-requested')
ipc.send(messages.UPDATE_REQUESTED)
},

/**
Expand All @@ -178,7 +179,7 @@ const AppActions = {
* Dispatches an event to the main process to quit the entire application
*/
quitApplication: function () {
ipc.send('quit-application')
ipc.send(messages.QUIT_APPLICATION)
},

/**
Expand Down
15 changes: 8 additions & 7 deletions js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const UpdateBar = require('./updateBar')

// Constants
const Config = require('../constants/config')
const messages = require('../constants/messages')

// State handling
const FrameStateUtil = require('../state/frameStateUtil')
Expand All @@ -33,29 +34,29 @@ class Main extends ImmutableComponent {
})
}

ipc.on('context-menu-opened', (e, nodeName) => {
ipc.on(messages.CONTEXT_MENU_OPENED, (e, nodeName) => {
console.log('got context menu open', nodeName)
contextMenus.onMainContextMenu(nodeName)
})
ipc.on('shortcut-new-frame', (event, url) => {
ipc.on(messages.SHORTCUT_NEW_FRAME, (event, url) => {
AppActions.newFrame({
location: url || Config.defaultUrl
})

// Focus URL bar when adding tab via shortcut
electron.remote.getCurrentWebContents().send('shortcut-focus-url')
electron.remote.getCurrentWebContents().send(messages.SHORTCUT_FOCUS_URL)
})

ipc.on('shortcut-close-frame', (e, i) => typeof i !== 'undefined'
ipc.on(messages.SHORTCUT_CLOSE_FRAME, (e, i) => typeof i !== 'undefined'
? AppActions.closeFrame(FrameStateUtil.getFrameByKey(self.props.browser, i))
: AppActions.closeFrame())
ipc.on('shortcut-undo-closed-frame', () => AppActions.undoClosedFrame())
ipc.on(messages.SHORTCUT_UNDO_CLOSED_FRAME, () => AppActions.undoClosedFrame())

const self = this
ipc.on('shortcut-set-active-frame-by-index', (e, i) =>
ipc.on(messages.SHORTCUT_SET_ACTIVE_FRAME_BY_INDEX, (e, i) =>
AppActions.setActiveFrame(FrameStateUtil.getFrameByIndex(self.props.browser, i)))

ipc.on('shortcut-set-active-frame-to-last', () =>
ipc.on(messages.SHORTCUT_SET_ACTIVE_FRAME_TO_LAST, () =>
AppActions.setActiveFrame(self.props.browser.getIn(['frames', self.props.browser.get('frames').size - 1])))

loadOpenSearch().then(searchDetail => AppActions.setSearchDetail(searchDetail))
Expand Down
7 changes: 4 additions & 3 deletions js/components/urlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ const cx = require('../lib/classSet.js')
const ipc = global.require('electron').ipcRenderer

const UrlBarSuggestions = require('./urlBarSuggestions.js')
const messages = require('../constants/messages')

import {isUrl} from '../lib/appUrlUtil.js'

class UrlBar extends ImmutableComponent {
constructor () {
super()
ipc.on('shortcut-focus-url', () => {
ipc.on(messages.SHORTCUT_FOCUS_URL, () => {
AppActions.setNavBarFocused(true)
AppActions.setUrlBarAutoselected(true)
})
// escape key handling
ipc.on('shortcut-active-frame-stop', () => {
ipc.on(messages.SHORTCUT_ACTIVE_FRAME_STOP, () => {
this.restore()
AppActions.setUrlBarAutoselected(true)
AppActions.setUrlBarActive(true)
})
process.on('focus-urlbar', () => {
process.on(messages.FOCUS_URLBAR, () => {
this.updateDOMInputFocus(true)
})
}
Expand Down
Loading