Skip to content

Commit 7181058

Browse files
underootgithub-actions[bot]
authored andcommitted
Refine sub-toggles API for several event handlers
GitOrigin-RevId: be01daa6b534390255f7ac37292bc9e6ea1dcaf9
1 parent bfe22f3 commit 7181058

6 files changed

Lines changed: 619 additions & 13 deletions

File tree

src/ui/handler/keyboard.ts

Lines changed: 149 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ class KeyboardHandler implements Handler {
3131
_panStep: number;
3232
_bearingStep: number;
3333
_pitchStep: number;
34-
_rotationDisabled: boolean;
34+
_bearingDisabled: boolean;
35+
_pitchDisabled: boolean;
3536
_panDisabled: boolean;
37+
_zoomDisabled: boolean;
3638

3739
/**
3840
* @private
@@ -42,8 +44,10 @@ class KeyboardHandler implements Handler {
4244
this._panStep = stepOptions.panStep;
4345
this._bearingStep = stepOptions.bearingStep;
4446
this._pitchStep = stepOptions.pitchStep;
45-
this._rotationDisabled = false;
47+
this._bearingDisabled = false;
48+
this._pitchDisabled = false;
4649
this._panDisabled = false;
50+
this._zoomDisabled = false;
4751
}
4852

4953
blur() {
@@ -56,6 +60,7 @@ class KeyboardHandler implements Handler {
5660

5761
keydown(e: KeyboardEvent): HandlerResult | null | undefined {
5862
if (e.altKey || e.ctrlKey || e.metaKey) return;
63+
if (!this.isEnabled()) return;
5964

6065
let zoomDir = 0;
6166
let bearingDir = 0;
@@ -117,8 +122,11 @@ class KeyboardHandler implements Handler {
117122
return;
118123
}
119124

120-
if (this._rotationDisabled) {
125+
if (this._bearingDisabled) {
121126
bearingDir = 0;
127+
}
128+
129+
if (this._pitchDisabled) {
122130
pitchDir = 0;
123131
}
124132

@@ -127,6 +135,10 @@ class KeyboardHandler implements Handler {
127135
yDir = 0;
128136
}
129137

138+
if (this._zoomDisabled) {
139+
zoomDir = 0;
140+
}
141+
130142
return {
131143
cameraAnimation: (map: Map) => {
132144
const zoom = map.getZoom();
@@ -176,7 +188,7 @@ class KeyboardHandler implements Handler {
176188
* const isKeyboardEnabled = map.keyboard.isEnabled();
177189
*/
178190
isEnabled(): boolean {
179-
return this._enabled;
191+
return this._enabled && !(this._bearingDisabled && this._pitchDisabled && this._panDisabled && this._zoomDisabled);
180192
}
181193

182194
/**
@@ -193,25 +205,40 @@ class KeyboardHandler implements Handler {
193205
}
194206

195207
/**
196-
* Disables the "keyboard pan/rotate" interaction, leaving the
208+
* Disables the "keyboard pitch/rotate" interaction, leaving the
197209
* "keyboard zoom" interaction enabled.
198210
*
199211
* @example
200212
* map.keyboard.disableRotation();
201213
*/
202214
disableRotation() {
203-
this._rotationDisabled = true;
215+
this._pitchDisabled = true;
216+
this._bearingDisabled = true;
204217
}
205218

206219
/**
207-
* Enables the "keyboard pan/rotate" interaction.
220+
* Enables the "keyboard pitch/rotate" interaction.
208221
*
209222
* @example
210223
* map.keyboard.enable();
211224
* map.keyboard.enableRotation();
212225
*/
213226
enableRotation() {
214-
this._rotationDisabled = false;
227+
this._pitchDisabled = false;
228+
this._bearingDisabled = false;
229+
}
230+
231+
/**
232+
* Returns a Boolean indicating whether the "keyboard pitch/rotate"
233+
* interaction is enabled.
234+
*
235+
* @returns {boolean} `true` if the "keyboard pitch/rotate"
236+
* interaction is enabled.
237+
* @example
238+
* const isRotationEnabled = map.keyboard.isRotationEnabled();
239+
*/
240+
isRotationEnabled(): boolean {
241+
return !this._pitchDisabled && !this._bearingDisabled;
215242
}
216243

217244
/**
@@ -235,6 +262,120 @@ class KeyboardHandler implements Handler {
235262
enablePan() {
236263
this._panDisabled = false;
237264
}
265+
266+
/**
267+
* Returns a Boolean indicating whether the "keyboard pan" interaction is
268+
* enabled.
269+
*
270+
* @returns {boolean} `true` if the "keyboard pan" interaction is enabled.
271+
* @example
272+
* const isPanEnabled = map.keyboard.isPanEnabled();
273+
*/
274+
isPanEnabled(): boolean {
275+
return !this._panDisabled;
276+
}
277+
278+
/**
279+
* Disables the "keyboard zoom" interaction (plus/minus keys), leaving the
280+
* "keyboard pan" and "keyboard rotate/pitch" interactions enabled.
281+
*
282+
* @example
283+
* map.keyboard.disableZoom();
284+
*/
285+
disableZoom() {
286+
this._zoomDisabled = true;
287+
}
288+
289+
/**
290+
* Enables the "keyboard zoom" interaction (plus/minus keys).
291+
*
292+
* @example
293+
* map.keyboard.enable();
294+
* map.keyboard.enableZoom();
295+
*/
296+
enableZoom() {
297+
this._zoomDisabled = false;
298+
}
299+
300+
/**
301+
* Returns a Boolean indicating whether the "keyboard zoom" interaction is
302+
* enabled.
303+
*
304+
* @returns {boolean} `true` if the "keyboard zoom" interaction is enabled.
305+
* @example
306+
* const isZoomEnabled = map.keyboard.isZoomEnabled();
307+
*/
308+
isZoomEnabled(): boolean {
309+
return !this._zoomDisabled;
310+
}
311+
312+
/**
313+
* Disables the "keyboard pitch" interaction (Shift+Up/Down), leaving the
314+
* "keyboard pan" and "keyboard rotate" interactions enabled.
315+
*
316+
* @example
317+
* map.keyboard.disablePitch();
318+
*/
319+
disablePitch() {
320+
this._pitchDisabled = true;
321+
}
322+
323+
/**
324+
* Enables the "keyboard pitch" interaction (Shift+Up/Down).
325+
*
326+
* @example
327+
* map.keyboard.enable();
328+
* map.keyboard.enablePitch();
329+
*/
330+
enablePitch() {
331+
this._pitchDisabled = false;
332+
}
333+
334+
/**
335+
* Returns a Boolean indicating whether the "keyboard pitch" interaction is
336+
* enabled.
337+
*
338+
* @returns {boolean} `true` if the "keyboard pitch" interaction is enabled.
339+
* @example
340+
* const isPitchEnabled = map.keyboard.isPitchEnabled();
341+
*/
342+
isPitchEnabled(): boolean {
343+
return !this._pitchDisabled;
344+
}
345+
346+
/**
347+
* Disables the "keyboard bearing" interaction (Shift+Left/Right), leaving the
348+
* "keyboard pan" and "keyboard pitch" interactions enabled.
349+
*
350+
* @example
351+
* map.keyboard.disableBearing();
352+
*/
353+
disableBearing() {
354+
this._bearingDisabled = true;
355+
}
356+
357+
/**
358+
* Enables the "keyboard bearing" interaction (Shift+Left/Right).
359+
*
360+
* @example
361+
* map.keyboard.enable();
362+
* map.keyboard.enableBearing();
363+
*/
364+
enableBearing() {
365+
this._bearingDisabled = false;
366+
}
367+
368+
/**
369+
* Returns a Boolean indicating whether the "keyboard bearing" interaction is
370+
* enabled.
371+
*
372+
* @returns {boolean} `true` if the "keyboard bearing" interaction is enabled.
373+
* @example
374+
* const isBearingEnabled = map.keyboard.isBearingEnabled();
375+
*/
376+
isBearingEnabled(): boolean {
377+
return !this._bearingDisabled;
378+
}
238379
}
239380

240381
function easeOut(t: number) {

src/ui/handler/shim/drag_rotate.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default class DragRotateHandler {
1313
_mousePitch: MousePitchHandler;
1414
_pitchWithRotate: boolean;
1515
_pitchDisabled: boolean;
16+
_rotationDisabled: boolean;
1617

1718
/**
1819
* @param {Object} [options]
@@ -28,6 +29,7 @@ export default class DragRotateHandler {
2829
this._mouseRotate = mouseRotate;
2930
this._mousePitch = mousePitch;
3031
this._pitchDisabled = false;
32+
this._rotationDisabled = false;
3133
}
3234

3335
/**
@@ -37,7 +39,7 @@ export default class DragRotateHandler {
3739
* map.dragRotate.enable();
3840
*/
3941
enable() {
40-
this._mouseRotate.enable();
42+
if (!this._rotationDisabled) this._mouseRotate.enable();
4143
if (this._pitchWithRotate && !this._pitchDisabled) this._mousePitch.enable();
4244
}
4345

@@ -60,7 +62,14 @@ export default class DragRotateHandler {
6062
* const isDragRotateEnabled = map.dragRotate.isEnabled();
6163
*/
6264
isEnabled(): boolean {
63-
return this._mouseRotate.isEnabled() && (!this._pitchWithRotate || this._pitchDisabled || this._mousePitch.isEnabled());
65+
// If pitchWithRotate is true, then the dragRotate interaction is considered enabled
66+
// if either the rotation or pitch interactions are enabled.
67+
if (this._pitchWithRotate) {
68+
return (!this._rotationDisabled && this._mouseRotate.isEnabled()) ||
69+
(!this._pitchDisabled && this._mousePitch.isEnabled());
70+
} else {
71+
return !this._rotationDisabled && this._mouseRotate.isEnabled();
72+
}
6473
}
6574

6675
/**
@@ -99,4 +108,48 @@ export default class DragRotateHandler {
99108
this._pitchDisabled = false;
100109
if (this._pitchWithRotate && this._mouseRotate.isEnabled()) this._mousePitch.enable();
101110
}
111+
112+
/**
113+
* Returns a Boolean indicating whether the "drag to pitch" interaction is enabled.
114+
*
115+
* @returns {boolean} `true` if the "drag to pitch" interaction is enabled.
116+
* @example
117+
* const isDragPitchEnabled = map.dragRotate.isPitchEnabled();
118+
*/
119+
isPitchEnabled(): boolean {
120+
return !this._pitchDisabled && this._mousePitch.isEnabled();
121+
}
122+
123+
/**
124+
* Disables the "drag to rotate" interaction, leaving the "drag to pitch"
125+
* interaction enabled.
126+
*
127+
* @example
128+
* map.dragRotate.disableRotation();
129+
*/
130+
disableRotation() {
131+
this._rotationDisabled = true;
132+
this._mouseRotate.disable();
133+
}
134+
135+
/**
136+
* Enables the "drag to rotate" interaction.
137+
*
138+
* @example
139+
* map.dragRotate.enableRotation();
140+
*/
141+
enableRotation() {
142+
this._rotationDisabled = false;
143+
this._mouseRotate.enable();
144+
}
145+
/**
146+
* Returns a Boolean indicating whether the "drag to rotate" interaction is enabled.
147+
*
148+
* @returns {boolean} `true` if the "drag to rotate" interaction is enabled.
149+
* @example
150+
* const isDragRotationEnabled = map.dragRotate.isRotationEnabled();
151+
*/
152+
isRotationEnabled(): boolean {
153+
return !this._rotationDisabled && this._mouseRotate.isEnabled();
154+
}
102155
}

src/ui/handler/shim/touch_zoom_rotate.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export default class TouchZoomRotateHandler {
7575
* const isTouchZoomRotateEnabled = map.touchZoomRotate.isEnabled();
7676
*/
7777
isEnabled(): boolean {
78-
return this._touchZoom.isEnabled() &&
79-
(this._rotationDisabled || this._touchRotate.isEnabled()) &&
80-
(this._tapDragZoomDisabled || this._tapDragZoom.isEnabled());
78+
return this._touchZoom.isEnabled() ||
79+
(!this._rotationDisabled && this._touchRotate.isEnabled()) ||
80+
(!this._tapDragZoomDisabled && this._tapDragZoom.isEnabled());
8181
}
8282

8383
/**
@@ -115,6 +115,17 @@ export default class TouchZoomRotateHandler {
115115
if (this._touchZoom.isEnabled()) this._touchRotate.enable();
116116
}
117117

118+
/**
119+
* Returns a Boolean indicating whether the "pinch to rotate" interaction is enabled.
120+
*
121+
* @returns {boolean} `true` if the "pinch to rotate" interaction is enabled.
122+
* @example
123+
* const isRotationEnabled = map.touchZoomRotate.isRotationEnabled();
124+
*/
125+
isRotationEnabled(): boolean {
126+
return !this._rotationDisabled;
127+
}
128+
118129
/**
119130
* Disables the "tap and drag to zoom" interaction (single-finger zoom by
120131
* tapping, then on a second tap holding and dragging vertically), leaving
@@ -141,4 +152,15 @@ export default class TouchZoomRotateHandler {
141152
this._tapDragZoomDisabled = false;
142153
if (this._touchZoom.isEnabled()) this._tapDragZoom.enable();
143154
}
155+
156+
/**
157+
* Returns a Boolean indicating whether the "tap and drag to zoom" interaction is enabled.
158+
*
159+
* @returns {boolean} `true` if the "tap and drag to zoom" interaction is enabled.
160+
* @example
161+
* const isTapDragZoomEnabled = map.touchZoomRotate.isTapDragZoomEnabled();
162+
*/
163+
isTapDragZoomEnabled(): boolean {
164+
return !this._tapDragZoomDisabled;
165+
}
144166
}

0 commit comments

Comments
 (0)