Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .changeset/displays-trace-unification.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
BREAKING (beta): the browser `screen_size` trace and the Electron `electron_displays` trace are retired and replaced by `displays`, the mobile-layout list (a bare array of display objects, snake_case keys).

- Browser: one entry `{id: 0, state: "on", width, height, scale, rotation?}` with `width`/`height` in device pixels (`round(screen.width * devicePixelRatio)`), `scale` the ratio used and `rotation` the screen-orientation angle in quarter turns. Approximate under Chromium page zoom. Not written by WebViews or Electron renderers (the host writes it): new `hostOwnedTraces` browser launch option, set by `launchRenderer` and the WebView launch.
- Electron main: one entry per display (`id` stable per-session index with the primary first, `native_id`, `state` on the primary, `type`, device-pixel `width`/`height`, `natural_width`/`natural_height`, `scale`, `max_fps`, `rotation` in quarter turns).
- Electron main: one entry per display (`id` numbered from the key window's display (0) then ascending Electron id, `native_id`, `state` on id 0, `type`, device-pixel `width`/`height`, `natural_width`/`natural_height`, `scale`, `max_fps`, `rotation` in quarter turns).
- `display_added`, `display_removed` and `display_metrics_changed` event params are now the same keys in camelCase: `id`, `nativeId`, `width`, `height`, `naturalWidth`, `naturalHeight`, `scale`, `maxFps`, `rotation`, `type` (plus `changed`). The old `scaleFactor`, `internal` and `refreshRate` params are gone, and `width`/`height` are device pixels rather than DIP.
- Electron sessions need `screen`: renderers never write `displays` and main writes it only when `screen` is passed to `launchMain` (after app ready), so without `screen` there is no display trace at all.
- Display `id` numbering restarts at 0 (primary first) for each session. Browser `scale` is rounded to 2 decimals. `rotation` direction is unverified on a real rotated display (Electron documents clockwise degrees; the field means Android's `Surface.ROTATION_*`).
- Display `id` is recomputed on every read (the key window's display is 0; the primary display when there is no window). Browser `scale` is rounded to 2 decimals. `rotation` direction is unverified on a real rotated display (Electron documents clockwise degrees; the field means Android's `Surface.ROTATION_*`).
- `@bugsee/core`: `hostOwnedTraces` is kept across `relaunch()` (`RELAUNCH_KEPT_OPTIONS`).
- `@bugsee/trackers`: the Electron display tracker's `DisplayInfo` has the new shape (`mapDisplay` returns `MappedDisplay`, now exported from `@bugsee/trackers/electron`).
7 changes: 7 additions & 0 deletions .changeset/displays-zoom-key-window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@bugsee/browser': minor
'@bugsee/electron': minor
'@bugsee/trackers': minor
---

Browser `displays` now carries a `zoom` field and recovers the physical size from it, but only when the page zoom can be trusted: Chromium, the top-level window, a pointer-fine device, and `outerWidth / innerWidth` within 1.5% of a real browser zoom preset (it is snapped to the preset, so the value does not flap). Otherwise `zoom` is omitted and `width`/`height`/`scale` use `devicePixelRatio` alone, as before. That is the case with a docked DevTools or side panel (the common developer setup), a custom zoom step, Firefox and Safari. Changes are written after 250 ms of quiet, so a window maximize animation does not write a burst of values. Electron `displays` ids follow the key window: id 0 is the display showing the focused window (else the most recently focused, else the first visible/created), the rest ascend by Electron id, `state: "on"` marks id 0, and the list is re-written when focus or a window move changes that display (moves are debounced 200 ms). `display_*` event ids are positions in the current list; correlate by `nativeId`. With `screen` passed, the displays trace keeps focus and move listeners on `app` and on each window even with `captureWindowEvents: false`.
1 change: 1 addition & 0 deletions docs/PROGRESS.md

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions packages/browser/src/launch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ describe('launch', () => {
'tok',
baseOptions({ window: w.win, captureStore: store }),
);
expect(m.thisSeen).toHaveLength(11); // dark: baseline read + watch (2); display mode: 3 queries x (baseline read + watch + previous-mode read) (9)
expect(m.thisSeen).toHaveLength(12); // dark: baseline read + watch (2); display mode: 3 queries x (baseline read + watch + previous-mode read) (9); displays: the (pointer: coarse) watch (1)
expect(m.thisSeen.every((t) => t === w.win)).toBe(true); // always called as a method of ITS window
for (const f of [...m.l]) f({ matches: true });
expect((await names(store)).filter((e) => e.name === 'DarkModeChanged')).toEqual([
Expand Down Expand Up @@ -2122,9 +2122,19 @@ describe('launch', () => {
const w = fakeWindow();
Object.assign(w.win, { devicePixelRatio: 2 });
const store = memStore();
await launchTracked('tok', noSampler({ window: w.win, captureStore: store }));
const settle: Array<() => void> = [];
const scheduler = {
setInterval: (cb: () => void, ms: number) => {
if (ms === 250) settle.push(cb); // the displays settle timer (never cleared in this test)
return settle.length;
},
clearInterval: () => {},
};
await launchTracked('tok', noSampler({ window: w.win, captureStore: store, scheduler }));
Object.assign(w.win, { devicePixelRatio: 3 });
w.emit('resize', {});
expect(settle).toHaveLength(1); // settled, not written immediately
settle[0]?.();
expect(await rowsOf(store, 'displays')).toEqual([
[{ id: 0, state: 'on', width: 780, height: 1688, scale: 2 }],
[{ id: 0, state: 'on', width: 1170, height: 2532, scale: 3 }],
Expand Down
157 changes: 153 additions & 4 deletions packages/browser/src/trace-producers.screen-lag.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TraceProducer } from '@bugsee/capture';
import { ApplicationTracker, ProcessTracker, UiTracker } from '@bugsee/trackers/browser';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import {
browserTraceEnvFrom,
createBrowserSystemTraceProducers,
Expand Down Expand Up @@ -31,6 +31,33 @@ const sinkOf = (out: Emit[]) => ({
});
const names = (ps: TraceProducer[]) => ps.map((p) => p.name);

/** A manual scheduler: the producer's settle timer fires only when `advance` covers it. */
function manualScheduler() {
let now = 0;
let next = 1;
const timers = new Map<number, { at: number; cb: () => void }>();
return {
setInterval: (cb: () => void, ms: number) => {
const id = next++;
timers.set(id, { at: now + ms, cb });
return id;
},
clearInterval: (id: unknown) => void timers.delete(id as number),
advance: (ms: number) => {
now += ms;
for (const [id, t] of [...timers]) {
if (t.at <= now) {
timers.delete(id);
t.cb();
}
}
},
pending: () => timers.size,
};
}
const chromeWin = (extra: Record<string, unknown> = {}) =>
Object.assign(mkTarget(), { chrome: {}, ...extra });

describe('DisplaysProducer', () => {
const mk = (screen: unknown = { width: 390, height: 844 }) => {
const win = Object.assign(mkTarget(), { devicePixelRatio: 2 });
Expand All @@ -41,7 +68,8 @@ describe('DisplaysProducer', () => {
it('emits the one-entry mobile list at start (device pixels, scale, state on) as a HELD step trace, then follows resize, de-duplicated', () => {
const { win, tracker } = mk();
const out: Emit[] = [];
new DisplaysProducer(tracker).start(sinkOf(out));
const sched = manualScheduler();
new DisplaysProducer(tracker, sched).start(sinkOf(out));
expect(out).toEqual([
{
name: 'displays',
Expand All @@ -50,16 +78,125 @@ describe('DisplaysProducer', () => {
},
]);
win.fire('resize'); // nothing moved
sched.advance(1000);
expect(out).toHaveLength(1);
win.devicePixelRatio = 3;
win.fire('resize');
win.fire('orientationchange');
sched.advance(250);
expect(out.map((o) => o.value)).toEqual([
[{ id: 0, state: 'on', width: 780, height: 1688, scale: 2 }],
[{ id: 0, state: 'on', width: 1170, height: 2532, scale: 3 }],
]);
});

it('trusted page zoom: width/height use screen x dpr / zoom (no drift across zoom levels) and zoom is written', () => {
// Chrome, device scale 2, screen 1440x900: 125% then 100% then 200%: the physical size never moves
const win = chromeWin({ devicePixelRatio: 2.5, outerWidth: 1440, innerWidth: 1152 });
const out: Emit[] = [];
const sched = manualScheduler();
new DisplaysProducer(
new UiTracker({ window: win, screen: { width: 1440, height: 900 } as never }),
sched,
).start(sinkOf(out));
expect(out[0]?.value).toEqual([
{ id: 0, state: 'on', width: 2880, height: 1800, scale: 2, zoom: 1.25 },
]);
Object.assign(win, { devicePixelRatio: 2, innerWidth: 1440 });
win.fire('resize');
sched.advance(250);
Object.assign(win, { devicePixelRatio: 4, innerWidth: 720 });
win.fire('resize');
sched.advance(250);
expect(out.map((o) => o.value)).toEqual([
[{ id: 0, state: 'on', width: 2880, height: 1800, scale: 2, zoom: 1.25 }],
[{ id: 0, state: 'on', width: 2880, height: 1800, scale: 2, zoom: 1 }],
[{ id: 0, state: 'on', width: 2880, height: 1800, scale: 2, zoom: 2 }],
]);
});

it('untrusted zoom (docked DevTools): dpr-only size, no zoom key (strict: not even undefined)', () => {
// 1200 window, innerWidth 645 at 100% zoom, dpr 2, screen 1920x1080: the old exact value
const win = chromeWin({ devicePixelRatio: 2, outerWidth: 1200, innerWidth: 645 });
const out: Emit[] = [];
new DisplaysProducer(
new UiTracker({ window: win, screen: { width: 1920, height: 1080 } as never }),
).start(sinkOf(out));
expect(out[0]?.value).toStrictEqual([
{ id: 0, state: 'on', width: 3840, height: 2160, scale: 2 },
]);
});

it('a Safari-like (non-Chromium) window keeps dpr-only sizes and omits zoom', () => {
const win = Object.assign(mkTarget(), {
devicePixelRatio: 2,
outerWidth: 1600,
innerWidth: 1280,
navigator: { userAgentData: undefined },
});
const out: Emit[] = [];
new DisplaysProducer(
new UiTracker({ window: win, screen: { width: 1600, height: 900 } as never }),
).start(sinkOf(out));
expect(out[0]?.value).toStrictEqual([
{ id: 0, state: 'on', width: 3200, height: 1800, scale: 2 },
]);
});

it('SETTLES changes: a burst of out-of-step resize values (maximize animation) writes ONE value after 250 ms quiet; the first value is immediate', () => {
const win = chromeWin({ devicePixelRatio: 2, outerWidth: 1000, innerWidth: 1000 });
const out: Emit[] = [];
const sched = manualScheduler();
new DisplaysProducer(
new UiTracker({ window: win, screen: { width: 1000, height: 500 } as never }),
sched,
).start(sinkOf(out));
expect(out).toHaveLength(1); // immediate
for (const [outer, inner] of [
[1200, 1000],
[1500, 1000],
[1800, 1000],
[2000, 2000],
] as const) {
Object.assign(win, { outerWidth: outer, innerWidth: inner });
win.fire('resize');
sched.advance(100); // each event arrives inside the previous one's window: the timer restarts
}
expect(out).toHaveLength(1);
expect(sched.pending()).toBe(1);
sched.advance(150);
expect(out).toHaveLength(1); // settled to the same value: de-duplicated
Object.assign(win, { devicePixelRatio: 3, outerWidth: 1000, innerWidth: 1000 });
win.fire('resize');
sched.advance(249);
expect(out).toHaveLength(1);
sched.advance(1);
expect(out).toHaveLength(2);
});

it('without a scheduler the global timers are used (a settle is armed and cancelled on stop)', () => {
vi.useFakeTimers();
try {
const win = Object.assign(mkTarget(), { devicePixelRatio: 2 });
const out: Emit[] = [];
const p = new DisplaysProducer(
new UiTracker({ window: win, screen: { width: 100, height: 100 } as never }),
);
p.start(sinkOf(out));
win.devicePixelRatio = 3;
win.fire('resize');
vi.advanceTimersByTime(250);
expect(out).toHaveLength(2);
win.devicePixelRatio = 4;
win.fire('resize');
p.stop();
vi.advanceTimersByTime(1000);
expect(out).toHaveLength(2);
} finally {
vi.useRealTimers();
}
});

it('rounds the device pixels from a fractional ratio and records the ratio used in scale', () => {
const win = Object.assign(mkTarget(), { devicePixelRatio: 1.25 });
const out: Emit[] = [];
Expand Down Expand Up @@ -95,12 +232,15 @@ describe('DisplaysProducer', () => {
it('rounds the zoom-polluted ratio to 2 decimals and de-duplicates on the rounded value', () => {
const win = Object.assign(mkTarget(), { devicePixelRatio: 1.100000023841858 });
const out: Emit[] = [];
const sched = manualScheduler();
new DisplaysProducer(
new UiTracker({ window: win, screen: { width: 1000, height: 1000 } as never }),
sched,
).start(sinkOf(out));
expect(out[0]?.value).toEqual([{ id: 0, state: 'on', width: 1100, height: 1100, scale: 1.1 }]);
win.devicePixelRatio = 1.1;
win.fire('resize');
sched.advance(250);
expect(out).toHaveLength(1);
});

Expand All @@ -109,7 +249,8 @@ describe('DisplaysProducer', () => {
const screen = { width: 390, height: 844, orientation: { type: 'portrait-primary', angle: 0 } };
const out: Emit[] = [];
const tracker = new UiTracker({ window: win, screen: screen as never });
new DisplaysProducer(tracker).start(sinkOf(out));
const sched = manualScheduler();
new DisplaysProducer(tracker, sched).start(sinkOf(out));
const rot = () => (out.at(-1)?.value as Array<{ rotation?: number }>)[0]?.rotation;
expect(rot()).toBe(0);
for (const [angle, turns] of [
Expand All @@ -121,10 +262,12 @@ describe('DisplaysProducer', () => {
] as const) {
screen.orientation.angle = angle;
win.fire('orientationchange');
sched.advance(250);
expect(rot()).toBe(turns);
}
screen.orientation.angle = 45; // not a quarter turn: omitted, not rounded
win.fire('orientationchange');
sched.advance(250);
expect(out.at(-1)?.value).toEqual([{ id: 0, state: 'on', width: 780, height: 1688, scale: 2 }]);
const noAngle: Emit[] = [];
new DisplaysProducer(
Expand Down Expand Up @@ -159,13 +302,19 @@ describe('DisplaysProducer', () => {
it('stop() releases the window listeners and later events write nothing', () => {
const { win, tracker } = mk();
const out: Emit[] = [];
const p = new DisplaysProducer(tracker);
const sched = manualScheduler();
const p = new DisplaysProducer(tracker, sched);
p.start(sinkOf(out));
expect(win.count()).toBeGreaterThan(0);
win.devicePixelRatio = 4;
win.fire('resize'); // a settle is pending
expect(sched.pending()).toBe(1);
p.stop();
expect(sched.pending()).toBe(0); // the pending write is cancelled
expect(win.count()).toBe(0);
win.devicePixelRatio = 5;
win.fire('resize');
sched.advance(1000);
expect(out).toHaveLength(1);
});
});
Expand Down
Loading