Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move 'tab-clicked' message response handling next to 'tab-mouseup' #2306

Merged
merged 4 commits into from
Jun 10, 2019
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
60 changes: 0 additions & 60 deletions webextensions/background/handle-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@ import * as Background from './background.js';
import * as TabsGroup from './tabs-group.js';
import * as Tree from './tree.js';
import * as Commands from './commands.js';
import * as HandleTabMultiselect from './handle-tab-multiselect.js';
Copy link
Owner

Choose a reason for hiding this comment

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

This module still need to be loaded from anywhere on the background page (for example background/index.js or background/background.js) to multiselect collapsed tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry wasn't really understanding how javascript import works. In any case you appear to have already been loading this module in background/index.js. I just tested that multiselect collapsed tree (where collapsed tabs are automatically selected as well) still works.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh, you right. I missed that it was already loaded from index.js.


function log(...args) {
internalLogger('background/handle-misc', ...args);
}
function logMouseEvent(...args) {
internalLogger('sidebar/mouse-event-listener', ...args);
}


let mInitialized = false;

Expand Down Expand Up @@ -310,61 +305,6 @@ function onMessage(message, sender) {
return { structure };
})();

case Constants.kNOTIFY_TAB_MOUSEDOWN:
return (async () => {
logMouseEvent('Constants.kNOTIFY_TAB_MOUSEDOWN');
await Tab.waitUntilTracked(message.tabId);
const tab = Tab.get(message.tabId);
if (!tab)
return;

logMouseEvent('Sending message to listeners');
const treeItem = new TSTAPI.TreeItem(tab);
const mousedownNotified = TSTAPI.sendMessage(Object.assign({}, message, {
type: TSTAPI.kNOTIFY_TAB_MOUSEDOWN,
tab: treeItem
}), { tabProperties: ['tab'] });

// We must send tab-mouseup after tab-mousedown is notified.
// So, we return to the caller process and do this post process asynchronously.
mousedownNotified.then(async (results) => {
results = results.concat(
await TSTAPI.sendMessage(Object.assign({}, message, {
type: TSTAPI.kNOTIFY_TAB_CLICKED,
tab: treeItem
}), { tabProperties: ['tab'] })
);
if (results.some(result => result && result.result))
return SidebarConnection.sendMessage({
type: Constants.kNOTIFY_TAB_MOUSEDOWN_CANCELED,
windowId: message.windowId,
button: message.button
});

logMouseEvent('Ready to handle click action on the tab');

// not canceled, then fallback to default behavior
const onRegularArea = (
!message.twisty &&
!message.soundButton &&
!message.closebox
);
const wasMultiselectionAction = (
onRegularArea &&
await HandleTabMultiselect.updateSelectionByTabClick(tab, message)
);
logMouseEvent(' => ', { onRegularArea, wasMultiselectionAction });
if (message.button == 0 &&
onRegularArea &&
!wasMultiselectionAction)
TabsInternalOperation.activateTab(tab, {
keepMultiselection: tab.highlighted
});
});

return true;
})();

case Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED:
return (async () => {
if (JSON.stringify(message.permissions) == JSON.stringify(Permissions.ALL_URLS)) {
Expand Down
145 changes: 0 additions & 145 deletions webextensions/background/handle-tab-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import {
log as internalLogger,
dumpTab,
configs
} from '/common/common.js';

import * as ApiTabs from '/common/api-tabs.js';
Expand Down Expand Up @@ -39,146 +37,3 @@ Tab.onUpdated.addListener((tab, info, options = {}) => {
}).catch(ApiTabs.createErrorHandler(ApiTabs.handleMissingTabError));
}
});

const mLastClickedTabInWindow = new Map();
const mIsInSelectionSession = new Map();

export async function updateSelectionByTabClick(tab, event) {
const ctrlKeyPressed = event.ctrlKey || (event.metaKey && /^Mac/i.test(navigator.platform));
const activeTab = Tab.getActiveTab(tab.windowId);
const highlightedTabIds = new Set(Tab.getHighlightedTabs(tab.windowId).map(tab => tab.id));
const inSelectionSession = mIsInSelectionSession.get(tab.windowId);
log('updateSelectionByTabClick ', { ctrlKeyPressed, activeTab, highlightedTabIds, inSelectionSession });
if (event.shiftKey) {
// select the clicked tab and tabs between last activated tab
const lastClickedTab = mLastClickedTabInWindow.get(tab.windowId) || activeTab;
const betweenTabs = Tab.getTabsBetween(lastClickedTab, tab);
const targetTabs = new Set([lastClickedTab].concat(betweenTabs));
targetTabs.add(tab);

log(' => ', { lastClickedTab, betweenTabs, targetTabs });

try {
if (!ctrlKeyPressed) {
const alreadySelectedTabs = Tab.getSelectedTabs(tab.windowId, { iterator: true });
log('clear old selection by shift-click: ', configs.debug && Array.from(alreadySelectedTabs, dumpTab));
for (const alreadySelectedTab of alreadySelectedTabs) {
if (!targetTabs.has(alreadySelectedTab))
highlightedTabIds.delete(alreadySelectedTab.id);
}
}

log('set selection by shift-click: ', configs.debug && Array.from(targetTabs, dumpTab));
for (const toBeSelectedTab of targetTabs) {
highlightedTabIds.add(toBeSelectedTab.id);
}

const rootTabs = [tab];
if (tab != activeTab &&
!inSelectionSession)
rootTabs.push(activeTab);
for (const root of rootTabs) {
if (!root.$TST.subtreeCollapsed)
continue;
for (const descendant of root.$TST.descendants) {
highlightedTabIds.add(descendant.id);
}
}

// for better performance, we should not call browser.tabs.update() for each tab.
const indices = Array.from(highlightedTabIds)
.filter(id => id != activeTab.id)
.map(id => Tab.get(id).index);
if (highlightedTabIds.has(activeTab.id))
indices.unshift(activeTab.index);
browser.tabs.highlight({
windowId: tab.windowId,
populate: false,
tabs: indices
}).catch(ApiTabs.createErrorSuppressor());
}
catch(_e) { // not implemented on old Firefox
return false;
}
mIsInSelectionSession.set(tab.windowId, true);
return true;
}
else if (ctrlKeyPressed) {
try {
log('change selection by ctrl-click: ', dumpTab(tab));
/* Special operation to toggle selection of collapsed descendants for the active tab.
- When there is no other multiselected foreign tab
=> toggle multiselection only descendants.
- When there is one or more multiselected foreign tab
=> toggle multiselection of the active tab and descendants.
=> one of multiselected foreign tabs will be activated.
- When a foreign tab is highlighted and there is one or more unhighlighted descendants
=> highlight all descendants (to prevent only the root tab is dragged).
*/
const activeTabDescendants = activeTab.$TST.descendants;
let toBeHighlighted = !tab.highlighted;
log('toBeHighlighted: ', toBeHighlighted);
if (tab == activeTab &&
tab.$TST.subtreeCollapsed &&
activeTabDescendants.length > 0) {
const highlightedCount = activeTabDescendants.filter(tab => tab.highlighted).length;
const partiallySelected = highlightedCount != 0 && highlightedCount != activeTabDescendants.length;
toBeHighlighted = partiallySelected || !activeTabDescendants[0].highlighted;
log(' => ', toBeHighlighted, { partiallySelected });
}
if (toBeHighlighted)
highlightedTabIds.add(tab.id);
else
highlightedTabIds.delete(tab.id);

if (tab.$TST.subtreeCollapsed) {
const descendants = tab == activeTab ? activeTabDescendants : tab.$TST.descendants;
for (const descendant of descendants) {
if (toBeHighlighted)
highlightedTabIds.add(descendant.id);
else
highlightedTabIds.delete(descendant.id);
}
}

if (tab == activeTab) {
if (highlightedTabIds.size == 0) {
log('Don\'t unhighlight only one highlighted active tab!');
highlightedTabIds.add(tab.id);
}
}
else if (!inSelectionSession) {
log('Select active tab and its descendants, for new selection session');
highlightedTabIds.add(activeTab.id);
if (activeTab.$TST.subtreeCollapsed) {
for (const descendant of activeTabDescendants) {
highlightedTabIds.add(descendant.id);
}
}
}

// for better performance, we should not call browser.tabs.update() for each tab.
const indices = Array.from(highlightedTabIds)
.filter(id => id != activeTab.id)
.map(id => Tab.get(id).index);
if (highlightedTabIds.has(activeTab.id))
indices.unshift(activeTab.index);
browser.tabs.highlight({
windowId: tab.windowId,
populate: false,
tabs: indices
}).catch(ApiTabs.createErrorSuppressor());
}
catch(_e) { // not implemented on old Firefox
return false;
}
mLastClickedTabInWindow.set(tab.windowId, tab);
mIsInSelectionSession.set(tab.windowId, true);
return true;
}
else {
mLastClickedTabInWindow.set(tab.windowId, tab);
mIsInSelectionSession.delete(tab.windowId);
return false;
}
}
1 change: 0 additions & 1 deletion webextensions/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const kCOMMAND_BOOKMARK_TAB_WITH_DIALOG = 'treestyletab:bookmark-tab-wit
export const kCOMMAND_BOOKMARK_TABS_WITH_DIALOG = 'treestyletab:bookmark-tabs-with-dialog';

export const kNOTIFY_TAB_MOUSEDOWN = 'treestyletab:tab-mousedown';
export const kNOTIFY_TAB_MOUSEDOWN_CANCELED = 'treestyletab:tab-mousedown-canceled';
export const kNOTIFY_TAB_MOUSEDOWN_EXPIRED = 'treestyletab:tab-mousedown-expired';

export const kNOTIFY_SIDEBAR_FOCUS = 'treestyletab:sidebar-focus';
Expand Down
Loading