forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allTabsMenuExpansionPack.uc.js
1263 lines (1233 loc) · 48.2 KB
/
allTabsMenuExpansionPack.uc.js
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name All Tabs Menu Expansion Pack
// @version 2.1.6
// @author aminomancer
// @homepageURL https://github.com/aminomancer
// @long-description
// @description
/*
Next to the "new tab" button in Firefox there's a V-shaped button that opens a big scrolling menu containing all the tabs. This script adds several new features to the "all tabs menu" to help it catch up to the functionality of the regular tabs bar.
1. Allows you to drag and drop tabs in the all tabs menu.
2. Adds an animated close button for every tab in this menu.
3. Allows you to multiselect tabs in the all tabs menu and close an unlimited number of tabs at once without closing/blurring the popup.
4. Significantly improves the mute/unmute button by making it work like the mute button in the tabs bar used to work.
- If you only have one tab selected, it mutes/unmutes that tab.
- If you have multiple tabs selected, it mutes/unmutes all of them.
- This also adds a tooltip to the mute button.
5. By default, Firefox doesn't do anything to differentiate loaded tabs from unloaded tabs. But for the regular tab bar, unloaded tabs gain an attribute `pending="true"` which you can use to dim them. This way you know which tabs are already initialized and which will actually start up when you click them. Pretty useful if you frequently have 100+ tabs like me.
- This script adds the same functionality to the all tabs menu, but does not add "pending" styling to regular tabs since it's outside the scope of this project. To do it yourself just add a rule like `.tabbrowser-tab .tab-content{opacity:.6;}`
- If you use [Unread Tab Mods][], this integrates with it to make unread tabs display with italic text.
6. Adds color stripes to multiselected tabs and container tabs in the "all tabs menu" so you can differentiate them from normal tabs.
7. Includes a preference `userChrome.tabs.all-tabs-menu.reverse-order` that lets you reverse the order of the tabs so that newer tabs are displayed on top rather than on bottom.
8. Modifies the all tabs button's tooltip to display the number of tabs as well as the shortcut to open the all tabs menu, Ctrl+Shift+Tab.
9. Allows the panel to display pinned tabs, and displays a pin icon on them.
10. Makes the sound icon show if the tab has blocked media or media in picture-in-picture, just like regular tabs.
11. Adds an optional preference `userChrome.ctrlTab.skip-show-all-button` that lets you skip past the "List All x Tabs" button when hitting Ctrl+Tab.
12. And a few other subtle improvements. All the relevant CSS for this is already included in and loaded by the script. It's designed to look consistent with my theme as well as with the latest vanilla (proton) Firefox. If you need to change anything, see the "const css" line in here, or the end of uc-tabs-bar.css on my repo.
[Unread Tab Mods]: https://github.com/aminomancer/uc.css.js/blob/master/JS/unreadTabMods.uc.js
*/
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/allTabsMenuExpansionPack.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/allTabsMenuExpansionPack.uc.js
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// ==/UserScript==
(function() {
let prefSvc = Services.prefs;
let reversePref = "userChrome.tabs.all-tabs-menu.reverse-order";
let skipShowAllPref = "userChrome.ctrlTab.skip-show-all-button";
const lazy = {};
ChromeUtils.defineModuleGetter(
lazy,
"TabsPanel",
"resource:///modules/TabsList.jsm"
);
/**
* create a DOM node with given parameters
* @param {object} aDoc (which doc to create the element in)
* @param {string} tag (an HTML tag name, like "button" or "p")
* @param {object} props (an object containing attribute name/value pairs, e.g. class: ".bookmark-item")
* @param {boolean} isHTML (if true, create an HTML element. if omitted or false, create a XUL element. generally avoid HTML when modding the UI, most UI elements are actually XUL elements.)
* @returns the created DOM node
*/
function create(aDoc, tag, props, isHTML = false) {
let el = isHTML ? aDoc.createElement(tag) : aDoc.createXULElement(tag);
for (let prop in props) el.setAttribute(prop, props[prop]);
return el;
}
function setAttributes(element, attrs) {
for (let [name, value] of Object.entries(attrs)) {
if (value) element.setAttribute(name, value);
else element.removeAttribute(name);
}
}
function findRow(el) {
return el.classList.contains("all-tabs-item")
? el
: el.closest(".all-tabs-item");
}
function l10nIfNeeded() {
let lazies = document
.getElementById("tabContextMenu")
.querySelectorAll("[data-lazy-l10n-id]");
if (lazies) {
MozXULElement.insertFTLIfNeeded("browser/tabContextMenu.ftl");
lazies.forEach(el => {
el.setAttribute("data-l10n-id", el.getAttribute("data-lazy-l10n-id"));
el.removeAttribute("data-lazy-l10n-id");
});
}
}
function reverseTabOrder() {
let panel = lazy.TabsPanel.prototype;
if (
prefSvc.getBoolPref(reversePref) &&
!lazy.TabsPanel.prototype.reversed
) {
eval(
`panel._populate = function ${panel._populate
.toSource()
.replace(
/super\.\_populate\(event\)\;/,
Object.getPrototypeOf(panel)
._populate.toSource()
.replace(/^.*\n\s*/, "")
.replace(/\n.*$/, "")
)
.replace(
/appendChild/,
`prepend`
)}\n panel._addTab = function ${panel._addTab
.toSource()
.replace(
/nextRow\.parentNode\.insertBefore\(newRow\, nextRow\)\;/,
`nextRow.after(newRow)`
)
.replace(/this\.\_addElement/, `this.containerNode.prepend`)}`
);
lazy.TabsPanel.prototype.reversed = true;
} else {
delete panel._populate;
delete panel._addTab;
lazy.TabsPanel.prototype.reversed = false;
}
}
// Adjust the PanelView class methods for each panelview instance to improve
// key navigation and prevent focusing hidden elements.
function modifyWalkers(tabsPanel) {
let panelViewClass = PanelView.forNode(tabsPanel.view);
if (
!panelViewClass.moveSelection.toSource().startsWith("(function uc_ATMEP_")
) {
panelViewClass.moveSelection = function uc_ATMEP_moveSelection(
isDown,
arrowKey = false
) {
let walker = arrowKey
? this._arrowNavigableWalker
: this._tabNavigableWalker;
let oldSel = this.selectedElement;
let newSel;
if (oldSel) {
walker.currentNode = oldSel;
newSel = isDown ? walker.nextNode() : walker.previousNode();
}
// If we couldn't find something, select the first or last item:
if (!newSel) {
walker.currentNode = walker.root;
newSel = isDown ? walker.firstChild() : walker.lastChild();
}
if (oldSel?.classList.contains("all-tabs-secondary-button")) {
if (oldSel.parentElement === newSel?.parentElement) {
walker.currentNode = newSel;
newSel = isDown ? walker.nextNode() : walker.previousNode();
}
}
this.selectedElement = newSel;
return newSel;
};
}
if (!panelViewClass.hasOwnProperty("moveSelectionHorizontal")) {
panelViewClass.moveSelectionHorizontal = function uc_ATMEP_moveSelectionHorizontal(
isNext
) {
let walker = this._horizontalNavigableWalker;
let oldSel = this.selectedElement;
let newSel;
if (oldSel) {
walker.currentNode = oldSel;
newSel = isNext ? walker.nextNode() : walker.previousNode();
}
// If we couldn't find something, select the first or last item:
if (!newSel) {
walker.currentNode = walker.root;
newSel = isNext ? walker.firstChild() : walker.lastChild();
}
this.selectedElement = newSel;
return newSel;
};
}
if (!panelViewClass.hasOwnProperty("_horizontalNavigableWalker")) {
Object.defineProperty(panelViewClass, "_horizontalNavigableWalker", {
get() {
if (!this.__horizontalNavigableWalker) {
this.__horizontalNavigableWalker = this._makeNavigableTreeWalker(
true,
false
);
}
return this.__horizontalNavigableWalker;
},
});
}
if (
!panelViewClass._makeNavigableTreeWalker
.toSource()
.startsWith("(function uc_ATMEP_")
) {
eval(
`panelViewClass._makeNavigableTreeWalker = function ${panelViewClass._makeNavigableTreeWalker
.toSource()
.replace(/^\(/, "")
.replace(/\)$/, "")
.replace(/^_makeNavigableTreeWalker\s*/, "")
.replace(/^function\s*/, "")
.replace(/^(.)/, `uc_ATMEP_makeNavigableTreeWalker $1`)
.replace(/\(arrowKey\)/, `(arrowKey, vertical = true)`)
// .replace(/(node\.disabled)/, `$1 || node.hidden`)
.replace(
/(let bounds = this)/,
`if (node.hidden) {\n return NodeFilter.FILTER_SKIP;\n }\n $1`
)
.replace(
/(\(!arrowKey && this\._isNavigableWithTabOnly\(node\)\)\n\s*\) \{)/,
/* javascript */ `$1
if (vertical && node.classList.contains("all-tabs-secondary-button")) return NodeFilter.FILTER_SKIP;`
)}`
);
}
if (
!panelViewClass.keyNavigation.toSource().startsWith("(function uc_ATMEP_")
) {
eval(
`panelViewClass.keyNavigation = function ${panelViewClass.keyNavigation
.toSource()
.replace(/^\(/, "")
.replace(/\)$/, "")
.replace(/^keyNavigation\s*/, "")
.replace(/^function\s*/, "")
.replace(/^(.)/, `uc_ATMEP_keyNavigation $1`)
.replace(
/(if \(\n\s*\(!this\.window\.RTL_UI && keyCode == \"ArrowLeft\"\) \|\|)/,
/* javascript */ `if (this.selectedElement && this.selectedElement.matches(".all-tabs-button, .all-tabs-secondary-button")) {
let isNext = (this.window.RTL_UI && keyCode == "ArrowLeft") || (!this.window.RTL_UI && keyCode == "ArrowRight");
let nextButton = this.moveSelectionHorizontal(isNext);
Services.focus.setFocus(nextButton, Services.focus.FLAG_BYKEY);
break;
}
$1`
)}`
);
delete panelViewClass.__arrowNavigableWalker;
delete panelViewClass.__tabNavigableWalker;
}
}
function prefHandler(_sub, _top, _pref) {
let multiview = gTabsPanel.allTabsPanel.panelMultiView;
if (multiview) {
multiview.addEventListener("PanelMultiViewHidden", reverseTabOrder, {
once: true,
});
} else {
reverseTabOrder();
}
}
function init() {
gTabsPanel.init();
registerSheet();
let tabsPanels = [
gTabsPanel.allTabsPanel,
gTabsPanel.hiddenAudioTabsPopup,
gTabsPanel.hiddenTabsPopup,
];
let vanillaTooltip = document.getElementById("tabbrowser-tab-tooltip");
let tooltip = vanillaTooltip.cloneNode(true);
vanillaTooltip.after(tooltip);
tooltip.id = "all-tabs-tooltip";
tooltip.setAttribute(
"onpopupshowing",
`gTabsPanel.createTabTooltip(event)`
);
tooltip.setAttribute("position", "after_end");
gTabsPanel.createTabTooltip = function(e) {
e.stopPropagation();
let row = e.target.triggerNode?.closest(".all-tabs-item");
let tab = row?.tab;
if (!tab) {
e.preventDefault();
return;
}
let id, args;
let align = true;
let { linkedBrowser } = tab;
const selectedTabs = gBrowser.selectedTabs;
const contextTabInSelection = selectedTabs.includes(tab);
const tabCount = contextTabInSelection ? selectedTabs.length : 1;
if (row.querySelector("[close-button]").matches(":hover")) {
id = "tabbrowser-close-tabs-tooltip";
args = { tabCount };
align = false;
} else if (row.querySelector("[toggle-mute]").matches(":hover")) {
args = { tabCount };
if (contextTabInSelection) {
id = linkedBrowser.audioMuted
? "tabbrowser-unmute-tab-audio-tooltip"
: "tabbrowser-mute-tab-audio-tooltip";
const keyElem = document.getElementById("key_toggleMute");
args.shortcut = ShortcutUtils.prettifyShortcut(keyElem);
} else if (tab.hasAttribute("activemedia-blocked")) {
id = "tabbrowser-unblock-tab-audio-tooltip";
} else {
id = linkedBrowser.audioMuted
? "tabbrowser-unmute-tab-audio-background-tooltip"
: "tabbrowser-mute-tab-audio-background-tooltip";
}
align = false;
} else {
id = "tabbrowser-tab-tooltip";
args = { title: gBrowser.getTabTooltip(tab, true) };
}
if (align) {
e.target.setAttribute("position", "after_start");
e.target.moveToAnchor(row, "after_start");
}
let title = e.target.querySelector(".places-tooltip-title");
let localized = {};
if (id) {
let [msg] = gBrowser.tabLocalization.formatMessagesSync([{ id, args }]);
localized.value = msg.value;
if (msg.attributes) {
for (let attr of msg.attributes) localized[attr.name] = attr.value;
}
}
title.textContent = localized.label ?? "";
if (tab.getAttribute("customizemode") === "true") {
e.target
.querySelector(".places-tooltip-box")
.setAttribute("desc-hidden", "true");
return;
}
let url = e.target.querySelector(".places-tooltip-uri");
url.value = linkedBrowser?.currentURI?.spec.replace(/^https:\/\//, "");
e.target
.querySelector(".places-tooltip-box")
.removeAttribute("desc-hidden");
// show a lock icon to show tab security/encryption
let icon = e.target.querySelector("#places-tooltip-insecure-icon");
let pending =
tab.hasAttribute("pending") || !linkedBrowser.browsingContext;
let docURI = pending
? linkedBrowser?.currentURI
: linkedBrowser?.documentURI || linkedBrowser?.currentURI;
if (docURI) {
let homePage = new RegExp(
`(${BROWSER_NEW_TAB_URL}|${HomePage.get(window)})`,
"i"
).test(docURI.spec);
if (homePage) {
icon.setAttribute("type", "home-page");
icon.hidden = false;
return;
}
switch (docURI.scheme) {
case "file":
case "resource":
case "chrome":
icon.setAttribute("type", "local-page");
icon.hidden = false;
return;
case "about":
let pathQueryRef = docURI?.pathQueryRef;
if (
pathQueryRef &&
/^(neterror|certerror|httpsonlyerror)/.test(pathQueryRef)
) {
icon.setAttribute("type", "error-page");
icon.hidden = false;
return;
}
if (docURI.filePath === "blocked") {
icon.setAttribute("type", "blocked-page");
icon.hidden = false;
return;
}
icon.setAttribute("type", "about-page");
icon.hidden = false;
return;
case "moz-extension":
icon.setAttribute("type", "extension-page");
icon.hidden = false;
return;
}
}
if (linkedBrowser.browsingContext) {
let prog = Ci.nsIWebProgressListener;
let state = linkedBrowser?.securityUI?.state;
if (typeof state != "number" || state & prog.STATE_IS_SECURE) {
icon.hidden = true;
icon.setAttribute("type", "secure");
return;
}
if (state & prog.STATE_IS_INSECURE) {
icon.setAttribute("type", "insecure");
icon.hidden = false;
return;
}
if (state & prog.STATE_IS_BROKEN) {
if (state & prog.STATE_LOADED_MIXED_ACTIVE_CONTENT) {
icon.hidden = false;
icon.setAttribute("type", "insecure");
} else {
icon.setAttribute("type", "mixed-passive");
icon.hidden = false;
}
return;
}
}
icon.hidden = true;
icon.setAttribute("type", pending ? "pending" : "secure");
};
gTabsPanel.allTabsButton.setAttribute(
"onmouseover",
/* javascript */ `this.tooltipText=(gBrowser.tabs.length>1?PluralForm.get(gBrowser.tabs.length,gNavigatorBundle.getString("ctrlTab.listAllTabs.label")).replace("#1",gBrowser.tabs.length).toLocaleLowerCase().replace(RTL_UI?/.$/i:/^./i,(function(letter){return letter.toLocaleUpperCase()})).trim():this.label)+(Services.prefs.getBoolPref("browser.ctrlTab.sortByRecentlyUsed",false)?" ("+ShortcutUtils.prettifyShortcut(key_showAllTabs)+")":"");`
);
if (!("reversed" in lazy.TabsPanel.prototype)) reverseTabOrder();
setupPIP();
setupCtrlTab();
tabsPanels.forEach(modifyWalkers);
tabsPanels.forEach(setupTabsPanel);
}
function setupPIP() {
let gNextWindowID = 0;
let handleRequestSrc = PictureInPicture.handlePictureInPictureRequest.toSource();
if (!handleRequestSrc.includes("_tabAttrModified")) {
eval(
`PictureInPicture.handlePictureInPictureRequest = async function ${handleRequestSrc
.replace(/async handlePictureInPictureRequest/, "")
.replace(/\sServices\.telemetry.*\s*.*\s*.*\s*.*/, "")
.replace(/gCurrentPlayerCount.*/g, "")
.replace(
/(tab\.setAttribute\(\"pictureinpicture\".*)/,
`$1 parentWin.gBrowser._tabAttrModified(tab, ["pictureinpicture"]);`
)}`
);
}
let clearIconSrc = PictureInPicture.clearPipTabIcon.toSource();
if (!clearIconSrc.includes("_tabAttrModified")) {
eval(
`PictureInPicture.clearPipTabIcon = function ${clearIconSrc
.replace(/WINDOW\_TYPE/, `"Toolkit:PictureInPicture"`)
.replace(
/(tab\.removeAttribute\(\"pictureinpicture\".*)/,
`$1 gBrowser._tabAttrModified(tab, ["pictureinpicture"]);`
)}`
);
}
}
function setupCtrlTab() {
function excludeShowAll() {
ctrlTab.showAllButton.setAttribute("tabindex", "-1");
ctrlTab.previews = ctrlTab.previews.filter(
b => b.id !== "ctrlTab-showAll"
);
}
if (prefSvc.getBoolPref(skipShowAllPref, false)) excludeShowAll();
prefSvc.addObserver(skipShowAllPref, (sub, top, pref) => {
if (sub.getBoolPref(pref)) {
excludeShowAll();
} else {
ctrlTab.showAllButton.removeAttribute("tabindex");
delete ctrlTab.previews;
ctrlTab.previews = [];
let previewsContainer = document.getElementById("ctrlTab-previews");
for (let i = 0; i < ctrlTab.maxTabPreviews; i++) {
let preview = ctrlTab._makePreview();
previewsContainer.appendChild(preview);
ctrlTab.previews.push(preview);
}
ctrlTab.previews.push(ctrlTab.showAllButton);
}
});
}
function setupTabsPanel(tabsPanel) {
tabsPanel.tabEvents = [
"TabAttrModified",
"TabClose",
"TabMove",
"TabHide",
"TabShow",
"TabPinned",
"TabUnpinned",
"TabSelect",
"TabBrowserDiscarded",
];
tabsPanel._setupListeners = function() {
this.listenersRegistered = true;
this.tabEvents.forEach(ev =>
gBrowser.tabContainer.addEventListener(ev, this)
);
this.gBrowser.addEventListener("TabMultiSelect", this);
this.panelMultiView.addEventListener("PanelMultiViewHidden", this);
};
tabsPanel._cleanupListeners = function() {
this.tabEvents.forEach(ev =>
gBrowser.tabContainer.removeEventListener(ev, this)
);
this.gBrowser.removeEventListener("TabMultiSelect", this);
this.panelMultiView.removeEventListener("PanelMultiViewHidden", this);
this.listenersRegistered = false;
};
tabsPanel._createRow = function(tab) {
let { doc } = this;
let row = create(doc, "toolbaritem", {
class: "all-tabs-item",
context: "tabContextMenu",
tooltip: "all-tabs-tooltip",
draggable: true,
});
if (this.className) row.classList.add(this.className);
row.tab = tab;
row.mOverSecondaryButton = false;
row.addEventListener("command", this);
row.addEventListener("mousedown", this);
row.addEventListener("mouseup", this);
row.addEventListener("click", this);
row.addEventListener("mouseover", this);
row.addEventListener("mouseout", this);
this.tabToElement.set(tab, row);
let button = row.appendChild(
create(document, "toolbarbutton", {
class: "all-tabs-button subviewbutton subviewbutton-iconic",
flex: "1",
crop: "right",
})
);
button.tab = tab;
let secondaryButton = row.appendChild(
create(document, "toolbarbutton", {
class: "all-tabs-secondary-button subviewbutton subviewbutton-iconic",
closemenu: "none",
"toggle-mute": "true",
})
);
secondaryButton.tab = tab;
let closeButton = row.appendChild(
create(document, "toolbarbutton", {
class: "all-tabs-secondary-button subviewbutton subviewbutton-iconic",
"close-button": "true",
})
);
closeButton.tab = tab;
this._setRowAttributes(row, tab);
return row;
};
tabsPanel._setRowAttributes = function(row, tab) {
setAttributes(row, {
selected: tab.selected,
pinned: tab.pinned,
pending: tab.getAttribute("pending"),
multiselected: tab.getAttribute("multiselected"),
notselectedsinceload: tab.getAttribute("notselectedsinceload"),
"tab-hidden": tab.hidden,
});
if (tab.userContextId) {
let idColor = ContextualIdentityService.getPublicIdentityFromId(
tab.userContextId
)?.color;
row.className = idColor
? `all-tabs-item identity-color-${idColor}`
: "all-tabs-item";
row.setAttribute("usercontextid", tab.userContextId);
} else {
row.className = "all-tabs-item";
row.removeAttribute("usercontextid");
}
let busy = tab.getAttribute("busy");
setAttributes(row.querySelector(".all-tabs-button"), {
busy,
label: tab.label,
image: !busy && tab.getAttribute("image"),
iconloadingprincipal: tab.getAttribute("iconloadingprincipal"),
});
this._setImageAttributes(row, tab);
let secondaryButton = row.querySelector(
".all-tabs-secondary-button[toggle-mute]"
);
setAttributes(secondaryButton, {
muted: tab.muted,
soundplaying: tab.soundPlaying,
"activemedia-blocked": tab.activeMediaBlocked,
pictureinpicture: tab.pictureinpicture,
hidden: !(
tab.muted ||
tab.soundPlaying ||
tab.activeMediaBlocked ||
tab.pictureinpicture
),
});
};
tabsPanel._moveTab = function(tab) {
let item = this.tabToElement.get(tab);
if (item) {
this._removeItem(item, tab);
this._addTab(tab);
this.containerNode
.querySelector(".all-tabs-item[selected]")
.scrollIntoView({ block: "nearest", behavior: "smooth" });
}
};
tabsPanel.handleEvent = function(e) {
let { tab } = e.target;
switch (e.type) {
case "ViewShowing":
if (!this.listenersRegistered && e.target == this.view) {
this.panelMultiView = this.view.panelMultiView;
this._populate(e);
}
break;
case "mousedown":
this._onMouseDown(e, tab);
break;
case "mouseup":
this._onMouseUp(e, tab);
break;
case "click":
this._onClick(e, tab);
break;
case "command":
this._onCommand(e, tab);
break;
case "mouseover":
this._onMouseOver(e, tab);
break;
case "mouseout":
this._onMouseOut(e);
break;
case "TabPinned":
case "TabUnpinned":
case "TabAttrModified":
case "TabBrowserDiscarded":
this._tabAttrModified(e.target);
break;
case "TabHide":
case "TabShow":
this._tabAttrModified(e.target);
let item = this.tabToElement.get(e.target);
if (item) {
item.scrollIntoView({ block: "nearest", behavior: "smooth" });
}
break;
case "TabClose":
this._tabClose(e.target);
break;
case "TabMove":
this._moveTab(e.target);
break;
case "dragstart":
this._onDragStart(e, tab);
break;
case "dragleave":
this._onDragLeave(e);
break;
case "dragover":
this._onDragOver(e);
break;
case "dragend":
this._onDragEnd(e);
break;
case "drop":
this._onDrop(e);
break;
case "TabMultiSelect":
this._onTabMultiSelect();
break;
case "TabSelect":
if (this.listenersRegistered) {
let item = this.tabToElement.get(e.target);
if (item) {
item.scrollIntoView({ block: "nearest", behavior: "smooth" });
}
}
break;
case "PanelMultiViewHidden":
if (e.target == this.panelMultiView) {
this._cleanup();
this.panelMultiView = null;
}
break;
}
};
tabsPanel._onMouseDown = function(e, tab) {
if (e.button !== 0) return;
if (tab.hidden) {
if (tab.getAttribute("pending") || tab.getAttribute("busy")) {
tab.noCanvas = true;
} else {
delete tab.noCanvas;
}
return;
}
let accelKey = AppConstants.platform == "macosx" ? e.metaKey : e.ctrlKey;
if (e.shiftKey) {
const lastSelectedTab = this.gBrowser.lastMultiSelectedTab;
if (!accelKey) {
this.gBrowser.selectedTab = lastSelectedTab;
this.gBrowser.clearMultiSelectedTabs();
}
this.gBrowser.addRangeToMultiSelectedTabs(lastSelectedTab, tab);
} else if (accelKey) {
if (tab.multiselected) {
this.gBrowser.removeFromMultiSelectedTabs(tab);
} else if (tab != this.gBrowser.selectedTab) {
this.gBrowser.addToMultiSelectedTabs(tab);
this.gBrowser.lastMultiSelectedTab = tab;
}
} else {
if (!tab.selected && tab.multiselected) {
this.gBrowser.lockClearMultiSelectionOnce();
}
if (
!e.shiftKey &&
!accelKey &&
!e.target.classList.contains("all-tabs-secondary-button") &&
tab !== this.gBrowser.selectedTab
) {
if (tab.getAttribute("pending") || tab.getAttribute("busy")) {
tab.noCanvas = true;
} else {
delete tab.noCanvas;
}
if (this.gBrowser.selectedTab != tab) this.gBrowser.selectedTab = tab;
else this.gBrowser.tabContainer._handleTabSelect();
}
}
if (e.target.closest(".all-tabs-item")?.mOverSecondaryButton) {
e.stopPropagation();
e.preventDefault();
}
};
tabsPanel._onMouseUp = function(e, tab) {
if (e.button === 2) return;
if (e.button === 1) {
this.gBrowser.removeTab(tab, {
animate: true,
byMouse: false,
});
return;
}
if (e.target.classList.contains("all-tabs-secondary-button")) return;
if (tab.hidden) return this._onCommand(e, tab);
if (
e.shiftKey ||
(AppConstants.platform == "macosx" ? e.metaKey : e.ctrlKey)
) {
return;
}
delete tab.noCanvas;
this.gBrowser.unlockClearMultiSelection();
this.gBrowser.clearMultiSelectedTabs();
PanelMultiView.hidePopup(
PanelMultiView.forNode(this.view.panelMultiView)._panel
);
};
tabsPanel._onClick = function(e, tab) {
if (e.button === 0) {
if (
e.target.classList.contains("all-tabs-secondary-button") &&
!e.shiftKey &&
!(AppConstants.platform == "macosx" ? e.metaKey : e.ctrlKey)
) {
return;
}
e.preventDefault();
}
};
tabsPanel._onCommand = function(e, tab) {
if (e.target.hasAttribute("activemedia-blocked")) {
if (tab.multiselected) {
this.gBrowser.resumeDelayedMediaOnMultiSelectedTabs(tab);
} else {
tab.resumeDelayedMedia();
}
return;
}
if (e.target.hasAttribute("toggle-mute")) {
if (tab.multiselected) {
this.gBrowser.toggleMuteAudioOnMultiSelectedTabs(tab);
} else {
tab.toggleMuteAudio();
}
return;
}
if (e.target.hasAttribute("close-button")) {
if (tab.multiselected) this.gBrowser.removeMultiSelectedTabs();
else this.gBrowser.removeTab(tab, { animate: true });
return;
}
if (!gSharedTabWarning.willShowSharedTabWarning(tab)) {
if (tab !== this.gBrowser.selectedTab) this._selectTab(tab);
}
delete tab.noCanvas;
};
tabsPanel._onDragStart = function(e, tab) {
let row = e.target;
if (!tab || this.gBrowser.tabContainer._isCustomizing) return;
let selectedTabs = this.gBrowser.selectedTabs;
let otherSelectedTabs = selectedTabs.filter(
selectedTab => selectedTab != tab
);
let dataTransferOrderedTabs = [tab].concat(otherSelectedTabs);
let dt = e.dataTransfer;
for (let i = 0; i < dataTransferOrderedTabs.length; i++) {
let dtTab = dataTransferOrderedTabs[i];
dt.mozSetDataAt("all-tabs-item", dtTab, i);
}
dt.mozCursor = "default";
dt.addElement(row);
// if multiselected tabs aren't adjacent, make them adjacent
if (tab.multiselected) {
function newIndex(aTab, index) {
if (aTab.pinned) {
return Math.min(index, this.gBrowser._numPinnedTabs - 1);
}
return Math.max(index, this.gBrowser._numPinnedTabs);
}
let tabIndex = selectedTabs.indexOf(tab);
let draggedTabPos = tab._tPos;
// tabs to the left of the dragged tab
let insertAtPos = draggedTabPos - 1;
for (let i = tabIndex - 1; i > -1; i--) {
insertAtPos = newIndex(selectedTabs[i], insertAtPos);
if (
insertAtPos &&
!selectedTabs[i].nextElementSibling.multiselected
) {
this.gBrowser.moveTabTo(selectedTabs[i], insertAtPos);
}
}
// tabs to the right
insertAtPos = draggedTabPos + 1;
for (let i = tabIndex + 1; i < selectedTabs.length; i++) {
insertAtPos = newIndex(selectedTabs[i], insertAtPos);
if (
insertAtPos &&
!selectedTabs[i].previousElementSibling.multiselected
) {
this.gBrowser.moveTabTo(selectedTabs[i], insertAtPos);
}
}
}
// tab preview
if (
!tab.noCanvas &&
(AppConstants.platform == "win" || AppConstants.platform == "macosx")
) {
delete tab.noCanvas;
let scale = window.devicePixelRatio;
let canvas = this._dndCanvas;
if (!canvas) {
this._dndCanvas = canvas = document.createElementNS(
"http://www.w3.org/1999/xhtml",
"canvas"
);
canvas.style.width = "100%";
canvas.style.height = "100%";
canvas.mozOpaque = true;
}
canvas.width = 160 * scale;
canvas.height = 90 * scale;
let toDrag = canvas;
let dragImageOffset = -16;
let browser = tab.linkedBrowser;
if (gMultiProcessBrowser) {
let context = canvas.getContext("2d");
context.fillStyle = getComputedStyle(this.view).getPropertyValue(
"background-color"
);
context.fillRect(0, 0, canvas.width, canvas.height);
let captureListener = () =>
dt.updateDragImage(canvas, dragImageOffset, dragImageOffset);
PageThumbs.captureToCanvas(browser, canvas).then(captureListener);
} else {
PageThumbs.captureToCanvas(browser, canvas);
dragImageOffset = dragImageOffset * scale;
}
dt.setDragImage(toDrag, dragImageOffset, dragImageOffset);
}
tab._dragData = {
movingTabs: (tab.multiselected
? this.gBrowser.selectedTabs
: [tab]
).filter(this.filterFn),
};
e.stopPropagation();
};
// set the drop target style with an attribute, "dragpos", which is either
// "after" or "before"
tabsPanel._onDragOver = function(e) {
let row = findRow(e.target);
let dt = e.dataTransfer;
if (
!dt.types.includes("all-tabs-item") ||
!row ||
row.tab.multiselected
) {
dt.mozCursor = "auto";
return;
}
dt.mozCursor = "default";
let draggedTab = dt.mozGetDataAt("all-tabs-item", 0);
if (row.tab === draggedTab) return;
if (row.tab.pinned !== draggedTab.pinned) return;
// whether a tab will be placed before or after the drop target depends on
// 1) whether the drop target is above or below the dragged tab, and 2)
// whether the order of the tab list is reversed.
function getPosition() {
return prefSvc.getBoolPref(reversePref)
? row.tab._tPos < draggedTab._tPos
: row.tab._tPos > draggedTab._tPos;
}
let position = getPosition() ? "after" : "before";
row.setAttribute("dragpos", position);
e.preventDefault();
};
// remove the drop target style.
tabsPanel._onDragLeave = function(e) {
let row = findRow(e.target);
let dt = e.dataTransfer;
dt.mozCursor = "auto";
if (!dt.types.includes("all-tabs-item") || !row) return;
this.containerNode
.querySelectorAll("[dragpos]")
.forEach(item => item.removeAttribute("dragpos"));
};
// move the tab(s)
tabsPanel._onDrop = function(e) {
let row = findRow(e.target);
let dt = e.dataTransfer;
let tabBar = this.gBrowser.tabContainer;
if (!dt.types.includes("all-tabs-item") || !row) return;
let draggedTab = dt.mozGetDataAt("all-tabs-item", 0);
let movingTabs = draggedTab._dragData.movingTabs;
if (
!movingTabs ||
dt.mozUserCancelled ||
dt.dropEffect === "none" ||
tabBar._isCustomizing
) {
delete draggedTab._dragData;
return;
}
tabBar._finishGroupSelectedTabs(draggedTab);
if (draggedTab) {
let newIndex = row.tab._tPos;
const dir = newIndex < movingTabs[0]._tPos;
movingTabs.forEach(tab => {
if (tab.pinned !== row.tab.pinned) return;
this.gBrowser.moveTabTo(
dt.dropEffect == "copy" ? this.gBrowser.duplicateTab(tab) : tab,
dir ? newIndex++ : newIndex
);
});
}
row.removeAttribute("dragpos");
e.stopPropagation();
};
// clean up remaining crap
tabsPanel._onDragEnd = function(e) {
let draggedTab = e.dataTransfer.mozGetDataAt("all-tabs-item", 0);
delete draggedTab._dragData;
delete draggedTab.noCanvas;
for (let row of this.rows) row.removeAttribute("dragpos");
};
tabsPanel._onTabMultiSelect = function() {
for (let item of this.rows) {
item.toggleAttribute("multiselected", !!item.tab.multiselected);
}
};
tabsPanel._onMouseOver = function(e, tab) {
let row = e.target.closest(".all-tabs-item");
SessionStore.speculativeConnectOnTabHover(tab);
if (e.target.classList.contains("all-tabs-secondary-button")) {
row.mOverSecondaryButton = true;
}
if (e.target.hasAttribute("close-button")) {
tab = gBrowser._findTabToBlurTo(tab);
}
gBrowser.warmupTab(tab);
};
tabsPanel._onMouseOut = function(e) {
let row = e.target.closest(".all-tabs-item");
if (e.target.classList.contains("all-tabs-secondary-button")) {
row.mOverSecondaryButton = false;
}
};
tabsPanel.view.addEventListener("ViewShowing", l10nIfNeeded, {