@playcanvas/inspector 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,268 @@
1
+ import { ListView } from './list-view.js';
2
+ import { formatBytes } from './model.js';
3
+ import { setTip } from './tooltip.js';
4
+
5
+ /** @import { SnapshotItem, TreemapData } from './treemap-view.js' */
6
+
7
+ /**
8
+ * What a tab measures when a snapshot of it is captured.
9
+ *
10
+ * @typedef {object} SnapshotContent
11
+ * @property {string} measure - What the sizes are, in a few words, such as 'GPU memory'.
12
+ * @property {SnapshotItem[]} items - What was measured.
13
+ * @property {string} [note] - What the items leave out, such as assets of unknown size.
14
+ * @ignore
15
+ */
16
+
17
+ /**
18
+ * A snapshot of a tab: what it measured at one moment, kept to be drawn as a treemap later.
19
+ *
20
+ * @typedef {object} Snapshot
21
+ * @property {number} number - Its number among the snapshots of its tab, from 1. Never reused.
22
+ * @property {number} time - When it was captured, in milliseconds since the epoch.
23
+ * @property {string} measure - What the sizes are.
24
+ * @property {SnapshotItem[]} items - What was measured.
25
+ * @property {string} note - What the items leave out, or an empty string.
26
+ * @property {number} total - The sum of the sizes of the items.
27
+ * @ignore
28
+ */
29
+
30
+ /**
31
+ * The snapshots of one tab. The inspector keeps them, so they survive the panel being built
32
+ * again when it docks back from its own window.
33
+ *
34
+ * @typedef {object} SnapshotList
35
+ * @property {Snapshot[]} snapshots - The snapshots, oldest first.
36
+ * @property {Set<number>} ticked - The numbers of the snapshots ticked.
37
+ * @property {number} next - The number the next snapshot takes.
38
+ * @ignore
39
+ */
40
+
41
+ /**
42
+ * @param {string} tag - The tag name.
43
+ * @param {string} className - The class names.
44
+ * @param {string} [text] - The text content.
45
+ * @returns {HTMLElement} The element.
46
+ */
47
+ function el(tag, className, text) {
48
+ const element = document.createElement(tag);
49
+ element.className = className;
50
+ if (text !== undefined) element.textContent = text;
51
+ return element;
52
+ }
53
+
54
+ /**
55
+ * @returns {SnapshotList} A list with no snapshots yet.
56
+ * @ignore
57
+ */
58
+ function createSnapshotList() {
59
+ return { snapshots: [], ticked: new Set(), next: 1 };
60
+ }
61
+
62
+ /**
63
+ * The snapshots section of a tab: a bar to capture, compare and delete snapshots, over the list of
64
+ * them. Each opens as a treemap in a tab of its own; ticking two and comparing opens one treemap
65
+ * marking what changed between them. A snapshot is taken only when asked, rather than on every
66
+ * refresh, so it holds still to be read and compared.
67
+ *
68
+ * @ignore
69
+ */
70
+ class SnapshotPanel {
71
+ /**
72
+ * The section, placed under the tab's own list.
73
+ *
74
+ * @type {HTMLElement}
75
+ */
76
+ el;
77
+
78
+ /**
79
+ * The snapshots, kept by the inspector.
80
+ *
81
+ * @type {SnapshotList}
82
+ * @private
83
+ */
84
+ _state;
85
+
86
+ /**
87
+ * The name of the tab, such as 'Memory'.
88
+ *
89
+ * @type {string}
90
+ * @private
91
+ */
92
+ _title;
93
+
94
+ /**
95
+ * Measures the tab now.
96
+ *
97
+ * @type {() => SnapshotContent}
98
+ * @private
99
+ */
100
+ _measure;
101
+
102
+ /**
103
+ * Shows a treemap, in a tab of its own.
104
+ *
105
+ * @type {(data: TreemapData) => void}
106
+ * @private
107
+ */
108
+ _show;
109
+
110
+ /**
111
+ * @type {ListView}
112
+ * @private
113
+ */
114
+ _list;
115
+
116
+ /**
117
+ * @type {Record<'capture'|'compare'|'delete'|'deleteAll', HTMLButtonElement>}
118
+ * @private
119
+ */
120
+ _buttons;
121
+
122
+ /**
123
+ * Says what the section is for, while it has no snapshots.
124
+ *
125
+ * @type {HTMLElement}
126
+ * @private
127
+ */
128
+ _hint;
129
+
130
+ /**
131
+ * @param {SnapshotList} state - The tab's snapshots.
132
+ * @param {string} title - The name of the tab.
133
+ * @param {() => SnapshotContent} measure - Measures the tab now.
134
+ * @param {(data: TreemapData) => void} show - Shows a treemap, in a tab of its own.
135
+ */
136
+ constructor(state, title, measure, show) {
137
+ this._state = state;
138
+ this._title = title;
139
+ this._measure = measure;
140
+ this._show = show;
141
+
142
+ this.el = el('div', 'pci-snapshots');
143
+ const bar = el('div', 'pci-subbar pci-snapbar');
144
+ const heading = el('span', 'pci-snaptitle', 'Snapshots');
145
+ setTip(heading, `Snapshots of what the ${title} tab measures, each opening as a treemap in a tab of its own.\n` +
146
+ 'Tick two to compare them');
147
+ const button = (text, tip, run) => {
148
+ const b = /** @type {HTMLButtonElement} */ (el('button', 'pci-btn', text));
149
+ setTip(b, tip);
150
+ b.addEventListener('click', run);
151
+ bar.appendChild(b);
152
+ return b;
153
+ };
154
+ bar.appendChild(heading);
155
+ this._buttons = {
156
+ capture: button('Capture', `Capture what the ${title} tab measures now, to explore as a treemap or compare with another snapshot`, () => this.capture()),
157
+ compare: button('Compare', '', () => this.compare()),
158
+ delete: button('Delete', 'Delete the ticked snapshots. Their treemap tabs stay open until closed', () => this.delete([...this._state.ticked])),
159
+ deleteAll: button('Delete all', `Delete every snapshot of the ${title} tab`, () => this.delete(this._state.snapshots.map(s => s.number)))
160
+ };
161
+
162
+ // the list keeps one height, so the buttons above stay put as snapshots are added
163
+ const body = el('div', 'pci-snapbody');
164
+ this._hint = el('div', 'pci-snaphint', `Capture what the ${title} tab measures, to explore it as a treemap. Tick two snapshots to compare them.`);
165
+ const listEl = el('div', 'pci-list pci-snaplist');
166
+ this._list = new ListView(listEl, () => {}, target => this.open(/** @type {Snapshot} */ (target)));
167
+ this._list.onToggle = (snapshot, checked) => {
168
+ if (checked) this._state.ticked.add(snapshot.number);
169
+ else this._state.ticked.delete(snapshot.number);
170
+ this.refresh();
171
+ };
172
+ body.append(this._hint, listEl);
173
+ this.el.append(bar, body);
174
+ this.refresh();
175
+ }
176
+
177
+ /**
178
+ * Captures a snapshot of the tab and adds it to the list.
179
+ *
180
+ * @returns {Snapshot} The snapshot.
181
+ */
182
+ capture() {
183
+ const content = this._measure();
184
+ /** @type {Snapshot} */
185
+ const snapshot = {
186
+ number: this._state.next++,
187
+ time: Date.now(),
188
+ measure: content.measure,
189
+ items: content.items,
190
+ note: content.note ?? '',
191
+ total: content.items.reduce((sum, item) => sum + item.size, 0)
192
+ };
193
+ this._state.snapshots.push(snapshot);
194
+ this.refresh();
195
+ return snapshot;
196
+ }
197
+
198
+ /**
199
+ * Opens the two ticked snapshots as one treemap, the earlier compared against the later.
200
+ */
201
+ compare() {
202
+ const ticked = this._state.snapshots.filter(s => this._state.ticked.has(s.number));
203
+ if (ticked.length === 2) this.open(ticked[1], ticked[0]);
204
+ }
205
+
206
+ /**
207
+ * Opens a snapshot as a treemap in a tab of its own, or two compared.
208
+ *
209
+ * @param {Snapshot} snapshot - The snapshot, or the later of the two compared.
210
+ * @param {Snapshot} [before] - The earlier snapshot to compare it against.
211
+ */
212
+ open(snapshot, before) {
213
+ this._show({ tab: this._title, measure: snapshot.measure, to: snapshot, from: before ?? null });
214
+ }
215
+
216
+ /**
217
+ * Deletes snapshots.
218
+ *
219
+ * @param {number[]} numbers - The numbers of the snapshots.
220
+ */
221
+ delete(numbers) {
222
+ const gone = new Set(numbers);
223
+ const state = this._state;
224
+ state.snapshots = state.snapshots.filter(s => !gone.has(s.number));
225
+ for (const number of gone) state.ticked.delete(number);
226
+ this.refresh();
227
+ }
228
+
229
+ /**
230
+ * Brings the list and the buttons up to date with the snapshots.
231
+ */
232
+ refresh() {
233
+ const { snapshots, ticked } = this._state;
234
+ this._list.setRows(snapshots.map((snapshot, i) => {
235
+ const before = snapshots[i - 1];
236
+ const change = before ? snapshot.total - before.total : 0;
237
+ const time = new Date(snapshot.time).toLocaleTimeString();
238
+ const count = `${snapshot.items.length} item${snapshot.items.length === 1 ? '' : 's'}`;
239
+ return {
240
+ key: `snapshot${snapshot.number}`,
241
+ item: snapshot,
242
+ name: `#${snapshot.number}`,
243
+ inert: true,
244
+ title: `Snapshot #${snapshot.number}, captured at ${time}\n${count}, ${formatBytes(snapshot.total)}${snapshot.note ? `\n${snapshot.note}` : ''}`,
245
+ cells: [
246
+ { text: '', toggle: true, checked: ticked.has(snapshot.number) },
247
+ { text: `#${snapshot.number} · ${time}`, cls: 'pci-cell-name', target: snapshot, title: 'Open this snapshot as a treemap, in a tab of its own' },
248
+ { text: count, cls: 'pci-cell-info' },
249
+ {
250
+ text: before ? `${change > 0 ? '+' : change < 0 ? '−' : '±'}${formatBytes(Math.abs(change))}` : '',
251
+ cls: 'pci-cell-info pci-cell-right',
252
+ title: before ? `The change since #${before.number}` : ''
253
+ },
254
+ { text: formatBytes(snapshot.total), cls: 'pci-cell-size', field: 'size' }
255
+ ]
256
+ };
257
+ }));
258
+ this._list.container.style.display = snapshots.length ? '' : 'none';
259
+ this._hint.style.display = snapshots.length ? 'none' : '';
260
+ this._buttons.compare.disabled = ticked.size !== 2;
261
+ this._buttons.delete.disabled = ticked.size === 0;
262
+ this._buttons.deleteAll.disabled = snapshots.length === 0;
263
+ setTip(this._buttons.compare, ticked.size === 2 ? 'Open the two ticked snapshots as one treemap, marking what was added and removed between them' :
264
+ 'Tick two snapshots to compare them in one treemap, marking what was added and removed between them');
265
+ }
266
+ }
267
+
268
+ export { SnapshotPanel, createSnapshotList };
package/src/styles.js CHANGED
@@ -906,6 +906,267 @@ ${ICON_RULES}
906
906
  color: #9ad7ff;
907
907
  }
908
908
 
909
+ /* the snapshots of a tab, between its list and the properties */
910
+ .pci-snapshots {
911
+ flex: 0 0 auto;
912
+ display: flex;
913
+ flex-direction: column;
914
+ border-top: 1px solid var(--pci-border-strong);
915
+ background: var(--pci-bar);
916
+ }
917
+
918
+ .pci-snapshots .pci-subbar {
919
+ gap: 4px 6px;
920
+ border-bottom: none;
921
+ }
922
+
923
+ .pci-snaptitle {
924
+ margin-right: auto;
925
+ color: var(--pci-text-label);
926
+ font-weight: 600;
927
+ }
928
+
929
+ .pci-snapshots .pci-btn {
930
+ padding: 1px 8px;
931
+ }
932
+
933
+ /* four rows, then it scrolls */
934
+ .pci-snapbody {
935
+ flex: 0 0 84px;
936
+ display: flex;
937
+ flex-direction: column;
938
+ min-height: 0;
939
+ }
940
+
941
+ .pci-list.pci-snaplist {
942
+ padding: 0 0 4px;
943
+ }
944
+
945
+ .pci-snaphint {
946
+ padding: 2px 8px;
947
+ color: var(--pci-text-muted);
948
+ font-size: 11px;
949
+ }
950
+
951
+ /* a treemap tab, closed from its cross */
952
+ .pci-tab.pci-tab-closable {
953
+ display: inline-flex;
954
+ align-items: center;
955
+ gap: 3px;
956
+ padding-right: 4px;
957
+ }
958
+
959
+ .pci-tab-close {
960
+ width: 14px;
961
+ height: 14px;
962
+ border-radius: 4px;
963
+ font-size: 12px;
964
+ line-height: 13px;
965
+ text-align: center;
966
+ color: var(--pci-text-muted);
967
+ }
968
+
969
+ .pci-tab-close:hover {
970
+ background: var(--pci-pressed);
971
+ color: var(--pci-text);
972
+ }
973
+
974
+ /* a treemap has the whole height, the properties put away while it shows */
975
+ .pci-body.pci-full .pci-hierarchy {
976
+ flex: 1 1 auto !important;
977
+ }
978
+
979
+ .pci-body.pci-full .pci-splitter,
980
+ .pci-body.pci-full .pci-properties {
981
+ display: none;
982
+ }
983
+
984
+ .pci-treemap .pci-subbar {
985
+ flex-wrap: nowrap;
986
+ gap: 4px 10px;
987
+ }
988
+
989
+ .pci-treemap .pci-group .pci-btn {
990
+ padding: 1px 7px;
991
+ }
992
+
993
+ .pci-tm-summary {
994
+ flex: 1 1 auto;
995
+ min-width: 0;
996
+ overflow: hidden;
997
+ text-overflow: ellipsis;
998
+ white-space: nowrap;
999
+ }
1000
+
1001
+ .pci-tm-scale {
1002
+ min-width: 36px;
1003
+ text-align: center;
1004
+ font-variant-numeric: tabular-nums;
1005
+ }
1006
+
1007
+ .pci-tm-crumbs {
1008
+ flex: 0 0 auto;
1009
+ padding: 4px 8px;
1010
+ color: var(--pci-text-secondary);
1011
+ font-size: 11px;
1012
+ white-space: nowrap;
1013
+ overflow: hidden;
1014
+ text-overflow: ellipsis;
1015
+ }
1016
+
1017
+ .pci-tm-crumb {
1018
+ font: inherit;
1019
+ color: var(--pci-text);
1020
+ background: none;
1021
+ border: none;
1022
+ padding: 0;
1023
+ text-decoration: underline;
1024
+ cursor: pointer;
1025
+ }
1026
+
1027
+ .pci-tm-dim {
1028
+ color: var(--pci-text-muted);
1029
+ }
1030
+
1031
+ .pci-tm-view {
1032
+ flex: 1 1 auto;
1033
+ min-height: 0;
1034
+ margin: 0 8px;
1035
+ border-radius: 6px;
1036
+ outline: none;
1037
+ }
1038
+
1039
+ .pci-tm-view.pci-tm-pan {
1040
+ cursor: grab;
1041
+ }
1042
+
1043
+ .pci-tm-view.pci-tm-panning,
1044
+ .pci-tm-view.pci-tm-panning .pci-tm-head {
1045
+ cursor: grabbing;
1046
+ user-select: none;
1047
+ }
1048
+
1049
+ .pci-tm-box {
1050
+ position: relative;
1051
+ overflow: hidden;
1052
+ }
1053
+
1054
+ .pci-tm-group {
1055
+ position: absolute;
1056
+ border: 1px solid var(--pci-border-strong);
1057
+ border-radius: 4px;
1058
+ overflow: hidden;
1059
+ }
1060
+
1061
+ .pci-tm-head {
1062
+ position: absolute;
1063
+ left: 0;
1064
+ right: 0;
1065
+ top: 0;
1066
+ height: 16px;
1067
+ padding: 0 5px;
1068
+ background: var(--pci-raised);
1069
+ color: var(--pci-text-secondary);
1070
+ font-size: 10.5px;
1071
+ line-height: 16px;
1072
+ white-space: nowrap;
1073
+ overflow: hidden;
1074
+ text-overflow: ellipsis;
1075
+ cursor: zoom-in;
1076
+ }
1077
+
1078
+ .pci-tm-head:hover {
1079
+ color: var(--pci-text);
1080
+ text-decoration: underline;
1081
+ }
1082
+
1083
+ .pci-tm-leaf {
1084
+ position: absolute;
1085
+ overflow: hidden;
1086
+ border: 1px solid var(--pci-bg);
1087
+ border-radius: 3px;
1088
+ padding: 1px 4px;
1089
+ color: #ffffff;
1090
+ font-size: 10.5px;
1091
+ line-height: 1.25;
1092
+ }
1093
+
1094
+ .pci-tm-leaf:hover {
1095
+ outline: 2px solid var(--pci-text);
1096
+ outline-offset: -2px;
1097
+ }
1098
+
1099
+ /* a box that shows what it measured in the tab listing it */
1100
+ .pci-tm-leaf.pci-tm-link {
1101
+ cursor: pointer;
1102
+ }
1103
+
1104
+ .pci-tm-leaf span {
1105
+ position: relative;
1106
+ display: block;
1107
+ white-space: nowrap;
1108
+ overflow: hidden;
1109
+ text-overflow: ellipsis;
1110
+ }
1111
+
1112
+ .pci-tm-size {
1113
+ opacity: 0.85;
1114
+ font-family: var(--pci-mono);
1115
+ }
1116
+
1117
+ /* comparing: a neutral box, with the bytes added or removed marked */
1118
+ .pci-tm-leaf.pci-tm-cmp,
1119
+ .pci-tm-legend i.pci-tm-same {
1120
+ background: #2e3a3c;
1121
+ color: var(--pci-text);
1122
+ }
1123
+
1124
+ .pci-tm-part {
1125
+ position: absolute;
1126
+ }
1127
+
1128
+ .pci-tm-added {
1129
+ background: #a83a3a;
1130
+ }
1131
+
1132
+ /* stripes as well as colour, so removed does not rely on telling red from green */
1133
+ .pci-tm-removed {
1134
+ background: repeating-linear-gradient(45deg, #1f7a1f, #1f7a1f 4px, #145214 4px, #145214 8px);
1135
+ }
1136
+
1137
+ .pci-tm-legend {
1138
+ flex: 0 0 auto;
1139
+ display: flex;
1140
+ flex-wrap: wrap;
1141
+ gap: 2px 12px;
1142
+ padding: 5px 8px 6px;
1143
+ color: var(--pci-text-label);
1144
+ font-size: 10.5px;
1145
+ }
1146
+
1147
+ .pci-tm-legend i {
1148
+ display: inline-block;
1149
+ width: 10px;
1150
+ height: 10px;
1151
+ margin-right: 4px;
1152
+ border-radius: 2px;
1153
+ vertical-align: -1px;
1154
+ }
1155
+
1156
+ .pci-tm-tip {
1157
+ position: fixed;
1158
+ z-index: 3;
1159
+ max-width: 360px;
1160
+ padding: 5px 8px;
1161
+ background: var(--pci-raised);
1162
+ color: var(--pci-text);
1163
+ border: 1px solid var(--pci-border-strong);
1164
+ border-radius: 6px;
1165
+ box-shadow: var(--pci-shadow);
1166
+ overflow-wrap: anywhere;
1167
+ pointer-events: none;
1168
+ }
1169
+
909
1170
  .pci-tree {
910
1171
  flex: 1 1 auto;
911
1172
  overflow: auto;
@@ -183,4 +183,4 @@ function buildTextureModel(texture, ctx) {
183
183
  return sections;
184
184
  }
185
185
 
186
- export { buildTextureModel, collectTextures, textureRows };
186
+ export { attachmentTargets, buildTextureModel, collectTextures, shapeText, textureRows };