xref: /aosp_15_r20/external/perfetto/ui/src/components/widgets/thread_state.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 {ThreadStateSqlId} from '../sql_utils/core_types';
17import {Anchor} from '../../widgets/anchor';
18import {Icons} from '../../base/semantic_icons';
19import {ThreadState} from '../sql_utils/thread_state';
20import {AppImpl} from '../../core/app_impl';
21
22interface ThreadStateRefAttrs {
23  id: ThreadStateSqlId;
24  // If not present, a placeholder name will be used.
25  name?: string;
26
27  // Whether clicking on the reference should change the current tab
28  // to "current selection" tab in addition to updating the selection
29  // and changing the viewport. True by default.
30  readonly switchToCurrentSelectionTab?: boolean;
31}
32
33export class ThreadStateRef implements m.ClassComponent<ThreadStateRefAttrs> {
34  view(vnode: m.Vnode<ThreadStateRefAttrs>) {
35    return m(
36      Anchor,
37      {
38        icon: Icons.UpdateSelection,
39        onclick: () => {
40          // TODO(primiano): the Trace object should be properly injected here.
41          AppImpl.instance.trace?.selection.selectSqlEvent(
42            'thread_state',
43            vnode.attrs.id,
44            {
45              switchToCurrentSelectionTab:
46                vnode.attrs.switchToCurrentSelectionTab,
47              scrollToSelection: true,
48            },
49          );
50        },
51      },
52      vnode.attrs.name ?? `Thread State ${vnode.attrs.id}`,
53    );
54  }
55}
56
57export function threadStateRef(state: ThreadState): m.Child {
58  if (state.thread === undefined) return null;
59
60  return m(ThreadStateRef, {
61    id: state.id,
62  });
63}
64