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, PopupMenu2} 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 if (dur === -1n) { 48 return '(Did not end)'; 49 } 50 51 return m( 52 PopupMenu2, 53 { 54 trigger: m(Anchor, formatDuration(this.trace, dur)), 55 }, 56 m(MenuItem, { 57 icon: Icons.Copy, 58 label: `Copy raw value`, 59 onclick: () => { 60 copyToClipboard(dur.toString()); 61 }, 62 }), 63 m(TimestampFormatMenuItem, {trace: this.trace}), 64 m(DurationPrecisionMenuItem, {trace: this.trace}), 65 attrs.extraMenuItems ? [m(MenuDivider), attrs.extraMenuItems] : null, 66 ); 67 } 68} 69