Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

withAndroidWindowDisplayMetrics.cjs

114 lines4.2 KB

1const { withMainActivity } = require("expo/config-plugins");
2​
3// React Native converts touches and measures text at the display's density, which can differ
4// from the activity's: external displays, and One UI split screen (321 vs 420 dpi on a Galaxy
5// Z Fold). Expo's root constructor and ReactHost configuration changes reset it, so rebind
6// after both with the activity's density, which layout uses.
7const DISPLAY_METRICS_METHODS = `
8 // Expo constructs the root view after onCreate; its constructor resets metrics.
9 override fun setContentView(view: View?) {
10 super.setContentView(view)
11 bindWindowDisplayMetrics()
12 // After a rotation, ReactRootView's global layout listener resets metrics to the
13 // display's. Pre-draw runs after every global layout listener in that traversal.
14 window.decorView.viewTreeObserver.addOnPreDrawListener {
15 if (DisplayMetricsHolder.getScreenDisplayMetrics().densityDpi != resources.displayMetrics.densityDpi) {
16 bindWindowDisplayMetrics()
17 }
18 true
19 }
20 }
21​
22 private fun bindWindowDisplayMetrics() {
23 DisplayMetricsHolder.initDisplayMetrics(this)
24 val activityMetrics = resources.displayMetrics
25 // Fabric measures text off the main thread; publish the corrected metrics whole.
26 DisplayMetricsHolder.setScreenDisplayMetrics(
27 DisplayMetrics().apply {
28 setTo(DisplayMetricsHolder.getScreenDisplayMetrics())
29 density = activityMetrics.density
30 densityDpi = activityMetrics.densityDpi
31 },
32 )
33 }
34`;
35​
36function insertAfter(contents, anchor, insertion, description) {
37 const index = contents.indexOf(anchor);
38 if (index === -1) {
39 throw new Error(
40 `withAndroidWindowDisplayMetrics: could not find ${description} in MainActivity — the Expo template changed; update the plugin anchors.`,
41 );
42 }
43 const end = index + anchor.length;
44 return contents.slice(0, end) + insertion + contents.slice(end);
46​
47function addImport(contents, importLine) {
48 if (contents.includes(importLine)) {
49 return contents;
50 }
51 return insertAfter(
52 contents,
53 "import android.os.Bundle",
54 `\n${importLine}`,
55 "the android.os.Bundle import",
56 );
58​
59module.exports = function withAndroidWindowDisplayMetrics(config) {
60 return withMainActivity(config, (nextConfig) => {
61 let contents = nextConfig.modResults.contents;
62 if (nextConfig.modResults.language !== "kt") {
63 throw new Error("withAndroidWindowDisplayMetrics: MainActivity must be Kotlin.");
64 }
65 if (contents.includes("bindWindowDisplayMetrics()")) {
66 return nextConfig;
67 }
68 if (contents.includes("DisplayMetricsHolder.initDisplayMetrics(this)")) {
69 throw new Error(
70 "withAndroidWindowDisplayMetrics: MainActivity has an older version of this plugin's code; run expo prebuild with --clean.",
71 );
72 }
73​
74 contents = addImport(contents, "import android.util.DisplayMetrics");
75 contents = addImport(contents, "import android.view.View");
76 contents = addImport(contents, "import com.facebook.react.uimanager.DisplayMetricsHolder");
77 contents = insertAfter(
78 contents,
79 "class MainActivity : ReactActivity() {",
80 DISPLAY_METRICS_METHODS,
81 "the MainActivity class declaration",
82 );
83 // Extend the tablet-orientation override, after ReactHost resets metrics.
84 const overrideStart = contents.indexOf(
85 "override fun onConfigurationChanged(newConfig: Configuration) {",
86 );
87 if (overrideStart !== -1) {
88 contents =
89 contents.slice(0, overrideStart) +
90 insertAfter(
91 contents.slice(overrideStart),
92 "super.onConfigurationChanged(newConfig)",
93 "\n bindWindowDisplayMetrics()",
94 "the super call in the onConfigurationChanged override",
95 );
96 } else {
97 contents = addImport(contents, "import android.content.res.Configuration");
98 contents = insertAfter(
99 contents,
100 "class MainActivity : ReactActivity() {",
101 `
102 override fun onConfigurationChanged(newConfig: Configuration) {
103 super.onConfigurationChanged(newConfig)
104 bindWindowDisplayMetrics()
105 }
106`,
107 "the MainActivity class declaration",
108 );
109 }
110​
111 nextConfig.modResults.contents = contents;
112 return nextConfig;
113 });
114};