-
Notifications
You must be signed in to change notification settings - Fork 0
/
infocard-ui.js
259 lines (227 loc) · 7.92 KB
/
infocard-ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-check
'use strict';
/**
* @fileoverview
* UI classes and methods for the info cards that display informations about
* symbols as the user hovers or focuses on them.
*/
{
// Infocard elements
const _containerInfocard = document.getElementById('infocard-container');
const _symbolInfocard = document.getElementById('infocard-symbol');
const _tableBody = _containerInfocard.querySelector('tbody');
/** @type {CanvasRenderingContext2D} Pie chart canvas from infocard */
const _ctx = _containerInfocard
.querySelector('.type-pie-info')
.getContext('2d');
/**
* @type {{[type:string]: HTMLTableRowElement}} Rows in the container infocard
* that represent a particular symbol type.
*/
const _INFO_ROWS = {
b: _containerInfocard.querySelector('.bss-info'),
d: _containerInfocard.querySelector('.data-info'),
r: _containerInfocard.querySelector('.rodata-info'),
t: _containerInfocard.querySelector('.text-info'),
v: _containerInfocard.querySelector('.vtable-info'),
'*': _containerInfocard.querySelector('.gen-info'),
x: _containerInfocard.querySelector('.dexnon-info'),
m: _containerInfocard.querySelector('.dex-info'),
p: _containerInfocard.querySelector('.pak-info'),
P: _containerInfocard.querySelector('.paknon-info'),
o: _containerInfocard.querySelector('.other-info'),
};
const _CANVAS_RADIUS = 40;
/** Update the DPI of the canvas for zoomed in and high density screens. */
function _updateCanvasDpi() {
_ctx.canvas.height = _CANVAS_RADIUS * 2 * devicePixelRatio;
_ctx.canvas.width = _CANVAS_RADIUS * 2 * devicePixelRatio;
_ctx.scale(devicePixelRatio, devicePixelRatio);
}
_updateCanvasDpi();
window.addEventListener('resize', _updateCanvasDpi);
/**
* Draw a slice of a pie chart.
* @param {CanvasRenderingContext2D} ctx
* @param {number} angleStart Starting angle, in radians.
* @param {number} percentage Percentage of circle to draw.
* @param {string} color Color of the pie slice.
* @returns {number} Ending angle, in radians.
*/
function _drawSlice(ctx, angleStart, percentage, color) {
const arcLength = percentage * 2 * Math.PI;
const angleEnd = angleStart + arcLength;
if (arcLength === 0) return angleEnd;
// Update DOM
ctx.fillStyle = color;
// Move cursor to center, where line will start
ctx.beginPath();
ctx.moveTo(40, 40);
// Move cursor to start of arc then draw arc
ctx.arc(40, 40, _CANVAS_RADIUS, angleStart, angleEnd);
// Move cursor back to center
ctx.closePath();
ctx.fill();
return angleEnd;
}
/**
* Update a row in the breakdown table with the given values.
* @param {HTMLTableRowElement} row
* @param {number} size Total size of the symbols of a given type in a
* container.
* @param {number} percentage How much the size represents in relation to the
* total size of the symbols in the container.
*/
function _updateBreakdownRow(row, size, percentage) {
if (size === 0) {
if (row.parentElement != null) {
_tableBody.removeChild(row);
}
return;
}
const sizeColumn = row.querySelector('.size');
const percentColumn = row.querySelector('.percent');
const sizeString = size.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
useGrouping: true,
});
const percentString = percentage.toLocaleString(undefined, {
style: 'percent',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
// Update DOM
sizeColumn.textContent = sizeString;
percentColumn.textContent = percentString;
_tableBody.appendChild(row);
}
/**
* @param {TreeNode} symbol
* @param {GetSize} getSizeLabel
* @returns {[DocumentFragment, DocumentFragment]}
*/
function _buildHeaderFragments(symbol, getSizeLabel) {
// Create size info fragment, such as "1,234 bytes (1.23 KiB)"
const {title, element} = getSizeLabel(
symbol.size,
state.get('byteunit', {default: 'MiB', valid: _BYTE_UNITS_SET})
);
const sizeInfoFragment = dom.createFragment([
document.createTextNode(`${title} (`),
element,
document.createTextNode(')'),
]);
// Create path fragment where the shortName is highlighted.
const path = symbol.idPath.slice(0, symbol.shortNameIndex);
const boldShortName = dom.textElement(
'span',
shortName(symbol),
'symbol-name-info'
);
const pathInfoFragment = dom.createFragment([
document.createTextNode(path),
boldShortName,
]);
return [sizeInfoFragment, pathInfoFragment];
}
function _insertHeaderFragments(
infocard,
symbol,
[sizeInfoFragment, pathInfoFragment]
) {
const sizeInfo = infocard.querySelector('.size-info');
// Update DOM
infocard.removeAttribute('hidden');
if (symbol.size < 0) {
sizeInfo.classList.add('negative');
} else {
sizeInfo.classList.remove('negative');
}
dom.replace(sizeInfo, sizeInfoFragment);
dom.replace(infocard.querySelector('.path-info'), pathInfoFragment);
}
/**
* Update DOM for the container infocard
* @param {TreeNode} container
* @param {Array<[string, number]>} sizeEntries
* @param {{[type: string]: HTMLTableRowElement}} extraRows
* @param {[DocumentFragment, DocumentFragment]} headerFragments
*/
function _updateContainerInfocard(
container,
sizeEntries,
extraRows,
headerFragments
) {
let angleStart = 0;
for (const [type, size] of sizeEntries) {
delete extraRows[type];
const {color} = getIconStyle(type);
const percentage = size / container.size;
// Update DOM
angleStart = _drawSlice(_ctx, angleStart, percentage, color);
_updateBreakdownRow(_INFO_ROWS[type], size, percentage);
}
// Hide unused types
for (const row of Object.values(extraRows)) {
// Update DOM
_updateBreakdownRow(row, 0, 0);
}
// Update DOM
_insertHeaderFragments(_containerInfocard, container, headerFragments);
_symbolInfocard.setAttribute('hidden', '');
}
/**
* Update DOM for the symbol infocard
* @param {TreeNode} symbol
* @param {SVGSVGElement} icon
* @param {HTMLDivElement} iconInfo
* @param {[DocumentFragment, DocumentFragment]} headerFragments
*/
function _updateSymbolInfocard(symbol, icon, iconInfo, headerFragments) {
const typeInfo = icon.querySelector('title').textContent;
// Update DOM
iconInfo.style.backgroundColor = icon.getAttribute('fill');
icon.setAttribute('fill', '#fff');
dom.replace(iconInfo, icon);
_symbolInfocard.querySelector('.type-info').textContent = typeInfo;
_insertHeaderFragments(_symbolInfocard, symbol, headerFragments);
_containerInfocard.setAttribute('hidden', '');
}
let _pendingFrame;
/**
* Displays an infocard for the given symbol on the next frame.
* @param {TreeNode} symbol
* @param {GetSize} getSizeLabel
*/
function displayInfocard(symbol, getSizeLabel) {
cancelAnimationFrame(_pendingFrame);
const headerFragments = _buildHeaderFragments(symbol, getSizeLabel);
if (_CONTAINER_TYPE_SET.has(symbol.type[0])) {
const extraRows = {..._INFO_ROWS};
const sizeEntries = Object.entries(symbol.childSizes).sort(
(a, b) => b[1] - a[1]
);
_pendingFrame = requestAnimationFrame(() =>
_updateContainerInfocard(
symbol,
sizeEntries,
extraRows,
headerFragments
)
);
} else {
const icon = getIconTemplate(symbol.type[0]);
/** @type {HTMLDivElement} */
const iconInfo = _symbolInfocard.querySelector('.icon-info');
_pendingFrame = requestAnimationFrame(() =>
_updateSymbolInfocard(symbol, icon, iconInfo, headerFragments)
);
}
}
self.displayInfocard = displayInfocard;
}