aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-05-27 14:47:57 +0100
committerStephen Blott2015-05-27 14:47:57 +0100
commit594abdc59db60e942e03086b4a66d7c4e1e5ab66 (patch)
tree9ea173a4d4ec649e09f3b078fa7de447fbfd40c2
parent1e236a21373f667f8fd1cec07df4b647b2b30e1c (diff)
parentf16c7f270563cc79a7d7280d5a91ababc3be6965 (diff)
downloadvimium-594abdc59db60e942e03086b4a66d7c4e1e5ab66.tar.bz2
Merge pull request #1689 from smblott-github/direct-next-previous-tab
Go directly to next/previous tab.
-rw-r--r--background_scripts/commands.coffee4
-rw-r--r--background_scripts/main.coffee38
2 files changed, 22 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..a13d9d98 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,23 @@ 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 = (toSelect + tabs.length * Math.abs count) % 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.