@playcanvas/inspector 0.2.0 → 0.4.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.
- package/README.md +147 -5
- package/package.json +1 -1
- package/src/asset-view.js +24 -17
- package/src/camera-view.js +5 -3
- package/src/context-menu.js +231 -0
- package/src/copy-json.js +692 -0
- package/src/describe.js +20 -1
- package/src/entity-inspector-script.js +10 -0
- package/src/frame-graph-view.js +41 -9
- package/src/hierarchy-view.js +29 -3
- package/src/inspector.js +958 -80
- package/src/list-view.js +118 -12
- package/src/material-view.js +7 -6
- package/src/memory-view.js +224 -66
- package/src/mesh-view.js +6 -5
- package/src/model.js +24 -2
- package/src/node-model.js +81 -11
- package/src/perf-capture.js +2034 -0
- package/src/perf-chart.js +3989 -0
- package/src/perf-copy.js +367 -0
- package/src/perf-view.js +1224 -0
- package/src/property-view.js +162 -5
- package/src/render-target-view.js +3 -3
- package/src/script-view.js +21 -12
- package/src/styles.js +374 -2
- package/src/texture-view.js +4 -4
- package/src/transform-tool.js +185 -0
- package/types/asset-view.d.ts +3 -1
- package/types/camera-view.d.ts +4 -0
- package/types/context-menu.d.ts +90 -0
- package/types/copy-json.d.ts +69 -0
- package/types/describe.d.ts +1 -1
- package/types/entity-inspector-script.d.ts +8 -0
- package/types/frame-graph-view.d.ts +7 -0
- package/types/hierarchy-view.d.ts +13 -0
- package/types/inspector.d.ts +339 -25
- package/types/list-view.d.ts +44 -3
- package/types/material-view.d.ts +3 -1
- package/types/memory-view.d.ts +24 -25
- package/types/mesh-view.d.ts +3 -1
- package/types/model.d.ts +16 -1
- package/types/node-model.d.ts +17 -1
- package/types/perf-capture.d.ts +871 -0
- package/types/perf-chart.d.ts +1407 -0
- package/types/perf-copy.d.ts +61 -0
- package/types/perf-view.d.ts +231 -0
- package/types/property-view.d.ts +41 -0
- package/types/script-view.d.ts +3 -1
- package/types/transform-tool.d.ts +92 -0
package/README.md
CHANGED
|
@@ -10,13 +10,16 @@
|
|
|
10
10
|
| [User Manual](https://developer.playcanvas.com) | [Blog](https://blog.playcanvas.com) | [Forum](https://forum.playcanvas.com) |
|
|
11
11
|
|
|
12
12
|
A debug panel for running [PlayCanvas](https://github.com/playcanvas/engine) apps. It shows the entity
|
|
13
|
-
hierarchy and every component's properties, and tabs for the cameras, assets, frame graph,
|
|
14
|
-
targets, textures, meshes, materials, scripts, memory, shaders and physics of
|
|
15
|
-
previews of textures and render targets, picking and flying in the view, and a
|
|
16
|
-
that steps through the draw calls of a render pass.
|
|
13
|
+
hierarchy and every component's properties, and tabs for the cameras, assets, frame graph,
|
|
14
|
+
performance, render targets, textures, meshes, materials, scripts, memory, shaders and physics of
|
|
15
|
+
the app, with live previews of textures and render targets, picking and flying in the view, and a
|
|
16
|
+
debug frame mode that steps through the draw calls of a render pass.
|
|
17
17
|
|
|
18
18
|
The inspector is read-only, apart from toggling entities on and off, pausing and stepping the app,
|
|
19
|
-
and view-only overrides (render modes, wireframe, a flown camera) it removes when it
|
|
19
|
+
and view-only overrides (render modes, wireframe, a flown or orbited camera) it removes when it
|
|
20
|
+
closes.
|
|
21
|
+
|
|
22
|
+

|
|
20
23
|
|
|
21
24
|
## Install
|
|
22
25
|
|
|
@@ -51,6 +54,145 @@ entity.script.create(EntityInspector, { properties: { dock: 'left' } });
|
|
|
51
54
|
app.root.addChild(entity);
|
|
52
55
|
```
|
|
53
56
|
|
|
57
|
+
### Step through a frame
|
|
58
|
+
|
|
59
|
+
In the Frame graph tab, select a forward pass or one of its layer steps and turn on **Debug frame**.
|
|
60
|
+
The app pauses and the pass draws only up to the selected draw; the arrow keys step through the
|
|
61
|
+
draws one at a time, with each one outlined in the view and listed with its mesh and material. It
|
|
62
|
+
needs the debug build of the engine.
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
### Record performance
|
|
67
|
+
|
|
68
|
+
On the Performance tab, press the record button (a ring around a dot) to capture every frame, up to
|
|
69
|
+
the last 1000; it shows red while recording, and pressing it again stops. The button beside it
|
|
70
|
+
clears the capture. Charts cover the whole capture along its time, each frame as wide as it lasted,
|
|
71
|
+
with a pause longer than a second folded to a break mark: the frame interval, the CPU time on the
|
|
72
|
+
main thread by phase, the GPU time and the draw calls by kind. The Loading section, on the same
|
|
73
|
+
time, marks the moments of the page and the app, such as the first paint, the preload and the first
|
|
74
|
+
frame, the assets loading, from asking for them to their resource being ready, with their request
|
|
75
|
+
solid and what came after it, as parsing, faint, and how many requests were in flight; hover it for
|
|
76
|
+
what was loading, and click an asset to show it on the Assets tab. Below them, the Memory chart
|
|
77
|
+
draws the JavaScript heap, the video memory and each kind of it as lines sharing a scale, and the
|
|
78
|
+
Resources chart the count of textures, render targets, shaders, buffers and pipelines; untick a line
|
|
79
|
+
in the list beside a chart to hide it, and the chart rescales to the others. The Events section has
|
|
80
|
+
a row, which can be unticked too, for each kind of event, marking the frames that had one: hitches,
|
|
81
|
+
main thread blocked, lightmap bakes (for an app with a lightmapper), shaders compiled, garbage
|
|
82
|
+
collections, resources created or destroyed and the app's own measures; the assets loading are in
|
|
83
|
+
the Loading section. A hitch is a frame taking over twice the median, whatever held it back, so it
|
|
84
|
+
is what the user sees stutter. Main thread blocked is what the browser reports, in Chromium
|
|
85
|
+
browsers, as a long animation frame: its main thread busy for 50 ms or more, with the script that
|
|
86
|
+
ran longest, during a frame or between them. It tells why a frame was slow: a hitch without it was
|
|
87
|
+
held back by the GPU, the browser or the display, which its hover says. Click the name of a chart or
|
|
88
|
+
a section to collapse it. Hover a mark for what happened, or a chart for the figures of the frame
|
|
89
|
+
under the pointer. A dashed line marks the display interval on the Frame, CPU and GPU charts, which
|
|
90
|
+
are scaled to show it.
|
|
91
|
+
|
|
92
|
+
Click a frame, or a mark, to show it on a timeline of its own, and everything recorded for it below:
|
|
93
|
+
what held it back, the CPU time by phase, the GPU time of each pass, what loaded in it, the work it
|
|
94
|
+
submitted, memory and resources, and its events. The timeline is a flame chart: the update,
|
|
95
|
+
post-update, inspector and render of the CPU, and any lightmap bake where it ran, with the physics
|
|
96
|
+
step, the animation and the phases of the render under them, then the GPU passes, the events, and
|
|
97
|
+
the moments, assets and requests of the frame. With frames selected together, it shows their average
|
|
98
|
+
frame. Scroll over the timeline to zoom into part of the frame, drag the scrollbar under it, or drag
|
|
99
|
+
across it holding Shift, to pan, and double-click it to see the whole frame again; click its title
|
|
100
|
+
to collapse it. Drag the divider between the names and the charts, or the timeline, to make the
|
|
101
|
+
names wider. Drag across the charts to select frames together, with their averages, 95th percentiles
|
|
102
|
+
and largest figures; scroll to zoom, then drag the scrollbar under the charts, or drag across them
|
|
103
|
+
holding Shift, to pan, and double-click to see the whole capture again. **‹** and **›** step a
|
|
104
|
+
frame, **«** shows the first and **»** the newest, following it as frames are recorded. While the
|
|
105
|
+
panel has focus, the arrow keys step a frame (ten with <kbd>Shift</kbd>), <kbd>Home</kbd> shows the
|
|
106
|
+
first and <kbd>End</kbd> follows the newest.
|
|
107
|
+
|
|
108
|
+
Turn on **GPU timings** to record the GPU time. On WebGPU each pass is timed, and the timeline draws
|
|
109
|
+
the GPU as a flame chart: the effects, such as bloom or depth of field, on the GPU line, with the
|
|
110
|
+
passes each runs under it, and the passes of their own on the GPU line too. The effect of a pass
|
|
111
|
+
comes from the frame graph, the pass it is a before or after pass of; where the frame graph no
|
|
112
|
+
longer matches the passes timed, every pass goes on the GPU line. Passes of one name in a row share
|
|
113
|
+
a label, as Downsample ×5, the frame details list the time of each, and clicking a pass or an effect
|
|
114
|
+
shows it on the Frame graph tab. An engine that sums the passes sharing a name, before 2.24, gives
|
|
115
|
+
one time for each name, and WebGL times the frame as a whole. An engine whose GPU profiler does not
|
|
116
|
+
say which frame its timings belong to, such as 2.23, gets them a few frames late, which the tab
|
|
117
|
+
notes. The inspector runs inside the app's update, so its own time is shown apart, as Inspector;
|
|
118
|
+
turn off **Inspector time** to leave it out of the CPU chart, which then scales to the app's own
|
|
119
|
+
time. The release engine times the update and the render as a whole; the profiler and debug engines
|
|
120
|
+
add the phases of the render, the batcher's updating and creating of batches among them, and the
|
|
121
|
+
breakdown of the draw calls. Spans the app measures with `performance.measure` show on the timeline
|
|
122
|
+
of their frame.
|
|
123
|
+
|
|
124
|
+
Turn on **Script timing** to time each script type while recording: the time of its `update`,
|
|
125
|
+
`postUpdate` and `initialize` calls, without the calls nested in them, such as the scripts of an
|
|
126
|
+
entity a script creates, and how many times each was called. The CPU chart then stacks the scripts
|
|
127
|
+
apart from the other systems, and the timeline draws the scripts of the update and of the
|
|
128
|
+
post-update where they ran, with a line below breaking each down by script type: as the calls of
|
|
129
|
+
the types interleave, entity by entity, each type is its time in all, back to back in the order the
|
|
130
|
+
types first ran. Click a type there to show it on the Scripts tab. The Scripts chart stacks the ten script types that took the longest, over the
|
|
131
|
+
frames selected together or else the whole capture, so selecting a spike brings forward the
|
|
132
|
+
scripts behind it; the others share one band, whose hover lists the longest of them. **▸ 10 more**
|
|
133
|
+
under the list gives ten more types a band of their own. To follow a script whatever its time, pin
|
|
134
|
+
it: **Pin** beside it in the Scripts details, or Shift+click it in the list. Alt+click a line of a
|
|
135
|
+
list to show it alone, and again to show them all. Timing a call costs the app about half a
|
|
136
|
+
microsecond, estimated as recording starts and counted as the inspector's time. Time spent in
|
|
137
|
+
event handlers, timers and promises is not counted. On a page that is not cross-origin isolated,
|
|
138
|
+
the browser's clock steps by a tenth of a millisecond, so the times of small scripts read best over
|
|
139
|
+
many frames selected together.
|
|
140
|
+
|
|
141
|
+
The Counters section charts the counters the app keeps in `app.stats.user`, as each frame ended: a
|
|
142
|
+
line each, with a scale of its own, as their units are the app's. The frame details list their
|
|
143
|
+
values, and the frames selected together their averages, 95th percentiles and largest:
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
// counted afresh each frame
|
|
147
|
+
app.on('frameupdate', () => app.stats.user.set('enemies', 0));
|
|
148
|
+
|
|
149
|
+
// wherever the app counts them
|
|
150
|
+
app.stats.user.set('enemies', app.stats.user.get('enemies') + 1);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
To record from the first frame the app runs, pass `record: true` to the constructor, or set the
|
|
154
|
+
`record` attribute of the `EntityInspector` script. Create the inspector before the app runs its
|
|
155
|
+
first frame, as right after `app.init()`, to record the start of the app too: the capture then
|
|
156
|
+
begins with a Startup entry, from the page opening to the first frame, with the requests of the
|
|
157
|
+
page, its moments, the assets loaded and the times its main thread was blocked; its details list the
|
|
158
|
+
slowest assets, with their request and processing apart. It is one of the 1000 entries, so it makes
|
|
159
|
+
way for frames like any other. From code, set `inspector.recording` to start or stop a capture
|
|
160
|
+
around a part of the app:
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
const inspector = new Inspector(app, { record: true });
|
|
164
|
+
|
|
165
|
+
// once the part to look at has run
|
|
166
|
+
inspector.recording = false;
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Edit transforms
|
|
170
|
+
|
|
171
|
+
Click a position, rotation or the local scale in the Transform section of an entity to edit it as
|
|
172
|
+
three numbers separated by commas (one number for a uniform scale); Enter applies it and Escape
|
|
173
|
+
gives up. Or turn on **Move**, **Rotate** or **Scale** in the toolbar and drag the handles of the
|
|
174
|
+
gizmo drawn on the selected entity. A drag on a handle is kept from the app, while away from the
|
|
175
|
+
handles the app has the view as usual. Pick, Fly and Orbit are off while a gizmo is on; Esc or its
|
|
176
|
+
button turns it off. An entity with a physics body takes its body along.
|
|
177
|
+
|
|
178
|
+
### Copy as JSON
|
|
179
|
+
|
|
180
|
+
Right-click a row, a section title or an item of a list to copy it to the clipboard as JSON, with
|
|
181
|
+
numbers at full precision. Right-click the header of a group, such as Textures on the Memory tab,
|
|
182
|
+
to copy the group, or the name of a tab to copy everything it lists: the Hierarchy tab copies the
|
|
183
|
+
hierarchy from its root. **Copy as JSON** copies it on its own; **Copy N levels deep** also copies
|
|
184
|
+
what it links to, such as the children of an entity, its mesh instances, their materials and their
|
|
185
|
+
textures, up to N links away; **Copy all levels** follows every link, so copying the root copies
|
|
186
|
+
the whole hierarchy. A link that is not followed is named by `$type` and its name or id, and
|
|
187
|
+
marked `$stub`; an object already copied elsewhere in the copy is marked `$ref`. A node's parent
|
|
188
|
+
and the lists of what uses an object are left out, so a copy never climbs out of what was copied.
|
|
189
|
+
|
|
190
|
+
On the Performance tab, right-click the name of the tab to copy the whole capture, every frame with
|
|
191
|
+
all it recorded, or the name of a chart or section, or a line of its list, to copy that series a row
|
|
192
|
+
per frame, as JSON or as CSV for a spreadsheet; both cover the frames selected together when there
|
|
193
|
+
are any. Right-click a frame in the charts to copy it, inside the frames selected together to copy
|
|
194
|
+
their figures, and the timeline to copy its bars.
|
|
195
|
+
|
|
54
196
|
## Engine versions
|
|
55
197
|
|
|
56
198
|
Each inspector release names the engine versions it supports in its `playcanvas` peer dependency.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@playcanvas/inspector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A debug panel for PlayCanvas apps: the entity hierarchy, assets, frame graph, render targets, textures, meshes, materials, scripts, cameras, memory, shaders and physics of a running app",
|
|
5
5
|
"author": "PlayCanvas <support@playcanvas.com>",
|
|
6
6
|
"homepage": "https://github.com/playcanvas/inspector#readme",
|
package/src/asset-view.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Asset } from 'playcanvas';
|
|
2
2
|
|
|
3
3
|
import { collectProperties, describeValue } from './describe.js';
|
|
4
|
-
import { formatBytes, makeSection, push, read, reflectRows } from './model.js';
|
|
4
|
+
import { backlink, formatBytes, makeSection, makeUsageSection, push, read, reflectRows } from './model.js';
|
|
5
5
|
|
|
6
6
|
/** @import { AppBase } from 'playcanvas' */
|
|
7
7
|
/** @import { AssetRegistry } from 'playcanvas' */
|
|
@@ -103,12 +103,9 @@ function assetPropertyNames(component) {
|
|
|
103
103
|
*/
|
|
104
104
|
function describeAssetValue(value, registry) {
|
|
105
105
|
if (Array.isArray(value)) {
|
|
106
|
+
if (!value.length) return describeValue(value);
|
|
106
107
|
const items = value.map((entry, i) => ({ label: `[${i}]`, ...describeAssetValue(entry, registry) }));
|
|
107
|
-
return {
|
|
108
|
-
text: value.length ? `Array(${value.length}) of asset` : '[]',
|
|
109
|
-
cls: value.length ? 'obj' : 'null',
|
|
110
|
-
items: value.length ? items : undefined
|
|
111
|
-
};
|
|
108
|
+
return { text: `Array(${value.length}) of asset`, cls: 'obj', items };
|
|
112
109
|
}
|
|
113
110
|
if (typeof value === 'number') {
|
|
114
111
|
const asset = registry?.get(value) ?? null;
|
|
@@ -292,7 +289,8 @@ function assetRows(registry, sort) {
|
|
|
292
289
|
* into (linked), the components using it (linked), and every other public property.
|
|
293
290
|
*
|
|
294
291
|
* @param {Asset} asset - The asset.
|
|
295
|
-
* @param {{ app: AppBase }} ctx - The app, to find the asset's users
|
|
292
|
+
* @param {{ app: AppBase, backlinks?: boolean }} ctx - The app, to find the asset's users, and
|
|
293
|
+
* whether to find them at all, which a copy has no use for.
|
|
296
294
|
* @returns {PropertySection[]} The sections.
|
|
297
295
|
*/
|
|
298
296
|
function buildAssetModel(asset, ctx) {
|
|
@@ -305,7 +303,7 @@ function buildAssetModel(asset, ctx) {
|
|
|
305
303
|
push(general, 'type', { text: asset.type, cls: 'obj' });
|
|
306
304
|
const { parentOf, parts } = containerParts(ctx.app?.assets ?? null);
|
|
307
305
|
const container = parentOf.get(asset) ?? null;
|
|
308
|
-
if (container) push(general, 'container', describeValue(container));
|
|
306
|
+
if (container) push(general, 'container', backlink(describeValue(container)));
|
|
309
307
|
push(general, 'state', { text: stateName(asset), cls: asset.loaded ? 'bool' : 'null' });
|
|
310
308
|
push(general, 'preload', read(asset, 'preload'));
|
|
311
309
|
if (container) {
|
|
@@ -343,8 +341,23 @@ function buildAssetModel(asset, ctx) {
|
|
|
343
341
|
sections.push(contents);
|
|
344
342
|
}
|
|
345
343
|
|
|
346
|
-
|
|
347
|
-
|
|
344
|
+
if (ctx.backlinks !== false) sections.push(usersSection(asset, ctx.app));
|
|
345
|
+
|
|
346
|
+
const rest = makeSection('props', 'Properties');
|
|
347
|
+
reflectRows(rest, asset, [], SKIP_ASSET);
|
|
348
|
+
if (rest.rows.length) sections.push(rest);
|
|
349
|
+
|
|
350
|
+
return sections;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* @param {Asset} asset - The asset.
|
|
355
|
+
* @param {AppBase} app - The app, to find the asset's users.
|
|
356
|
+
* @returns {PropertySection} The section listing the components using the asset.
|
|
357
|
+
*/
|
|
358
|
+
function usersSection(asset, app) {
|
|
359
|
+
const users = assetUsers(app, asset);
|
|
360
|
+
const usage = makeUsageSection('users', 'Used by');
|
|
348
361
|
push(usage, 'components', {
|
|
349
362
|
text: users.length ? `${users.length} component${users.length === 1 ? '' : 's'}` : 'no component in the scene',
|
|
350
363
|
cls: users.length ? 'obj' : 'null',
|
|
@@ -355,13 +368,7 @@ function buildAssetModel(asset, ctx) {
|
|
|
355
368
|
target: user.entity
|
|
356
369
|
}))
|
|
357
370
|
});
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
const rest = makeSection('props', 'Properties');
|
|
361
|
-
reflectRows(rest, asset, [], SKIP_ASSET);
|
|
362
|
-
if (rest.rows.length) sections.push(rest);
|
|
363
|
-
|
|
364
|
-
return sections;
|
|
371
|
+
return usage;
|
|
365
372
|
}
|
|
366
373
|
|
|
367
374
|
export {
|
package/src/camera-view.js
CHANGED
|
@@ -17,6 +17,7 @@ import { RENDER_MODES, isScreenCamera } from './viewport-tools.js';
|
|
|
17
17
|
* @typedef {object} CameraActions
|
|
18
18
|
* @property {(camera: CameraComponent) => void} fly - Flies the camera.
|
|
19
19
|
* @property {CameraComponent|null} flying - The camera being flown, if any.
|
|
20
|
+
* @property {string|null} [unavailable] - Why no camera can be flown now, such as a gizmo being on.
|
|
20
21
|
* @ignore
|
|
21
22
|
*/
|
|
22
23
|
|
|
@@ -96,13 +97,14 @@ function buildCameraModel(camera, ctx) {
|
|
|
96
97
|
const general = makeSection('camera', `Camera "${entity.name}"`);
|
|
97
98
|
push(general, 'entity', describeValue(entity));
|
|
98
99
|
push(general, 'enabled', { text: String(camera.enabled && entity.enabled), cls: 'bool' });
|
|
100
|
+
const unavailable = cameraActions.unavailable ?? null;
|
|
99
101
|
push(general, 'fly', {
|
|
100
|
-
text: camera === cameraActions.flying ? 'flying now, Esc to stop' : 'fly it with the mouse and WASD',
|
|
102
|
+
text: camera === cameraActions.flying ? 'flying now, Esc to stop' : (unavailable ?? 'fly it with the mouse and WASD'),
|
|
101
103
|
cls: 'obj',
|
|
102
104
|
actions: [{
|
|
103
105
|
text: camera === cameraActions.flying ? 'Flying' : 'Fly',
|
|
104
|
-
title: 'Fly this camera: WASD and QE move, drag to look, the wheel sets the speed, Shift is faster, Esc stops',
|
|
105
|
-
disabled: camera === cameraActions.flying,
|
|
106
|
+
title: unavailable ?? 'Fly this camera: WASD and QE move, drag to look, the wheel sets the speed, Shift is faster, Esc stops',
|
|
107
|
+
disabled: camera === cameraActions.flying || !!unavailable,
|
|
106
108
|
run: () => cameraActions.fly(camera)
|
|
107
109
|
}]
|
|
108
110
|
});
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { setTip } from './tooltip.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An entry of a context menu.
|
|
5
|
+
*
|
|
6
|
+
* @typedef {object} MenuItem
|
|
7
|
+
* @ignore
|
|
8
|
+
* @property {() => string} text - The text, read again whenever the entry redraws.
|
|
9
|
+
* @property {() => void} run - Called when the entry is chosen, once the menu has closed.
|
|
10
|
+
* @property {string} [title] - The tooltip.
|
|
11
|
+
* @property {MenuButton[]} [buttons] - Small buttons at the end of the entry, which change the
|
|
12
|
+
* entry without choosing it or closing the menu.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} MenuButton
|
|
17
|
+
* @ignore
|
|
18
|
+
* @property {string} text - The text.
|
|
19
|
+
* @property {string} title - The tooltip.
|
|
20
|
+
* @property {() => boolean} disabled - Whether it is disabled, read again whenever it redraws.
|
|
21
|
+
* @property {() => void} run - Called when it is clicked.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// milliseconds a note stays up
|
|
25
|
+
const NOTE_TIME = 2500;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} tag - The element tag.
|
|
29
|
+
* @param {string} className - The class name.
|
|
30
|
+
* @param {string} [text] - The text content.
|
|
31
|
+
* @returns {HTMLElement} The element.
|
|
32
|
+
*/
|
|
33
|
+
function el(tag, className, text) {
|
|
34
|
+
const element = document.createElement(tag);
|
|
35
|
+
element.className = className;
|
|
36
|
+
if (text !== undefined) element.textContent = text;
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A menu opened where the pointer is, and a note shown briefly at the bottom of the panel, such as
|
|
42
|
+
* for what was copied. Both live in the panel, so they move with it into a popped-out window. The
|
|
43
|
+
* menu closes on a choice, a press anywhere outside it, Escape, scrolling and leaving the window.
|
|
44
|
+
*
|
|
45
|
+
* @ignore
|
|
46
|
+
*/
|
|
47
|
+
class ContextMenu {
|
|
48
|
+
/**
|
|
49
|
+
* @type {HTMLElement}
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
_panel;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @type {HTMLElement}
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
_menu;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @type {HTMLElement}
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
_note;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @type {number}
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
_noteTimer = 0;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The document the closing listeners are on while the menu is open.
|
|
74
|
+
*
|
|
75
|
+
* @type {Document|null}
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
_doc = null;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {HTMLElement} panel - The panel to show the menu and notes in.
|
|
82
|
+
*/
|
|
83
|
+
constructor(panel) {
|
|
84
|
+
this._panel = panel;
|
|
85
|
+
this._menu = el('div', 'pci-menu');
|
|
86
|
+
this._menu.setAttribute('role', 'menu');
|
|
87
|
+
this._note = el('div', 'pci-toast');
|
|
88
|
+
panel.append(this._menu, this._note);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Whether the menu is open.
|
|
93
|
+
*
|
|
94
|
+
* @type {boolean}
|
|
95
|
+
*/
|
|
96
|
+
get open() {
|
|
97
|
+
return this._doc !== null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Opens the menu at a point, in place of any menu already open.
|
|
102
|
+
*
|
|
103
|
+
* @param {number} x - The point, in client pixels.
|
|
104
|
+
* @param {number} y - The point, in client pixels.
|
|
105
|
+
* @param {string} title - What the menu acts on.
|
|
106
|
+
* @param {MenuItem[]} items - The entries.
|
|
107
|
+
*/
|
|
108
|
+
show(x, y, title, items) {
|
|
109
|
+
this.hide();
|
|
110
|
+
const menu = this._menu;
|
|
111
|
+
menu.textContent = '';
|
|
112
|
+
const titleEl = el('div', 'pci-menu-title', title);
|
|
113
|
+
setTip(titleEl, title);
|
|
114
|
+
menu.appendChild(titleEl);
|
|
115
|
+
for (const item of items) menu.appendChild(this._entry(item));
|
|
116
|
+
menu.style.display = 'block';
|
|
117
|
+
|
|
118
|
+
const view = this._panel.ownerDocument.defaultView ?? window;
|
|
119
|
+
const width = menu.offsetWidth;
|
|
120
|
+
const height = menu.offsetHeight;
|
|
121
|
+
menu.style.left = `${Math.max(4, Math.min(x, view.innerWidth - width - 4))}px`;
|
|
122
|
+
menu.style.top = `${Math.max(4, Math.min(y, view.innerHeight - height - 4))}px`;
|
|
123
|
+
|
|
124
|
+
const doc = this._panel.ownerDocument;
|
|
125
|
+
doc.addEventListener('pointerdown', this._onPointerDown, true);
|
|
126
|
+
doc.addEventListener('keydown', this._onKeyDown, true);
|
|
127
|
+
doc.addEventListener('wheel', this.hide, { capture: true, passive: true });
|
|
128
|
+
view.addEventListener('blur', this.hide);
|
|
129
|
+
this._doc = doc;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Closes the menu, if it is open.
|
|
134
|
+
*/
|
|
135
|
+
hide = () => {
|
|
136
|
+
const doc = this._doc;
|
|
137
|
+
if (!doc) return;
|
|
138
|
+
this._doc = null;
|
|
139
|
+
this._menu.style.display = 'none';
|
|
140
|
+
doc.removeEventListener('pointerdown', this._onPointerDown, true);
|
|
141
|
+
doc.removeEventListener('keydown', this._onKeyDown, true);
|
|
142
|
+
doc.removeEventListener('wheel', this.hide, { capture: true });
|
|
143
|
+
doc.defaultView?.removeEventListener('blur', this.hide);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Shows a note at the bottom of the panel for a moment.
|
|
148
|
+
*
|
|
149
|
+
* @param {string} text - The note.
|
|
150
|
+
*/
|
|
151
|
+
note(text) {
|
|
152
|
+
clearTimeout(this._noteTimer);
|
|
153
|
+
this._note.textContent = text;
|
|
154
|
+
this._note.style.display = 'block';
|
|
155
|
+
this._noteTimer = setTimeout(() => {
|
|
156
|
+
this._note.style.display = 'none';
|
|
157
|
+
}, NOTE_TIME);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Closes the menu and removes its elements.
|
|
162
|
+
*/
|
|
163
|
+
destroy() {
|
|
164
|
+
this.hide();
|
|
165
|
+
clearTimeout(this._noteTimer);
|
|
166
|
+
this._menu.remove();
|
|
167
|
+
this._note.remove();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @param {MenuItem} item - An entry.
|
|
172
|
+
* @returns {HTMLElement} Its element.
|
|
173
|
+
* @private
|
|
174
|
+
*/
|
|
175
|
+
_entry(item) {
|
|
176
|
+
const entry = el('div', 'pci-menu-item');
|
|
177
|
+
entry.setAttribute('role', 'menuitem');
|
|
178
|
+
const label = el('span', 'pci-menu-label', item.text());
|
|
179
|
+
entry.appendChild(label);
|
|
180
|
+
if (item.title) setTip(entry, item.title);
|
|
181
|
+
|
|
182
|
+
/** @type {HTMLButtonElement[]} */
|
|
183
|
+
const buttons = [];
|
|
184
|
+
const redraw = () => {
|
|
185
|
+
label.textContent = item.text();
|
|
186
|
+
item.buttons?.forEach((button, i) => {
|
|
187
|
+
buttons[i].disabled = button.disabled();
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
if (item.buttons?.length) {
|
|
191
|
+
const wrap = el('span', 'pci-menu-buttons');
|
|
192
|
+
for (const button of item.buttons) {
|
|
193
|
+
const buttonEl = /** @type {HTMLButtonElement} */ (el('button', 'pci-menu-button', button.text));
|
|
194
|
+
setTip(buttonEl, button.title);
|
|
195
|
+
buttonEl.addEventListener('click', (e) => {
|
|
196
|
+
e.stopPropagation();
|
|
197
|
+
button.run();
|
|
198
|
+
redraw();
|
|
199
|
+
});
|
|
200
|
+
buttons.push(buttonEl);
|
|
201
|
+
wrap.appendChild(buttonEl);
|
|
202
|
+
}
|
|
203
|
+
entry.appendChild(wrap);
|
|
204
|
+
redraw();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
entry.addEventListener('click', () => {
|
|
208
|
+
this.hide();
|
|
209
|
+
item.run();
|
|
210
|
+
});
|
|
211
|
+
return entry;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {PointerEvent} e - The event.
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
_onPointerDown = (e) => {
|
|
219
|
+
if (!e.composedPath().includes(this._menu)) this.hide();
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {KeyboardEvent} e - The event.
|
|
224
|
+
* @private
|
|
225
|
+
*/
|
|
226
|
+
_onKeyDown = (e) => {
|
|
227
|
+
if (e.key === 'Escape') this.hide();
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export { ContextMenu };
|