• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/**
16 * These interactions may be added to a ZonedInteractionHandler. They define
17 * some common operations that are used by several parts of the timeline such as
18 * shift+drag to pan and mouse wheel navigation.
19 */
20
21import {Rect2D} from '../../base/geom';
22import {TimeScale} from '../../base/time_scale';
23import {Zone} from '../../base/zoned_interaction_handler';
24import {TraceImpl} from '../../core/trace_impl';
25
26const WHEEL_ZOOM_SPEED = -0.02;
27
28export function shiftDragPanInteraction(
29  trace: TraceImpl,
30  rect: Rect2D,
31  timescale: TimeScale,
32): Zone {
33  return {
34    id: 'drag-pan',
35    area: rect,
36    cursor: 'grab',
37    keyModifier: 'shift',
38    drag: {
39      cursorWhileDragging: 'grabbing',
40      onDrag: (e) => {
41        trace.timeline.panVisibleWindow(
42          timescale.pxToDuration(-e.deltaSinceLastEvent.x),
43        );
44      },
45    },
46  };
47}
48
49export function wheelNavigationInteraction(
50  trace: TraceImpl,
51  rect: Rect2D,
52  timescale: TimeScale,
53): Zone {
54  return {
55    id: 'mouse-wheel-navigation',
56    area: rect,
57    onWheel: (e) => {
58      if (Math.abs(e.deltaX) > Math.abs(e.deltaY)) {
59        const tDelta = timescale.pxToDuration(e.deltaX);
60        trace.timeline.panVisibleWindow(tDelta);
61      } else {
62        if (e.ctrlKey) {
63          const sign = e.deltaY < 0 ? -1 : 1;
64          const deltaY = sign * Math.log2(1 + Math.abs(e.deltaY));
65          const zoomPx = e.position.x - rect.left;
66          const centerPoint = zoomPx / rect.width;
67          trace.timeline.zoomVisibleWindow(
68            1 - deltaY * WHEEL_ZOOM_SPEED,
69            centerPoint,
70          );
71        }
72      }
73    },
74  };
75}
76