Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

More robust next tab selection after closing an active tab #12193

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions app/browser/activeTabHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

// static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put the standard preamble at the top of this file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, and rebased

const activeTabsByWindow = new Map()

const api = {
/**
* Inform this store that a tab is active for a window.
* This information will be added to index 0 in the
* active-tab history for the window.
*/
setActiveTabForWindow: function (windowId, tabId) {
const existing = activeTabsByWindow.get(windowId)
if (existing) {
existing.unshift(tabId)
} else {
activeTabsByWindow.set(windowId, [ tabId ])
}
},

/**
* Retrieve the tabId that was active at the specified history index,
* 0 for most recent (default)
*/
getActiveTabForWindow: function (windowId, historyIndex = 0) {
// get history of active tabs for specified window
const windowActiveTabs = activeTabsByWindow.get(windowId)
// handle no history for specified window
if (!windowActiveTabs || !windowActiveTabs.length) {
return null
}
// verify specified index in active-tab history exists
const lastIndex = windowActiveTabs.length - 1
if (lastIndex < historyIndex) {
return null
}
// get tabId at specified index in active-tab history
return windowActiveTabs[historyIndex]
},

/**
* Removes specified tab from active-tab history in specified window
*/
clearTabFromWindow: function (windowId, tabId) {
const windowActiveTabs = activeTabsByWindow.get(windowId)
if (windowActiveTabs && windowActiveTabs.length) {
activeTabsByWindow.set(windowId, windowActiveTabs.filter(previousTabId => previousTabId !== tabId))
}
},

/**
* Forget history of active tabs for specified window
*/
clearTabbedWindow: function (windowId) {
activeTabsByWindow.delete(windowId)
}
}

module.exports = api
7 changes: 0 additions & 7 deletions app/browser/reducers/tabsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ const tabsReducer = (state, action, immutableAction) => {
if (tabId === tabState.TAB_ID_NONE) {
break
}
const nextActiveTabId = tabs.getNextActiveTab(state, tabId)

// Must be called before tab is removed
// But still check for no tabId because on tab detach there's a dummy tabId
const tabValue = tabState.getByTabId(state, tabId)
Expand All @@ -211,11 +209,6 @@ const tabsReducer = (state, action, immutableAction) => {
state = tabs.updateTabsStateForWindow(state, windowIdOfTabBeingRemoved)
}
state = tabState.removeTabByTabId(state, tabId)
setImmediate(() => {
if (nextActiveTabId !== tabState.TAB_ID_NONE) {
tabs.setActive(nextActiveTabId)
}
})
tabs.forgetTab(tabId)
}
break
Expand Down
Loading