xref: /aosp_15_r20/external/perfetto/ui/src/components/widgets/slice.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 m from 'mithril';
16import {asSliceSqlId, SliceSqlId} from '../sql_utils/core_types';
17import {Anchor} from '../../widgets/anchor';
18import {Icons} from '../../base/semantic_icons';
19import {getSlice, SliceDetails} from '../sql_utils/slice';
20import {
21  createSqlIdRefRenderer,
22  sqlIdRegistry,
23} from './sql/details/sql_ref_renderer_registry';
24import {AppImpl} from '../../core/app_impl';
25
26interface SliceRefAttrs {
27  readonly id: SliceSqlId;
28  readonly name: string;
29
30  // Whether clicking on the reference should change the current tab
31  // to "current selection" tab in addition to updating the selection
32  // and changing the viewport. True by default.
33  readonly switchToCurrentSelectionTab?: boolean;
34}
35
36export class SliceRef implements m.ClassComponent<SliceRefAttrs> {
37  view(vnode: m.Vnode<SliceRefAttrs>) {
38    return m(
39      Anchor,
40      {
41        icon: Icons.UpdateSelection,
42        onclick: () => {
43          // TODO(primiano): the Trace object should be properly injected here.
44          AppImpl.instance.trace?.selection.selectSqlEvent(
45            'slice',
46            vnode.attrs.id,
47            {
48              switchToCurrentSelectionTab:
49                vnode.attrs.switchToCurrentSelectionTab,
50              scrollToSelection: true,
51            },
52          );
53        },
54      },
55      vnode.attrs.name,
56    );
57  }
58}
59
60export function sliceRef(slice: SliceDetails, name?: string): m.Child {
61  return m(SliceRef, {
62    id: slice.id,
63    name: name ?? slice.name,
64  });
65}
66
67sqlIdRegistry['slice'] = createSqlIdRefRenderer<{
68  slice: SliceDetails | undefined;
69  id: bigint;
70}>(
71  async (engine, id) => {
72    return {
73      id,
74      slice: await getSlice(engine, asSliceSqlId(Number(id))),
75    };
76  },
77  ({id, slice}) => ({
78    value: slice !== undefined ? sliceRef(slice) : `Unknown slice ${id}`,
79  }),
80);
81