@pencil-lang/figure 0.0.6 → 0.0.7

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/src/host.js CHANGED
@@ -7,6 +7,11 @@
7
7
  // import { ready, Player, gather } from '@pencil-lang/figure/host';
8
8
  // await ready();
9
9
  // const player = await Player.create(canvas, false);
10
+ // player.install(await player.prepare(doc, sheets, names, files));
11
+ //
12
+ // `prepare` throws a compile error at once, and resolves once the GPU has compiled the figure's
13
+ // shaders, or rejects with its error. On WebGPU the browser does that off the thread that
14
+ // composites the page. The player draws on meanwhile, and `install` puts the new figure in.
10
15
  //
11
16
  // `scripts/build-web.sh` writes `../pkg`, which isn't checked in.
12
17
  import init from '../pkg/pencil_web.js';
package/src/wiring.js CHANGED
@@ -32,6 +32,66 @@ export function identity(el) {
32
32
  return el.classList.contains('more') ? '.more' : null;
33
33
  }
34
34
 
35
+ /** How near the edges of the screen an opened menu is shown, in CSS pixels. */
36
+ const EDGE = 8;
37
+
38
+ /**
39
+ * The defaults' menu on a phone, which drops down from under their bar over the page. It's an auto
40
+ * popover, so it's in the top layer, where with `position: absolute` its containing block is the
41
+ * initial containing block, which is the page's own coordinates. Placed there where the controls
42
+ * end, it scrolls with the page and stays under its bar with nothing to catch up. While it's open
43
+ * it looks once a frame for where the controls are, and moves only when that has changed: when
44
+ * something above them grows, or a fixed or sticky box carries them, or a scrolling box they're in
45
+ * scrolls, which it follows a frame behind. It closes when its controls leave the screen, or the
46
+ * page becomes too wide for a menu, and stops looking once it's closed or gone from the page.
47
+ * Opened by a reader, it scrolls the page just enough to show it, if it runs past the bottom of the
48
+ * screen, but never so far that the bar leaves the top.
49
+ */
50
+ function dropDown(menu, signal) {
51
+ const controls = menu.parentElement;
52
+ const bar = controls.querySelector(':scope > .bar') ?? controls;
53
+ const phone = matchMedia(PHONE);
54
+ const still = matchMedia('(prefers-reduced-motion: reduce)');
55
+ let frame = 0;
56
+ let at = '';
57
+ const place = () => {
58
+ const box = controls.getBoundingClientRect();
59
+ const where = [box.bottom + scrollY, box.left + scrollX, box.width];
60
+ if (where.join() !== at) {
61
+ at = where.join();
62
+ menu.style.setProperty('--menu-top', `${where[0]}px`);
63
+ menu.style.setProperty('--menu-left', `${where[1]}px`);
64
+ menu.style.setProperty('--menu-width', `${where[2]}px`);
65
+ }
66
+ return box;
67
+ };
68
+ const stop = () => {
69
+ cancelAnimationFrame(frame);
70
+ frame = 0;
71
+ };
72
+ const look = () => {
73
+ frame = 0;
74
+ if (!menu.isConnected || !menu.matches(':popover-open')) return;
75
+ const box = place();
76
+ const gone = box.bottom <= 0 || box.top >= innerHeight || box.right <= 0 || box.left >= innerWidth;
77
+ if (gone || !phone.matches) menu.hidePopover();
78
+ else frame = requestAnimationFrame(look);
79
+ };
80
+ const follow = () => {
81
+ place();
82
+ if (!frame) frame = requestAnimationFrame(look);
83
+ };
84
+ const show = () => {
85
+ const below = menu.getBoundingClientRect().bottom + EDGE - innerHeight;
86
+ const by = Math.min(below, bar.getBoundingClientRect().top - EDGE);
87
+ // At the end of a page the open menu adds to its height, which closing it gives back, so the
88
+ // page can jump up then.
89
+ if (by > 0) scrollBy({ top: by, behavior: still.matches ? 'instant' : 'smooth' });
90
+ };
91
+ signal.addEventListener('abort', stop);
92
+ return { follow, stop, show };
93
+ }
94
+
35
95
  export class Wiring {
36
96
  #figure;
37
97
  #roots = [];
@@ -65,36 +125,55 @@ export class Wiring {
65
125
  }
66
126
 
67
127
  /** A menu that closes with the focus in it, on Escape, gives the focus back to its button, as a
68
- * menu should, where a browser can leave it nowhere. */
128
+ * menu should, where a browser can leave it nowhere. The defaults' menu drops down from their
129
+ * bar, and opening it shows it. */
69
130
  #menus(root, signal) {
70
131
  const scope = root.getRootNode?.() ?? document;
71
132
  for (const menu of root.querySelectorAll('[popover][id]')) {
72
133
  const button = root.querySelector(`[popovertarget="${CSS.escape(menu.id)}"]`);
134
+ const drops = root instanceof ShadowRoot && menu.matches('.controls > .rest') ? dropDown(menu, signal) : null;
73
135
  let inside = false;
136
+ // Whether this heard it begin to open. A menu that controls written again open again opens
137
+ // before this is listening, and only a reader's opening scrolls the page to show it.
138
+ let opening = false;
74
139
  menu.addEventListener(
75
140
  'beforetoggle',
76
141
  (e) => {
77
- if (e.newState === 'closed') inside = menu.contains(scope.activeElement);
142
+ opening = e.newState === 'open';
143
+ if (e.newState === 'open') drops?.follow();
144
+ if (e.newState === 'closed') {
145
+ inside = menu.contains(scope.activeElement);
146
+ drops?.stop();
147
+ }
78
148
  },
79
149
  { signal },
80
150
  );
81
151
  menu.addEventListener(
82
152
  'toggle',
83
153
  (e) => {
84
- if (e.newState !== 'closed' || !inside) return;
154
+ if (e.newState === 'open') {
155
+ if (opening) drops?.show();
156
+ opening = false;
157
+ return;
158
+ }
159
+ if (!inside) return;
85
160
  inside = false;
86
161
  const now = scope.activeElement;
87
162
  if (!now || now === document.body || menu.contains(now)) button?.focus();
88
163
  },
89
164
  { signal },
90
165
  );
166
+ // Controls written again open their menu again before this hears of it.
167
+ if (menu.matches(':popover-open')) drops?.follow();
91
168
  }
92
169
  }
93
170
 
94
171
  /**
95
172
  * On a phone a bar keeps one row on one line, and one that doesn't fit, as radios with long
96
- * words or a long caption, goes behind the menu with the rest, so nothing in the bar is cut off.
97
- * The bar is as tall either way, so nothing moves.
173
+ * words or a long caption, or that's taller than the bar, as a fraction at a large size or radios
174
+ * whose legend WebKit sets on a line of its own, goes behind the menu with the rest, so nothing in
175
+ * the bar is cut off. The bar is as tall either way, so nothing moves. Its button is
176
+ * called what it opens: "More controls" beside a row, and "Controls" alone.
98
177
  */
99
178
  fit() {
100
179
  const phone = typeof matchMedia !== 'undefined' && matchMedia(PHONE).matches;
@@ -110,18 +189,22 @@ export class Wiring {
110
189
  if (moved) {
111
190
  moved.removeAttribute('data-moved');
112
191
  bar.insertBefore(moved, bar.querySelector(':scope > .more'));
113
- bar.removeAttribute('data-crowded');
114
192
  controls.toggleAttribute('data-more', controls.hasAttribute('data-menu'));
115
193
  this.#parts = null;
116
194
  }
117
195
  const row = bar.querySelector(':scope > .row');
118
- if (phone && row && row.scrollWidth > row.clientWidth + 0.5) {
196
+ if (phone && row && (row.scrollWidth > row.clientWidth + 0.5 || row.getBoundingClientRect().height > bar.clientHeight + 0.5)) {
119
197
  menu.prepend(row);
120
198
  row.setAttribute('data-moved', '');
121
- bar.setAttribute('data-crowded', '');
122
199
  controls.setAttribute('data-more', '');
123
200
  this.#parts = null;
124
201
  }
202
+ const more = bar.querySelector(':scope > .more');
203
+ const name = bar.querySelector(':scope > .row') ? 'More controls' : 'Controls';
204
+ if (more && more.getAttribute('aria-label') !== name) {
205
+ more.setAttribute('aria-label', name);
206
+ more.title = name;
207
+ }
125
208
  // Moving what has the focus takes it away, so it's given back.
126
209
  if (focused && focused !== scope.activeElement && focused.isConnected) focused.focus({ preventScroll: true });
127
210
  }
@@ -170,6 +253,8 @@ export class Wiring {
170
253
  for (const control of root.querySelectorAll('[data-param], [data-time], [data-play], [data-step]')) {
171
254
  if ('disabled' in control) control.disabled = false;
172
255
  }
256
+ // And the defaults' menu button, which a build writes disabled with the rest.
257
+ if (root instanceof ShadowRoot) for (const more of root.querySelectorAll('.bar > .more')) more.disabled = false;
173
258
  }
174
259
  this.#parts = null;
175
260
  const info = this.#figure.info;