1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* 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/. */
"use strict";
const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
const { gDevTools } = require("devtools/client/framework/devtools");
const Services = require("Services");
const Telemetry = require("devtools/client/shared/telemetry");
const TABS_REORDERED_SCALAR = "devtools.toolbox.tabs_reordered";
const PREFERENCE_NAME = "devtools.toolbox.tabsOrder";
/**
* Manage the order of devtools tabs.
*/
class ToolboxTabsOrderManager {
constructor(toolbox, onOrderUpdated, panelDefinitions) {
this.toolbox = toolbox;
this.onOrderUpdated = onOrderUpdated;
this.currentPanelDefinitions = panelDefinitions || [];
this.onMouseDown = this.onMouseDown.bind(this);
this.onMouseMove = this.onMouseMove.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
Services.prefs.addObserver(PREFERENCE_NAME, this.onOrderUpdated);
this.telemetry = new Telemetry();
}
async destroy() {
Services.prefs.removeObserver(PREFERENCE_NAME, this.onOrderUpdated);
// Call mouseUp() to clear the state to prepare for in case a dragging was in progress
// when the destroy() was called.
await this.onMouseUp();
}
insertBefore(target) {
const xBefore = this.dragTarget.offsetLeft;
this.toolboxTabsElement.insertBefore(this.dragTarget, target);
const xAfter = this.dragTarget.offsetLeft;
this.dragStartX += xAfter - xBefore;
this.isOrderUpdated = true;
}
isFirstTab(tabElement) {
return !tabElement.previousSibling;
}
isLastTab(tabElement) {
return (
!tabElement.nextSibling ||
tabElement.nextSibling.id === "tools-chevron-menu-button"
);
}
isRTL() {
return this.toolbox.direction === "rtl";
}
async saveOrderPreference() {
const tabs = [...this.toolboxTabsElement.querySelectorAll(".devtools-tab")];
const tabIds = tabs.map(tab => tab.dataset.extensionId || tab.dataset.id);
// Concat the overflowed tabs id since they are not contained in visible tabs.
// The overflowed tabs cannot be reordered so we just append the id from current
// panel definitions on their order.
const overflowedTabIds = this.currentPanelDefinitions
.filter(definition => !tabs.some(tab => tab.dataset.id === definition.id))
.map(definition => definition.extensionId || definition.id);
const currentTabIds = tabIds.concat(overflowedTabIds);
const dragTargetId =
this.dragTarget.dataset.extensionId || this.dragTarget.dataset.id;
const prefIds = getTabsOrderFromPreference();
const absoluteIds = toAbsoluteOrder(prefIds, currentTabIds, dragTargetId);
// Remove panel id which is not in panel definitions and addons list.
const extensions = await AddonManager.getAllAddons();
const definitions = gDevTools.getToolDefinitionArray();
const result = absoluteIds.filter(
id =>
definitions.find(d => id === (d.extensionId || d.id)) ||
extensions.find(e => id === e.id)
);
Services.prefs.setCharPref(PREFERENCE_NAME, result.join(","));
}
setCurrentPanelDefinitions(currentPanelDefinitions) {
this.currentPanelDefinitions = currentPanelDefinitions;
}
onMouseDown(e) {
if (!e.target.classList.contains("devtools-tab")) {
return;
}
this.dragStartX = e.pageX;
this.dragTarget = e.target;
this.previousPageX = e.pageX;
this.toolboxContainerElement = this.dragTarget.closest(
"#toolbox-container"
);
this.toolboxTabsElement = this.dragTarget.closest(".toolbox-tabs");
this.isOrderUpdated = false;
this.eventTarget = this.dragTarget.ownerGlobal.top;
this.eventTarget.addEventListener("mousemove", this.onMouseMove);
this.eventTarget.addEventListener("mouseup", this.onMouseUp);
this.toolboxContainerElement.classList.add("tabs-reordering");
}
onMouseMove(e) {
const diffPageX = e.pageX - this.previousPageX;
let dragTargetCenterX =
this.dragTarget.offsetLeft + diffPageX + this.dragTarget.clientWidth / 2;
let isDragTargetPreviousSibling = false;
const tabElements = this.toolboxTabsElement.querySelectorAll(
".devtools-tab"
);
// Calculate the minimum and maximum X-offset that can be valid for the drag target.
const firstElement = tabElements[0];
const firstElementCenterX =
firstElement.offsetLeft + firstElement.clientWidth / 2;
const lastElement = tabElements[tabElements.length - 1];
const lastElementCenterX =
lastElement.offsetLeft + lastElement.clientWidth / 2;
const max = Math.max(firstElementCenterX, lastElementCenterX);
const min = Math.min(firstElementCenterX, lastElementCenterX);
// Normalize the target center X so to remain between the first and last tab.
dragTargetCenterX = Math.min(max, dragTargetCenterX);
dragTargetCenterX = Math.max(min, dragTargetCenterX);
for (const tabElement of tabElements) {
if (tabElement === this.dragTarget) {
isDragTargetPreviousSibling = true;
continue;
}
// Is the dragTarget near the center of the other tab?
const anotherCenterX = tabElement.offsetLeft + tabElement.clientWidth / 2;
const distanceWithDragTarget = Math.abs(
dragTargetCenterX - anotherCenterX
);
const isReplaceable = distanceWithDragTarget < tabElement.clientWidth / 3;
if (isReplaceable) {
const replaceableElement = isDragTargetPreviousSibling
? tabElement.nextSibling
: tabElement;
this.insertBefore(replaceableElement);
break;
}
}
let distance = e.pageX - this.dragStartX;
// To accomodate for RTL locales, we cannot rely on the first/last element of the
// NodeList. We cannot have negative distances for the leftmost tab, and we cannot
// have positive distances for the rightmost tab.
const isFirstTab = this.isFirstTab(this.dragTarget);
const isLastTab = this.isLastTab(this.dragTarget);
const isLeftmostTab = this.isRTL() ? isLastTab : isFirstTab;
const isRightmostTab = this.isRTL() ? isFirstTab : isLastTab;
if ((isLeftmostTab && distance < 0) || (isRightmostTab && distance > 0)) {
// If the drag target is already edge of the tabs and the mouse will make the
// element to move to same direction more, keep the position.
distance = 0;
}
this.dragTarget.style.left = `${distance}px`;
this.previousPageX = e.pageX;
}
async onMouseUp() {
if (!this.dragTarget) {
// The case in here has two type:
// 1. Although destroy method was called, it was not during reordering.
// 2. Although mouse event occur, destroy method was called during reordering.
return;
}
if (this.isOrderUpdated) {
await this.saveOrderPreference();
// Log which tabs reordered. The question we want to answer is:
// "How frequently are the tabs re-ordered, also which tabs get re-ordered?"
const toolId =
this.dragTarget.dataset.extensionId || this.dragTarget.dataset.id;
this.telemetry.keyedScalarAdd(TABS_REORDERED_SCALAR, toolId, 1);
}
this.eventTarget.removeEventListener("mousemove", this.onMouseMove);
this.eventTarget.removeEventListener("mouseup", this.onMouseUp);
this.toolboxContainerElement.classList.remove("tabs-reordering");
this.dragTarget.style.left = null;
this.dragTarget = null;
this.toolboxContainerElement = null;
this.toolboxTabsElement = null;
this.eventTarget = null;
}
}
function getTabsOrderFromPreference() {
const pref = Services.prefs.getCharPref(PREFERENCE_NAME, "");
return pref ? pref.split(",") : [];
}
function sortPanelDefinitions(definitions) {
const toolIds = getTabsOrderFromPreference();
return definitions.sort((a, b) => {
let orderA = toolIds.indexOf(a.extensionId || a.id);
let orderB = toolIds.indexOf(b.extensionId || b.id);
orderA = orderA < 0 ? Number.MAX_VALUE : orderA;
orderB = orderB < 0 ? Number.MAX_VALUE : orderB;
return orderA - orderB;
});
}
/*
* This function returns absolute tab ids that were merged the both ids that are in
* preference and tabs.
* Some tabs added with add-ons etc show/hide depending on conditions.
* However, all of tabs that include hidden tab always keep the relationship with
* left side tab, except in case the left tab was target of dragging. If the left
* tab has been moved, it keeps its relationship with the tab next to it.
*
* Case 1: Drag a tab to left
* currentTabIds: [T1, T2, T3, T4, T5]
* prefIds : [T1, T2, T3, E1(hidden), T4, T5]
* drag T4 : [T1, T2, T4, T3, T5]
* result : [T1, T2, T4, T3, E1, T5]
*
* Case 2: Drag a tab to right
* currentTabIds: [T1, T2, T3, T4, T5]
* prefIds : [T1, T2, T3, E1(hidden), T4, T5]
* drag T2 : [T1, T3, T4, T2, T5]
* result : [T1, T3, E1, T4, T2, T5]
*
* Case 3: Hidden tab was left end and drag a tab to left end
* currentTabIds: [T1, T2, T3, T4, T5]
* prefIds : [E1(hidden), T1, T2, T3, T4, T5]
* drag T4 : [T4, T1, T2, T3, T5]
* result : [E1, T4, T1, T2, T3, T5]
*
* Case 4: Hidden tab was right end and drag a tab to right end
* currentTabIds: [T1, T2, T3, T4, T5]
* prefIds : [T1, T2, T3, T4, T5, E1(hidden)]
* drag T1 : [T2, T3, T4, T5, T1]
* result : [T2, T3, T4, T5, E1, T1]
*
* @param Array - prefIds: id array of preference
* @param Array - currentTabIds: id array of appearanced tabs
* @param String - dragTargetId: id of dragged target
* @return Array
*/
function toAbsoluteOrder(prefIds, currentTabIds, dragTargetId) {
currentTabIds = [...currentTabIds];
let indexAtCurrentTabs = 0;
for (const prefId of prefIds) {
if (prefId === dragTargetId) {
// do nothing
} else if (currentTabIds.includes(prefId)) {
indexAtCurrentTabs = currentTabIds.indexOf(prefId) + 1;
} else {
currentTabIds.splice(indexAtCurrentTabs, 0, prefId);
indexAtCurrentTabs += 1;
}
}
return currentTabIds;
}
module.exports.ToolboxTabsOrderManager = ToolboxTabsOrderManager;
module.exports.sortPanelDefinitions = sortPanelDefinitions;
module.exports.toAbsoluteOrder = toAbsoluteOrder;
|