Skip to content

Commit 3b69498

Browse files
satchljclaude
andcommitted
fix(mobile): text no longer clips in Android desktop windows on external displays
React Native fills DisplayMetricsHolder from the application context, which always reports the built-in screen. On Pixel Desktop / Samsung DeX the window is laid out at the external display's density while text is measured at the phone's, so text gets line boxes for the wrong density. Re-point the holder at the activity's display after the root view is created and after configuration changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 98a6246 commit 3b69498

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎apps/mobile/app.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ const config: ExpoConfig = {
465465
"./plugins/withAndroidModernPopupMenu.cjs",
466466
"./plugins/withAndroidModernAlertDialog.cjs",
467467
"./plugins/withAndroidPredictiveBackCompat.cjs",
468+
// Listed before withAndroidTabletOrientation so its MainActivity mod runs
469+
// after that one's and extends the onConfigurationChanged override it adds.
470+
"./plugins/withAndroidWindowDisplayMetrics.cjs",
468471
"./plugins/withAndroidTabletOrientation.cjs",
469472
...(isIosPersonalTeamBuild ? ["./plugins/withoutIosPersonalTeamCapabilities.cjs"] : []),
470473
],
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const { withMainActivity } = require("expo/config-plugins");
2+
3+
// React Native converts dp/sp to pixels (PixelUtil, text measurement) with the
4+
// density in DisplayMetricsHolder, which it fills from the application context:
5+
// `defaultDisplay.getRealMetrics()` there is always the built-in screen. When the
6+
// activity runs on an external display (Pixel Desktop, Samsung DeX), Fabric
7+
// lays the surface out with the window's density but measures text with the
8+
// phone's, so text gets line boxes sized for a different density and is clipped
9+
// and overlapped. Re-point the holder at the activity's own display after React
10+
// Native fills it. The root view's constructor re-reads it from the application
11+
// context, and Expo's delegate builds that view after onCreate returns, so bind
12+
// when the view is handed to setContentView (before the first JS commit can
13+
// measure text), and again after every configuration change, where ReactHost
14+
// re-reads it too. On the built-in screen the two are the same metrics, so this
15+
// is a no-op there.
16+
17+
const BIND_METHOD = `
18+
// Keeps React Native on this window's display density; added by
19+
// withAndroidWindowDisplayMetrics.
20+
@Suppress("DEPRECATION")
21+
private fun bindWindowDisplayMetrics() {
22+
val windowMetrics = resources.displayMetrics
23+
val metrics = DisplayMetrics()
24+
metrics.setTo(windowMetrics)
25+
windowManager.defaultDisplay.getRealMetrics(metrics)
26+
metrics.scaledDensity = windowMetrics.scaledDensity
27+
DisplayMetricsHolder.setScreenDisplayMetrics(metrics)
28+
}
29+
`;
30+
31+
const CONFIGURATION_CHANGED_METHOD = `
32+
// Re-binds after ReactHost re-reads display metrics; added by
33+
// withAndroidWindowDisplayMetrics.
34+
override fun onConfigurationChanged(newConfig: Configuration) {
35+
super.onConfigurationChanged(newConfig)
36+
bindWindowDisplayMetrics()
37+
}
38+
`;
39+
40+
const SET_CONTENT_VIEW_METHOD = `
41+
// React Native's root view re-reads display metrics when it is constructed,
42+
// just before it is set as the content view; added by
43+
// withAndroidWindowDisplayMetrics.
44+
override fun setContentView(view: View?) {
45+
super.setContentView(view)
46+
bindWindowDisplayMetrics()
47+
}
48+
`;
49+
50+
const BIND_CALL = `
51+
bindWindowDisplayMetrics()`;
52+
53+
function insertAfter(contents, anchor, insertion, description) {
54+
const index = contents.indexOf(anchor);
55+
if (index === -1) {
56+
throw new Error(
57+
`withAndroidWindowDisplayMetrics: could not find ${description} in MainActivity — the Expo template changed; update the plugin anchors.`,
58+
);
59+
}
60+
const end = index + anchor.length;
61+
return contents.slice(0, end) + insertion + contents.slice(end);
62+
}
63+
64+
function addImport(contents, importLine) {
65+
if (contents.includes(importLine)) {
66+
return contents;
67+
}
68+
return insertAfter(
69+
contents,
70+
"import android.os.Bundle",
71+
`\n${importLine}`,
72+
"the android.os.Bundle import",
73+
);
74+
}
75+
76+
module.exports = function withAndroidWindowDisplayMetrics(config) {
77+
return withMainActivity(config, (nextConfig) => {
78+
let contents = nextConfig.modResults.contents;
79+
if (nextConfig.modResults.language !== "kt") {
80+
throw new Error("withAndroidWindowDisplayMetrics: MainActivity must be Kotlin.");
81+
}
82+
if (contents.includes("bindWindowDisplayMetrics")) {
83+
return nextConfig;
84+
}
85+
86+
contents = addImport(contents, "import android.util.DisplayMetrics");
87+
contents = addImport(contents, "import android.view.View");
88+
contents = addImport(contents, "import com.facebook.react.uimanager.DisplayMetricsHolder");
89+
contents = insertAfter(
90+
contents,
91+
"class MainActivity : ReactActivity() {",
92+
BIND_METHOD + SET_CONTENT_VIEW_METHOD,
93+
"the MainActivity class declaration",
94+
);
95+
// Another plugin (withAndroidTabletOrientation) may already override
96+
// onConfigurationChanged; extend it rather than declaring a second override.
97+
if (contents.includes("super.onConfigurationChanged(newConfig)")) {
98+
contents = insertAfter(
99+
contents,
100+
"super.onConfigurationChanged(newConfig)",
101+
BIND_CALL,
102+
"the super.onConfigurationChanged call",
103+
);
104+
} else {
105+
contents = addImport(contents, "import android.content.res.Configuration");
106+
contents = insertAfter(
107+
contents,
108+
"class MainActivity : ReactActivity() {",
109+
CONFIGURATION_CHANGED_METHOD,
110+
"the MainActivity class declaration",
111+
);
112+
}
113+
114+
nextConfig.modResults.contents = contents;
115+
return nextConfig;
116+
});
117+
};

0 commit comments

Comments
 (0)