• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 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 {copyToClipboard} from '../../base/clipboard';
17import {assertExists} from '../../base/logging';
18import {Icons} from '../../base/semantic_icons';
19import {duration} from '../../base/time';
20import {AppImpl} from '../../core/app_impl';
21import {Anchor} from '../../widgets/anchor';
22import {MenuDivider, MenuItem, PopupMenu} from '../../widgets/menu';
23import {Trace} from '../../public/trace';
24import {formatDuration} from '../time_utils';
25import {DurationPrecisionMenuItem} from './duration_precision_menu_items';
26import {TimestampFormatMenuItem} from './timestamp_format_menu';
27
28interface DurationWidgetAttrs {
29  dur: duration;
30  extraMenuItems?: m.Child[];
31}
32
33export class DurationWidget implements m.ClassComponent<DurationWidgetAttrs> {
34  private readonly trace: Trace;
35
36  constructor() {
37    // TODO(primiano): the Trace object should be injected into the attrs, but
38    // there are too many users of this class and doing so requires a larger
39    // refactoring CL. Either that or we should find a different way to plumb
40    // the hoverCursorTimestamp.
41    this.trace = assertExists(AppImpl.instance.trace);
42  }
43
44  view({attrs}: m.Vnode<DurationWidgetAttrs>) {
45    const {dur} = attrs;
46
47    const value: m.Children =
48      dur === -1n ? m('i', '(Did not end)') : formatDuration(this.trace, dur);
49
50    return m(
51      PopupMenu,
52      {
53        trigger: m(Anchor, value),
54      },
55      m(MenuItem, {
56        icon: Icons.Copy,
57        label: `Copy raw value`,
58        onclick: () => {
59          copyToClipboard(dur.toString());
60        },
61      }),
62      m(TimestampFormatMenuItem, {trace: this.trace}),
63      m(DurationPrecisionMenuItem, {trace: this.trace}),
64      attrs.extraMenuItems ? [m(MenuDivider), attrs.extraMenuItems] : null,
65    );
66  }
67}
68