xref: /aosp_15_r20/external/perfetto/ui/src/public/scroll_helper.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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
15import {time} from '../base/time';
16
17/**
18 * A helper to scroll to a combination of tracks and time ranges.
19 * This exist to decouple the selection logic to the scrolling logic. Nothing in
20 * this file changes the selection status. Use SelectionManager for that.
21 */
22export interface ScrollToArgs {
23  // Given a start and end timestamp (in ns), move the viewport to center this
24  //  range and zoom if necessary:
25  //  - If [viewPercentage] is specified, the viewport will be zoomed so that
26  //    the given time range takes up this percentage of the viewport.
27  //  The following scenarios assume [viewPercentage] is undefined.
28  //  - If the new range is more than 50% of the viewport, zoom out to a level
29  //  where
30  //    the range is 1/5 of the viewport.
31  //  - If the new range is already centered, update the zoom level for the
32  //  viewport
33  //    to cover 1/5 of the viewport.
34  //  - Otherwise, preserve the zoom range.
35  //
36  time?: {
37    start: time;
38    end?: time;
39    viewPercentage?: number;
40  };
41  // Find the track with a given uri in the current workspace and scroll it into
42  // view. Iftrack is nested inside a track group, scroll to that track group
43  // instead. If `expandGroup` == true, open the track group and scroll to the
44  // track.
45  // TODO(primiano): 90% of the times we seem to want expandGroup: true, so we
46  // should probably flip the default value, and pass false in the few places
47  // where we do NOT want this behavior.
48  track?: {
49    uri: string;
50    expandGroup?: boolean;
51  };
52}
53
54// TODO(primiano): remove this injection once we plumb Trace into all the
55// components. Right now too many places need this. This is a temporary solution
56// to avoid too many invasive refactorings at once.
57
58type ScrollToFunction = (a: ScrollToArgs) => void;
59let _scrollToFunction: ScrollToFunction | undefined = undefined;
60
61// If a Trace object is avilable, prefer Trace.scrollTo(). It points to the
62// same function.
63export function scrollTo(args: ScrollToArgs) {
64  _scrollToFunction?.(args);
65}
66
67export function setScrollToFunction(f: ScrollToFunction | undefined) {
68  _scrollToFunction = f;
69}
70