1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.requireStylesheet('tracing.tracks.slice_track');
9 tvcm.require('tvcm.sorted_array_utils');
10 tvcm.require('tracing.tracks.heading_track');
11 tvcm.require('tracing.fast_rect_renderer');
12 tvcm.require('tracing.draw_helpers');
13 tvcm.require('tvcm.ui');
15 tvcm.exportTo('tracing.tracks', function() {
18 * A track that displays an array of Slice objects.
20 * @extends {HeadingTrack}
22 var SliceTrack = tvcm.ui.define(
23 'slice-track', tracing.tracks.HeadingTrack);
25 SliceTrack.prototype = {
27 __proto__: tracing.tracks.HeadingTrack.prototype,
29 decorate: function(viewport) {
30 tracing.tracks.HeadingTrack.prototype.decorate.call(this, viewport);
31 this.classList.add('slice-track');
32 this.asyncStyle_ = false;
37 return this.asyncStyle_;
41 this.asyncStyle_ = !!v;
49 this.slices_ = slices || [];
53 return window.getComputedStyle(this).height;
57 this.style.height = height;
60 get hasVisibleContent() {
61 return this.slices.length > 0;
64 draw: function(type, viewLWorld, viewRWorld) {
66 case tracing.tracks.DrawType.SLICE:
67 this.drawSlices_(viewLWorld, viewRWorld);
72 drawSlices_: function(viewLWorld, viewRWorld) {
73 var ctx = this.context();
76 var bounds = this.getBoundingClientRect();
79 this.viewport.currentDisplayTransform,
87 if (bounds.height <= 8)
92 this.viewport.currentDisplayTransform,
99 addEventsToTrackMap: function(eventToTrackMap) {
100 if (this.slices_ === undefined || this.slices_ === null)
103 this.slices_.forEach(function(slice) {
104 eventToTrackMap.addEvent(slice, this);
108 addIntersectingItemsInRangeToSelectionInWorldSpace: function(
109 loWX, hiWX, viewPixWidthWorld, selection) {
110 function onSlice(slice) {
111 selection.push(slice);
113 tvcm.iterateOverIntersectingIntervals(this.slices_,
114 function(x) { return x.start; },
115 function(x) { return x.duration; },
121 * Find the index for the given slice.
122 * @return {index} Index of the given slice, or undefined.
125 indexOfSlice_: function(slice) {
126 var index = tvcm.findLowIndexInSortedArray(this.slices_,
127 function(x) { return x.start; },
129 while (index < this.slices_.length &&
130 slice.start == this.slices_[index].start &&
131 slice.colorId != this.slices_[index].colorId) {
134 return index < this.slices_.length ? index : undefined;
138 * Add the item to the left or right of the provided event, if any, to the
140 * @param {slice} The current slice.
141 * @param {Number} offset Number of slices away from the event to look.
142 * @param {Selection} selection The selection to add an event to,
144 * @return {boolean} Whether an event was found.
147 addItemNearToProvidedEventToSelection: function(event, offset, selection) {
148 var index = this.indexOfSlice_(event);
149 if (index === undefined)
152 var newIndex = index + offset;
153 if (newIndex < 0 || newIndex >= this.slices_.length)
156 selection.push(this.slices_[newIndex]);
160 addAllObjectsMatchingFilterToSelection: function(filter, selection) {
161 for (var i = 0; i < this.slices_.length; ++i) {
162 if (filter.matchSlice(this.slices_[i]))
163 selection.push(this.slices_[i]);
167 addClosestEventToSelection: function(worldX, worldMaxDist, loY, hiY,
169 var slice = tvcm.findClosestIntervalInSortedIntervals(
171 function(x) { return x.start; },
172 function(x) { return x.end; },
177 selection.push(slice);
182 SliceTrack: SliceTrack