Skip to content

fix(mobile): render correctly in Android multi-window density mismatch - #20853

Closed
jetd1 wants to merge 1 commit into
stablyai:mainfrom
jetd1:fix/mobile-multiwindow-density
Closed

jetd1 wants to merge 1 commit into
stablyai:mainfrom
jetd1:fix/mobile-multiwindow-density

Conversation

@jetd1

@jetd1 jetd1 commented Sep 15, 2026 •

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 355f4587-1677-4b19-a7a9-6211cf3e587e

📥 Commits

Reviewing files that changed from the base of the PR and between 7ec681f970fae362da3c9c484050ea56b6f01697 and 6eadbe8.

📒 Files selected for processing (9)
  • mobile/app.json
  • mobile/plugins/android-multiwindow-metrics.js
  • mobile/plugins/android-multiwindow-metrics.test.js
  • mobile/pnpm-workspace.yaml
  • mobile/src/terminal/TerminalWebView.tsx
  • mobile/src/terminal/terminal-webview-html/host-message-router.ts
  • mobile/src/terminal/terminal-webview-messages.ts
  • mobile/src/terminal/terminal-webview-payload-hash.test.ts
  • mobile/vitest.config.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The mobile app registers an Android multi-window metrics plugin. The plugin updates activity configuration and injects Kotlin display-metric handling into MainActivity. Terminal measurement messages now include the React Native pixel ratio, and WebView measurement converts container height to CSS pixels. The app pins react-native-screens to 4.26.2 and applies a type-only patch. Plugin tests are included in Vitest.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 6eadb

No merge-blocking behavior is identified in the Android metrics, terminal scaling, or dependency updates.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 7 files. (2 skipped: 2 … Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The pull request description contains only a hyphen. It omits the required ELI5, change summary, rationale, linked issue, visual proof, testing, review, notes, and checklist information. Complete the repository template. Describe the change and reason, provide a linked issue, add visual proof or state N/A with a reason, document manual and automated testing, complete the required checklist, and address the remaining templat…
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the main change: fixing Android multi-window density mismatches in the mobile application.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 7 files. (2 skipped: 2 unsupported.)

Full details: Description check

Resolution

Complete the repository template. Describe the change and reason, provide a linked issue, add visual proof or state N/A with a reason, document manual and automated testing, complete the required checklist, and address the remaining template sections.

  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

mobile/app.json fails oxfmt --check, so the Mobile Checks format:check step (oxfmt --check .) will fail on this PR. Revert the formatting-only edits (or run pnpm format in mobile/).

Reviewed changes

  • react-native-screens pin + patch — pins the dependency to exactly 4.26.2 and patches StackHeaderConfigAndroidNativeComponent.ts so RN 0.83.10 codegen accepts the command's viewRef type.
  • Multi-window metrics plugin — new mobile/plugins/android-multiwindow-metrics.js reseeds both DisplayMetricsHolder holders from the activity's windowed resources.displayMetrics on create/resume/configuration change, requests a decor relayout, emits didUpdateDimensions, and adds density to the activity configChanges.
  • Registration + lockfile — plugin registered in app.json; patchedDependencies and the lockfile patch_hash updated to match.

ℹ️ Exported plugin transforms have no tests

injectDisplayMetrics and applyMultiWindowMetricsManifest are exported specifically for testing, but no test accompanies them. They do fragile string surgery (import insertion, super.onCreate(null) insertion, lastIndexOf('\n}') class-end detection) against an Expo template this repo does not pin, and no CI path runs expo prebuild — so template drift is only caught at Android release-build time (the plugin throws, which is at least loud). The sibling android-respect-rotation-lock.js is a single manifest write; this one is materially more logic.

Technical details
# Add unit tests for the multi-window metrics plugin

## Affected sites
- `mobile/plugins/android-multiwindow-metrics.js:55-84` — `injectDisplayMetrics` string surgery
- `mobile/plugins/android-multiwindow-metrics.js:86-95` — `applyMultiWindowMetricsManifest`
- `mobile/vitest.config.ts` — `include` only collects `src/**/*.test.ts(x)`, so a test must live under `src/` (or the include extended)

## Required outcome
- Cover the pure transforms with a small fixture resembling the Expo SDK 55 `MainActivity.kt`: assert idempotency (the `ORCA_MULTIWINDOW_METRICS` marker short-circuits), that both imports and both methods land inside the class, that `applyActivityDisplayMetrics()` precedes `super.onCreate(null)`, and that `density` is appended to (and not duplicated in) `configChanges`.
- Assert the throw paths for a missing `super.onCreate(null)` and a non-Kotlin MainActivity.

## Open questions for the human
- Is there a reason plugin tests are intentionally omitted (e.g. no prebuild harness), or was this just not done?

ℹ️ Nitpicks

  • The reason for the exact [email protected] pin and its patch exists only in the PR description. pnpm-workspace.yaml's patchedDependencies key and the codegen constraint mean the next bump fails loudly, but a one-line note next to the pin (or a tracking issue for RN/react-native-screens fixing the codegen parser) would save the next maintainer the investigation. Upstream [email protected] still emits React.ComponentRef, so this is not a "bump fixes it" situation.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Comment thread mobile/app.json Resolved
On Samsung One UI, split-window panes are assigned a density different
from fullscreen (e.g. 321 vs 420 dpi). Three measurement paths in the
app convert with a density different from the one Fabric/the WebView
actually paint at:

1. react-native-screens FabricEnabledViewGroup.updateState converted
   the measured pane px back to dp with the process-global main-display
   density captured at process start, collapsing all ScreenStack
   content into a top-left box of pane*density-ratio (upstream
   software-mansion/react-native-screens#4159, fixed in 4.26.0). Pin
   react-native-screens to 4.26.2 - the first fix line that remains
   compatible with react-native 0.83 codegen (4.27+ needs newer
   codegen), plus a pnpm patch respelling one React.ComponentRef
   command argument that the 0.83.10 codegen parser rejects.
2. RN's process-global DisplayMetricsHolder stays frozen at process
   start (seeded once from the Application context, so activity
   recreation does not help). Text measurement (PixelUtil.pxToDp) and
   all other PixelUtil conversions then disagree with the mount density
   by exactly the mismatch ratio, so text is laid out into boxes that
   are the wrong fraction of the glyphs - clipped lines, cut
   descenders. Add an android-multiwindow-metrics config plugin that
   re-seeds BOTH holders from the windowed resources on
   create/resume/configurationChange, emits didUpdateDimensions,
   requests a decorView relayout, and adds density to configChanges so
   the switch reaches the hook without activity recreation. Unlike the
   DeX variant in stablyai#16018, the screen holder also takes windowed
   metrics, which the text pipeline requires.
3. The Android WebView pins its CSS-px-at-construction density to the
   display (Chrome-side; activity recreation does not change it), while
   the terminal fit protocol passes an RN-dp container height the web
   side consumed as CSS px. xterm then overallocated rows by the
   density ratio and its bottom rows sat below the visible viewport -
   the terminal could not scroll to the last lines in split view. Pass
   rnPixelRatio alongside containerHeight in the measure message and
   convert into the WebView's CSS px in measureFitDimensions; bump the
   terminal payload hash canary accordingly.

The plugin's exported transforms are unit-tested (idempotent
injection, hook ordering before super.onCreate(null), density append
without duplication, throw paths) under a new plugins/**/*.test.js
vitest include.

Verified on Samsung foldable hardware (One UI, Android 16): layout and
text fill the pane correctly in split panes and pop-up windows across
recreation-free fullscreen<->split transitions, and the terminal
viewport reaches its final buffer rows in split view.

Co-Authored-By: CCCha <[email protected]> via Claude Code
@jetd1
jetd1 force-pushed the fix/mobile-multiwindow-density branch from 7ec681f to 6eadbe8 Compare September 16, 2026 03:03

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — the prior app.json formatting blocker is fixed and the new density conversion is dimensionally correct. One coverage gap inline.

Reviewed changes — the delta since the prior Pullfrog review (7ec681f970), focused on the app.json formatting revert and the new WebView density conversion:

  • Reverted the app.json formatting churn — only the plugin registration entry (app.json:85) remains; oxfmt --check now passes on the file.
  • Added plugin transform tests — mobile/plugins/android-multiwindow-metrics.test.js covers idempotent injection, applyActivityDisplayMetrics() preceding super.onCreate(null), configChanges append/dedup, and the missing-anchor throw; vitest.config.ts:16 now collects plugins/**/*.test.js.
  • Documented the exact [email protected] pin + patch rationale above patchedDependencies in pnpm-workspace.yaml.
  • Added a WebView density conversion — TerminalWebView.tsx:319 sends rnPixelRatio: PixelRatio.get() with measure, and measureFitDimensions converts the RN-dp frame height into the WebView's own CSS px before computing rows; the payload golden hash/length were updated.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Comment on lines +100 to +102
if (typeof rnPixelRatioInjected === 'number' && rnPixelRatioInjected > 0 && containerHeightPx > 0 && window.devicePixelRatio > 0) {
vpHeight = containerHeightPx * (rnPixelRatioInjected / window.devicePixelRatio);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new conversion is the only new logic in this commit and it has no unit coverage — the payload-hash test only proves the emitted document changed, not that the ratio is right. The repo already executes injected WebView JS in tests (terminal-webview-engine.test.ts via new Function, plus happy-dom suites), so a case with rnPixelRatio !== devicePixelRatio would pin the math against a future inversion or unit regression.

Technical details
# Test the density conversion in the measure path

## Affected sites
- `mobile/src/terminal/terminal-webview-html/host-message-router.ts:100-102` — RN dp → WebView CSS px conversion, untested
- `mobile/src/terminal/terminal-webview-payload-hash.test.ts:9-10` — golden hash/length only proves the document text changed

## Required outcome
- Exercise `measureFitDimensions` (directly or via a `measure` message) with `rnPixelRatio !== window.devicePixelRatio` and assert `rows === Math.max(8, Math.floor(containerHeight * rnPixelRatio / devicePixelRatio / cellHeight))`.
- Assert the identity case (`rnPixelRatio === devicePixelRatio`) leaves the pre-existing `rows` calculation unchanged.

## Suggested approach
- Follow the harness pattern in `mobile/src/terminal/terminal-webview-engine.test.ts` / `terminal-webview-query-reply.test.ts` (`new Function` over the injected fragment) with a stub `term._core._renderService.dimensions` and a settable `window.devicePixelRatio`.

## Open questions for the human
- Is the conversion intentionally left untested because the injected-JS harness is too costly, or was this just not done?

@Nelef

Nelef commented Oct 30, 2026

Copy link
Copy Markdown

Hi @jetd1, I tested this on a Samsung Galaxy Z Fold7 (One UI 9.0, Android 17) and wrote it up in #36305 with before/after videos.

On One UI, entering and leaving split screen changes the window density (360 → 321 dpi here). With only density in configChanges, the activity survives, but the split pane is laid out too small and button labels clip. Applying just this PR's Android plugin (android-multiwindow-metrics.js and its app.json entry) to current main (46237aa) fixed all of it on that device: no relaunch, the pane fills, full text.

This branch no longer merges cleanly with main (the terminal WebView files moved since September). If it helps, I'm happy to rebase it, or to open a focused PR with only the plugin and its test, credited to you. Whichever you and the maintainers prefer.

@AmoabaKelvin

Copy link
Copy Markdown
Collaborator

Thanks for this! Your plugin pointed the way. #36977 lands the split-screen fix on current main, building on the same re-seeding approach.

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.

3 participants