@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
cjs 87 lines 3.01 KB
Raw
1 const { withMainActivity } = require("expo/config-plugins");
2
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 // Expo constructs the root view after onCreate; its constructor resets metrics.
9 override fun setContentView(view: View?) {
10 super.setContentView(view)
11 DisplayMetricsHolder.initDisplayMetrics(this)
12 }
13 `;
14
15 function insertAfter(contents, anchor, insertion, description) {
16 const index = contents.indexOf(anchor);
17 if (index === -1) {
18 throw new Error(
19 `withAndroidWindowDisplayMetrics: could not find ${description} in MainActivity — the Expo template changed; update the plugin anchors.`,
20 );
21 }
22 const end = index + anchor.length;
23 return contents.slice(0, end) + insertion + contents.slice(end);
24 }
25
26 function addImport(contents, importLine) {
27 if (contents.includes(importLine)) {
28 return contents;
29 }
30 return insertAfter(
31 contents,
32 "import android.os.Bundle",
33 `\n${importLine}`,
34 "the android.os.Bundle import",
35 );
36 }
37
38 module.exports = function withAndroidWindowDisplayMetrics(config) {
39 return withMainActivity(config, (nextConfig) => {
40 let contents = nextConfig.modResults.contents;
41 if (nextConfig.modResults.language !== "kt") {
42 throw new Error("withAndroidWindowDisplayMetrics: MainActivity must be Kotlin.");
43 }
44 if (contents.includes("DisplayMetricsHolder.initDisplayMetrics(this)")) {
45 return nextConfig;
46 }
47
48 contents = addImport(contents, "import android.view.View");
49 contents = addImport(contents, "import com.facebook.react.uimanager.DisplayMetricsHolder");
50 contents = insertAfter(
51 contents,
52 "class MainActivity : ReactActivity() {",
53 DISPLAY_METRICS_METHODS,
54 "the MainActivity class declaration",
55 );
56 // Extend the tablet-orientation override, after ReactHost resets metrics.
57 const overrideStart = contents.indexOf(
58 "override fun onConfigurationChanged(newConfig: Configuration) {",
59 );
60 if (overrideStart !== -1) {
61 contents =
62 contents.slice(0, overrideStart) +
63 insertAfter(
64 contents.slice(overrideStart),
65 "super.onConfigurationChanged(newConfig)",
66 "\n DisplayMetricsHolder.initDisplayMetrics(this)",
67 "the super call in the onConfigurationChanged override",
68 );
69 } else {
70 contents = addImport(contents, "import android.content.res.Configuration");
71 contents = insertAfter(
72 contents,
73 "class MainActivity : ReactActivity() {",
74 `
75 override fun onConfigurationChanged(newConfig: Configuration) {
76 super.onConfigurationChanged(newConfig)
77 DisplayMetricsHolder.initDisplayMetrics(this)
78 }
79 `,
80 "the MainActivity class declaration",
81 );
82 }
83
84 nextConfig.modResults.contents = contents;
85 return nextConfig;
86 });
87 };