Skip to content

Commit 848c236

Browse files
committed
refactor: simplify controller and menu-bar deactivation
1 parent e0b7159 commit 848c236

3 files changed

Lines changed: 18 additions & 29 deletions

File tree

packages/context-menu/src/vaadin-safe-triangle-controller.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class SafeTriangleController {
1717
* Activate the safe triangle tracking for the given submenu overlay.
1818
* Should be called when a submenu opens.
1919
*/
20-
activate(submenuOverlay: HTMLElement, parentItem: HTMLElement): void;
20+
activate(submenuOverlay: HTMLElement, parentItem: HTMLElement, parentContainer?: HTMLElement): void;
2121

2222
/**
2323
* Deactivate the safe triangle tracking.

packages/context-menu/src/vaadin-safe-triangle-controller.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ const FALLBACK_TIMEOUT_MS = 400;
2626
* - Only active for pointer/mouse input; ignored for touch and pen
2727
*/
2828
export class SafeTriangleController {
29-
#hasLastPosition = false;
30-
3129
#lastX = 0;
3230

3331
#lastY = 0;
3432

3533
#invalidCount = 0;
3634

37-
#active = false;
38-
3935
#lastMoveTime = 0;
4036

4137
#submenuElement = null;
@@ -58,17 +54,17 @@ export class SafeTriangleController {
5854
if (now - this.#lastMoveTime < THROTTLE_MS) {
5955
return;
6056
}
61-
this.#lastMoveTime = now;
6257

6358
const x = event.clientX;
6459
const y = event.clientY;
6560

66-
if (!this.#hasLastPosition) {
67-
this.#hasLastPosition = true;
61+
if (this.#lastMoveTime === 0) {
62+
this.#lastMoveTime = now;
6863
this.#lastX = x;
6964
this.#lastY = y;
7065
return;
7166
}
67+
this.#lastMoveTime = now;
7268

7369
if (!this.#submenuElement) {
7470
this.#lastX = x;
@@ -139,11 +135,11 @@ export class SafeTriangleController {
139135
*/
140136
activate(submenuOverlay, parentItem, parentContainer) {
141137
this.#cancelPendingSwitch();
138+
const wasActive = this.#submenuElement !== null;
142139
this.#submenuElement = submenuOverlay;
143140
this.#parentItemElement = parentItem;
144141
this.#invalidCount = 0;
145142
this.#lastMoveTime = 0;
146-
this.#hasLastPosition = false;
147143
this.#lastX = 0;
148144
this.#lastY = 0;
149145

@@ -155,8 +151,7 @@ export class SafeTriangleController {
155151
parentContainer.setAttribute('safe-triangle-active', '');
156152
}
157153

158-
if (!this.#active) {
159-
this.#active = true;
154+
if (!wasActive) {
160155
document.addEventListener('pointermove', this.#onPointerMove);
161156
}
162157
}
@@ -170,8 +165,7 @@ export class SafeTriangleController {
170165
this.#parentContainer.removeAttribute('safe-triangle-active');
171166
this.#parentContainer = null;
172167
}
173-
if (this.#active) {
174-
this.#active = false;
168+
if (this.#submenuElement) {
175169
document.removeEventListener('pointermove', this.#onPointerMove);
176170
}
177171
this.#submenuElement = null;
@@ -187,7 +181,7 @@ export class SafeTriangleController {
187181
* @return {boolean}
188182
*/
189183
shouldKeepOpen() {
190-
if (!this.#active || !this.#submenuElement) {
184+
if (!this.#submenuElement) {
191185
return false;
192186
}
193187
// Only block switches if we've actually tracked pointer movement.
@@ -215,18 +209,15 @@ export class SafeTriangleController {
215209
}
216210

217211
#cancelPendingSwitch() {
218-
this.#pendingSwitch = null;
219-
if (this.#pendingTimeout) {
220-
clearTimeout(this.#pendingTimeout);
221-
this.#pendingTimeout = null;
222-
}
223-
}
224-
225-
#executePendingSwitch() {
226212
const callback = this.#pendingSwitch;
227213
this.#pendingSwitch = null;
228214
clearTimeout(this.#pendingTimeout);
229215
this.#pendingTimeout = null;
216+
return callback;
217+
}
218+
219+
#executePendingSwitch() {
220+
const callback = this.#cancelPendingSwitch();
230221
if (callback) {
231222
callback();
232223
}

packages/menu-bar/src/vaadin-menu-bar-mixin.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ export const MenuBarMixin = (superClass) =>
285285

286286
menu.addEventListener('item-selected', this.__onItemSelected.bind(this));
287287
menu.addEventListener('close-all-menus', this.__onEscapeClose.bind(this));
288+
menu.addEventListener('opened-changed', (e) => {
289+
if (!e.detail.value && this.__safeTriangle) {
290+
this.__safeTriangle.deactivate();
291+
}
292+
});
288293

289294
const overlay = menu._overlayElement;
290295
overlay._contentRoot.addEventListener('keydown', this.__boundOnContextMenuKeydown);
@@ -1059,9 +1064,6 @@ export const MenuBarMixin = (superClass) =>
10591064
/** @private */
10601065
__onEscapeClose() {
10611066
this.__deactivateButton(true);
1062-
if (this.__safeTriangle) {
1063-
this.__safeTriangle.deactivate();
1064-
}
10651067
}
10661068

10671069
/** @private */
@@ -1087,10 +1089,6 @@ export const MenuBarMixin = (superClass) =>
10871089
if (this._subMenu.opened) {
10881090
this._subMenu.close();
10891091
}
1090-
// Deactivate safe triangle tracking when submenu closes
1091-
if (this.__safeTriangle) {
1092-
this.__safeTriangle.deactivate();
1093-
}
10941092
}
10951093

10961094
/**

0 commit comments

Comments
 (0)