import CanvasView from "../lib/CanvasView";
import type Frame from "../lib/model/Frame";
import { SELF_TIME_FRAME_IDENTIFIER } from "../lib/model/Frame";
import { hash, map, parseColor, sampleGradient } from "../lib/utils";
import TimelineCanvasViewTooltip, { estimateWidth, type TooltipFrameInfo } from "./TimelineCanvasViewTooltip.svelte";
import type { ComponentProps } from "svelte";
const BACKGROUND_COLOR = "#212325";
const FRAME_PITCH = 18;
const FRAME_HEIGHT = 17;
const X_MARGIN = 28;
const Y_MARGIN = 17;
const Y_FRAME_INSET = 29; // vertical space between y margin and first frame, where the axis markers are drawn
const GRADIENT_STR = [
"#3475BA",
"#318DBC",
"#47A298",
"#8AAE5D",
"#C1A731",
"#C07210",
"#B84210",
"#B53134",
"#9A3586",
"#4958B5",
"#3475BA",
];
const GRADIENT = GRADIENT_STR.map(parseColor);
export interface TimelineFrame {
frame: Frame;
depth: number;
isApplicationCode: boolean;
library: string | null;
className: string;
filePathShort: string | null;
}
export default class TimelineCanvasView extends CanvasView {
zoom: number = 1; // pixels per second
startT: number = 0; // seconds
yOffset: number = 0; // pixels
frames: TimelineFrame[] = [];
isZoomedIn: boolean = false;
// Add state for search
searchQuery: string = "";
matchingFrames: Set<TimelineFrame> = new Set();
tooltipContainer: HTMLElement;
tooltipComponent: TimelineCanvasViewTooltip | null = null;
constructor(container: HTMLElement) {
super(container);
this.onWheel = this.onWheel.bind(this);
this.onMouseMove = this.onMouseMove.bind(this);
this.onMouseLeave = this.onMouseLeave.bind(this);
this.onMouseDown = this.onMouseDown.bind(this);
this.windowMouseUp = this.windowMouseUp.bind(this);
this.onSearchInput = this.onSearchInput.bind(this); // Bind new search handler
this.canvas.addEventListener("wheel", this.onWheel);
this.canvas.addEventListener("mousemove", this.onMouseMove);
this.canvas.addEventListener("mouseleave", this.onMouseLeave);
this.canvas.addEventListener("mousedown", this.onMouseDown);
// Create and attach search bar
const searchBar = document.createElement("input");
searchBar.type = "text";
searchBar.placeholder = "Search for a function...";
searchBar.style.position = "absolute";
searchBar.style.top = "10px";
searchBar.style.left = "10px";
searchBar.style.zIndex = "1000";
searchBar.style.padding = "5px";
searchBar.style.border = "1px solid #ccc";
searchBar.style.borderRadius = "5px";
searchBar.style.backgroundColor = "#fff";
searchBar.addEventListener("input", this.onSearchInput);
this.container.appendChild(searchBar);
this.tooltipContainer = document.createElement("div");
this.tooltipContainer.style.position = "absolute";
this.tooltipContainer.style.pointerEvents = "none";
this.container.appendChild(this.tooltipContainer);
}
onSearchInput(event: Event) {
const input = event.target as HTMLInputElement;
this.searchQuery = input.value.toLowerCase();
// Clear previous matches
this.matchingFrames.clear();
// Find matching frames
for (const frame of this.frames) {
const name = this.frameName(frame).toLowerCase();
if (name.includes(this.searchQuery)) {
this.matchingFrames.add(frame);
}
}
this.setNeedsRedraw();
}
destroy(): void {
this.canvas.removeEventListener("wheel", this.onWheel);
this.canvas.removeEventListener("mousemove", this.onMouseMove);
this.canvas.removeEventListener("mouseleave", this.onMouseLeave);
this.canvas.removeEventListener("mousedown", this.onMouseDown);
this.tooltipContainer.remove();
super.destroy();
}
drawFrame(ctx: CanvasRenderingContext2D, timelineFrame: TimelineFrame) {
const { x, y, w, h } = this.frameDims(timelineFrame);
const endX = x + w;
if (endX < 0 || x > this.width) {
// offscreen
return;
}
// Highlight matching frames
if (this.matchingFrames.has(timelineFrame)) {
ctx.strokeStyle = "yellow";
ctx.lineWidth = 2;
ctx.strokeRect(x, y, w, h);
}
ctx.fillStyle = this.colorForFrame(timelineFrame);
ctx.globalAlpha = timelineFrame.isApplicationCode ? 1 : 0.5;
if (w < 2) {
// fast path
ctx.fillRect(x, y, w, h);
return;
}
let name = this.frameName(timelineFrame);
// the minimum width per character is 3.3px (that's an 'l')
// no point in drawing more characters than that, it'll be clipped
const maxChars = Math.floor(w / 3.3);
if (name.length > maxChars) {
name = name.substring(0, maxChars);
}
if (name.length == 0) {
// fast path
ctx.fillRect(x, y, w, h);
return;
}
ctx.save();
ctx.beginPath();
ctx.rect(x, y, w, h);
ctx.fill();
ctx.clip();
ctx.font = `13px "Source Sans Pro", sans-serif`;
ctx.fillStyle = "white";
let textX = x;
if (textX < 0) {
textX = 0;
}
ctx.fillText(name, textX + 2, y + 13);
ctx.restore();
}
}