Skip to content

Commit

Permalink
move default keybindings to a separate module
Browse files Browse the repository at this point in the history
This helps avoid a situation where a default keybinding depends on a module that tries to register another keybinding, creating a circular dependency
  • Loading branch information
PalmerAL committed Mar 20, 2020
1 parent b93a226 commit 907a622
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 224 deletions.
2 changes: 1 addition & 1 deletion js/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ require('navbar/goBackButton.js').initialize()
require('downloadManager.js').initialize()
require('webviewMenu.js').initialize()
require('menuRenderer.js').initialize()
require('keybindings.js').initialize()
require('defaultKeybindings.js').initialize()
require('pdfViewer.js').initialize()
require('autofillSetup.js').initialize()
require('passwordManager/passwordManager.js').initialize()
Expand Down
231 changes: 231 additions & 0 deletions js/defaultKeybindings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
const keybindings = require('keybindings.js')
var webviews = require('webviews.js')
var browserUI = require('browserUI.js')
var focusMode = require('focusMode.js')
var modalMode = require('modalMode.js')

const defaultKeybindings = {
initialize: function () {
keybindings.defineShortcut('addTab', function () {
/* new tabs can't be created in modal mode */
if (modalMode.enabled()) {
return
}

/* new tabs can't be created in focus mode */
if (focusMode.enabled()) {
focusMode.warn()
return
}

browserUI.addTab()
})

keybindings.defineShortcut('addPrivateTab', function () {
/* new tabs can't be created in modal mode */
if (modalMode.enabled()) {
return
}

/* new tabs can't be created in focus mode */
if (focusMode.enabled()) {
focusMode.warn()
return
}

browserUI.addTab(tabs.add({
private: true
}))
})

keybindings.defineShortcut('duplicateTab', function () {
if (modalMode.enabled()) {
return
}

if (focusMode.enabled()) {
focusMode.warn()
return
}

browserUI.duplicateTab(tabs.getSelected())
})

keybindings.defineShortcut('enterEditMode', function (e) {
tabBar.enterEditMode(tabs.getSelected())
return false
})

keybindings.defineShortcut('runShortcut', function (e) {
tabBar.enterEditMode(tabs.getSelected(), '!')
})

keybindings.defineShortcut('closeTab', function (e) {
browserUI.closeTab(tabs.getSelected())
})

keybindings.defineShortcut('restoreTab', function (e) {
if (focusMode.enabled()) {
focusMode.warn()
return
}

var restoredTab = tasks.getSelected().tabHistory.pop()

// The tab history stack is empty
if (!restoredTab) {
return
}

browserUI.addTab(tabs.add(restoredTab), {
enterEditMode: false
})
})

keybindings.defineShortcut('addToFavorites', function (e) {
tabBar.getTab(tabs.getSelected()).querySelector('.bookmarks-button').click()
tabBar.enterEditMode(tabs.getSelected(), null, false) // we need to show the bookmarks button, which is only visible in edit mode
})

// cmd+x should switch to tab x. Cmd+9 should switch to the last tab

for (var i = 1; i < 9; i++) {
(function (i) {
keybindings.defineShortcut({ keys: 'mod+' + i }, function (e) {
var currentIndex = tabs.getIndex(tabs.getSelected())
var newTab = tabs.getAtIndex(currentIndex + i) || tabs.getAtIndex(currentIndex - i)
if (newTab) {
browserUI.switchToTab(newTab.id)
}
})

keybindings.defineShortcut({ keys: 'shift+mod+' + i }, function (e) {
var currentIndex = tabs.getIndex(tabs.getSelected())
var newTab = tabs.getAtIndex(currentIndex - i) || tabs.getAtIndex(currentIndex + i)
if (newTab) {
browserUI.switchToTab(newTab.id)
}
})
})(i)
}

keybindings.defineShortcut('gotoLastTab', function (e) {
browserUI.switchToTab(tabs.getAtIndex(tabs.count() - 1).id)
})

keybindings.defineShortcut('gotoFirstTab', function (e) {
browserUI.switchToTab(tabs.getAtIndex(0).id)
})

keybindings.defineShortcut({ keys: 'esc' }, function (e) {
tabBar.leaveEditMode()

// exit full screen mode
webviews.callAsync(tabs.getSelected(), 'executeJavaScript', 'if(document.webkitIsFullScreen){document.webkitExitFullscreen()}')

webviews.callAsync(tabs.getSelected(), 'focus')
})

keybindings.defineShortcut('goBack', function (d) {
webviews.get(tabs.getSelected()).goBack()
})

keybindings.defineShortcut('goForward', function (d) {
webviews.get(tabs.getSelected()).goForward()
})

keybindings.defineShortcut('switchToPreviousTab', function (d) {
var currentIndex = tabs.getIndex(tabs.getSelected())
var previousTab = tabs.getAtIndex(currentIndex - 1)

if (previousTab) {
browserUI.switchToTab(previousTab.id)
} else {
browserUI.switchToTab(tabs.getAtIndex(tabs.count() - 1).id)
}
})

keybindings.defineShortcut('switchToNextTab', function (d) {
var currentIndex = tabs.getIndex(tabs.getSelected())
var nextTab = tabs.getAtIndex(currentIndex + 1)

if (nextTab) {
browserUI.switchToTab(nextTab.id)
} else {
browserUI.switchToTab(tabs.getAtIndex(0).id)
}
})

keybindings.defineShortcut('switchToNextTask', function (d) {
const taskSwitchList = tasks.filter(t => !tasks.isCollapsed(t.id))

const currentTaskIdx = taskSwitchList.findIndex(t => t.id === tasks.getSelected().id)

const nextTask = taskSwitchList[currentTaskIdx + 1] || taskSwitchList[0]
browserUI.switchToTask(nextTask.id)
})

keybindings.defineShortcut('switchToPreviousTask', function (d) {
const taskSwitchList = tasks.filter(t => !tasks.isCollapsed(t.id))

const currentTaskIdx = taskSwitchList.findIndex(t => t.id === tasks.getSelected().id)
taskCount = taskSwitchList.length

const previousTask = taskSwitchList[currentTaskIdx - 1] || taskSwitchList[taskCount - 1]
browserUI.switchToTask(previousTask.id)
})

// option+cmd+x should switch to task x

for (var i = 1; i < 10; i++) {
(function (i) {
keybindings.defineShortcut({ keys: 'shift+option+mod+' + i }, function (e) {
const taskSwitchList = tasks.filter(t => !tasks.isCollapsed(t.id))
if (taskSwitchList[i - 1]) {
browserUI.switchToTask(taskSwitchList[i - 1].id)
}
})
})(i)
}

keybindings.defineShortcut('closeAllTabs', function (d) { // destroys all current tabs, and creates a new, empty tab. Kind of like creating a new window, except the old window disappears.
var tset = tabs.get()
for (var i = 0; i < tset.length; i++) {
browserUI.destroyTab(tset[i].id)
}

browserUI.addTab() // create a new, blank tab
})

var lastReload = 0

keybindings.defineShortcut('reload', function () {
var time = Date.now()

// pressing mod+r twice in a row reloads the whole browser
if (time - lastReload < 500) {
ipc.send('destroyAllViews')
remote.getCurrentWindow().webContents.reload()
} else if (webviews.get(tabs.getSelected()).getURL().startsWith(webviews.internalPages.error)) {
// reload the original page rather than show the error page again
browserUI.navigate(tabs.getSelected(), new URL(webviews.get(tabs.getSelected()).getURL()).searchParams.get('url'))
} else {
// this can't be an error page, use the normal reload method
webviews.callAsync(tabs.getSelected(), 'reload')
}

lastReload = time
})

// reload the webview when the F5 key is pressed
document.body.addEventListener('keydown', function (e) {
if (e.keyCode === 116) {
try {
webviews.get(tabs.getSelected()).reloadIgnoringCache()
} catch (e) { }
}
})
}
}

module.exports = defaultKeybindings
Loading

0 comments on commit 907a622

Please sign in to comment.