forked from bwinton/TabCenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverticaltabs.jsm
591 lines (520 loc) · 20.8 KB
/
verticaltabs.jsm
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/* -*- Mode: javascript; indent-tabs-mode: nil -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Vertical Tabs.
*
* The Initial Developer of the Original Code is
* Philipp von Weitershausen.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*global VTTabIDs:false*/
/* exported EXPORTED_SYMBOLS, TAB_DROP_TYPE, vtInit*/
Components.utils.import('resource://gre/modules/Services.jsm');
Components.utils.import('resource://tabcenter/tabdatastore.jsm');
Components.utils.import('resource://tabcenter/multiselect.jsm');
const EXPORTED_SYMBOLS = ['VerticalTabs', 'vtInit'];
const NS_XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
const TAB_DROP_TYPE = 'application/x-moz-tabbrowser-tab';
function vtInit() {
let sss = Components.classes['@mozilla.org/content/style-sheet-service;1']
.getService(Components.interfaces.nsIStyleSheetService);
let ios = Components.classes['@mozilla.org/network/io-service;1']
.getService(Components.interfaces.nsIIOService);
let installStylesheet = function (uri) {
uri = ios.newURI(uri, null, null);
sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
};
let removeStylesheet = function (uri) {
uri = ios.newURI(uri, null, null);
sss.unregisterSheet(uri, sss.USER_SHEET);
};
installStylesheet('resource://tabcenter/override-bindings.css');
installStylesheet('resource://tabcenter/skin/base.css');
installStylesheet('resource://tabcenter/skin/light/light.css');
return () => {
removeStylesheet('resource://tabcenter/skin/light/light.css');
removeStylesheet('resource://tabcenter/override-bindings.css');
removeStylesheet('resource://tabcenter/skin/base.css');
let windows = Services.wm.getEnumerator(null);
while (windows.hasMoreElements()) {
let window = windows.getNext();
let tabs = window.document.getElementById('tabbrowser-tabs');
if (tabs) {
tabs.removeAttribute('overflow');
tabs._positionPinnedTabs();
}
}
};
}
/*
* Vertical Tabs
*
* Main entry point of this add-on.
*/
function VerticalTabs(window, {newPayload, addPingStats, AppConstants, setDefaultPrefs}) {
this.window = window;
this.document = window.document;
this.unloaders = [];
this.addPingStats = addPingStats;
this.newPayload = newPayload;
this.setDefaultPrefs = setDefaultPrefs;
this.AppConstants = AppConstants;
this.stats = this.newPayload();
this.init();
}
VerticalTabs.prototype = {
init: function () {
this.window.VerticalTabs = this;
this._endRemoveTab = this.window.gBrowser._endRemoveTab;
this.inferFromText = this.window.ToolbarIconColor.inferFromText;
let AppConstants = this.AppConstants;
let window = this.window;
let document = this.document;
this.BrowserOpenTab = this.window.BrowserOpenTab;
this.window.BrowserOpenTab = function () {
this.pushToTop = Services.prefs.getBoolPref('extensions.verticaltabs.opentabstop');
this.window.openUILinkIn(this.window.BROWSER_NEW_TAB_URL, 'tab');
this.pushToTop = false;
}.bind(this);
window.addEventListener('animationend', (e) => {
let tab = e.target;
if (e.animationName === 'slide-fade-in') {
tab.classList.remove('tab-visible');
} else if (e.animationName === 'fade-out') {
let tabStack = this.document.getAnonymousElementByAttribute(tab, 'class', 'tab-stack');
tabStack.collapsed = true; //there is a visual jump if we do not collapse the tab before the end of the animation
} else if (e.animationName === 'slide-out') {
this._endRemoveTab.bind(this.window.gBrowser)(tab);
}
});
window.gBrowser._endRemoveTab = (aTab) => {
aTab.classList.add('tab-hidden');
};
window.ToolbarIconColor.inferFromText = function () {
if (!this._initialized){
return;
}
function parseRGB(aColorString) {
let rgb = aColorString.match(/^rgba?\((\d+), (\d+), (\d+)/);
rgb.shift();
return rgb.map(x => parseInt(x));
}
let toolbarSelector = '#verticaltabs-box, #verticaltabs-box > toolbar:not([collapsed=true]):not(#addon-bar)';
if (AppConstants.platform === 'macosx') {
toolbarSelector += ':not([type=menubar])';
}
// The getComputedStyle calls and setting the brighttext are separated in
// two loops to avoid flushing layout and making it dirty repeatedly.
let luminances = new Map;
for (let toolbar of document.querySelectorAll(toolbarSelector)) {
let [r, g, b] = parseRGB(window.getComputedStyle(toolbar).color);
let luminance = 0.2125 * r + 0.7154 * g + 0.0721 * b;
luminances.set(toolbar, luminance);
}
for (let [toolbar, luminance] of luminances) {
if (luminance <= 110) {
toolbar.removeAttribute('brighttext');
} else {
toolbar.setAttribute('brighttext', 'true');
}
}
}.bind(this.window.ToolbarIconColor);
this.unloaders.push(function () {
this.window.ToolbarIconColor.inferFromText = this.inferFromText;
this.window.gBrowser._endRemoveTab = this._endRemoveTab;
this.window.BrowserOpenTab = this.BrowserOpenTab;
delete this.window.VerticalTabs;
});
this.window.onunload = () => {
this.sendStats();
};
this.rearrangeXUL();
this.initContextMenu();
let tabs = this.document.getElementById('tabbrowser-tabs');
let results = this.document.getElementById('PopupAutoCompleteRichResult');
let leftbox = this.document.getElementById('verticaltabs-box');
if (results) {
results.removeAttribute('width');
}
this.tabIDs = new VTTabIDs(tabs);
this.tabObserver = new this.document.defaultView.MutationObserver((mutations) => {
this.tabObserver.disconnect();
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' &&
mutation.target.localName === 'tab') {
let tab = mutation.target;
if (mutation.attributeName === 'crop' && leftbox.getAttribute('expanded') !== 'true') {
tab.removeAttribute('crop');
} else if (mutation.attributeName === 'selected' && tab.getAttribute('visuallyselected') !== 'true'){
this.checkScrollToTab(tab);
}
} else if (mutation.type === 'attributes' &&
mutation.target.id === 'PopupAutoCompleteRichResult' &&
mutation.attributeName === 'width') {
results.removeAttribute('width');
} else if (mutation.type === 'childList' &&
leftbox.getAttribute('expanded') !== 'true') {
for (let node of mutation.addedNodes) {
node.removeAttribute('crop');
}
}
});
this.tabObserver.observe(tabs, {childList: true, attributes: true, subtree: true});
if (results) {
this.tabObserver.observe(results, {attributes: true});
}
});
this.tabObserver.observe(tabs, {childList: true, attributes: true, subtree: true});
if (results) {
this.tabObserver.observe(results, {attributes: true});
}
this.unloaders.push(function () {
this.tabIDs.unload();
this.tabObserver.disconnect();
});
},
createElement: function (label, attrs) {
let rv = this.document.createElementNS(NS_XUL, label);
if (attrs) {
for (let attr in attrs) {
rv.setAttribute(attr, attrs[attr]);
}
}
return rv;
},
rearrangeXUL: function () {
const window = this.window;
const document = this.document;
// Move the bottom stuff (findbar, addonbar, etc.) in with the
// tabbrowser. That way it will share the same (horizontal)
// space as the brower. In other words, the bottom stuff no
// longer extends across the whole bottom of the window.
let mainWindow = document.getElementById('main-window');
let contentbox = document.getElementById('appcontent');
let bottom = document.getElementById('browser-bottombox');
contentbox.appendChild(bottom);
let top = document.getElementById('navigator-toolbox');
let browserPanel = document.getElementById('browser-panel');
// save the label of the first tab, and the toolbox palette for later…
let tabs = document.getElementById('tabbrowser-tabs');
let label = tabs.firstChild.label;
let palette = top.palette;
// Save the position of the tabs in the toolbar, for later restoring.
let toolbar = document.getElementById('TabsToolbar');
let tabsIndex = 0;
for (let i = 0; i < toolbar.children.length; i++) {
if (toolbar.children[i] === tabs) {
tabsIndex = i;
break;
}
}
contentbox.insertBefore(top, contentbox.firstChild);
// Create a box next to the app content. It will hold the tab
// bar and the tab toolbar.
let browserbox = document.getElementById('browser');
let leftbox = this.createElement('vbox', {'id': 'verticaltabs-box'});
let splitter = this.createElement('vbox', {'id': 'verticaltabs-splitter'});
browserbox.insertBefore(leftbox, contentbox);
browserbox.insertBefore(splitter, browserbox.firstChild);
mainWindow.setAttribute('persist',
mainWindow.getAttribute('persist') + ' tabspinned tabspinnedwidth');
this.pinnedWidth = +mainWindow.getAttribute('tabspinnedwidth').replace('px', '') ||
+window.getComputedStyle(document.documentElement)
.getPropertyValue('--pinned-width').replace('px', '');
document.documentElement.style.setProperty('--pinned-width', `${this.pinnedWidth}px`);
splitter.addEventListener('mousedown', (event) => {
let initialX = event.screenX - this.pinnedWidth;
let mousemove = (event) => {
// event.preventDefault();
let xDelta = event.screenX - initialX;
this.pinnedWidth = xDelta;
if (this.pinnedWidth < 30) {
this.pinnedWidth = 30;
}
if (this.pinnedWidth > document.width / 2) {
this.pinnedWidth = document.width / 2;
}
document.documentElement.style.setProperty('--pinned-width', `${this.pinnedWidth}px`);
mainWindow.setAttribute('tabspinnedwidth', `${this.pinnedWidth}px`);
};
let mouseup = (event) => {
document.removeEventListener('mousemove', mousemove);
document.removeEventListener('mouseup', mouseup);
};
document.addEventListener('mousemove', mousemove);
document.addEventListener('mouseup', mouseup);
});
// Move the tabs next to the app content, make them vertical,
// and restore their width from previous session
tabs.setAttribute('vertical', true);
leftbox.insertBefore(tabs, leftbox.firstChild);
tabs.orient = 'vertical';
tabs.mTabstrip.orient = 'vertical';
tabs.tabbox.orient = 'horizontal'; // probably not necessary
// And restore the label and palette here.
tabs.firstChild.label = label;
top.palette = palette;
// Move the tabs toolbar into the tab strip
toolbar.setAttribute('collapsed', 'false'); // no more vanishing new tab toolbar
toolbar._toolbox = null; // reset value set by constructor
toolbar.setAttribute('toolboxid', 'navigator-toolbox');
let spacer = this.createElement('spacer', {'id': 'new-tab-spacer'});
toolbar.appendChild(spacer);
let pin_button = this.createElement('toolbarbutton', {
'id': 'pin-button',
'tooltiptext': 'Keep sidebar open',
'onclick': `let box = document.getElementById('main-window');
let newstate = box.getAttribute('tabspinned') == 'true' ? 'false' : 'true';
box.setAttribute('tabspinned', newstate);
if (newstate == 'true') {
window.VerticalTabs.stats.tab_center_pinned++;
} else {
window.VerticalTabs.stats.tab_center_unpinned++;
}
`
});
toolbar.appendChild(pin_button);
leftbox.insertBefore(toolbar, leftbox.firstChild);
// change the text in the tab context box
let close_next_tabs_message = document.getElementById('context_closeTabsToTheEnd');
close_next_tabs_message.setAttribute('label', 'Close Tabs Below');
let enter = (event) => {
if (event.type === 'mouseenter' && leftbox.getAttribute('expanded') !== 'true') {
this.stats.tab_center_expanded++;
leftbox.setAttribute('expanded', 'true');
}
if (event.pageX <= 4) {
leftbox.style.transition = 'box-shadow 150ms ease-out, width 150ms ease-out';
window.setTimeout(() => {
leftbox.style.transition = '';
}, 300);
}
window.setTimeout(() => {
for (let i = 0; i < tabs.childNodes.length; i++) {
tabs.childNodes[i].setAttribute('crop', 'end');
}
}, 300);
};
leftbox.addEventListener('mouseenter', enter);
leftbox.addEventListener('mousemove', enter);
leftbox.addEventListener('mouseleave', () => {
if (mainWindow.getAttribute('tabspinned') !== 'true') {
leftbox.removeAttribute('expanded');
let tabsPopup = document.getElementById('alltabs-popup');
if (tabsPopup.state === 'open') {
tabsPopup.hidePopup();
}
}
});
tabs.addEventListener('TabOpen', this, false);
tabs.addEventListener('TabClose', this, false);
tabs.addEventListener('TabPinned', this, false);
tabs.addEventListener('TabUnpinned', this, false);
window.setTimeout(() => {
if (mainWindow.getAttribute('tabspinned') === 'true') {
leftbox.setAttribute('expanded', 'true');
}
for (let i = 0; i < tabs.childNodes.length; i++) {
this.initTab(tabs.childNodes[i]);
}
}, 150);
window.addEventListener('beforecustomization', function () {
browserPanel.insertBefore(top, browserPanel.firstChild);
top.palette = palette;
});
window.addEventListener('customizationchange', () => {
this.setDefaultPrefs();
});
window.addEventListener('aftercustomization', function () {
contentbox.insertBefore(top, contentbox.firstChild);
top.palette = palette;
});
let tab_context_menu = document.getElementById('tabContextMenu');
tab_context_menu.addEventListener('mouseover', function () {
leftbox.setAttribute('expanded', 'true');
});
tab_context_menu.addEventListener('mouseout', function () {
if (mainWindow.getAttribute('tabspinned') !== 'true') {
leftbox.removeAttribute('expanded');
}
});
this.unloaders.push(function () {
// Move the tabs toolbar back to where it was
toolbar._toolbox = null; // reset value set by constructor
toolbar.removeAttribute('toolboxid');
toolbar.removeAttribute('collapsed');
toolbar.removeChild(spacer);
toolbar.removeChild(pin_button);
let toolbox = document.getElementById('navigator-toolbox');
let navbar = document.getElementById('nav-bar');
let browserPanel = document.getElementById('browser-panel');
//remove customization event listeners which move the toolbox
window.removeEventListener('beforecustomization');
window.removeEventListener('aftercustomization');
window.removeEventListener('customizationchange');
// Put the tabs back up top
tabs.orient = 'horizontal';
tabs.mTabstrip.orient = 'horizontal';
tabs.tabbox.orient = 'vertical'; // probably not necessary
tabs.removeAttribute('width');
tabs.removeEventListener('TabOpen', this, false);
tabs.removeEventListener('TabClose', this, false);
tabs.removeEventListener('TabPinned', this, false);
tabs.removeEventListener('TabUnpinned', this, false);
tabs.removeAttribute('vertical');
// Restore all individual tabs.
for (let i = 0; i < tabs.childNodes.length; i++) {
let tab = tabs.childNodes[i];
tab.setAttribute('crop', 'end');
tab.removeAttribute('verticaltabs-id');
}
// Remove all the crap we added.
browserbox.removeChild(leftbox);
browserbox.removeChild(splitter);
browserbox.removeAttribute('dir');
mainWindow.removeAttribute('tabspinned');
mainWindow.removeAttribute('tabspinnedwidth');
mainWindow.setAttribute('persist',
mainWindow.getAttribute('persist').replace(' tabspinnned', ''));
leftbox = null;
// Restore the tab strip.
toolbar.insertBefore(tabs, toolbar.children[tabsIndex]);
toolbox.insertBefore(toolbar, navbar);
browserPanel.insertBefore(toolbox, browserPanel.firstChild);
browserPanel.insertBefore(bottom, document.getElementById('fullscreen-warning').nextSibling);
this.window.TabsInTitlebar.updateAppearance(true);
});
},
initContextMenu: function () {
const document = this.document;
const tabs = document.getElementById('tabbrowser-tabs');
let closeMultiple = null;
if (this.multiSelect) {
closeMultiple = this.createElement('menuitem', {
'id': 'context_verticalTabsCloseMultiple',
'label': 'Close Selected Tabs',
'tbattr': 'tabbrowser-multiple',
'oncommand': 'gBrowser.tabContainer.VTMultiSelect.closeSelected();'
});
tabs.contextMenu.appendChild(closeMultiple);
}
tabs.contextMenu.addEventListener('popupshowing', this, false);
this.unloaders.push(function () {
if (closeMultiple) {
tabs.contextMenu.removeChild(closeMultiple);
}
tabs.contextMenu.removeEventListener('popupshowing', this, false);
});
},
initTab: function (aTab) {
if (this.pushToTop) {
this.window.gBrowser.moveTabTo(aTab, 0);
}
aTab.classList.add('tab-visible');
aTab.classList.remove('tab-hidden');
if (this.document.getElementById('main-window').getAttribute('tabspinned') !== 'true') {
aTab.removeAttribute('crop');
} else {
aTab.setAttribute('crop', 'end');
}
},
unload: function () {
this.unloaders.forEach(function (func) {
func.call(this);
}, this);
},
checkScrollToTab: function (tab) {
let elemTop = tab.getBoundingClientRect().top;
let elemBottom = tab.getBoundingClientRect().bottom;
let overTop = elemTop < 63;
let overBottom = elemBottom > this.window.innerHeight;
if (overTop) {
tab.scrollIntoView(true);
} else if (overBottom) {
tab.scrollIntoView(false);
}
},
/*** Event handlers ***/
handleEvent: function (aEvent) {
switch (aEvent.type) {
case 'DOMContentLoaded':
this.init();
return;
case 'TabOpen':
this.onTabOpen(aEvent);
return;
case 'TabClose':
this.onTabClose(aEvent);
return;
case 'TabPinned':
this.onTabPinned(aEvent);
return;
case 'TabUnpinned':
this.onTabUnpinned(aEvent);
return;
case 'mouseup':
this.onMouseUp(aEvent);
return;
case 'popupshowing':
this.onPopupShowing(aEvent);
return;
}
},
onTabOpen: function (aEvent) {
let tab = aEvent.target;
this.stats.tabs_created++;
this.initTab(tab);
},
onTabClose: function (aEvent) {
this.stats.tabs_destroyed++;
},
onTabPinned: function (aEvent) {
this.stats.tabs_pinned++;
},
onTabUnpinned: function (aEvent) {
this.stats.tabs_unpinned++;
},
onPopupShowing: function (aEvent) {
if (!this.multiSelect) {
return;
}
let closeTabs = this.document.getElementById('context_verticalTabsCloseMultiple');
let tabs = this.multiSelect.getSelected();
if (tabs.length > 1) {
closeTabs.disabled = false;
} else {
closeTabs.disabled = true;
}
},
sendStats: function (payload) {
this.addPingStats(this.stats);
this.stats = this.newPayload();
}
};