Skip to content

Commit

Permalink
Merge branch 'cooperative-gestures-fix' into cooperative-gestures-enable
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinschaul committed Feb 7, 2023
2 parents 92f0297 + 4b7fa98 commit d9be389
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/ui/handler/scroll_zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 4 additions & 11 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class Map extends Camera {
_interactive: boolean;
_cooperativeGestures: boolean | GestureOptions;
_cooperativeGesturesScreen: HTMLElement;
_metaPress: boolean;
_metaKey: string;
_showTileBoundaries: boolean;
_showCollisionBoxes: boolean;
_showPadding: boolean;
Expand Down Expand Up @@ -2601,28 +2601,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 = `
<div class="maplibregl-desktop-message">${desktopMessage}</div>
<div class="maplibregl-mobile-message">${mobileMessage}</div>
`;
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');
Expand Down
152 changes: 152 additions & 0 deletions test/debug-pages/cooperative-gestures.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>MapLibre GL JS debug page</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='../../dist/maplibre-gl.css' />
<style>
body { margin: 0; padding: 0; }
html, body { height: 2000px; }
#map {
height: 300px;
margin-top: 100px;
}

#checkboxes {
position: absolute;
background: #fff;
top:0;
left:0;
padding:10px;
}
</style>
</head>

<body>
<div id='map'></div>
<div id='checkboxes'>
<label><input id='show-tile-boundaries-checkbox' type='checkbox'> tile debug</label><br />
<label><input id='show-symbol-collision-boxes-checkbox' type='checkbox'> collision debug</label><br />
<label><input id='show-overdraw-checkbox' type='checkbox'> overdraw debug</label><br />
<label><input id='pitch-checkbox' type='checkbox' checked> pitch with rotate</label><br />
</div>

<script src='../../dist/maplibre-gl-dev.js'></script>
<script>

var map = window.map = new maplibregl.Map({
container: 'map',
zoom: 12.5,
center: [-77.01866, 38.888],
style: 'https://demotiles.maplibre.org/style.json',
hash: true,
cooperativeGestures: true
});

map.addControl(new maplibregl.NavigationControl());
map.addControl(new maplibregl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true,
showUserLocation: true,
fitBoundsOptions: {
maxZoom: 20
}
}));
map.addControl(new maplibregl.ScaleControl());
map.addControl(new maplibregl.FullscreenControl());

map.on('load', function() {
map.addSource('geojson', {
'type': 'geojson',
'data': '../integration/assets/data/linestring.geojson',
'attribution': 'GeoJSON Attribution'
});
map.addLayer({
'id': 'route',
'type': 'line',
'source': 'geojson',
'paint': {
'line-color': '#EC8D8D',
'line-width': {'base': 1.5, 'stops': [[5, 0.75], [18, 32]]}
}
}, 'country-label-lg');

map.addSource('points', {
'type': 'geojson',
'data': '/test/debug-pages/cross_source_points.geojson'
});

map.addLayer({
'id': 'points',
'type': 'symbol',
'source': 'points',
'layout': {
'text-field': '{name}',
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold']
}
});

map.addLayer({
id: 'marker',
type: 'symbol',
source: 'points',
layout: {
'icon-image': 'bus-11',
'icon-size': 2,
'icon-ignore-placement': true,
'icon-allow-overlap': true
}
});

map.addLayer({
id: 'marker-hover',
type: 'symbol',
filter: ['==', 'name', ''],
source: 'points',
layout: {
'icon-image': 'bus-15',
'icon-size': 2,
'icon-ignore-placement': true,
'icon-allow-overlap': true
}
});

map.on('mouseenter', 'marker', function(e) {
map.setFilter('marker-hover', ['==', 'name', e.features[0].properties.name]);
});
map.on('mouseleave', 'marker', function(_) {
map.setFilter('marker-hover', ['==', 'name', '']);
});

});

map.on('click', function(e) {
if (e.originalEvent.shiftKey) return;
(new maplibregl.Popup())
.setLngLat(map.unproject(e.point))
.setHTML('<h1>Hello World!</h1>')
.addTo(map);
});

document.getElementById('show-tile-boundaries-checkbox').onclick = function() {
map.showTileBoundaries = !!this.checked;
};

document.getElementById('show-symbol-collision-boxes-checkbox').onclick = function() {
map.showCollisionBoxes = !!this.checked;
};

document.getElementById('show-overdraw-checkbox').onclick = function() {
map.showOverdrawInspector = !!this.checked;
};

document.getElementById('pitch-checkbox').onclick = function() {
map.dragRotate._pitchWithRotate = !!this.checked;
};

</script>
</body>
</html>

0 comments on commit d9be389

Please sign in to comment.