Skip to content

Create the preview's browser at its real size, so it never comes up blank - #15

Merged
vlidholt merged 1 commit into
mainfrom
fix/blank-preview-browser-size
Dec 2, 2026
Merged

vlidholt merged 1 commit into
mainfrom
fix/blank-preview-browser-size

Conversation

@vlidholt

@vlidholt vlidholt commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

The bug

Sometimes a project starts and the preview stays blank while the toolbar says "Running". Pressing Hot Restart brings the app up.

The app was running the whole time, in a browser one pixel wide:

  • A CEF browser starts at 1x1 and scale 1. It was only sized by the first setSize, once the preview surface was mounted.
  • On a Retina Mac that first size also moves the scale from 1 to 2. WasResized() does not deliver a scale change to a page that is already rendering: the page takes the new scale and keeps its old size until its next navigation. That is why Hot Restart, which reloads the page, cured it.
  • Usually the size arrives about 100 ms after creation and wins the race with the renderer. On a project switch it always lost: _ensureWebView clears the profile and reloads before the surface is built, about four seconds later.

Measured in the stuck page: innerWidth 1, devicePixelRatio 2, flutter-view 1x1.

It was not the Flutter tool, DWDS or the runner. In Chrome, reloading at every point of the first connection, hot-restarting mid-load and calling main() early all still brought the app up.

The fix

  • WebViewController.initialize takes initialSize and devicePixelRatio, and the fork creates the browser with them. GetViewRect and GetScreenInfo answer from them while CreateBrowserSync is still running.
  • PreviewPane passes the phone viewport, or on desktop the room under its header (previewInitialSurfaceSize).
  • changeSize calls NotifyScreenInfoChanged() before WasResized() when the scale changes, so a later scale change lands too. This half alone already fixed the project switch.

AGENTS.md records the cause and corrects the earlier note that said it was not found.

Testing

  • Reproduced in a debug build by opening one project and then another, then confirmed the same sequence shows the app with the fix.
  • Also checked live: first open, Full Restart, switching between phone, landscape and desktop, and a browser created in desktop mode.
  • flutter test passes, including the new test/preview/preview_initial_surface_test.dart.

Not verified:

  • Windows. The native change is in the plugin's shared C++ code and was not built or run there.
  • Moving the window between displays with different scales. The changeSize change should cover it, but it was not tested.

🤖 Generated with Claude Code

… up blank.

The preview sometimes came up blank under a toolbar reading "Running", and a
hot restart brought the app back. The app was running the whole time — in a
browser one pixel wide.

A CEF browser starts at 1x1 and scale 1 and was only sized by the first
setSize, once its surface was mounted. On a Retina Mac that first size also
moves the scale from 1 to 2, and WasResized() does not deliver a scale change
to a page that is already rendering: the page takes the new scale and keeps
its old size until its next navigation. Usually the size won the race with
the renderer. On a project switch it always lost, because the profile is
cleared and the page reloaded before the surface is built.

- WebViewController.initialize takes the initial size and device pixel
  ratio, and the fork creates the browser with them.
- PreviewPane passes the phone viewport, or on desktop the room under its
  header.
- changeSize calls NotifyScreenInfoChanged() before WasResized() when the
  scale changes, so a later scale change lands too.

Verified in a debug build with two projects: first open, project switch,
Full Restart, and phone, landscape and desktop modes. The Windows side of
the shared plugin code is not built or tested here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@vlidholt
vlidholt merged commit a09f010 into main Dec 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant