2021-01-07 16:15:34 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-01-28 09:33:20 -08:00
|
|
|
import { ContextEntry, InterestingPageEvent, ActionEntry, trace } from '../../../cli/traceViewer/traceModel';
|
2021-01-07 16:15:34 -08:00
|
|
|
import './timeline.css';
|
|
|
|
|
import { Boundaries } from '../geometry';
|
|
|
|
|
import * as React from 'react';
|
2021-01-26 15:09:17 -08:00
|
|
|
import { msToString, useMeasure } from './helpers';
|
2021-01-15 18:30:55 -08:00
|
|
|
|
|
|
|
|
type TimelineBar = {
|
|
|
|
|
entry?: ActionEntry;
|
|
|
|
|
event?: InterestingPageEvent;
|
|
|
|
|
leftPosition: number;
|
|
|
|
|
rightPosition: number;
|
|
|
|
|
leftTime: number;
|
|
|
|
|
rightTime: number;
|
|
|
|
|
type: string;
|
|
|
|
|
label: string;
|
|
|
|
|
priority: number;
|
|
|
|
|
};
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
|
export const Timeline: React.FunctionComponent<{
|
|
|
|
|
context: ContextEntry,
|
|
|
|
|
boundaries: Boundaries,
|
2021-01-14 20:16:02 -08:00
|
|
|
selectedAction: ActionEntry | undefined,
|
|
|
|
|
highlightedAction: ActionEntry | undefined,
|
|
|
|
|
onSelected: (action: ActionEntry) => void,
|
2021-01-27 19:42:51 -08:00
|
|
|
onTimeSelected: (time: number | undefined) => void,
|
|
|
|
|
}> = ({ context, boundaries, selectedAction, highlightedAction, onSelected, onTimeSelected }) => {
|
2021-01-07 16:15:34 -08:00
|
|
|
const [measure, ref] = useMeasure<HTMLDivElement>();
|
|
|
|
|
const [previewX, setPreviewX] = React.useState<number | undefined>();
|
2021-01-15 18:30:55 -08:00
|
|
|
const [hoveredBar, setHoveredBar] = React.useState<TimelineBar | undefined>();
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
|
const offsets = React.useMemo(() => {
|
|
|
|
|
return calculateDividerOffsets(measure.width, boundaries);
|
|
|
|
|
}, [measure.width, boundaries]);
|
2021-01-15 18:30:55 -08:00
|
|
|
|
|
|
|
|
let targetBar: TimelineBar | undefined = hoveredBar;
|
|
|
|
|
const bars = React.useMemo(() => {
|
|
|
|
|
const bars: TimelineBar[] = [];
|
|
|
|
|
for (const page of context.pages) {
|
|
|
|
|
for (const entry of page.actions) {
|
2021-01-20 16:19:01 -08:00
|
|
|
let detail = entry.action.selector || '';
|
|
|
|
|
if (entry.action.action === 'goto')
|
|
|
|
|
detail = entry.action.value || '';
|
2021-01-15 18:30:55 -08:00
|
|
|
bars.push({
|
|
|
|
|
entry,
|
|
|
|
|
leftTime: entry.action.startTime,
|
|
|
|
|
rightTime: entry.action.endTime,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, entry.action.startTime),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, entry.action.endTime),
|
2021-01-20 16:19:01 -08:00
|
|
|
label: entry.action.action + ' ' + detail,
|
2021-01-15 18:30:55 -08:00
|
|
|
type: entry.action.action,
|
|
|
|
|
priority: 0,
|
|
|
|
|
});
|
|
|
|
|
if (entry === (highlightedAction || selectedAction))
|
|
|
|
|
targetBar = bars[bars.length - 1];
|
|
|
|
|
}
|
|
|
|
|
let lastDialogOpened: trace.DialogOpenedEvent | undefined;
|
|
|
|
|
for (const event of page.interestingEvents) {
|
|
|
|
|
if (event.type === 'dialog-opened') {
|
|
|
|
|
lastDialogOpened = event;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (event.type === 'dialog-closed' && lastDialogOpened) {
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: lastDialogOpened.timestamp,
|
|
|
|
|
rightTime: event.timestamp,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, lastDialogOpened.timestamp),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
label: lastDialogOpened.message ? `${event.dialogType} "${lastDialogOpened.message}"` : event.dialogType,
|
|
|
|
|
type: 'dialog',
|
|
|
|
|
priority: -1,
|
|
|
|
|
});
|
|
|
|
|
} else if (event.type === 'navigation') {
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: event.timestamp,
|
|
|
|
|
rightTime: event.timestamp,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
label: `navigated to ${event.url}`,
|
|
|
|
|
type: event.type,
|
|
|
|
|
priority: 1,
|
|
|
|
|
});
|
|
|
|
|
} else if (event.type === 'load') {
|
|
|
|
|
bars.push({
|
|
|
|
|
event,
|
|
|
|
|
leftTime: event.timestamp,
|
|
|
|
|
rightTime: event.timestamp,
|
|
|
|
|
leftPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
rightPosition: timeToPosition(measure.width, boundaries, event.timestamp),
|
|
|
|
|
label: `load`,
|
|
|
|
|
type: event.type,
|
|
|
|
|
priority: 1,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bars.sort((a, b) => a.priority - b.priority);
|
|
|
|
|
return bars;
|
|
|
|
|
}, [context, boundaries, measure.width]);
|
|
|
|
|
|
|
|
|
|
const findHoveredBar = (x: number) => {
|
2021-01-14 20:16:02 -08:00
|
|
|
const time = positionToTime(measure.width, boundaries, x);
|
|
|
|
|
const time1 = positionToTime(measure.width, boundaries, x - 5);
|
|
|
|
|
const time2 = positionToTime(measure.width, boundaries, x + 5);
|
2021-01-15 18:30:55 -08:00
|
|
|
let bar: TimelineBar | undefined;
|
2021-01-14 20:16:02 -08:00
|
|
|
let distance: number | undefined;
|
2021-01-15 18:30:55 -08:00
|
|
|
for (const b of bars) {
|
|
|
|
|
const left = Math.max(b.leftTime, time1);
|
|
|
|
|
const right = Math.min(b.rightTime, time2);
|
|
|
|
|
const middle = (b.leftTime + b.rightTime) / 2;
|
2021-01-14 20:16:02 -08:00
|
|
|
const d = Math.abs(time - middle);
|
2021-01-15 18:30:55 -08:00
|
|
|
if (left <= right && (!bar || d < distance!)) {
|
|
|
|
|
bar = b;
|
2021-01-14 20:16:02 -08:00
|
|
|
distance = d;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 18:30:55 -08:00
|
|
|
return bar;
|
2021-01-14 20:16:02 -08:00
|
|
|
};
|
|
|
|
|
|
2021-01-07 16:15:34 -08:00
|
|
|
const onMouseMove = (event: React.MouseEvent) => {
|
2021-01-14 20:16:02 -08:00
|
|
|
if (ref.current) {
|
|
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
|
|
|
|
setPreviewX(x);
|
2021-01-27 19:42:51 -08:00
|
|
|
onTimeSelected(positionToTime(measure.width, boundaries, x));
|
|
|
|
|
setHoveredBar(findHoveredBar(x));
|
2021-01-14 20:16:02 -08:00
|
|
|
}
|
2021-01-07 16:15:34 -08:00
|
|
|
};
|
|
|
|
|
const onMouseLeave = () => {
|
|
|
|
|
setPreviewX(undefined);
|
2021-01-27 19:42:51 -08:00
|
|
|
onTimeSelected(undefined);
|
2021-01-07 16:15:34 -08:00
|
|
|
};
|
2021-01-26 15:09:17 -08:00
|
|
|
const onActionClick = (event: React.MouseEvent) => {
|
2021-01-14 20:16:02 -08:00
|
|
|
if (ref.current) {
|
|
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
2021-01-15 18:30:55 -08:00
|
|
|
const bar = findHoveredBar(x);
|
|
|
|
|
if (bar && bar.entry)
|
|
|
|
|
onSelected(bar.entry);
|
2021-01-26 15:09:17 -08:00
|
|
|
event.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const onTimeClick = (event: React.MouseEvent) => {
|
|
|
|
|
if (ref.current) {
|
|
|
|
|
const x = event.clientX - ref.current.getBoundingClientRect().left;
|
|
|
|
|
const time = positionToTime(measure.width, boundaries, x);
|
|
|
|
|
onTimeSelected(time);
|
|
|
|
|
event.stopPropagation();
|
2021-01-14 20:16:02 -08:00
|
|
|
}
|
|
|
|
|
};
|
2021-01-07 16:15:34 -08:00
|
|
|
|
2021-01-26 15:09:17 -08:00
|
|
|
return <div ref={ref} className='timeline-view' onMouseMove={onMouseMove} onMouseOver={onMouseMove} onMouseLeave={onMouseLeave} onClick={onTimeClick}>
|
2021-01-07 16:15:34 -08:00
|
|
|
<div className='timeline-grid'>{
|
|
|
|
|
offsets.map((offset, index) => {
|
2021-01-15 18:30:55 -08:00
|
|
|
return <div key={index} className='timeline-divider' style={{ left: offset.position + 'px' }}>
|
|
|
|
|
<div className='timeline-time'>{msToString(offset.time - boundaries.minimum)}</div>
|
2021-01-07 16:15:34 -08:00
|
|
|
</div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-01-15 18:30:55 -08:00
|
|
|
<div className='timeline-lane timeline-labels'>{
|
|
|
|
|
bars.map((bar, index) => {
|
|
|
|
|
return <div key={index}
|
|
|
|
|
className={'timeline-label ' + bar.type + (targetBar === bar ? ' selected' : '')}
|
2021-01-14 20:16:02 -08:00
|
|
|
style={{
|
2021-01-15 18:30:55 -08:00
|
|
|
left: bar.leftPosition + 'px',
|
|
|
|
|
width: Math.max(1, bar.rightPosition - bar.leftPosition) + 'px',
|
2021-01-14 20:16:02 -08:00
|
|
|
}}
|
2021-01-07 16:15:34 -08:00
|
|
|
>
|
2021-01-15 18:30:55 -08:00
|
|
|
{bar.label}
|
2021-01-07 16:15:34 -08:00
|
|
|
</div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-01-26 15:09:17 -08:00
|
|
|
<div className='timeline-lane timeline-bars' onClick={onActionClick}>{
|
2021-01-15 18:30:55 -08:00
|
|
|
bars.map((bar, index) => {
|
|
|
|
|
return <div key={index}
|
|
|
|
|
className={'timeline-bar ' + bar.type + (targetBar === bar ? ' selected' : '')}
|
2021-01-07 16:15:34 -08:00
|
|
|
style={{
|
2021-01-15 18:30:55 -08:00
|
|
|
left: bar.leftPosition + 'px',
|
|
|
|
|
width: Math.max(1, bar.rightPosition - bar.leftPosition) + 'px',
|
2021-01-07 16:15:34 -08:00
|
|
|
}}
|
|
|
|
|
></div>;
|
|
|
|
|
})
|
|
|
|
|
}</div>
|
2021-01-15 18:30:55 -08:00
|
|
|
<div className='timeline-marker timeline-marker-hover' style={{
|
2021-01-07 16:15:34 -08:00
|
|
|
display: (previewX !== undefined) ? 'block' : 'none',
|
|
|
|
|
left: (previewX || 0) + 'px',
|
|
|
|
|
}}></div>
|
|
|
|
|
</div>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-15 18:30:55 -08:00
|
|
|
function calculateDividerOffsets(clientWidth: number, boundaries: Boundaries): { position: number, time: number }[] {
|
2021-01-07 16:15:34 -08:00
|
|
|
const minimumGap = 64;
|
|
|
|
|
let dividerCount = clientWidth / minimumGap;
|
|
|
|
|
const boundarySpan = boundaries.maximum - boundaries.minimum;
|
|
|
|
|
const pixelsPerMillisecond = clientWidth / boundarySpan;
|
|
|
|
|
let sectionTime = boundarySpan / dividerCount;
|
|
|
|
|
|
|
|
|
|
const logSectionTime = Math.ceil(Math.log(sectionTime) / Math.LN10);
|
|
|
|
|
sectionTime = Math.pow(10, logSectionTime);
|
|
|
|
|
if (sectionTime * pixelsPerMillisecond >= 5 * minimumGap)
|
|
|
|
|
sectionTime = sectionTime / 5;
|
|
|
|
|
if (sectionTime * pixelsPerMillisecond >= 2 * minimumGap)
|
|
|
|
|
sectionTime = sectionTime / 2;
|
|
|
|
|
|
|
|
|
|
const firstDividerTime = boundaries.minimum;
|
|
|
|
|
let lastDividerTime = boundaries.maximum;
|
|
|
|
|
lastDividerTime += minimumGap / pixelsPerMillisecond;
|
|
|
|
|
dividerCount = Math.ceil((lastDividerTime - firstDividerTime) / sectionTime);
|
|
|
|
|
|
|
|
|
|
if (!sectionTime)
|
|
|
|
|
dividerCount = 0;
|
|
|
|
|
|
|
|
|
|
const offsets = [];
|
|
|
|
|
for (let i = 0; i < dividerCount; ++i) {
|
|
|
|
|
const time = firstDividerTime + sectionTime * i;
|
2021-01-15 18:30:55 -08:00
|
|
|
offsets.push({ position: timeToPosition(clientWidth, boundaries, time), time });
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
|
return offsets;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 18:30:55 -08:00
|
|
|
function timeToPosition(clientWidth: number, boundaries: Boundaries, time: number): number {
|
|
|
|
|
return (time - boundaries.minimum) / (boundaries.maximum - boundaries.minimum) * clientWidth;
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 20:16:02 -08:00
|
|
|
function positionToTime(clientWidth: number, boundaries: Boundaries, x: number): number {
|
2021-01-15 18:30:55 -08:00
|
|
|
return x / clientWidth * (boundaries.maximum - boundaries.minimum) + boundaries.minimum;
|
2021-01-14 20:16:02 -08:00
|
|
|
}
|