From dffd14a93faf345f02c3c7a7def6b176ed5aeb7d Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 27 May 2015 13:35:58 +0100 Subject: 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. --- background_scripts/commands.coffee | 4 ++-- background_scripts/main.coffee | 39 ++++++++++++++++++++------------------ 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. -- cgit v1.2.3 From f16c7f270563cc79a7d7280d5a91ababc3be6965 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 27 May 2015 14:46:51 +0100 Subject: Simplify index calculation. --- background_scripts/main.coffee | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index d4ba7850..a13d9d98 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -360,8 +360,7 @@ selectTab = (direction, count = 1) -> when "last" tabs.length - 1 # Bring toSelect into the range [0,tabs.length). - toSelect += tabs.length while toSelect < 0 - toSelect %= tabs.length + toSelect = (toSelect + tabs.length * Math.abs count) % tabs.length chrome.tabs.update tabs[toSelect].id, selected: true updateOpenTabs = (tab, deleteFrames = false) -> -- cgit v1.2.3