@@ -26,16 +26,12 @@ const FALLBACK_TIMEOUT_MS = 400;
2626 * - Only active for pointer/mouse input; ignored for touch and pen
2727 */
2828export 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 }
0 commit comments