-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathViewport.js
More file actions
991 lines (911 loc) · 34.5 KB
/
Viewport.js
File metadata and controls
991 lines (911 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
// This is using the existential types in the generics, which would be harder to
// remove. It might be possible to switch this took use hooks.
/* eslint-disable flowtype/no-existential-type */
import * as React from 'react';
import classNames from 'classnames';
import explicitConnect from 'firefox-profiler/utils/connect';
import { getResizeObserverWrapper } from 'firefox-profiler/utils/resize-observer-wrapper';
import {
getHasZoomedViaMousewheel,
getPanelLayoutGeneration,
} from 'firefox-profiler/selectors/app';
import { setHasZoomedViaMousewheel } from 'firefox-profiler/actions/app';
import { updatePreviewSelection } from 'firefox-profiler/actions/profile-view';
import type {
CssPixels,
UnitIntervalOfProfileRange,
StartEndRange,
PreviewSelection,
} from 'firefox-profiler/types';
import type { ConnectedProps } from 'firefox-profiler/utils/connect';
import {
getObjectValuesAsUnion,
assertExhaustiveCheck,
} from 'firefox-profiler/utils/flow';
/**
* Viewport terminology:
* (this time is relative to current
* profile range, not the total profile)
* <------ e.g. 1000px ------> 0.7 - Sample's unit time
* ___________________________ |
* _______|___________________________|_________|______________________
* | | | v |
* |<-------------|---------------------------|---------*------- Total profile samples ------>|
* | | | |
* | | Viewport | |
* | | | Current profile range |
* |_______|___________________________|________________________________|
* |___________________________|
* ^ ^ ^ ^
* 0.0 0.1 0.6 1.0
* ^ viewportLeft ^ viewportRight
*
* viewportLeft = 0.1
* viewportRight = 0.6
* viewportLength = viewportRight - viewportLeft
* viewportTop = 30 (in pixels)
* viewportPixelWidth = 1000 (= containerWidth - marginLeft - marginRight)
* unitPixel = viewportLength / viewportPixelWidth
* viewportRight += mouseMoveDelta * unitPixel
* viewportLeft += mouseMoveDelta * unitPixel
*
* Placement of margins:
*
* Margins are outside the viewport but inside containerWidth.
*
* (1) Fully zoomed out:
*
* viewportLeft: 0 viewportRight: 1
* | |
* marginLeft v Viewport v marginRight
* |<---------->|<--------------------------------->|<------------->|
* #####################################
* |<-------------------------------------------------------------->|
* containerWidth
*
* (2) Zoomed in by 5x (viewportLength = 0.2) centered around 0.5:
*
* viewportLeft: 0.4 viewportRight: 0.6
* | |
* marginLeft v Viewport v marginRight
* |<---------->|<--------------------------------->|<------------->|
* ...#####################################################################...
* |<-------------------------------------------------------------->|
* containerWidth
**/
const { DOM_DELTA_PAGE, DOM_DELTA_LINE } =
typeof window === 'object' && window.WheelEvent
? new WheelEvent('mouse')
: { DOM_DELTA_LINE: 1, DOM_DELTA_PAGE: 2 };
/**
* How fast the viewport should move when navigating with the keyboard
* in pixels per second.
*/
const KEYBOARD_NAVIGATION_SPEED = 2000;
const MAX_KEYBOARD_DELTA = 500;
type NavigationKey = 'zoomIn' | 'zoomOut' | 'up' | 'down' | 'left' | 'right';
/**
* Mapping from keycode to navigation key when no modifiers are down.
*/
const BARE_KEYMAP: { [string]: NavigationKey } = {
KeyQ: 'zoomIn',
KeyY: 'zoomIn',
KeyE: 'zoomOut',
KeyU: 'zoomOut',
KeyW: 'up',
KeyK: 'up',
KeyS: 'down',
KeyJ: 'down',
KeyA: 'left',
KeyH: 'left',
KeyD: 'right',
KeyL: 'right',
};
/**
* Mapping from keycode to navigation key when the ctrl modifier is down.
*/
const CTRL_KEYMAP: { [string]: NavigationKey } = {
ArrowUp: 'zoomIn',
ArrowDown: 'zoomOut',
};
// These viewport values (most of which are computed dynamically by
// the HOC) are passed into the props of the wrapped component.
export type Viewport = {|
+containerWidth: CssPixels,
+containerHeight: CssPixels,
+viewportLeft: UnitIntervalOfProfileRange,
+viewportRight: UnitIntervalOfProfileRange,
+viewportTop: CssPixels,
+viewportBottom: CssPixels,
+isDragging: boolean,
+moveViewport: (CssPixels, CssPixels) => void,
+isSizeSet: boolean,
|};
type ViewportStateProps = {|
+panelLayoutGeneration: number,
+hasZoomedViaMousewheel?: boolean,
|};
type ViewportDispatchProps = {|
+updatePreviewSelection: typeof updatePreviewSelection,
+setHasZoomedViaMousewheel?: typeof setHasZoomedViaMousewheel,
|};
// These are the props consumed by this Higher-Order Component (HOC), but can be
// optionally used by the wrapped component.
type ViewportOwnProps<ChartProps> = {|
+viewportProps: {|
// The "committed range", whose endpoints correspond to 0 and 1.
+timeRange: StartEndRange,
// The preview selection, whose endpoints correspond to viewportLeft and viewportRight.
+previewSelection: PreviewSelection,
// The left margin. Margins are outside the viewport but inside containerWidth.
+marginLeft: CssPixels,
// The right margin. Margins are outside the viewport but inside containerWidth.
+marginRight: CssPixels,
+maxViewportHeight: number,
+startsAtBottom?: boolean,
+maximumZoom: UnitIntervalOfProfileRange,
+disableHorizontalMovement?: boolean,
+className?: string,
+containerRef?: (HTMLDivElement | null) => void,
// These props are defined by the generic variables passed into to the type
// WithChartViewport when calling withChartViewport. This is how the relationship
// is guaranteed. e.g. here with OwnProps:
//
// (withChartViewport: WithChartViewport<OwnProps, Props>)(
// MarkerChartCanvas
// )
+viewportNeedsUpdate: (
prevProps: ChartProps,
nextProps: ChartProps
) => boolean,
|},
+chartProps: ChartProps,
|};
type HorizontalViewport = {|
// The position of the profile range that should be drawn at the left edge of
// the chart's "inner box", i.e. after the marginLeft.
viewportLeft: UnitIntervalOfProfileRange,
// The position of the profile range that should be drawn at the right edge of
// the chart's "inner box", i.e. to the left of marginRight.
viewportRight: UnitIntervalOfProfileRange,
|};
type State = {|
containerWidth: CssPixels,
containerHeight: CssPixels,
containerLeft: CssPixels,
viewportTop: CssPixels,
viewportBottom: CssPixels,
horizontalViewport: HorizontalViewport,
isDragging: boolean,
isScrollHintVisible: boolean,
isSizeSet: boolean,
|};
import './Viewport.css';
// The overall zoom speed for shift and pinch zooming.
const ZOOM_SPEED = 1.003;
// This value makes the pinch zooming faster than shift+scroll zooming.
const PINCH_ZOOM_FACTOR = 3;
/**
* This is the type signature for the higher order component. It's easier to use generics
* by separating out the type definition.
*/
export type WithChartViewport<
// False positive generic trait bounds.
// eslint-disable-next-line flowtype/no-weak-types
ChartOwnProps: Object,
// The chart component's props are given the viewport object, as well as the original
// ChartOwnProps.
ChartProps: $ReadOnly<{|
...ChartOwnProps,
viewport: Viewport,
|}>,
> = (
// Take as input a React component whose props accept the { +viewport: Viewport }.
ChartComponent: React.ComponentType<ChartProps>
) => React.ComponentType<
// Finally the returned component takes as input the InternalViewportProps, and
// the ChartProps, but NOT { +viewport: Viewport }.
ViewportOwnProps<ChartOwnProps>,
>;
// Create the implementation of the WithChartViewport type, but let flow infer the
// generic parameters.
export const withChartViewport: WithChartViewport<*, *> =
// ChartOwnProps is the only generic actually used in the implementation. Infer
// the type signature of the arguments as the WithChartViewport will apply them.
<ChartOwnProps>(
ChartComponent: React.ComponentType<$Subtype<{ +viewport: Viewport }>>
): * => {
type ViewportProps = ConnectedProps<
ViewportOwnProps<ChartOwnProps>,
ViewportStateProps,
ViewportDispatchProps,
>;
class ChartViewport extends React.PureComponent<ViewportProps, State> {
zoomScrollId: number = 0;
_pendingPreviewSelectionUpdates: Array<
(HorizontalViewport) => PreviewSelection,
> = [];
_container: HTMLElement | null = null;
_takeContainerRef = (container) => {
if (this.props.viewportProps.containerRef) {
this.props.viewportProps.containerRef(container);
}
this._container = container;
};
_lastKeyboardNavigationFrame: number = 0;
_keysDown: Set<NavigationKey> = new Set();
_deltaToZoomFactor = (delta) => Math.pow(ZOOM_SPEED, delta);
_dragX: number = 0;
_dragY: number = 0;
constructor(props: ViewportProps) {
super(props);
this.state = this.getDefaultState(props);
}
getHorizontalViewport(
previewSelection: PreviewSelection,
timeRange: StartEndRange
) {
if (previewSelection.hasSelection) {
const { selectionStart, selectionEnd } = previewSelection;
const timeRangeLength = timeRange.end - timeRange.start;
return {
viewportLeft: (selectionStart - timeRange.start) / timeRangeLength,
viewportRight: (selectionEnd - timeRange.start) / timeRangeLength,
};
}
return {
viewportLeft: 0,
viewportRight: 1,
};
}
getDefaultState(props: ViewportProps) {
const { previewSelection, timeRange } = props.viewportProps;
const horizontalViewport = this.getHorizontalViewport(
previewSelection,
timeRange
);
const { startsAtBottom, maxViewportHeight } = props.viewportProps;
return {
containerWidth: 0,
containerHeight: 0,
containerLeft: 0,
viewportTop: 0,
viewportBottom: startsAtBottom ? maxViewportHeight : 0,
horizontalViewport,
isDragging: false,
isScrollHintVisible: false,
isSizeSet: false,
};
}
/**
* Let the viewport know when we are actively scrolling.
*/
showScrollingHint() {
// Only show this message if we haven't ctrl/shift zoomed yet.
if (this.props.hasZoomedViaMousewheel) {
return;
}
const scrollId = ++this.zoomScrollId;
if (!this.state.isScrollHintVisible) {
this.setState({ isScrollHintVisible: true });
}
setTimeout(() => {
if (scrollId === this.zoomScrollId) {
this.setState({ isScrollHintVisible: false });
}
}, 1000);
}
UNSAFE_componentWillReceiveProps(newProps: ViewportProps) {
if (
this.props.viewportProps.viewportNeedsUpdate(
this.props.chartProps,
newProps.chartProps
)
) {
this.setState(this.getDefaultState(newProps));
this._setSizeNextFrame();
} else if (
this.props.viewportProps.previewSelection !==
newProps.viewportProps.previewSelection ||
this.props.viewportProps.timeRange !==
newProps.viewportProps.timeRange
) {
const { previewSelection, timeRange } = newProps.viewportProps;
const horizontalViewport = this.getHorizontalViewport(
previewSelection,
timeRange
);
this.setState({
horizontalViewport,
});
} else if (
this.props.panelLayoutGeneration !== newProps.panelLayoutGeneration
) {
this._setSizeNextFrame();
}
}
_setSize = () => {
if (this._container) {
const rect = this._container.getBoundingClientRect();
const { startsAtBottom } = this.props.viewportProps;
if (
this.state.containerWidth !== rect.width ||
this.state.containerHeight !== rect.height
) {
this.setState((prevState) => ({
containerWidth: rect.width,
containerHeight: rect.height,
containerLeft: rect.left,
viewportBottom: startsAtBottom
? prevState.viewportBottom
: prevState.viewportTop + rect.height,
viewportTop: startsAtBottom
? prevState.viewportBottom - rect.height
: prevState.viewportTop,
isSizeSet: true,
}));
}
}
};
_setSizeNextFrame = () => {
requestAnimationFrame(this._setSize);
};
// To scroll and zoom the chart, we need to install a wheel event listener.
// This listener needs to call `preventDefault()` in order to be able to
// consume wheel events, so that the browser does not trigger additional
// scrolling, zooming, or back/forward swiping for events that the Viewport
// already handles. In other words, this listener cannot be a "passive"
// event listener.
// In the past, we were using ReactDOM's onWheel attribute to install the
// event listener. However, this has two drawbacks:
//
// 1. It does not let us control which DOM element the listener is
// installed on - ReactDOM will use event delegation and install the
// actual listener on an ancester DOM node. More specifically, on
// React versions before v17, the listener will be installed on the
// document, and starting with v17 the listener will be installed on
// the React root.
// 2. It does not let us control the event listener options ("passive").
//
// As a general rule, non-passive wheel event listeners should be attached
// to an element that only covers the area of the page that actually needs
// to consume wheel events - the listener should be scoped as "tightly" as
// possible. That's because these listeners require additional roundtrips
// to the main thread for asynchronous scrolling, and browsers have added
// optimizations to ensure that this extra roundtrip only affects the area
// of the page covered by the DOM subtree that the listener is attached to.
// So we really don't want React to put our wheel event listener on the
// document or on the React root; we want it to be on the DOM element for
// our Viewport component so that there is no scrolling performance impact
// on elements outside the Viewport component.
// Another problem with React setting the listener on the document is the
// fact that, as of 2019 [1][2], `preventDefault()` no longer has any effect
// in wheel event listeners that are set on the document, unless that
// listener is explicitly marked with `{passive: false}` (which React
// doesn't let us do).
//
// So, instead of using a ReactDOM onWheel listener, we use a native DOM
// wheel event listener. We set/unset it when the Viewport component
// mounts/unmounts.
// This solves both problems: It makes `preventDefault()` work, and it
// limits the performance impact from the non-passiveness to the Viewport
// component itself, so that scrolling outside of the Viewport can proceed
// in a fully accelerated and asynchronous fashion.
//
// [1] https://developer.chrome.com/blog/scrolling-intervention-2/
// [2] https://bugzilla.mozilla.org/show_bug.cgi?id=1526725
_pointerWheelListener = (event: WheelEvent) => {
// We handle the wheel event, so disable the browser's handling, such
// as back/forward swiping or scrolling.
event.preventDefault();
const { disableHorizontalMovement } = this.props.viewportProps;
if (event.ctrlKey || event.shiftKey) {
if (!disableHorizontalMovement) {
// Pinch and zoom needs to be faster to feel nice, which happens on the
// ctrlKey modifier.
const zoomModifier = event.ctrlKey ? PINCH_ZOOM_FACTOR : 1;
this.zoomWithMouseWheel(event, zoomModifier);
}
return;
}
if (!disableHorizontalMovement) {
this.showScrollingHint();
}
// Do the work to move the viewport.
const { containerHeight } = this.state;
this.moveViewport(
-getNormalizedScrollDelta(event, containerHeight, 'deltaX'),
-getNormalizedScrollDelta(event, containerHeight, 'deltaY')
);
};
/**
* Add a batched update to the preview selection.
*
* This method works asynchronously in order to avoid spamming the redux store
* with updates (which cause synchronous react renders) in response to mouse events.
* The actual redux update happens in _flushPendingPreviewSelectionUpdates(), which
* processes all queued updates from a requestAnimationFrame callback.
*/
_addBatchedPreviewSelectionUpdate(
callback: (HorizontalViewport) => PreviewSelection
) {
if (this._pendingPreviewSelectionUpdates.length === 0) {
requestAnimationFrame(() =>
this._flushPendingPreviewSelectionUpdates()
);
}
this._pendingPreviewSelectionUpdates.push(callback);
}
/**
* Flush all batched preview selection updates at once, with only a single
* call to update the Redux store.
*/
_flushPendingPreviewSelectionUpdates() {
if (this._pendingPreviewSelectionUpdates.length !== 0) {
const pendingUpdates = this._pendingPreviewSelectionUpdates;
this._pendingPreviewSelectionUpdates = [];
const {
updatePreviewSelection,
viewportProps: { previewSelection, timeRange },
} = this.props;
let nextSelection = previewSelection;
for (const callback of pendingUpdates) {
const horizontalViewport = this.getHorizontalViewport(
nextSelection,
timeRange
);
nextSelection = callback(horizontalViewport);
}
updatePreviewSelection(nextSelection);
}
}
zoomWithMouseWheel(
event: WheelEvent,
// Allow different handlers to make the zoom faster or slower.
zoomModifier: number = 1
) {
const {
hasZoomedViaMousewheel,
setHasZoomedViaMousewheel,
viewportProps: { marginLeft, marginRight },
} = this.props;
if (!hasZoomedViaMousewheel && setHasZoomedViaMousewheel) {
setHasZoomedViaMousewheel();
}
const deltaY = getNormalizedScrollDelta(
event,
this.state.containerHeight,
// Shift is a modifier that will change some mice to scroll horizontally, check
// for that here.
event.deltaY === 0 ? 'deltaX' : 'deltaY'
);
const zoomFactor = this._deltaToZoomFactor(deltaY * zoomModifier);
const mouseX = event.clientX;
const { containerLeft, containerWidth } = this.state;
const viewportPixelWidth = containerWidth - marginLeft - marginRight;
const mouseCenter =
(mouseX - containerLeft - marginLeft) / viewportPixelWidth;
this.zoomRangeSelection(mouseCenter, zoomFactor);
}
zoomRangeSelection = (
// A number between 0 and 1 indicating the horizontal position of the
// zoom center.
center,
// The factor to zoom by. Factors smaller than 1 zoom in, larger than 1 zoom out.
zoomFactor
) => {
const { disableHorizontalMovement, maximumZoom } =
this.props.viewportProps;
if (disableHorizontalMovement) {
return;
}
this._addBatchedPreviewSelectionUpdate(
({ viewportLeft, viewportRight }) => {
const viewportLength = viewportRight - viewportLeft;
const newViewportLength = clamp(
maximumZoom,
1,
viewportLength * zoomFactor
);
const deltaViewportLength = newViewportLength - viewportLength;
const newViewportLeft = clamp(
0,
1 - newViewportLength,
viewportLeft - deltaViewportLength * center
);
const newViewportRight = clamp(
newViewportLength,
1,
viewportRight + deltaViewportLength * (1 - center)
);
const {
viewportProps: { timeRange },
} = this.props;
if (newViewportLeft === 0 && newViewportRight === 1) {
return {
hasSelection: false,
isModifying: false,
};
}
const timeRangeLength = timeRange.end - timeRange.start;
return {
hasSelection: true,
isModifying: false,
selectionStart:
timeRange.start + timeRangeLength * newViewportLeft,
selectionEnd:
timeRange.start + timeRangeLength * newViewportRight,
};
}
);
};
_pointerDownListener = (event: SyntheticPointerEvent<>) => {
event.preventDefault();
const container = this._container;
if (container) {
container.focus();
container.setPointerCapture((event.pointerId:any));
container.addEventListener(
'pointermove',
this._pointerMoveListener,
true
);
container.addEventListener(
'pointerup',
this._pointerUpListener,
true
);
container.addEventListener(
'pointercancel',
this._pointerUpListener,
true
);
}
};
_removePointerListeners() {
const container = this._container;
if (container) {
container.removeEventListener(
'pointermove',
this._pointerMoveListener,
true
);
container.removeEventListener(
'pointerup',
this._pointerUpListener,
true
);
container.removeEventListener(
'pointercancel',
this._pointerUpListener,
true
);
}
}
_pointerMoveListener = (event: PointerEvent) => {
event.preventDefault();
if (!event.isPrimary) {
// If two (or more) fingers are dragging, ignore everything but the
// primary, so that our delta computation below doesn't get confused.
// TODO: Implement pinch zooming
return;
}
let { _dragX: dragX, _dragY: dragY } = this;
if (!this.state.isDragging) {
dragX = event.clientX;
dragY = event.clientY;
}
const offsetX = event.clientX - dragX;
const offsetY = event.clientY - dragY;
this._dragX = event.clientX;
this._dragY = event.clientY;
this.setState({ isDragging: true });
this.moveViewport(offsetX, offsetY);
};
_keyDownListener = (
event: { nativeEvent: KeyboardEvent } & SyntheticKeyboardEvent<>
) => {
let navigationKey;
if (
!event.ctrlKey &&
!event.shiftKey &&
!event.altKey &&
!event.metaKey
) {
navigationKey = BARE_KEYMAP[event.nativeEvent.code];
} else if (event.ctrlKey) {
/* Having the ctrl key down changes the meaning of the key
* it's modifying, so pick the navigation key from another keymap. */
navigationKey = CTRL_KEYMAP[event.nativeEvent.code];
}
if (navigationKey !== undefined) {
// Start requesting frames if there were no keys down before
// this event triggered.
if (this._keysDown.size === 0) {
requestAnimationFrame(this._keyboardNavigation);
this._lastKeyboardNavigationFrame = performance.now();
}
this._keysDown.add(navigationKey);
event.preventDefault();
}
};
_keyUpListener = (
event: { nativeEvent: KeyboardEvent } & SyntheticKeyboardEvent<>
) => {
if (!event.ctrlKey) {
// The ctrl modifier might have been released here. Try to
// delete all keys associated with the modifier. Since the
// navigation is aliased with non-ctrl-modified keys also,
// this will affect (stop) the operation even if it was
// introduced without a ctrl-modified key.
for (const code of getObjectValuesAsUnion(CTRL_KEYMAP)) {
this._keysDown.delete(code);
}
}
this._keysDown.delete(CTRL_KEYMAP[event.nativeEvent.code]);
this._keysDown.delete(BARE_KEYMAP[event.nativeEvent.code]);
};
_onBlur = () => {
this._keysDown.clear();
};
_keyboardNavigation = (timestamp) => {
if (this._keysDown.size === 0) {
// No keys are down, nothing to do. Don't request a new
// animation frame.
return;
}
const delta = Math.min(
KEYBOARD_NAVIGATION_SPEED *
(timestamp - this._lastKeyboardNavigationFrame) *
0.001,
MAX_KEYBOARD_DELTA // Don't jump like crazy if we experience long janks
);
this._lastKeyboardNavigationFrame = timestamp;
for (const navigationKey of this._keysDown.values()) {
switch (navigationKey) {
case 'zoomIn':
this.zoomRangeSelection(0.5, this._deltaToZoomFactor(-delta));
break;
case 'zoomOut':
this.zoomRangeSelection(0.5, this._deltaToZoomFactor(delta));
break;
case 'up':
this.moveViewport(0, delta);
break;
case 'down':
this.moveViewport(0, -delta);
break;
case 'left':
this.moveViewport(delta, 0);
break;
case 'right':
this.moveViewport(-delta, 0);
break;
default:
throw assertExhaustiveCheck(navigationKey);
}
}
requestAnimationFrame(this._keyboardNavigation);
};
moveViewport = (offsetX: CssPixels, offsetY: CssPixels): void => {
const {
viewportProps: {
maxViewportHeight,
timeRange,
startsAtBottom,
disableHorizontalMovement,
marginLeft,
marginRight,
},
} = this.props;
const { containerWidth, containerHeight, viewportTop, isSizeSet } =
this.state;
if (!isSizeSet) {
// Moving the viewport and calculating its dimensions
// assumes its size is actually set. Just ignore the move
// request if it's not.
return;
}
// Calculate top and bottom in terms of pixels.
let newViewportTop: CssPixels = viewportTop - offsetY;
let newViewportBottom: CssPixels = newViewportTop + containerHeight;
if (maxViewportHeight < containerHeight) {
// If the view is extra small, anchor content to the top or bottom.
if (startsAtBottom) {
newViewportTop = maxViewportHeight - containerHeight;
newViewportBottom = maxViewportHeight;
} else {
newViewportTop = 0;
newViewportBottom = containerHeight;
}
} else if (newViewportBottom > maxViewportHeight) {
// Constrain the viewport to the bottom.
newViewportTop = maxViewportHeight - containerHeight;
newViewportBottom = maxViewportHeight;
} else if (newViewportTop < 0) {
// Constrain the viewport to the top.
newViewportTop = 0;
newViewportBottom = containerHeight;
}
if (newViewportTop !== viewportTop) {
this.setState({
viewportTop: newViewportTop,
viewportBottom: newViewportBottom,
});
}
if (!disableHorizontalMovement) {
this._addBatchedPreviewSelectionUpdate(
({ viewportLeft, viewportRight }) => {
// Calculate left and right in terms of the unit interval of the profile range.
const viewportLength = viewportRight - viewportLeft;
if (viewportLength >= 1) {
return {
hasSelection: false,
isModifying: false,
};
}
const viewportPixelWidth =
containerWidth - marginLeft - marginRight;
const unitOffsetX =
offsetX * (viewportLength / viewportPixelWidth);
let newViewportLeft = viewportLeft - unitOffsetX;
let newViewportRight = viewportRight - unitOffsetX;
if (newViewportLeft < 0) {
newViewportLeft = 0;
newViewportRight = viewportLength;
}
if (newViewportRight > 1) {
newViewportLeft = 1 - viewportLength;
newViewportRight = 1;
}
const timeRangeLength = timeRange.end - timeRange.start;
return {
hasSelection: true,
isModifying: false,
selectionStart:
timeRange.start + timeRangeLength * newViewportLeft,
selectionEnd:
timeRange.start + timeRangeLength * newViewportRight,
};
}
);
}
};
_pointerUpListener = (event: PointerEvent) => {
event.preventDefault();
if (!event.isPrimary) {
// Keep dragging if the primary pointer is still down.
return;
}
this._removePointerListeners();
this.setState({
isDragging: false,
});
};
componentDidMount() {
// The first _setSize ensures that the screen does not blip when mounting
// the component, while the second ensures that it lays out correctly if the DOM
// is not fully layed out correctly yet.
this._setSize();
this._setSizeNextFrame();
if (this._container) {
const container = this._container;
getResizeObserverWrapper().subscribe(container, this._setSize);
container.addEventListener('wheel', this._pointerWheelListener, {
passive: false,
});
}
}
componentWillUnmount() {
window.removeEventListener('resize', this._setSizeNextFrame, false);
const container = this._container;
if (container) {
getResizeObserverWrapper().unsubscribe(container, this._setSize);
container.removeEventListener('wheel', this._pointerWheelListener, {
passive: false,
});
this._removePointerListeners();
}
}
render() {
const {
chartProps,
hasZoomedViaMousewheel,
viewportProps: { className },
} = this.props;
const {
containerWidth,
containerHeight,
viewportTop,
viewportBottom,
horizontalViewport: { viewportLeft, viewportRight },
isDragging,
isScrollHintVisible,
isSizeSet,
} = this.state;
const viewportClassName = classNames(
{
chartViewport: true,
dragging: isDragging,
},
className
);
const scrollClassName = classNames({
chartViewportScroll: true,
hidden: hasZoomedViaMousewheel || !isScrollHintVisible,
});
const viewport: Viewport = {
containerWidth,
containerHeight,
viewportLeft,
viewportRight,
viewportTop,
viewportBottom,
isDragging,
moveViewport: this.moveViewport,
isSizeSet,
};
return (
<div
className={viewportClassName}
style={{ 'touch-action': 'none' }}
onPointerDown={this._pointerDownListener}
onKeyDown={this._keyDownListener}
onKeyUp={this._keyUpListener}
onBlur={this._onBlur}
ref={this._takeContainerRef}
tabIndex={0}
>
<ChartComponent {...chartProps} viewport={viewport} />
<div className={scrollClassName}>
Zoom Chart:
<kbd className="chartViewportScrollKbd">Ctrl + Scroll</kbd>or
<kbd className="chartViewportScrollKbd">Shift + Scroll</kbd>
</div>
</div>
);
}
}
// Connect this component so that it knows whether or not to nag the user to use ctrl
// for zooming on range selections.
return explicitConnect<
ViewportOwnProps<ChartOwnProps>,
ViewportStateProps,
ViewportDispatchProps,
>({
mapStateToProps: (state) => ({
panelLayoutGeneration: getPanelLayoutGeneration(state),
hasZoomedViaMousewheel: getHasZoomedViaMousewheel(state),
}),
mapDispatchToProps: { setHasZoomedViaMousewheel, updatePreviewSelection },
component: ChartViewport,
});
};
function clamp(min, max, value) {
return Math.max(min, Math.min(max, value));
}
const SCROLL_LINE_SIZE = 15;
/**
* Scroll wheel events can by of various types. Do the right thing by converting these
* into CssPixels. https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaMode
*/
function getNormalizedScrollDelta(
event: WheelEvent,
pageHeight: number,
key: 'deltaY' | 'deltaX'
): CssPixels {
const delta = key === 'deltaY' ? event.deltaY : event.deltaX;
switch (event.deltaMode) {
case DOM_DELTA_PAGE:
return delta * pageHeight;
case DOM_DELTA_LINE:
return delta * SCROLL_LINE_SIZE;
default:
}
// Scroll by pixel.
return delta;
}