Debian Codegraph @

html_renderer/src/components/Frame.svelte

raw
1 <script lang="ts" context="module">
2 // focusable code taken from https://stackoverflow.com/a/35173443/382749
3 function getFocusableElements() {
4 const FOCUSSABLE_ELEMENTS =
5 'a:not([disabled]), button:not([disabled]), input[type=text]:not([disabled]), [tabindex]:not([disabled]):not([tabindex="-1"])';
6 const callStackElement = document.querySelector('.call-stack-view')
7 if (!callStackElement) throw new Error('callStackElement not found')
8
9 var focussable = Array.prototype.filter.call(
10 callStackElement.querySelectorAll(FOCUSSABLE_ELEMENTS),
11 function (element) {
12 //check for visibility while always include the current activeElement
13 return (
14 element.offsetWidth > 0 ||
15 element.offsetHeight > 0 ||
16 element === document.activeElement
17 );
18 },
19 );
20 return focussable;
21 }
22 function focusNextElement() {
23 const focussable = getFocusableElements();
24
25 var index = focussable.indexOf(document.activeElement);
26 if (index > -1) {
27 var nextElement = focussable[index + 1];
28 if (nextElement) {
29 nextElement.focus();
30 }
31 }
32 }
33 function focusPreviousElement() {
34 const focussable = getFocusableElements();
35
36 var index = focussable.indexOf(document.activeElement);
37 if (index > -1) {
38 var prevElement = focussable[index - 1];
39 if (prevElement) {
40 prevElement.focus();
41 }
42 }
43 }
44 </script>
45
46 <script lang="ts">
47 import { visibleGroups, collapsedFrames } from "../lib/appState";
48 import { colorForFrameProportionOfTotal } from "../lib/color";
49 import type Frame from "../lib/model/Frame";
50 import { viewOptionsCallStack } from "../lib/settings";
51 export let frame: Frame;
52 export let rootFrame: Frame;
53 export let indent: number = 0;
54
55 let isVisible: boolean;
56 $: {
57 if (!frame.group) {
58 isVisible = true;
59 } else if ($visibleGroups[frame.group.id ?? ""]) {
60 isVisible = true;
61 } else if (frame.group?.rootFrame === frame) {
62 isVisible = true;
63 } else if (frame.children.filter((f) => !f.group).length > 1) {
64 isVisible = true;
65 } else {
66 isVisible = false;
67 }
68 }
69
70 const frameProportionOfTotal = frame.time / rootFrame.time;
71
72 let name: string;
73
74 $: if (frame.className) {
75 name = `${frame.className}.${frame.function}`;
76 } else {
77 name = frame.function;
78 }
79
80 let codePosition: string
81 if (frame.isSynthetic) {
82 codePosition = "";
83 } else if (frame.filePathShort == null) {
84 codePosition = "";
85 } else if (frame.lineNo == null || frame.lineNo === 0) {
86 codePosition = frame.filePathShort;
87 } else {
88 codePosition = `${frame.filePathShort}:${frame.lineNo}`;
89 }
90
91 let formattedTime: string;
92 $: if ($viewOptionsCallStack.timeFormat === "absolute") {
93 formattedTime = frame.time.toLocaleString(undefined, {
94 minimumFractionDigits: frame.context.precision,
95 maximumFractionDigits: frame.context.precision,
96 });
97 } else if ($viewOptionsCallStack.timeFormat === "proportion") {
98 formattedTime = `${(frameProportionOfTotal * 100).toLocaleString(
99 undefined,
100 {
101 minimumFractionDigits: 1,
102 maximumFractionDigits: 1,
103 },
104 )}%`;
105 } else {
106 throw new Error("unknown timeFormat");
107 }
108
109 let groupLibrarySummary: string | null = null;
110 if (frame.group) {
111 const libraries = frame.group.libraries;
112 if (libraries.length < 4) {
113 groupLibrarySummary = libraries.join(", ");
114 } else {
115 groupLibrarySummary = `${libraries[0]}, ${libraries[1]}, ${libraries[2]}...`;
116 }
117 }
118
119 let timeColor: string;
120 timeColor = colorForFrameProportionOfTotal(frameProportionOfTotal);
121
122 function descriptionClicked(event: MouseEvent | KeyboardEvent) {
123 setCollapsed(frame, !collapsed, event.altKey);
124 }
125
126 $: isGroupVisible = $visibleGroups[frame.group?.id ?? ""] === true;
127 $: collapsed = $collapsedFrames[frame.uuid] === true;
128
129 function setCollapsed(
130 frame: Frame,
131 value: boolean,
132 recursive: boolean = true,
133 ) {
134 collapsedFrames.update((collapsedFrames) => ({
135 ...collapsedFrames,
136 [frame.uuid]: value,
137 }));
138
139 if (recursive) {
140 for (const child of frame.children) {
141 setCollapsed(child, value, true);
142 if (frame.group && frame.group.rootFrame == frame) {
143 setGroupVisible(frame.group.id, !value);
144 }
145 }
146 }
147 }
148
149 function setGroupVisible(groupId: string, value: boolean) {
150 visibleGroups.update((groups) => ({
151 ...groups,
152 [groupId]: value,
153 }));
154 }
155
156 function groupHeaderClicked() {
157 if (!frame.group) {
158 return;
159 }
160 setGroupVisible(frame.group.id, !isGroupVisible);
161 }
162 function onKeydown(event: KeyboardEvent) {
163 let wasHandled = true;
164 if (event.key === "Enter" || event.key === " ") {
165 descriptionClicked(event);
166 } else if (event.key === "ArrowLeft" && !collapsed) {
167 setCollapsed(frame, true, event.altKey);
168 } else if (event.key === "ArrowRight" && collapsed) {
169 setCollapsed(frame, false, event.altKey);
170 } else if (event.key === "ArrowUp") {
171 focusPreviousElement();
172 } else if (event.key === "ArrowDown") {
173 focusNextElement();
174 } else {
175 wasHandled = false;
176 }
177
178 if (wasHandled) {
179 event.preventDefault();
180 event.stopPropagation();
181 }
182 }
183 function onGroupHeaderKeydown(event: KeyboardEvent) {
184 let wasHandled = true;
185 if (event.key === "Enter" || event.key === " ") {
186 groupHeaderClicked();
187 } else if (event.key === "ArrowLeft" && frame.group) {
188 setGroupVisible(frame.group.id, false);
189 } else if (event.key === "ArrowRight" && frame.group) {
190 setGroupVisible(frame.group.id, true);
191 } else if (event.key === "ArrowUp") {
192 focusPreviousElement();
193 } else if (event.key === "ArrowDown") {
194 focusNextElement();
195 } else {
196 wasHandled = false;
197 }
198
199 if (wasHandled) {
200 event.preventDefault();
201 event.stopPropagation();
202 }
203 }
204 </script>
205
206 <div class="frame">
207 {#if isVisible}
208 <!-- svelte-ignore a11y-click-events-have-key-events -->
209 <div
210 class="frame-description"
211 class:application-code={frame.isApplicationCode}
212 class:children-visible={!collapsed}
213 style:padding-left={`${indent * 35}px`}
214 role="button"
215 tabindex="0"
216 on:keydown={onKeydown}
217 on:click|preventDefault|stopPropagation={descriptionClicked}
218 >
219 <div
220 class="frame-triangle"
221 class:rotate={!collapsed}
222 style:visibility={frame.children.length > 0 ? "visible" : "hidden"}
223 >
224 <svg width="6" height="10"
225 ><path
226 d="M.937-.016L5.793 4.84.937 9.696z"
227 fill={timeColor}
228 fill-rule="evenodd"
229 fill-opacity=".582"
230 /></svg
231 >
232 </div>
233 <div
234 class="time"
235 style:color={timeColor}
236 style:font-weight={frameProportionOfTotal < 0.15 ? 500 : 600}
237 >
238 {formattedTime}
239 </div>
240 <div class="name">{name}</div>
241 <div class="code-position">
242 {codePosition}
243 </div>
244 </div>
245
246 <div
247 class="visual-guide"
248 style:left={`${indent * 35 + 21}px`}
249 style:background-color={timeColor}
250 ></div>
251 {/if}
252
253 {#if frame.group && frame.group.rootFrame == frame && !collapsed}
254 <!-- svelte-ignore a11y-click-events-have-key-events -->
255 <div
256 class="group-header"
257 style:padding-left={`${indent * 35}px`}
258 role="button"
259 tabindex="0"
260 on:keydown={onGroupHeaderKeydown}
261 on:click|preventDefault|stopPropagation={groupHeaderClicked}
262 >
263 <div class="group-header-button">
264 <div class="group-triangle" class:rotate={isGroupVisible}>
265 <svg width="6" height="10"
266 ><path
267 d="M.937-.016L5.793 4.84.937 9.696z"
268 fill="#FFF"
269 fill-rule="evenodd"
270 fill-opacity=".582"
271 /></svg
272 >
273 </div>
274 {frame.group.frames.length-1} frames hidden ({groupLibrarySummary})
275 </div>
276 </div>
277 {/if}
278
279 {#if !collapsed && frame.children.length > 0}
280 <div class="children">
281 {#each frame.children as child (child.uuid)}
282 <svelte:self
283 frame={child}
284 {rootFrame}
285 indent={indent + (isVisible ? 1 : 0)}
286 />
287 {/each}
288 </div>
289 {/if}
290 </div>
291
292 <style lang="scss">
293 .frame {
294 font-family: "Source Code Pro", "Roboto Mono", Consolas, Monaco, monospace;
295 font-size-adjust: 0.486094;
296 font-size: 14px;
297 z-index: 0;
298 position: relative;
299 user-select: none;
300 }
301 .group-header {
302 user-select: none;
303 }
304 .group-header-button {
305 margin-left: 35px;
306 display: inline-block;
307 color: rgba(255, 255, 255, 0.58);
308 user-select: none;
309 cursor: default;
310 position: relative;
311 }
312 .group-header-button::before {
313 position: absolute;
314 left: -3px;
315 right: -3px;
316 top: 0px;
317 bottom: 0px;
318 content: "";
319
320 z-index: -1;
321 background-color: #3b4043;
322 }
323 .group-header-button:hover::before {
324 background-color: #4a4f54;
325 }
326 .group-triangle,
327 .frame-triangle {
328 width: 6px;
329 height: 10px;
330 padding-left: 6px;
331 padding-right: 5px;
332 display: inline-block;
333 }
334 .group-triangle.rotate,
335 .frame-triangle.rotate {
336 transform: translate(6px, 4px) rotate(90deg);
337 }
338
339 .frame-description {
340 display: flex;
341 white-space: nowrap;
342 }
343 .frame-description:hover {
344 background-color: #35475980;
345 }
346 .frame-description:focus-visible,
347 .group-header:focus-visible {
348 outline: none;
349 background-color: #37516c;
350 }
351 .frame-triangle {
352 opacity: 1;
353 }
354 .frame-description.children-visible .frame-triangle {
355 opacity: 0;
356 }
357 .frame-description.children-visible:hover .frame-triangle,
358 .frame-description.children-visible:focus-visible .frame-triangle {
359 opacity: 1;
360 }
361 .name,
362 .time,
363 .code-position {
364 user-select: text;
365 cursor: default;
366 }
367 .application-code .name {
368 color: rgba(93, 179, 255, 1);
369 }
370 .time {
371 margin-right: 0.55em;
372 color: rgba(184, 233, 134, 0.52);
373 }
374 .code-position {
375 color: rgba(255, 255, 255, 0.5);
376 text-align: right;
377 margin-left: 2em;
378 }
379
380 .visual-guide {
381 top: 21px;
382 bottom: 0;
383 left: 0;
384 width: 2px;
385 background-color: white;
386 position: absolute;
387 opacity: 0.08;
388 pointer-events: none;
389 }
390 :global(.frame-description:hover) ~ .visual-guide {
391 opacity: 0.4;
392 }
393 :global(.frame-description:hover) ~ .children :global(.visual-guide) {
394 opacity: 0.15;
395 }
396 </style>