1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use size 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'; 16 17import {Time} from '../base/time'; 18 19import {TRACK_SHELL_WIDTH} from './css_constants'; 20import {globals} from './globals'; 21import { 22 getMaxMajorTicks, 23 TickGenerator, 24 TickType, 25 timeScaleForVisibleWindow, 26} from './gridline_helper'; 27import {PanelSize} from './panel'; 28import {Panel} from './panel_container'; 29 30// This is used to display the summary of search results. 31export class TickmarkPanel implements Panel { 32 readonly kind = 'panel'; 33 readonly selectable = false; 34 35 render(): m.Children { 36 return m('.tickbar'); 37 } 38 39 renderCanvas(ctx: CanvasRenderingContext2D, size: PanelSize) { 40 const {visibleTimeScale} = globals.timeline; 41 42 ctx.fillStyle = '#999'; 43 ctx.fillRect(TRACK_SHELL_WIDTH - 2, 0, 2, size.height); 44 45 ctx.save(); 46 ctx.beginPath(); 47 ctx.rect(TRACK_SHELL_WIDTH, 0, size.width - TRACK_SHELL_WIDTH, size.height); 48 ctx.clip(); 49 50 const visibleSpan = globals.timeline.visibleTimeSpan; 51 if (size.width > TRACK_SHELL_WIDTH && visibleSpan.duration > 0n) { 52 const maxMajorTicks = getMaxMajorTicks(size.width - TRACK_SHELL_WIDTH); 53 const map = timeScaleForVisibleWindow(TRACK_SHELL_WIDTH, size.width); 54 55 const offset = globals.timestampOffset(); 56 const tickGen = new TickGenerator(visibleSpan, maxMajorTicks, offset); 57 for (const {type, time} of tickGen) { 58 const px = Math.floor(map.timeToPx(time)); 59 if (type === TickType.MAJOR) { 60 ctx.fillRect(px, 0, 1, size.height); 61 } 62 } 63 } 64 65 const data = globals.searchSummary; 66 for (let i = 0; i < data.tsStarts.length; i++) { 67 const tStart = Time.fromRaw(data.tsStarts[i]); 68 const tEnd = Time.fromRaw(data.tsEnds[i]); 69 if (!visibleSpan.intersects(tStart, tEnd)) { 70 continue; 71 } 72 const rectStart = 73 Math.max(visibleTimeScale.timeToPx(tStart), 0) + TRACK_SHELL_WIDTH; 74 const rectEnd = visibleTimeScale.timeToPx(tEnd) + TRACK_SHELL_WIDTH; 75 ctx.fillStyle = '#ffe263'; 76 ctx.fillRect( 77 Math.floor(rectStart), 78 0, 79 Math.ceil(rectEnd - rectStart), 80 size.height, 81 ); 82 } 83 const index = globals.state.searchIndex; 84 if (index !== -1 && index < globals.currentSearchResults.tses.length) { 85 const start = globals.currentSearchResults.tses[index]; 86 if (start !== -1n) { 87 const triangleStart = 88 Math.max(visibleTimeScale.timeToPx(Time.fromRaw(start)), 0) + 89 TRACK_SHELL_WIDTH; 90 ctx.fillStyle = '#000'; 91 ctx.beginPath(); 92 ctx.moveTo(triangleStart, size.height); 93 ctx.lineTo(triangleStart - 3, 0); 94 ctx.lineTo(triangleStart + 3, 0); 95 ctx.lineTo(triangleStart, size.height); 96 ctx.fill(); 97 ctx.closePath(); 98 } 99 } 100 101 ctx.restore(); 102 } 103} 104