GeezerVison is showing github.com — fix(mobile): text no longer clips in Android desktop windows on exter… · incognitojam/styal@6c41890
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/app.config.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -453,6 +453,8 @@ const config: ExpoConfig = { | 453 | 453 | "./plugins/withAndroidModernPopupMenu.cjs", | 454 | 454 | "./plugins/withAndroidModernAlertDialog.cjs", | 455 | 455 | "./plugins/withAndroidPredictiveBackCompat.cjs", | | 456 | + // MainActivity mods run in reverse order; extend the tablet-orientation override. | | 457 | + "./plugins/withAndroidWindowDisplayMetrics.cjs", | 456 | 458 | "./plugins/withAndroidTabletOrientation.cjs", | 457 | 459 | ...(isIosPersonalTeamBuild ? ["./plugins/withoutIosPersonalTeamCapabilities.cjs"] : []), | 458 | 460 | ], | |
Collapse file Copy file name to clipboard +87Lines changed: 87 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +}; |
Collapse file Copy file name to clipboard +89Lines changed: 89 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as NodeChildProcess from "node:child_process"; | | 2 | +import * as NodeFS from "node:fs"; | | 3 | +import * as NodeModule from "node:module"; | | 4 | +import * as NodeOS from "node:os"; | | 5 | +import * as NodePath from "node:path"; | | 6 | +import { afterEach, describe, expect, it } from "vite-plus/test"; | | 7 | + | | 8 | +const require = NodeModule.createRequire(import.meta.url); | | 9 | +const { compileModsAsync } = require("expo/config-plugins"); | | 10 | +const { getConfig } = require("expo/config"); | | 11 | +const template = NodeChildProcess.execFileSync( | | 12 | + "tar", | | 13 | + [ | | 14 | + "-xOf", | | 15 | + NodePath.join(NodePath.dirname(require.resolve("expo/package.json")), "template.tgz"), | | 16 | + "package/android/app/src/main/java/com/helloworld/MainActivity.kt", | | 17 | + ], | | 18 | + { encoding: "utf8" }, | | 19 | +); | | 20 | +const projectRoots = []; | | 21 | + | | 22 | +afterEach(() => { | | 23 | + for (const root of projectRoots.splice(0)) NodeFS.rmSync(root, { recursive: true, force: true }); | | 24 | +}); | | 25 | + | | 26 | +async function prebuildMainActivity(contents) { | | 27 | + const projectRoot = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "display-metrics-plugin-")); | | 28 | + projectRoots.push(projectRoot); | | 29 | + const activityPath = NodePath.join( | | 30 | + projectRoot, | | 31 | + "android/app/src/main/java/com/helloworld/MainActivity.kt", | | 32 | + ); | | 33 | + NodeFS.mkdirSync(NodePath.dirname(activityPath), { recursive: true }); | | 34 | + NodeFS.writeFileSync(activityPath, contents); | | 35 | + const { exp } = getConfig(NodePath.resolve(import.meta.dirname, ".."), { | | 36 | + isModdedConfig: true, | | 37 | + }); | | 38 | + // Keep the app's registration order: Expo executes same-type mods in reverse. | | 39 | + // Unrelated native-file mods need the rest of a complete prebuild project. | | 40 | + const mainActivity = exp.mods?.android?.mainActivity; | | 41 | + await compileModsAsync( | | 42 | + { ...exp, mods: { android: mainActivity ? { mainActivity } : {} } }, | | 43 | + { projectRoot, platforms: ["android"] }, | | 44 | + ); | | 45 | + return NodeFS.readFileSync(activityPath, "utf8"); | | 46 | +} | | 47 | + | | 48 | +function methodBody(contents, name) { | | 49 | + const methods = [ | | 50 | + ...contents.matchAll( | | 51 | + new RegExp(`^ (?:override|private) fun ${name}\\([^]*?\\) \\{\\n([^]*?)^ \\}`, "gm"), | | 52 | + ), | | 53 | + ]; | | 54 | + expect(methods, `exactly one ${name} declaration`).toHaveLength(1); | | 55 | + return methods[0][1].trim(); | | 56 | +} | | 57 | + | | 58 | +describe("withAndroidWindowDisplayMetrics", () => { | | 59 | + it("rebinds the generated activity after root installation and configuration changes, preserving orientation", async () => { | | 60 | + const result = await prebuildMainActivity(template); | | 61 | + | | 62 | + expect(methodBody(result, "setContentView")).toMatch( | | 63 | + /^super\.setContentView\(view\)\s+DisplayMetricsHolder\.initDisplayMetrics\(this\)$/, | | 64 | + ); | | 65 | + expect(methodBody(result, "onConfigurationChanged")).toMatch( | | 66 | + /^super\.onConfigurationChanged\(newConfig\)\s+DisplayMetricsHolder\.initDisplayMetrics\(this\)\s+applyTabletOrientation\(\)$/, | | 67 | + ); | | 68 | + const onCreate = methodBody(result, "onCreate"); | | 69 | + expect(onCreate).toContain("applyTabletOrientation()"); | | 70 | + expect(onCreate).not.toContain("DisplayMetricsHolder.initDisplayMetrics(this)"); | | 71 | + expect(methodBody(result, "applyTabletOrientation")).toMatch( | | 72 | + /requestedOrientation = if \(resources\.configuration\.smallestScreenWidthDp >= 600\) \{\s+ActivityInfo\.SCREEN_ORIENTATION_FULL_USER\s+\} else \{\s+ActivityInfo\.SCREEN_ORIENTATION_PORTRAIT\s+\}/, | | 73 | + ); | | 74 | + | | 75 | + expect(result).toContain("import android.view.View"); | | 76 | + expect(result).toContain("import com.facebook.react.uimanager.DisplayMetricsHolder"); | | 77 | + expect(result.match(/^import android\.content\.res\.Configuration$/gm)).toHaveLength(1); | | 78 | + }); | | 79 | + | | 80 | + it("leaves the composed activity unchanged when prebuild runs again", async () => { | | 81 | + const once = await prebuildMainActivity(template); | | 82 | + const twice = await prebuildMainActivity(once); | | 83 | + | | 84 | + expect(methodBody(once, "setContentView")).toContain( | | 85 | + "DisplayMetricsHolder.initDisplayMetrics(this)", | | 86 | + ); | | 87 | + expect(twice).toBe(once); | | 88 | + }); | | 89 | +}); |
|