11const { withMainActivity } = require ( "expo/config-plugins" ) ;
22
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.
3+ // React Native measures text with application-display density, which differs
4+ // from the activity on external displays. Expo constructs its root after
5+ // onCreate, resetting those metrics; ReactHost resets them on configuration
6+ // changes too. Rebind after both, before text is measured.
7+ const DISPLAY_METRICS_METHODS = `
8+ // Keep deprecated APIs to match React Native screen metrics on Android 24+.
9+ // Physical display metrics omit the configured font scale.
2010 @Suppress("DEPRECATION")
2111 private fun bindWindowDisplayMetrics() {
2212 val windowMetrics = resources.displayMetrics
@@ -26,33 +16,14 @@ const BIND_METHOD = `
2616 metrics.scaledDensity = windowMetrics.scaledDensity
2717 DisplayMetricsHolder.setScreenDisplayMetrics(metrics)
2818 }
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- ` ;
3919
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.
20+ // Expo constructs the root view after onCreate; its constructor resets metrics.
4421 override fun setContentView(view: View?) {
4522 super.setContentView(view)
4623 bindWindowDisplayMetrics()
4724 }
4825` ;
4926
50- const CONFIGURATION_CHANGED_SIGNATURE =
51- "override fun onConfigurationChanged(newConfig: Configuration) {" ;
52-
53- const BIND_CALL = `
54- bindWindowDisplayMetrics()` ;
55-
5627function insertAfter ( contents , anchor , insertion , description ) {
5728 const index = contents . indexOf ( anchor ) ;
5829 if ( index === - 1 ) {
@@ -92,27 +63,33 @@ module.exports = function withAndroidWindowDisplayMetrics(config) {
9263 contents = insertAfter (
9364 contents ,
9465 "class MainActivity : ReactActivity() {" ,
95- BIND_METHOD + SET_CONTENT_VIEW_METHOD ,
66+ DISPLAY_METRICS_METHODS ,
9667 "the MainActivity class declaration" ,
9768 ) ;
98- // Another plugin (withAndroidTabletOrientation) may already override
99- // onConfigurationChanged; extend it rather than declaring a second override.
100- const overrideStart = contents . indexOf ( CONFIGURATION_CHANGED_SIGNATURE ) ;
69+ // Extend the tablet-orientation override, after ReactHost resets metrics.
70+ const overrideStart = contents . indexOf (
71+ "override fun onConfigurationChanged(newConfig: Configuration) {" ,
72+ ) ;
10173 if ( overrideStart !== - 1 ) {
10274 contents =
10375 contents . slice ( 0 , overrideStart ) +
10476 insertAfter (
10577 contents . slice ( overrideStart ) ,
10678 "super.onConfigurationChanged(newConfig)" ,
107- BIND_CALL ,
79+ "\n bindWindowDisplayMetrics()" ,
10880 "the super call in the onConfigurationChanged override" ,
10981 ) ;
11082 } else {
11183 contents = addImport ( contents , "import android.content.res.Configuration" ) ;
11284 contents = insertAfter (
11385 contents ,
11486 "class MainActivity : ReactActivity() {" ,
115- CONFIGURATION_CHANGED_METHOD ,
87+ `
88+ override fun onConfigurationChanged(newConfig: Configuration) {
89+ super.onConfigurationChanged(newConfig)
90+ bindWindowDisplayMetrics()
91+ }
92+ ` ,
11693 "the MainActivity class declaration" ,
11794 ) ;
11895 }
0 commit comments