@mobilewright/inspector 0.0.59 → 0.0.61

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.
Files changed (43) hide show
  1. package/dist/index.d.ts +4 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +42 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/device-manager.d.ts +24 -9
  6. package/dist/lib/device-manager.d.ts.map +1 -1
  7. package/dist/lib/device-manager.js +95 -14
  8. package/dist/lib/device-manager.js.map +1 -1
  9. package/dist/lib/inspect-screen.d.ts +47 -0
  10. package/dist/lib/inspect-screen.d.ts.map +1 -0
  11. package/dist/lib/inspect-screen.js +104 -0
  12. package/dist/lib/inspect-screen.js.map +1 -0
  13. package/dist/lib/locator-derivation.d.ts +20 -5
  14. package/dist/lib/locator-derivation.d.ts.map +1 -1
  15. package/dist/lib/locator-derivation.js +64 -7
  16. package/dist/lib/locator-derivation.js.map +1 -1
  17. package/dist/lib/timeout.d.ts +3 -0
  18. package/dist/lib/timeout.d.ts.map +1 -0
  19. package/dist/lib/timeout.js +9 -0
  20. package/dist/lib/timeout.js.map +1 -0
  21. package/dist/routes/device-actions.d.ts +7 -0
  22. package/dist/routes/device-actions.d.ts.map +1 -0
  23. package/dist/routes/device-actions.js +79 -0
  24. package/dist/routes/device-actions.js.map +1 -0
  25. package/dist/routes/devices.d.ts +1 -1
  26. package/dist/routes/devices.d.ts.map +1 -1
  27. package/dist/routes/devices.js +10 -11
  28. package/dist/routes/devices.js.map +1 -1
  29. package/dist/routes/inspect.d.ts.map +1 -1
  30. package/dist/routes/inspect.js +22 -92
  31. package/dist/routes/inspect.js.map +1 -1
  32. package/package.json +4 -4
  33. package/public/codegen.html +119 -0
  34. package/public/css/app.css +276 -6
  35. package/public/favicon.ico +0 -0
  36. package/public/fonts/codicon-LICENSE.txt +21 -0
  37. package/public/fonts/codicon.ttf +0 -0
  38. package/public/index.html +5 -2
  39. package/public/js/app.js +253 -51
  40. package/public/js/codegen.js +301 -0
  41. package/public/js/inspect.js +7 -0
  42. package/public/js/view-tree.js +182 -0
  43. package/public/logo.png +0 -0
package/public/js/app.js CHANGED
@@ -1,4 +1,4 @@
1
- // Mobilewright Inspector frontend. No framework, no build step.
1
+ // Shared Mobilewright Inspector frontend, used by inspect.js and codegen.js. No framework, no build step.
2
2
 
3
3
  // ---- Pure locator utilities ----
4
4
 
@@ -9,7 +9,7 @@ function locatorKey(locator) {
9
9
  return `${locator.kind}:${locator.value}`
10
10
  }
11
11
 
12
- function escQ(s) {
12
+ export function escQ(s) {
13
13
  return s
14
14
  .replace(/\\/g, '\\\\')
15
15
  .replace(/'/g, "\\'")
@@ -31,6 +31,19 @@ function locatorLabel(locator) {
31
31
  return ''
32
32
  }
33
33
 
34
+ // The code that selects this element: its best locator, plus .nth() when the query engine
35
+ // resolves that locator to several elements (el.match comes from the server).
36
+ export function locatorCode(el) {
37
+ if (!el.locator) {
38
+ return null
39
+ }
40
+ const base = locatorLabel(el.locator)
41
+ if (el.match && el.match.count > 1) {
42
+ return `${base}.nth(${el.match.index})`
43
+ }
44
+ return base
45
+ }
46
+
34
47
  function buildDuplicateSet(elements) {
35
48
  const counts = new Map()
36
49
  for (const el of elements) {
@@ -46,7 +59,7 @@ function buildDuplicateSet(elements) {
46
59
  // ---- ScreenshotPane ----
47
60
  // Owns the screenshot image, SVG highlight overlay, and placeholder state.
48
61
 
49
- class ScreenshotPane {
62
+ export class ScreenshotPane {
50
63
  #img
51
64
  #overlay
52
65
  #placeholder
@@ -54,6 +67,7 @@ class ScreenshotPane {
54
67
  #placeholderSub
55
68
  #logicalWidth = 0
56
69
  #logicalHeight = 0
70
+ #deviceScale = 0 // device pixels per logical point, e.g. 3 on most iPhones
57
71
  #elements = []
58
72
  #hiddenIndices = new Set()
59
73
  #selectedIndex = null
@@ -61,19 +75,71 @@ class ScreenshotPane {
61
75
  #screenshotPane // cached pane element for #constrainSize
62
76
  // O(1) lookup from element index to its SVG rect; rebuilt on each renderHighlights call.
63
77
  #rectByIndex = new Map()
78
+ #showAllHighlights
79
+ #hoverRect = null
64
80
 
65
- constructor() {
81
+ // showAllHighlights: false draws no element boxes, only the one passed to showHoverBox (codegen).
82
+ constructor({ showAllHighlights = true } = {}) {
83
+ this.#showAllHighlights = showAllHighlights
66
84
  this.#img = document.getElementById('screenshot-img')
67
85
  this.#overlay = document.getElementById('highlight-overlay')
68
86
  this.#placeholder = document.getElementById('no-device-msg')
69
87
  this.#placeholderTitle = document.getElementById('placeholder-title')
70
88
  this.#placeholderSub = document.getElementById('placeholder-sub')
71
89
  this.#screenshotPane = document.getElementById('screenshot-pane')
72
- window.addEventListener('resize', () => this.#constrainSize())
90
+ new ResizeObserver(() => this.#constrainSize()).observe(this.#screenshotPane)
73
91
  }
74
92
 
75
93
  onElementClick(cb) { this.#onClickCb = cb }
76
94
 
95
+ // Report clicks / pointer moves on the screenshot as integer device coordinates (logical points, not image pixels).
96
+ // The third argument is whether Shift was held.
97
+ onScreenTap(cb) {
98
+ this.#img.addEventListener('click', e => cb(...this.#devicePointOf(e), e.shiftKey))
99
+ }
100
+
101
+ onScreenHover(cb) {
102
+ this.#img.addEventListener('mousemove', e => cb(...this.#devicePointOf(e), e.shiftKey))
103
+ this.#img.addEventListener('mouseleave', () => cb(null, null, false))
104
+ }
105
+
106
+ // The smallest visible element under (x, y) that passes `accepts`, or null.
107
+ elementAt(x, y, accepts = () => true) {
108
+ let best = null
109
+ for (const el of this.#visibleElements()) {
110
+ const b = el.bounds
111
+ const isInside = x >= b.x && x < b.x + b.width && y >= b.y && y < b.y + b.height
112
+ if (isInside && accepts(el) && (!best || b.width * b.height < best.bounds.width * best.bounds.height)) {
113
+ best = el
114
+ }
115
+ }
116
+ return best
117
+ }
118
+
119
+ showHoverBox(el) {
120
+ this.#hoverRect?.remove()
121
+ this.#hoverRect = null
122
+ if (!el) {
123
+ return
124
+ }
125
+ const { x, y, width, height } = el.bounds
126
+ const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
127
+ rect.setAttribute('x', x)
128
+ rect.setAttribute('y', y)
129
+ rect.setAttribute('width', width)
130
+ rect.setAttribute('height', height)
131
+ rect.classList.add('hover-rect')
132
+ this.#hoverRect = rect
133
+ this.#overlay.appendChild(rect)
134
+ }
135
+
136
+ #devicePointOf(e) {
137
+ const box = this.#img.getBoundingClientRect()
138
+ const scaleX = (this.#logicalWidth || this.#img.naturalWidth) / box.width
139
+ const scaleY = (this.#logicalHeight || this.#img.naturalHeight) / box.height
140
+ return [Math.round((e.clientX - box.left) * scaleX), Math.round((e.clientY - box.top) * scaleY)]
141
+ }
142
+
77
143
  get isScreenshotHidden() { return this.#img.hidden }
78
144
 
79
145
  showPlaceholder(title, sub = '', loading = false) {
@@ -85,9 +151,10 @@ class ScreenshotPane {
85
151
  this.#placeholder.classList.toggle('loading', loading)
86
152
  }
87
153
 
88
- showScreenshot(dataUrl, logicalWidth = 0, logicalHeight = 0) {
154
+ showScreenshot(dataUrl, logicalWidth = 0, logicalHeight = 0, deviceScale = 0) {
89
155
  this.#logicalWidth = logicalWidth
90
156
  this.#logicalHeight = logicalHeight
157
+ this.#deviceScale = deviceScale
91
158
  this.#placeholder.hidden = true
92
159
  this.#placeholder.classList.remove('loading')
93
160
  this.#img.hidden = false
@@ -112,7 +179,11 @@ class ScreenshotPane {
112
179
 
113
180
  #buildRects() {
114
181
  this.#overlay.innerHTML = ''
182
+ this.#hoverRect = null
115
183
  this.#rectByIndex.clear()
184
+ if (!this.#showAllHighlights) {
185
+ return
186
+ }
116
187
  for (const el of this.#visibleElements()) {
117
188
  const { x, y, width, height } = el.bounds
118
189
  const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
@@ -140,11 +211,27 @@ class ScreenshotPane {
140
211
  )
141
212
  }
142
213
 
214
+ // Ask for a half-size screenshot when the pane can show at most half of the device's
215
+ // pixel height anyway; the full-size image would only be downscaled by the browser.
216
+ preferredScreenshotScale() {
217
+ const devicePixelHeight = this.#logicalHeight * this.#deviceScale
218
+ if (!devicePixelHeight) {
219
+ return 1
220
+ }
221
+ const panePixelHeight = this.#availableHeight() * window.devicePixelRatio
222
+ return panePixelHeight <= devicePixelHeight / 2 ? 0.5 : 1
223
+ }
224
+
225
+ // Height left for the screenshot below anything stacked above it in the pane (codegen's device toolbar).
226
+ #availableHeight() {
227
+ const cs = getComputedStyle(this.#screenshotPane)
228
+ const container = this.#img.parentElement
229
+ return this.#screenshotPane.clientHeight - container.offsetTop - parseFloat(cs.paddingBottom)
230
+ }
231
+
143
232
  #constrainSize() {
144
233
  if (this.#img.hidden) return
145
- const cs = getComputedStyle(this.#screenshotPane)
146
- const availH = this.#screenshotPane.clientHeight - parseFloat(cs.paddingTop) - parseFloat(cs.paddingBottom)
147
- this.#img.style.maxHeight = availH + 'px'
234
+ this.#img.style.maxHeight = this.#availableHeight() + 'px'
148
235
  const vw = this.#logicalWidth || this.#img.naturalWidth
149
236
  const vh = this.#logicalHeight || this.#img.naturalHeight
150
237
  this.#overlay.setAttribute('width', this.#img.offsetWidth)
@@ -156,12 +243,13 @@ class ScreenshotPane {
156
243
  // ---- DetailPane ----
157
244
  // Shows element properties, all locator strategies, raw attributes.
158
245
 
159
- class DetailPane {
246
+ export class DetailPane {
160
247
  #pane
161
248
  #typeEl
162
249
  #locatorsEl
163
250
  #propsEl
164
251
  #rawEl
252
+ #isRawExpanded = false // survives re-renders, so the section stays open across row clicks
165
253
  #closeBtn
166
254
  #onCloseCb = null
167
255
 
@@ -289,20 +377,21 @@ class DetailPane {
289
377
 
290
378
  const toggle = document.createElement('button')
291
379
  toggle.className = 'detail-raw-toggle'
292
- toggle.textContent = `▶ Raw Attributes (${Object.keys(el.raw).length})`
293
380
  this.#rawEl.appendChild(toggle)
294
381
 
295
382
  const content = document.createElement('div')
296
383
  content.className = 'detail-raw-content'
297
- content.hidden = true
298
384
  content.textContent = JSON.stringify(el.raw, null, 2)
299
385
 
386
+ const renderExpanded = () => {
387
+ content.hidden = !this.#isRawExpanded
388
+ toggle.textContent = `${this.#isRawExpanded ? '▼' : '▶'} Raw Attributes (${Object.keys(el.raw).length})`
389
+ }
390
+ renderExpanded()
391
+
300
392
  toggle.addEventListener('click', () => {
301
- const isHidden = content.hidden
302
- content.hidden = !isHidden
303
- toggle.textContent = isHidden
304
- ? `▼ Raw Attributes (${Object.keys(el.raw).length})`
305
- : `▶ Raw Attributes (${Object.keys(el.raw).length})`
393
+ this.#isRawExpanded = !this.#isRawExpanded
394
+ renderExpanded()
306
395
  })
307
396
 
308
397
  this.#rawEl.appendChild(content)
@@ -363,6 +452,8 @@ class ElementsPane {
363
452
  if (this.#selectedRow) {
364
453
  this.#selectedRow.classList.add('selected')
365
454
  this.#selectedRow.scrollIntoView({ block: 'nearest' })
455
+ // Focus follows selection so arrow keys work after clicking the screenshot too.
456
+ this.#selectedRow.focus({ preventScroll: true })
366
457
  }
367
458
  }
368
459
 
@@ -388,7 +479,7 @@ class ElementsPane {
388
479
  if (!el.locator) row.classList.add('no-locator')
389
480
  if (hiddenIndices.has(el.index)) row.classList.add('element-hidden')
390
481
 
391
- const locLabel = el.locator ? locatorLabel(el.locator) : null
482
+ const locLabel = locatorCode(el)
392
483
  row.setAttribute('aria-label', locLabel ?? `${el.type ?? 'unknown'} (no locator)`)
393
484
 
394
485
  const badge = document.createElement('span')
@@ -440,6 +531,11 @@ class ElementsPane {
440
531
  e.preventDefault()
441
532
  this.#onClickCb?.(el.index)
442
533
  }
534
+ const neighbour = { ArrowUp: row.previousElementSibling, ArrowDown: row.nextElementSibling }[e.key]
535
+ if (neighbour) {
536
+ e.preventDefault()
537
+ this.#onClickCb?.(Number(neighbour.dataset.index))
538
+ }
443
539
  })
444
540
  return row
445
541
  }
@@ -448,7 +544,7 @@ class ElementsPane {
448
544
  // ---- Inspector ----
449
545
  // Orchestrates device management, the refresh cycle, and shared selection/visibility state.
450
546
 
451
- class Inspector {
547
+ export class Inspector {
452
548
  #state = {
453
549
  devices: [],
454
550
  activeId: null,
@@ -462,37 +558,60 @@ class Inspector {
462
558
  // Pruned on each refresh to keys present in the new element list.
463
559
  #userOverrides = new Map()
464
560
  #autoRefreshTimer = null
561
+ // Etag of the payload on screen. Cleared whenever the screen stops showing it, so a 304 never
562
+ // leaves a placeholder or another device's state up.
563
+ #shownEtag = null
465
564
  #tickInFlight = false
466
565
  #refreshInFlight = false
467
566
  #connectInFlight = false
468
567
  #consecutiveErrors = 0
469
568
  static #MAX_CONSECUTIVE_ERRORS = 3
470
-
471
- #screenshotPane = new ScreenshotPane()
472
- #elementsPane = new ElementsPane()
473
- #detailPane = new DetailPane()
569
+ static #MIN_FRAME_MS = 100
570
+ // Continuous mode has no refresh controls, so the device list is polled on its own to pick up
571
+ // devices connected after the page opened (and disconnects between frames).
572
+ static #DEVICE_LIST_REFRESH_MS = 5000
573
+
574
+ #screenshotPane
575
+ #elementsPane
576
+ #detailPane
474
577
  #deviceSelect = document.getElementById('device-select')
475
578
  #refreshBtn = document.getElementById('refresh-btn')
476
579
  #autoRefreshToggle = document.getElementById('auto-refresh-toggle')
477
580
  #autoRefreshInterval = document.getElementById('auto-refresh-interval')
478
581
  #statusBar = document.getElementById('status-bar')
479
582
 
480
- constructor() {
583
+ #onActiveDeviceChangeCb
584
+ #isContinuousRefresh
585
+
586
+ // Panes are injectable so pages without an element list (codegen) can pass no-op stand-ins.
587
+ // onActiveDeviceChange(device | null) fires whenever the active device may have changed; it must be idempotent.
588
+ // continuousRefresh: request the next screenshot as soon as the previous one arrives, forever,
589
+ // for pages without the refresh controls (codegen).
590
+ constructor({ screenshotPane = new ScreenshotPane(), elementsPane = new ElementsPane(), detailPane = new DetailPane(), onActiveDeviceChange = null, continuousRefresh = false } = {}) {
591
+ this.#isContinuousRefresh = continuousRefresh
592
+ this.#screenshotPane = screenshotPane
593
+ this.#elementsPane = elementsPane
594
+ this.#detailPane = detailPane
595
+ this.#onActiveDeviceChangeCb = onActiveDeviceChange
481
596
  this.#screenshotPane.onElementClick(i => this.#selectElement(i))
482
597
  this.#elementsPane.onElementClick(i => this.#selectElement(i))
483
598
  this.#elementsPane.onToggleHidden(i => this.#toggleHidden(i))
484
599
  this.#detailPane.onClose(() => { this.#state.selectedIndex = null; this.#screenshotPane.setSelectedIndex(null); this.#elementsPane.setSelectedIndex(null) })
485
600
 
486
- this.#refreshBtn.addEventListener('click', () => this.refresh())
601
+ this.#refreshBtn?.addEventListener('click', () => this.refresh())
487
602
  this.#deviceSelect.addEventListener('change', () => {
488
603
  const opt = this.#deviceSelect.selectedOptions[0]
489
604
  if (!opt?.value) return
490
605
  const device = this.#state.devices.find(d => d.id === opt.value)
491
606
  if (device) this.#connectDevice(device)
492
607
  })
493
- this.#autoRefreshToggle.addEventListener('change', () => this.#applyAutoRefresh())
494
- this.#autoRefreshInterval.addEventListener('change', () => this.#applyAutoRefresh())
608
+ this.#autoRefreshToggle?.addEventListener('change', () => this.#applyAutoRefresh())
609
+ this.#autoRefreshInterval?.addEventListener('change', () => this.#applyAutoRefresh())
495
610
 
611
+ if (continuousRefresh) {
612
+ this.#refreshContinuously()
613
+ setInterval(() => this.#fetchDevices().catch(() => {}), Inspector.#DEVICE_LIST_REFRESH_MS)
614
+ }
496
615
  this.#loadDevices()
497
616
  }
498
617
 
@@ -500,16 +619,28 @@ class Inspector {
500
619
  if (!this.#state.activeId) return
501
620
  if (this.#refreshInFlight) return
502
621
  this.#refreshInFlight = true
503
- this.#refreshBtn.disabled = true
622
+ this.#setRefreshButtonDisabled(true)
504
623
 
505
624
  try {
506
- const res = await fetch('/api/inspect')
625
+ const query = new URLSearchParams({ scale: this.#screenshotPane.preferredScreenshotScale() })
626
+ if (this.#shownEtag) {
627
+ query.set('etag', this.#shownEtag)
628
+ }
629
+ const res = await fetch(`/api/inspect?${query}`)
507
630
  if (res.status === 503) return // another inspect in flight, skip this tick — no state changed yet
631
+ if (res.status === 304) {
632
+ // Screenshot, elements and screen are exactly what is already shown; keep hover and selection.
633
+ // Still reset the status, which may show an earlier failed refresh.
634
+ this.#setStatus(`${this.#state.elements.length} elements`)
635
+ this.#consecutiveErrors = 0
636
+ return
637
+ }
508
638
  this.#setStatus('Loading...', 'loading')
509
639
  if (this.#screenshotPane.isScreenshotHidden) {
510
640
  this.#screenshotPane.showPlaceholder('Loading screenshot...', '', true)
511
641
  }
512
642
  if (res.status === 409) {
643
+ this.#shownEtag = null
513
644
  this.#state.activeId = null
514
645
  this.#state.elements = []
515
646
  this.#state.hiddenIndices = new Set()
@@ -542,10 +673,11 @@ class Inspector {
542
673
 
543
674
  this.#state.hiddenIndices = this.#computeHiddenIndices()
544
675
 
545
- this.#screenshotPane.showScreenshot(data.screenshot, this.#state.logicalWidth, this.#state.logicalHeight)
676
+ this.#screenshotPane.showScreenshot(data.screenshot, this.#state.logicalWidth, this.#state.logicalHeight, data.screen?.scale ?? 0)
546
677
  this.#elementsPane.render(this.#state.elements, this.#state.hiddenIndices)
547
678
  this.#screenshotPane.renderHighlights(this.#state.elements, this.#state.hiddenIndices, null)
548
679
  this.#setStatus(`${this.#state.elements.length} elements`)
680
+ this.#shownEtag = data.etag ?? null
549
681
  this.#consecutiveErrors = 0
550
682
  } catch (err) {
551
683
  this.#consecutiveErrors++
@@ -553,13 +685,14 @@ class Inspector {
553
685
  if (this.#screenshotPane.isScreenshotHidden) {
554
686
  this.#screenshotPane.showPlaceholder('Could not load screenshot', err.message)
555
687
  }
556
- if (this.#consecutiveErrors >= Inspector.#MAX_CONSECUTIVE_ERRORS) {
688
+ // A fixed refresh has no toggle to turn it back on, so it keeps retrying instead.
689
+ if (!this.#isContinuousRefresh && this.#consecutiveErrors >= Inspector.#MAX_CONSECUTIVE_ERRORS) {
557
690
  this.#stopAutoRefresh()
558
691
  this.#setStatus(`Auto-refresh stopped after ${this.#consecutiveErrors} consecutive failures`, 'error')
559
692
  }
560
693
  } finally {
561
694
  this.#refreshInFlight = false
562
- this.#refreshBtn.disabled = false
695
+ this.#setRefreshButtonDisabled(false)
563
696
  }
564
697
  }
565
698
 
@@ -584,6 +717,7 @@ class Inspector {
584
717
  if (this.#state.activeId !== prevActiveId) this.#userOverrides.clear()
585
718
  this.#renderDevicePicker()
586
719
  if (prevActiveId && !this.#state.activeId) {
720
+ this.#shownEtag = null
587
721
  this.#state.elements = []
588
722
  this.#state.selectedIndex = null
589
723
  this.#state.hiddenIndices = new Set()
@@ -594,6 +728,8 @@ class Inspector {
594
728
  }
595
729
 
596
730
  #renderDevicePicker() {
731
+ // Every change of activeId re-renders the picker, so this is the one place to report it.
732
+ this.#onActiveDeviceChangeCb?.(this.#state.devices.find(d => d.id === this.#state.activeId) ?? null)
597
733
  const currentIds = [...this.#deviceSelect.options].filter(o => o.value).map(o => o.value)
598
734
  const newIds = this.#state.devices.map(d => d.id)
599
735
  const sameList = currentIds.length === newIds.length && currentIds.every((id, i) => id === newIds[i])
@@ -629,14 +765,16 @@ class Inspector {
629
765
  if (this.#connectInFlight) return
630
766
  this.#connectInFlight = true
631
767
  this.#setStatus('Connecting...', 'loading')
768
+ this.#shownEtag = null
632
769
  this.#screenshotPane.showPlaceholder('Connecting...', device.name ?? device.id, true)
633
- this.#refreshBtn.disabled = true
770
+ this.#setRefreshButtonDisabled(true)
634
771
  this.#deviceSelect.disabled = true
635
772
  try {
636
- const res = await fetch(`/api/devices/${encodeURIComponent(device.id)}/select`, {
773
+ // The server only accepts JSON POSTs (it keeps other sites from driving the device), hence the empty body.
774
+ const res = await fetch(`/api/devices/select?${new URLSearchParams({ device: device.id })}`, {
637
775
  method: 'POST',
638
776
  headers: { 'Content-Type': 'application/json' },
639
- body: JSON.stringify({ platform: device.platform }),
777
+ body: '{}',
640
778
  })
641
779
  if (!res.ok) {
642
780
  const err = await res.json()
@@ -653,7 +791,7 @@ class Inspector {
653
791
  } finally {
654
792
  this.#connectInFlight = false
655
793
  this.#deviceSelect.disabled = false
656
- this.#refreshBtn.disabled = false
794
+ this.#setRefreshButtonDisabled(false)
657
795
  }
658
796
  }
659
797
 
@@ -705,17 +843,39 @@ class Inspector {
705
843
  this.#autoRefreshInterval.disabled = !this.#autoRefreshToggle.checked
706
844
  this.#consecutiveErrors = 0 // reset when user manually reconfigures auto-refresh
707
845
  if (this.#autoRefreshToggle.checked) {
708
- const ms = Number(this.#autoRefreshInterval.value)
709
- this.#autoRefreshTimer = setInterval(async () => {
710
- if (this.#tickInFlight) return
711
- this.#tickInFlight = true
712
- try {
713
- await this.#fetchDevices().catch(() => {})
714
- await this.refresh()
715
- } finally {
716
- this.#tickInFlight = false
717
- }
718
- }, ms)
846
+ this.#startRefreshTimer(Number(this.#autoRefreshInterval.value))
847
+ }
848
+ }
849
+
850
+ // Back-to-back refreshes. A refresh that returns quickly (no device, error, another one
851
+ // in flight) is padded to MIN_FRAME_MS so the loop never spins.
852
+ async #refreshContinuously() {
853
+ for (;;) {
854
+ const startedAt = performance.now()
855
+ await this.refresh()
856
+ const remainingMs = Inspector.#MIN_FRAME_MS - (performance.now() - startedAt)
857
+ if (remainingMs > 0) {
858
+ await new Promise(resolve => setTimeout(resolve, remainingMs))
859
+ }
860
+ }
861
+ }
862
+
863
+ #startRefreshTimer(ms) {
864
+ this.#autoRefreshTimer = setInterval(async () => {
865
+ if (this.#tickInFlight) return
866
+ this.#tickInFlight = true
867
+ try {
868
+ await this.#fetchDevices().catch(() => {})
869
+ await this.refresh()
870
+ } finally {
871
+ this.#tickInFlight = false
872
+ }
873
+ }, ms)
874
+ }
875
+
876
+ #setRefreshButtonDisabled(isDisabled) {
877
+ if (this.#refreshBtn) {
878
+ this.#refreshBtn.disabled = isDisabled
719
879
  }
720
880
  }
721
881
 
@@ -735,7 +895,7 @@ class Inspector {
735
895
 
736
896
  // ---- Theme ----
737
897
 
738
- function applyTheme(name) {
898
+ export function applyTheme(name) {
739
899
  document.documentElement.setAttribute('data-theme', name)
740
900
  localStorage.setItem('mobilewright-inspector-theme', name)
741
901
  const sel = document.getElementById('theme-select')
@@ -744,7 +904,49 @@ function applyTheme(name) {
744
904
 
745
905
  document.getElementById('theme-select')?.addEventListener('change', e => applyTheme(e.target.value))
746
906
 
747
- // ---- Bootstrap ----
907
+ // ---- Resizable panes ----
908
+
909
+ const KEYBOARD_RESIZE_STEP = 16
910
+
911
+ // direction: 1 when the pane is left of its splitter, -1 when right of it.
912
+ // CSS min/max-width on the pane does the clamping.
913
+ // axis: 'x' resizes the pane's width (vertical splitter), 'y' its height (horizontal splitter).
914
+ export function makeResizable(splitter, pane, cssVar, direction, axis = 'x') {
915
+ const main = splitter.parentElement
916
+ const storageKey = 'mobilewright-inspector' + cssVar
917
+ const isVertical = axis === 'y'
918
+ const paneSize = () => isVertical ? pane.offsetHeight : pane.offsetWidth
919
+ const pointerPos = ev => isVertical ? ev.clientY : ev.clientX
920
+ const setWidth = px => {
921
+ main.style.setProperty(cssVar, px + 'px')
922
+ localStorage.setItem(storageKey, paneSize())
923
+ }
748
924
 
749
- applyTheme(localStorage.getItem('mobilewright-inspector-theme') || 'void')
750
- new Inspector()
925
+ const saved = localStorage.getItem(storageKey)
926
+ if (saved) {
927
+ main.style.setProperty(cssVar, saved + 'px')
928
+ }
929
+
930
+ splitter.addEventListener('pointerdown', e => {
931
+ e.preventDefault()
932
+ splitter.setPointerCapture(e.pointerId)
933
+ splitter.classList.add('dragging')
934
+ const startPos = pointerPos(e)
935
+ const startSize = paneSize()
936
+ const onMove = ev => setWidth(startSize + (pointerPos(ev) - startPos) * direction)
937
+ splitter.addEventListener('pointermove', onMove)
938
+ splitter.addEventListener('lostpointercapture', () => {
939
+ splitter.removeEventListener('pointermove', onMove)
940
+ splitter.classList.remove('dragging')
941
+ }, { once: true })
942
+ })
943
+
944
+ splitter.addEventListener('keydown', e => {
945
+ const keys = isVertical ? { ArrowUp: -KEYBOARD_RESIZE_STEP, ArrowDown: KEYBOARD_RESIZE_STEP } : { ArrowLeft: -KEYBOARD_RESIZE_STEP, ArrowRight: KEYBOARD_RESIZE_STEP }
946
+ const step = keys[e.key]
947
+ if (step) {
948
+ e.preventDefault()
949
+ setWidth(paneSize() + step * direction)
950
+ }
951
+ })
952
+ }