-
Notifications
You must be signed in to change notification settings - Fork 0
/
infocard-ui.js
322 lines (286 loc) · 9.8 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// 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.
*/
{
const _CANVAS_RADIUS = 40;
class Infocard {
/**
* @param {string} id
*/
constructor(id) {
this._infocard = document.getElementById(id);
/** @type {HTMLHeadingElement} */
this._sizeInfo = this._infocard.querySelector('.size-info');
/** @type {HTMLParagraphElement} */
this._pathInfo = this._infocard.querySelector('.path-info');
/** @type {HTMLDivElement} */
this._iconInfo = this._infocard.querySelector('.icon-info');
/** @type {HTMLParagraphElement} */
this._typeInfo = this._infocard.querySelector('.type-info');
/**
* Last symbol type displayed.
* Tracked to avoid re-cloning the same icon.
* @type {string}
*/
this._lastType = '';
}
/**
* Updates the size header, which normally displayed the byte size of the
* node followed by an abbreviated version.
*
* Example: "1,234 bytes (1.23 KiB)"
* @param {TreeNode} node
* @param {GetSize} getSizeLabel
*/
_updateSize(node, getSizeLabel) {
const {description, element} = getSizeLabel(
node,
state.get('byteunit', {default: 'MiB', valid: _BYTE_UNITS_SET})
);
const sizeFragment = dom.createFragment([
document.createTextNode(`${description} (`),
element,
document.createTextNode(')'),
]);
// Update DOM
if (node.size < 0) {
this._sizeInfo.classList.add('negative');
} else {
this._sizeInfo.classList.remove('negative');
}
dom.replace(this._sizeInfo, sizeFragment);
}
/**
* Updates the path text, which shows the idPath of the node but highlights
* the symbol name portion using bold text.
* @param {TreeNode} node
*/
_updatePath(node) {
const path = node.idPath.slice(0, node.shortNameIndex);
const boldShortName = dom.textElement(
'span',
shortName(node),
'symbol-name-info'
);
const pathFragment = dom.createFragment([
document.createTextNode(path),
boldShortName,
]);
// Update DOM
dom.replace(this._pathInfo, pathFragment);
}
/**
* Updates the icon and type text. The type label is pulled from the
* title of the icon supplied.
* @param {SVGSVGElement} icon Icon to display
*/
_setTypeContent(icon) {
const typeDescription = icon.querySelector('title').textContent;
icon.setAttribute('fill', '#fff');
this._typeInfo.textContent = typeDescription;
this._iconInfo.removeChild(this._iconInfo.lastElementChild);
this._iconInfo.appendChild(icon);
}
/**
* Toggle wheter or not the card is visible.
* @param {boolean} isHidden
*/
setHidden(isHidden) {
if (isHidden) {
this._infocard.setAttribute('hidden', '');
} else {
this._infocard.removeAttribute('hidden');
}
}
/**
* Updates the DOM for the info card.
* @param {TreeNode} node
* @param {GetSize} getSizeLabel
*/
_updateInfocard(node, getSizeLabel) {
const type = node.type[0];
// Update DOM
this._updateSize(node, getSizeLabel);
this._updatePath(node);
if (type !== this._lastType) {
// No need to create a new icon if it is identical.
const icon = getIconTemplate(type);
this._setTypeContent(icon);
this._lastType = type;
}
}
/**
* Updates the card on the next animation frame.
* @param {TreeNode} node
* @param {GetSize} getSizeLabel
*/
updateInfocard(node, getSizeLabel) {
cancelAnimationFrame(Infocard._pendingFrame);
Infocard._pendingFrame = requestAnimationFrame(() =>
this._updateInfocard(node, getSizeLabel)
);
}
}
class SymbolInfocard extends Infocard {
/**
* @param {SVGSVGElement} icon Icon to display
*/
_setTypeContent(icon) {
const color = icon.getAttribute('fill');
super._setTypeContent(icon);
this._iconInfo.style.backgroundColor = color;
}
}
class ContainerInfocard extends Infocard {
constructor(id) {
super(id);
this._tableBody = this._infocard.querySelector('tbody');
this._ctx = this._infocard.querySelector('canvas').getContext('2d');
/**
* @type {{[type:string]: HTMLTableRowElement}} Rows in the container
* infocard that represent a particular symbol type.
*/
this._infoRows = {
b: this._tableBody.querySelector('.bss-info'),
d: this._tableBody.querySelector('.data-info'),
r: this._tableBody.querySelector('.rodata-info'),
t: this._tableBody.querySelector('.text-info'),
v: this._tableBody.querySelector('.vtable-info'),
'*': this._tableBody.querySelector('.gen-info'),
x: this._tableBody.querySelector('.dexnon-info'),
m: this._tableBody.querySelector('.dex-info'),
p: this._tableBody.querySelector('.pak-info'),
P: this._tableBody.querySelector('.paknon-info'),
o: this._tableBody.querySelector('.other-info'),
};
/**
* Update the DPI of the canvas for zoomed in and high density screens.
*/
const _updateCanvasDpi = () => {
this._ctx.canvas.height = _CANVAS_RADIUS * 2 * devicePixelRatio;
this._ctx.canvas.width = _CANVAS_RADIUS * 2 * devicePixelRatio;
this._ctx.scale(devicePixelRatio, devicePixelRatio);
};
_updateCanvasDpi();
window.addEventListener('resize', _updateCanvasDpi);
}
/**
* @param {SVGSVGElement} icon Icon to display
*/
_setTypeContent(icon) {
super._setTypeContent(icon);
icon.classList.add('canvas-overlay');
}
/**
* Draw a slice of a pie chart.
* @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.
*/
_drawSlice(angleStart, percentage, color) {
const arcLength = percentage * 2 * Math.PI;
const angleEnd = angleStart + arcLength;
if (arcLength === 0) return angleEnd;
// Update DOM
this._ctx.fillStyle = color;
// Move cursor to center, where line will start
this._ctx.beginPath();
this._ctx.moveTo(40, 40);
// Move cursor to start of arc then draw arc
this._ctx.arc(40, 40, _CANVAS_RADIUS, angleStart, angleEnd);
// Move cursor back to center
this._ctx.closePath();
this._ctx.fill();
return angleEnd;
}
/**
* Update a row in the breakdown table with the given values.
* @param {HTMLTableRowElement} row
* @param {{size:number,count:number} | null} stats 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.
*/
_updateBreakdownRow(row, stats, percentage) {
if (stats == null || stats.size === 0) {
if (row.parentElement != null) {
this._tableBody.removeChild(row);
}
return;
}
const countColumn = row.querySelector('.count');
const sizeColumn = row.querySelector('.size');
const percentColumn = row.querySelector('.percent');
const countString = stats.count.toLocaleString(_LOCALE, {
useGrouping: true,
});
const sizeString = stats.size.toLocaleString(_LOCALE, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
useGrouping: true,
});
const percentString = percentage.toLocaleString(_LOCALE, {
style: 'percent',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
// Update DOM
countColumn.textContent = countString;
sizeColumn.textContent = sizeString;
percentColumn.textContent = percentString;
this._tableBody.appendChild(row);
}
/**
* Update DOM for the container infocard
* @param {TreeNode} containerNode
* @param {GetSize} getSizeLabel
*/
_updateInfocard(containerNode, getSizeLabel) {
const extraRows = {...this._infoRows};
const statsEntries = Object.entries(containerNode.childStats).sort(
(a, b) => b[1].size - a[1].size
);
// Update DOM
super._updateInfocard(containerNode, getSizeLabel);
let angleStart = 0;
for (const [type, stats] of statsEntries) {
delete extraRows[type];
const {color} = getIconStyle(type);
const percentage = stats.size / containerNode.size;
angleStart = this._drawSlice(angleStart, percentage, color);
this._updateBreakdownRow(this._infoRows[type], stats, percentage);
}
// Hide unused types
for (const row of Object.values(extraRows)) {
this._updateBreakdownRow(row, null, 0);
}
}
}
const _containerInfo = new ContainerInfocard('infocard-container');
const _symbolInfo = new SymbolInfocard('infocard-symbol');
/**
* Displays an infocard for the given symbol on the next frame.
* @param {TreeNode} node
* @param {GetSize} getSizeLabel
*/
function displayInfocard(node, getSizeLabel) {
if (_CONTAINER_TYPE_SET.has(node.type[0])) {
_containerInfo.updateInfocard(node, getSizeLabel);
_containerInfo.setHidden(false);
_symbolInfo.setHidden(true);
} else {
_symbolInfo.updateInfocard(node, getSizeLabel);
_symbolInfo.setHidden(false);
_containerInfo.setHidden(true);
}
}
self.displayInfocard = displayInfocard;
}