From 4b7fa9865aa5d43ff68af6df30bfb4eefb898490 Mon Sep 17 00:00:00 2001 From: Kevin Schaul Date: Mon, 30 Jan 2023 11:10:00 -0600 Subject: [PATCH] Change how meta key is detected for coop. gestures UIEvents like 'wheel' include properties for whether some keys are currently pressed, including ctrl and meta. [0] This should be less prone to error, specifically when the user presses or depresses one of these keys while the browser document is not active. Adds a debug page for cooperative gestures because this feature cannot be tested with a fullscreen map, and the only other test page is a fullscreen map. [0] https://w3c.github.io/uievents/#dom-mouseevent-ctrlkey --- src/ui/handler/scroll_zoom.ts | 2 +- src/ui/map.ts | 15 +- test/debug-pages/cooperative-gestures.html | 152 +++++++++++++++++++++ 3 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 test/debug-pages/cooperative-gestures.html diff --git a/src/ui/handler/scroll_zoom.ts b/src/ui/handler/scroll_zoom.ts index 936c549bbd..854dc6428a 100644 --- a/src/ui/handler/scroll_zoom.ts +++ b/src/ui/handler/scroll_zoom.ts @@ -158,7 +158,7 @@ class ScrollZoomHandler { wheel(e: WheelEvent) { if (!this.isEnabled()) return; if (this._map._cooperativeGestures) { - if (this._map._metaPress) { + if (e[this._map._metaKey]) { e.preventDefault(); } else { return; diff --git a/src/ui/map.ts b/src/ui/map.ts index d73a56dac0..a764171c6b 100644 --- a/src/ui/map.ts +++ b/src/ui/map.ts @@ -297,7 +297,7 @@ class Map extends Camera { _interactive: boolean; _cooperativeGestures: boolean | GestureOptions; _cooperativeGesturesScreen: HTMLElement; - _metaPress: boolean; + _metaKey: string; _showTileBoundaries: boolean; _showCollisionBoxes: boolean; _showPadding: boolean; @@ -2589,28 +2589,21 @@ class Map extends Camera { _setupCooperativeGestures() { const container = this._container; - this._metaPress = false; this._cooperativeGesturesScreen = DOM.create('div', 'maplibregl-cooperative-gesture-screen', container); - let modifierKeyName = 'Control'; + this._metaKey = 'ctrlKey'; let desktopMessage = typeof this._cooperativeGestures !== 'boolean' && this._cooperativeGestures.windowsHelpText ? this._cooperativeGestures.windowsHelpText : 'Use Ctrl + scroll to zoom the map'; if (navigator.platform.indexOf('Mac') === 0) { desktopMessage = typeof this._cooperativeGestures !== 'boolean' && this._cooperativeGestures.macHelpText ? this._cooperativeGestures.macHelpText : 'Use ⌘ + scroll to zoom the map'; - modifierKeyName = 'Meta'; + this._metaKey = 'metaKey'; } const mobileMessage = typeof this._cooperativeGestures !== 'boolean' && this._cooperativeGestures.mobileHelpText ? this._cooperativeGestures.mobileHelpText : 'Use two fingers to move the map'; this._cooperativeGesturesScreen.innerHTML = `
${desktopMessage}
${mobileMessage}
`; - document.addEventListener('keydown', (event) => { - if (event.key === modifierKeyName) this._metaPress = true; - }); - document.addEventListener('keyup', (event) => { - if (event.key === modifierKeyName) this._metaPress = false; - }); // Add event to canvas container since gesture container is pointer-events: none this._canvasContainer.addEventListener('wheel', (e) => { - this._onCooperativeGesture(e, this._metaPress, 1); + this._onCooperativeGesture(e, e[this._metaKey], 1); }, false); // Remove the traditional pan classes this._canvasContainer.classList.remove('maplibregl-touch-drag-pan'); diff --git a/test/debug-pages/cooperative-gestures.html b/test/debug-pages/cooperative-gestures.html new file mode 100644 index 0000000000..3fd87b2096 --- /dev/null +++ b/test/debug-pages/cooperative-gestures.html @@ -0,0 +1,152 @@ + + + + MapLibre GL JS debug page + + + + + + + +
+
+
+
+
+
+
+ + + + +