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

More app menu work-in-progress #84

Merged
merged 6 commits into from
Dec 23, 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
15 changes: 10 additions & 5 deletions app/content/webviewPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,35 @@

var webFrame = require('electron').webFrame
var ipc = require('electron').ipcRenderer
var messages = require('../../js/constants/messages')

var browserZoomLevel = 0
var browserMaxZoom = 9
var browserMinZoom = -8

ipc.on('zoom-in', function () {
ipc.on(messages.ZOOM_IN, function () {
if (browserMaxZoom > browserZoomLevel) {
browserZoomLevel += 1
}
webFrame.setZoomLevel(browserZoomLevel)
})

ipc.on('zoom-out', function () {
ipc.on(messages.ZOOM_OUT, function () {
if (browserMinZoom < browserZoomLevel) {
browserZoomLevel -= 1
}
webFrame.setZoomLevel(browserZoomLevel)
})

ipc.on('zoom-reset', function () {
ipc.on(messages.ZOOM_RESET, function () {
browserZoomLevel = 0
webFrame.setZoomLevel(browserZoomLevel)
})

ipc.on(messages.PRINT_PAGE, function () {
window.print()
})

/**
* Ensures a node replacement div is visible and has a proper zIndex
*/
Expand Down Expand Up @@ -130,7 +135,7 @@ function processAdNode (node, iframeData, replacementUrl) {
}

// Fires when the browser has ad replacement information to give
ipc.on('set-ad-div-candidates', function (e, adDivCandidates, placeholderUrl) {
ipc.on(messages.SET_AD_DIV_CANDIDATES, function (e, adDivCandidates, placeholderUrl) {
// Keep a lookup for skipped common elements
var fallbackNodeDataForCommon = {}

Expand Down Expand Up @@ -171,6 +176,6 @@ ipc.on('set-ad-div-candidates', function (e, adDivCandidates, placeholderUrl) {
})

document.addEventListener('contextmenu', (e) => {
ipc.send('context-menu-opened', e.target.nodeName)
ipc.send(messages.CONTEXT_MENU_OPENED, e.target.nodeName)
e.preventDefault()
}, false)
1 change: 0 additions & 1 deletion app/localShortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports.register = (win) => {
// the URL bar. In those cases it's acceptable for the individual components to
// listen to the events.
const simpleWebContentEvents = [
['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],
Expand Down
94 changes: 57 additions & 37 deletions app/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ const electron = require('electron')
const app = electron.app
const Menu = require('menu')
const messages = require('../js/constants/messages')
const dialog = electron.dialog

/**
* Sends a message to the web contents of the focused window.
* @param {Object} focusedWindow the focusedWindow if any
* @param {Array} message message and arguments to send
* @return {boolean} whether the message was sent
*/
const sendToFocusedWindow = (focusedWindow, message) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I've been wanting to do this, so much cleaner.

if (focusedWindow) {
focusedWindow.webContents.send.apply(focusedWindow.webContents, message)
return true
} else {
return false
}
}

const init = () => {
var template = [
Expand All @@ -22,9 +38,7 @@ const init = () => {
label: 'New Tab',
accelerator: 'CmdOrCtrl+T',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME)
} else {
if (!sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_NEW_FRAME])) {
// no active windows
process.emit(messages.NEW_WINDOW)
}
Expand All @@ -33,7 +47,7 @@ const init = () => {
label: 'New Private Tab',
accelerator: 'CmdOrCtrl+Alt+T',
click: function (item, focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME)
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_NEW_FRAME])
}
}, {
label: 'New Window',
Expand All @@ -47,10 +61,24 @@ const init = () => {
type: 'separator'
}, {
label: 'Open File...',
accelerator: 'CmdOrCtrl+O'
accelerator: 'CmdOrCtrl+O',
click: (item, focusedWindow) => {
dialog.showOpenDialog(focusedWindow, {
properties: ['openFile', 'multiSelections']
}, function (paths) {
if (paths) {
paths.forEach((path) => {
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_NEW_FRAME, path])
})
}
})
}
}, {
label: 'Open Location...',
accelerator: 'CmdOrCtrl+L'
accelerator: 'CmdOrCtrl+L',
click: function (item, focusedWindow) {
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_FOCUS_URL])
}
}, {
type: 'separator'
}, {
Expand All @@ -68,9 +96,7 @@ const init = () => {
label: 'Close Tab',
accelerator: 'CmdOrCtrl+W',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_CLOSE_FRAME)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_CLOSE_FRAME])
}
}, {
// this should be disabled when
Expand All @@ -86,7 +112,10 @@ const init = () => {
type: 'separator'
}, {
label: 'Save Page As...',
accelerator: 'CmdOrCtrl+S'
accelerator: 'CmdOrCtrl+S',
click: function (item, focusedWindow) {
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_SAVE])
}
}, {
label: 'Share...',
submenu: [
Expand All @@ -100,7 +129,10 @@ const init = () => {
type: 'separator'
}, {
label: 'Print...',
accelerator: 'CmdOrCtrl+P'
accelerator: 'CmdOrCtrl+P',
click: function (item, focusedWindow) {
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_PRINT])
}
}
]
}, {
Expand Down Expand Up @@ -164,25 +196,19 @@ const init = () => {
label: 'Actual Size',
accelerator: 'CmdOrCtrl+0',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_ZOOM_RESET)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_ZOOM_RESET])
}
}, {
label: 'Zoom In',
accelerator: 'CmdOrCtrl+=',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_ZOOM_IN)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_ZOOM_IN])
}
}, {
label: 'Zoom Out',
accelerator: 'CmdOrCtrl+-',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_ZOOM_OUT)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_ZOOM_OUT])
}
}, {
type: 'separator'
Expand All @@ -200,17 +226,13 @@ const init = () => {
label: 'Reload Page',
accelerator: 'CmdOrCtrl+R',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_RELOAD)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_RELOAD])
}
}, {
label: 'Clean Reload',
accelerator: 'CmdOrCtrl+Shift+R',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_CLEAN_RELOAD)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_CLEAN_RELOAD])
}
}, {
type: 'separator'
Expand Down Expand Up @@ -238,9 +260,7 @@ const init = () => {
label: 'Toggle Developer Tools',
accelerator: 'CmdOrCtrl+Alt+I',
click: function (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_ACTIVE_FRAME_TOGGLE_DEV_TOOLS)
}
sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_TOGGLE_DEV_TOOLS])
}
}, {
label: 'Toggle Browser Console',
Expand Down Expand Up @@ -418,22 +438,22 @@ const init = () => {
{
label: 'Brave Help',
click: function (item, focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
sendToFocusedWindow(focusedWindow,
[messages.SHORTCUT_NEW_FRAME, 'https://brave.com/'])
}
}, {
type: 'separator'
}, {
label: 'Submit Feedback...',
click: function (item, focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
sendToFocusedWindow(focusedWindow,
[messages.SHORTCUT_NEW_FRAME, 'https://brave.com/'])
}
}, {
label: 'Spread the word about Brave...',
click: function (item, focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
sendToFocusedWindow(focusedWindow,
[messages.SHORTCUT_NEW_FRAME, 'https://brave.com/'])
}
}
]
Expand All @@ -458,8 +478,8 @@ const init = () => {
}, {
label: 'Send us Feedback...',
click: function (item, focusedWindow) {
focusedWindow.webContents.send(messages.SHORTCUT_NEW_FRAME,
'https://brave.com')
sendToFocusedWindow(focusedWindow,
[messages.SHORTCUT_NEW_FRAME, 'https://brave.com/'])
}
}, {
type: 'separator'
Expand Down
18 changes: 14 additions & 4 deletions js/components/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const WindowActions = require('../actions/windowActions')
const ImmutableComponent = require('./immutableComponent')
const cx = require('../lib/classSet.js')
const UrlUtil = require('./../../node_modules/urlutil.js/dist/node-urlutil.js')
const messages = require('../constants/messages.js')
const remote = global.require('electron').remote

import adInfo from '../data/adInfo.js'
import Config from '../constants/config.js'
Expand Down Expand Up @@ -38,13 +40,13 @@ class Frame extends ImmutableComponent {
this.webview.reloadIgnoringCache()
break
case 'zoom-in':
this.webview.send('zoom-in')
this.webview.send(messages.ZOOM_IN)
break
case 'zoom-out':
this.webview.send('zoom-out')
this.webview.send(messages.ZOOM_OUT)
break
case 'zoom-reset':
this.webview.send('zoom-reset')
this.webview.send(messages.ZOOM_RESET)
break
case 'toggle-dev-tools':
if (this.webview.isDevToolsOpened()) {
Expand All @@ -58,6 +60,13 @@ class Frame extends ImmutableComponent {
WindowActions.loadUrl(src)
// TODO: Make the URL bar show the view-source: prefix
break
case 'save':
// TODO: Sometimes this tries to save in a non-existent directory
remote.getCurrentWebContents().downloadURL(this.webview.getURL())
break
case 'print':
this.webview.send(messages.PRINT_PAGE)
break
}
if (activeShortcut) {
WindowActions.setActiveFrameShortcut(null)
Expand Down Expand Up @@ -128,7 +137,8 @@ class Frame extends ImmutableComponent {
let host = new window.URL(currentLocation).hostname.replace('www.', '')
let adDivCandidates = adInfo[host]
if (adDivCandidates) {
this.webview.send('set-ad-div-candidates', adDivCandidates, Config.vault.replacementUrl)
this.webview.send(messages.SET_AD_DIV_CANDIDATES,
adDivCandidates, Config.vault.replacementUrl)
}
}

Expand Down
5 changes: 4 additions & 1 deletion js/components/urlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class UrlBar extends ImmutableComponent {
}

componentWillMount () {
ipc.on(messages.SHORTCUT_FOCUS_URL, this.onFocus.bind(this))
ipc.on(messages.SHORTCUT_FOCUS_URL, () => {
this.onFocus()
WindowActions.setUrlBarAutoselected(true)
})
// escape key handling
ipc.on(messages.SHORTCUT_ACTIVE_FRAME_STOP, this.onActiveFrameStop.bind(this))
}
Expand Down
13 changes: 10 additions & 3 deletions js/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const messages = {
SHORTCUT_SET_ACTIVE_FRAME_BY_INDEX: _, /** @arg {number} index of frame */
SHORTCUT_ACTIVE_FRAME_VIEW_SOURCE: _,
SHORTCUT_SET_ACTIVE_FRAME_TO_LAST: _,
SHORTCUT_ACTIVE_FRAME_SAVE: _,
SHORTCUT_ACTIVE_FRAME_PRINT: _,
// Frame management shortcuts
SHORTCUT_NEW_FRAME: _, /** @arg {string} opt_url to load if any */
SHORTCUT_CLOSE_FRAME: _, /** @arg {number} opt_key of frame, defaults to active frame */
Expand All @@ -30,14 +32,19 @@ const messages = {
// Window management
CLOSE_WINDOW: _,
NEW_WINDOW: _,
// Misc
CONTEXT_MENU_OPENED: _, /** @arg {string} nodeName of node being clicked */
QUIT_APPLICATION: _,
// Updates
UPDATE_REQUESTED: _,
UPDATE_AVAILABLE: _,
UPDATE_NOT_AVAILABLE: _,
CHECK_FOR_UPDATE: _
CHECK_FOR_UPDATE: _,
// Webview page messages
ZOOM_IN: _,
ZOOM_OUT: _,
ZOOM_RESET: _,
PRINT_PAGE: _,
SET_AD_DIV_CANDIDATES: _, /** @arg {Array} adDivCandidates, @arg {string} placeholderUrl */
CONTEXT_MENU_OPENED: _ /** @arg {string} nodeName of node being clicked */
}

module.exports = mapValuesByKeys(messages)
2 changes: 1 addition & 1 deletion js/stores/windowStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ ipc.on(messages.SHORTCUT_PREV_TAB, () => {
windowStore.emitChange()
})

const frameShortcuts = ['stop', 'reload', 'zoom-in', 'zoom-out', 'zoom-reset', 'toggle-dev-tools', 'clean-reload', 'view-source', 'mute']
const frameShortcuts = ['stop', 'reload', 'zoom-in', 'zoom-out', 'zoom-reset', 'toggle-dev-tools', 'clean-reload', 'view-source', 'mute', 'save', 'print']
frameShortcuts.forEach(shortcut => {
// Listen for actions on the active frame
ipc.on(`shortcut-active-frame-${shortcut}`, () => {
Expand Down