aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-05-27 13:35:58 +0100
committerStephen Blott2015-05-27 13:38:19 +0100
commitdffd14a93faf345f02c3c7a7def6b176ed5aeb7d (patch)
treecf2fe146cb7b5da4dcfc27ea391685b6caace932
parent1e236a21373f667f8fd1cec07df4b647b2b30e1c (diff)
downloadvimium-dffd14a93faf345f02c3c7a7def6b176ed5aeb7d.tar.bz2
Go directly to next/previous tab.
This makes nextTab and previousTab go directly to the relevant tab, without visiting intervening tabs -- all of which avoids a nasty flicker for 5J or 5K.
-rw-r--r--background_scripts/commands.coffee4
-rw-r--r--background_scripts/main.coffee39
2 files changed, 23 insertions, 20 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee
index bca1c3a4..abfbd9e2 100644
--- a/background_scripts/commands.coffee
+++ b/background_scripts/commands.coffee
@@ -321,8 +321,8 @@ commandDescriptions =
goToRoot: ["Go to root of current URL hierarchy", { passCountToFunction: true }]
# Manipulating tabs
- nextTab: ["Go one tab right", { background: true }]
- previousTab: ["Go one tab left", { background: true }]
+ nextTab: ["Go one tab right", { background: true, passCountToFunction: true }]
+ previousTab: ["Go one tab left", { background: true, passCountToFunction: true }]
firstTab: ["Go to the first tab", { background: true }]
lastTab: ["Go to the last tab", { background: true }]
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index 6ee0e8e7..d4ba7850 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -268,10 +268,10 @@ BackgroundCommands =
chrome.tabs.query {active: true, currentWindow: true}, (tabs) ->
tab = tabs[0]
chrome.windows.create {tabId: tab.id, incognito: tab.incognito}
- nextTab: (callback) -> selectTab(callback, "next")
- previousTab: (callback) -> selectTab(callback, "previous")
- firstTab: (callback) -> selectTab(callback, "first")
- lastTab: (callback) -> selectTab(callback, "last")
+ nextTab: (count) -> selectTab "next", count
+ previousTab: (count) -> selectTab "previous", count
+ firstTab: -> selectTab "first"
+ lastTab: -> selectTab "last"
removeTab: (callback) ->
chrome.tabs.getSelected(null, (tab) ->
chrome.tabs.remove(tab.id)
@@ -345,21 +345,24 @@ removeTabsRelative = (direction) ->
# Selects a tab before or after the currently selected tab.
# - direction: "next", "previous", "first" or "last".
-selectTab = (callback, direction) ->
- chrome.tabs.getAllInWindow(null, (tabs) ->
+selectTab = (direction, count = 1) ->
+ chrome.tabs.getAllInWindow null, (tabs) ->
return unless tabs.length > 1
- chrome.tabs.getSelected(null, (currentTab) ->
- switch direction
- when "next"
- toSelect = tabs[(currentTab.index + 1 + tabs.length) % tabs.length]
- when "previous"
- toSelect = tabs[(currentTab.index - 1 + tabs.length) % tabs.length]
- when "first"
- toSelect = tabs[0]
- when "last"
- toSelect = tabs[tabs.length - 1]
- selectionChangedHandlers.push(callback)
- chrome.tabs.update(toSelect.id, { selected: true })))
+ chrome.tabs.getSelected null, (currentTab) ->
+ toSelect =
+ switch direction
+ when "next"
+ currentTab.index + count
+ when "previous"
+ currentTab.index - count
+ when "first"
+ 0
+ when "last"
+ tabs.length - 1
+ # Bring toSelect into the range [0,tabs.length).
+ toSelect += tabs.length while toSelect < 0
+ toSelect %= tabs.length
+ chrome.tabs.update tabs[toSelect].id, selected: true
updateOpenTabs = (tab, deleteFrames = false) ->
# Chrome might reuse the tab ID of a recently removed tab.