From 4ada22e85dcd061fa806a5fe72a9dc3f1cfe0442 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 9 Feb 2016 15:28:50 +0000 Subject: BgUtils; move tabRecency to bg_tiles.coffee. Certain background-page utilities are actually shared, and are therefore best placed in place that reflects that. Here, tabRecency is moved to the new gb_utils.coffee in preparation for implementing a go-to-previous-tab command. In particular, it is no longer appropriate that tabRecency be embedded within the completion code. logMessage() from main.coffee is also a candidate for moving to bg_utils.coffee. --- background_scripts/bg_utils.coffee | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 background_scripts/bg_utils.coffee (limited to 'background_scripts/bg_utils.coffee') diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee new file mode 100644 index 00000000..9c1674bf --- /dev/null +++ b/background_scripts/bg_utils.coffee @@ -0,0 +1,45 @@ +root = exports ? window + +# TabRecency associates a logical timestamp with each tab id. These are used to provide an initial +# recency-based ordering in the tabs vomnibar (which allows jumping quickly between recently-visited tabs). +class TabRecency + timestamp: 1 + current: -1 + cache: {} + lastVisited: null + lastVisitedTime: null + timeDelta: 500 # Milliseconds. + + constructor: -> + chrome.tabs.onActivated.addListener (activeInfo) => @register activeInfo.tabId + chrome.tabs.onRemoved.addListener (tabId) => @deregister tabId + + chrome.tabs.onReplaced.addListener (addedTabId, removedTabId) => + @deregister removedTabId + @register addedTabId + + register: (tabId) -> + currentTime = new Date() + # Register tabId if it has been visited for at least @timeDelta ms. Tabs which are visited only for a + # very-short time (e.g. those passed through with `5J`) aren't registered as visited at all. + if @lastVisitedTime? and @timeDelta <= currentTime - @lastVisitedTime + @cache[@lastVisited] = ++@timestamp + + @current = @lastVisited = tabId + @lastVisitedTime = currentTime + + deregister: (tabId) -> + if tabId == @lastVisited + # Ensure we don't register this tab, since it's going away. + @lastVisited = @lastVisitedTime = null + delete @cache[tabId] + + # Recently-visited tabs get a higher score (except the current tab, which gets a low score). + recencyScore: (tabId) -> + @cache[tabId] ||= 1 + if tabId == @current then 0.0 else @cache[tabId] / @timestamp + +BgUtils = + tabRecency: new TabRecency() + +root.BgUtils = BgUtils -- cgit v1.2.3 From 60d33d20026cdcdd0c4ecef20410d38341c86633 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 9 Feb 2016 15:55:49 +0000 Subject: BgUtils; implement visitPreviousTab. Implements visitPreviousTab (as discussed in #1955). --- background_scripts/bg_utils.coffee | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'background_scripts/bg_utils.coffee') diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee index 9c1674bf..bd00fd24 100644 --- a/background_scripts/bg_utils.coffee +++ b/background_scripts/bg_utils.coffee @@ -39,6 +39,13 @@ class TabRecency @cache[tabId] ||= 1 if tabId == @current then 0.0 else @cache[tabId] / @timestamp + # Get the tab Id of the count-th most recently visited tab (excluding tabId, which is the current tab). + getRecentTab: (tabId, count) -> + tabId = tabId.toString() + tabIds = (tId for own tId of @cache when tId != tabId) + tabIds.sort (a,b) => @cache[b] - @cache[a] + parseInt tabIds[(count-1)%tabIds.length] + BgUtils = tabRecency: new TabRecency() -- cgit v1.2.3 From 1d809afe18d8638b899a1016d9cca0ccc4a32253 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 10 Feb 2016 06:26:55 +0000 Subject: BgUtils; two fixes... - handle case where there's only one tab - also focus the selected tab's window --- background_scripts/bg_utils.coffee | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'background_scripts/bg_utils.coffee') diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee index bd00fd24..96c1282a 100644 --- a/background_scripts/bg_utils.coffee +++ b/background_scripts/bg_utils.coffee @@ -39,12 +39,11 @@ class TabRecency @cache[tabId] ||= 1 if tabId == @current then 0.0 else @cache[tabId] / @timestamp - # Get the tab Id of the count-th most recently visited tab (excluding tabId, which is the current tab). - getRecentTab: (tabId, count) -> - tabId = tabId.toString() - tabIds = (tId for own tId of @cache when tId != tabId) + # Returns a list of tab Ids sorted by recency, most recent tab first. + getTabsByRecency: -> + tabIds = (tId for own tId of @cache) tabIds.sort (a,b) => @cache[b] - @cache[a] - parseInt tabIds[(count-1)%tabIds.length] + tabIds.map (tId) -> parseInt tId BgUtils = tabRecency: new TabRecency() -- cgit v1.2.3