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
|
/* 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";
// This is loaded into all XUL windows. Wrap in a block to prevent
// leaking to window scope.
{
const { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
// For the Windows 10 custom context menu styling, we need to know if we need
// a gutter for checkboxes. To do this, check whether there are any
// radio/checkbox type menuitems in a menupopup when showing it. We use a
// system bubbling event listener to ensure we run *after* the "normal"
// popupshowing listeners, so (visibility) changes they make to their items
// take effect first, before we check for checkable menuitems.
if (AppConstants.isPlatformAndVersionAtLeast("win", "6.4")) {
Services.els.addSystemEventListener(
document,
"popupshowing",
function(e) {
if (e.target.nodeName == "menupopup") {
let haveCheckableChild = e.target.querySelector(
":scope > menuitem:not([hidden]):is([type=checkbox],[type=radio])"
);
e.target.toggleAttribute("needsgutter", haveCheckableChild);
}
},
false
);
}
class MozMenuPopup extends MozElements.MozElementMixin(XULPopupElement) {
constructor() {
super();
this.AUTOSCROLL_INTERVAL = 25;
this.NOT_DRAGGING = 0;
this.DRAG_OVER_BUTTON = -1;
this.DRAG_OVER_POPUP = 1;
this._draggingState = this.NOT_DRAGGING;
this._scrollTimer = 0;
this.attachShadow({ mode: "open" });
this.addEventListener("popupshowing", event => {
if (event.target != this) {
return;
}
// Make sure we generated shadow DOM to place menuitems into.
this.ensureInitialized();
});
this.addEventListener("DOMMenuItemActive", this);
}
connectedCallback() {
if (this.delayConnectedCallback() || this.hasConnected) {
return;
}
this.hasConnected = true;
if (this.parentNode?.localName == "menulist") {
this._setUpMenulistPopup();
}
}
initShadowDOM() {
// Retarget events from shadow DOM arrowscrollbox to the host.
this.scrollBox.addEventListener("scroll", ev =>
this.dispatchEvent(new Event("scroll"))
);
this.scrollBox.addEventListener("overflow", ev =>
this.dispatchEvent(new Event("overflow"))
);
this.scrollBox.addEventListener("underflow", ev =>
this.dispatchEvent(new Event("underflow"))
);
}
ensureInitialized() {
this.shadowRoot;
}
get shadowRoot() {
if (!super.shadowRoot.firstChild) {
// We generate shadow DOM lazily on popupshowing event to avoid extra
// load on the system during browser startup.
super.shadowRoot.appendChild(this.fragment);
this.initShadowDOM();
}
return super.shadowRoot;
}
get fragment() {
if (!this.constructor.hasOwnProperty("_fragment")) {
this.constructor._fragment = MozXULElement.parseXULToFragment(
this.markup
);
}
return document.importNode(this.constructor._fragment, true);
}
get markup() {
return `
<html:link rel="stylesheet" href="chrome://global/skin/global.css"/>
<html:style>${this.styles}</html:style>
<arrowscrollbox class="menupopup-arrowscrollbox"
part="arrowscrollbox content"
exportparts="scrollbox: arrowscrollbox-scrollbox"
flex="1"
orient="vertical"
smoothscroll="false">
<html:slot></html:slot>
</arrowscrollbox>
`;
}
get styles() {
return `
:host(.in-menulist) arrowscrollbox::part(scrollbutton-up),
:host(.in-menulist) arrowscrollbox::part(scrollbutton-down) {
display: none;
}
:host(.in-menulist) arrowscrollbox::part(scrollbox) {
overflow: auto;
margin: 0;
}
:host(.in-menulist) arrowscrollbox::part(scrollbox-clip) {
overflow: visible;
}
`;
}
get scrollBox() {
if (!this._scrollBox) {
this._scrollBox = this.shadowRoot.querySelector("arrowscrollbox");
}
return this._scrollBox;
}
/**
* Adds event listeners for a MozMenuPopup inside a menulist element.
*/
_setUpMenulistPopup() {
// Access shadow root to generate menupoup shadow DOMs. We do generate
// shadow DOM on popupshowing, but it doesn't work for HTML:selects,
// which are implemented via menulist elements living in the main process.
// So make them a special case then.
this.ensureInitialized();
this.classList.add("in-menulist");
this.addEventListener("popupshown", () => {
// Enable drag scrolling even when the mouse wasn't used. The
// mousemove handler will remove it if the mouse isn't down.
this._enableDragScrolling(false);
});
this.addEventListener("popuphidden", () => {
this._draggingState = this.NOT_DRAGGING;
this._clearScrollTimer();
this.releaseCapture();
this.scrollBox.scrollbox.scrollTop = 0;
});
this.addEventListener("mousedown", event => {
if (event.button != 0) {
return;
}
if (
this.state == "open" &&
(event.target.localName == "menuitem" ||
event.target.localName == "menu" ||
event.target.localName == "menucaption")
) {
this._enableDragScrolling(true);
}
});
this.addEventListener("mouseup", event => {
if (event.button != 0) {
return;
}
this._draggingState = this.NOT_DRAGGING;
this._clearScrollTimer();
});
this.addEventListener("mousemove", event => {
if (!this._draggingState) {
return;
}
this._clearScrollTimer();
// If the user released the mouse before the menupopup opens, we will
// still be capturing, so check that the button is still pressed. If
// not, release the capture and do nothing else. This also handles if
// the dropdown was opened via the keyboard.
if (!(event.buttons & 1)) {
this._draggingState = this.NOT_DRAGGING;
this.releaseCapture();
return;
}
// If dragging outside the top or bottom edge of the menupopup, but
// within the menupopup area horizontally, scroll the list in that
// direction. The _draggingState flag is used to ensure that scrolling
// does not start until the mouse has moved over the menupopup first,
// preventing scrolling while over the dropdown button.
let popupRect = this.getOuterScreenRect();
if (
event.screenX >= popupRect.left &&
event.screenX <= popupRect.right
) {
if (this._draggingState == this.DRAG_OVER_BUTTON) {
if (
event.screenY > popupRect.top &&
event.screenY < popupRect.bottom
) {
this._draggingState = this.DRAG_OVER_POPUP;
}
}
if (
this._draggingState == this.DRAG_OVER_POPUP &&
(event.screenY <= popupRect.top ||
event.screenY >= popupRect.bottom)
) {
let scrollAmount = event.screenY <= popupRect.top ? -1 : 1;
this.scrollBox.scrollByIndex(scrollAmount, true);
let win = this.ownerGlobal;
this._scrollTimer = win.setInterval(() => {
this.scrollBox.scrollByIndex(scrollAmount, true);
}, this.AUTOSCROLL_INTERVAL);
}
}
});
this._menulistPopupIsSetUp = true;
}
_enableDragScrolling(overItem) {
if (!this._draggingState) {
this.setCaptureAlways();
this._draggingState = overItem
? this.DRAG_OVER_POPUP
: this.DRAG_OVER_BUTTON;
}
}
_clearScrollTimer() {
if (this._scrollTimer) {
this.ownerGlobal.clearInterval(this._scrollTimer);
this._scrollTimer = 0;
}
}
on_DOMMenuItemActive(event) {
// Scroll buttons may overlap the active item. In that case, scroll
// further to stay clear of the buttons.
if (
this.parentNode?.localName == "menulist" ||
!this.scrollBox.hasAttribute("overflowing")
) {
return;
}
let item = event.target;
if (item.parentNode != this) {
return;
}
let itemRect = item.getBoundingClientRect();
let buttonRect = this.scrollBox._scrollButtonUp.getBoundingClientRect();
if (buttonRect.bottom > itemRect.top) {
this.scrollBox.scrollByPixels(itemRect.top - buttonRect.bottom, true);
} else {
buttonRect = this.scrollBox._scrollButtonDown.getBoundingClientRect();
if (buttonRect.top < itemRect.bottom) {
this.scrollBox.scrollByPixels(itemRect.bottom - buttonRect.top, true);
}
}
}
}
customElements.define("menupopup", MozMenuPopup);
MozElements.MozMenuPopup = MozMenuPopup;
}
|