Repository navigation
fix(rn): 修复折叠屏相交检测的坐标与视口刷新 - #2612
Open
mackwang112 wants to merge 1 commit into
Open
mackwang112 wants to merge 1 commit into
mackwang112 wants to merge 1 commit into
Conversation
mackwang112
marked this pull request as draft
November 3, 2026 09:24
mackwang112
marked this pull request as ready for review
November 3, 2026 10:02
hiyuki
reviewed
Nov 5, 2026
| const viewport = navigation.__mpxViewportRef?.current | ||
| if (viewport) { | ||
| return new Promise(resolve => { | ||
| viewport.measureInWindow((left, top, width, height) => { |
mackwang112
marked this pull request as draft
November 5, 2026 07:43
mackwang112
force-pushed
the
fix/rn-intersection-viewport
branch
from
November 18, 2026 06:13
e5e5fe6 to
69021a5
Compare
mackwang112
marked this pull request as ready for review
November 18, 2026 07:13
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题背景与表现
折叠屏半屏容器、平行视界或 RN 嵌入原生页面时,RN 根容器可能不在原生窗口原点。原实现用
measureInWindow()获取目标和节点参照的窗口坐标,却从navigation.layout生成根容器内的视口;坐标系不一致会导致完整可见节点返回intersectionRatio: 0,或四向裁剪比例错误。另外,视口矩形首次生成后永久缓存;页面虽会在 Dimensions 变化时更新
navigation.layout,已有 observer 不会因此主动检测。保留同一实例折叠、展开或旋转后,可能继续使用旧视口。布局在 Dimensions 事件之后随安全区更新时,也需要重新检测。本 PR 统一相交计算的坐标系,并让已有 observer 在视口布局变化后继续工作。
原因定位与修复方案
统一节点坐标。 目标与
relativeTo()参照节点都改用measure((x, y, width, height, pageX, pageY) => ...)的pageX/pageY;不使用前两个父节点局部坐标x/y,移除手动状态栏高度补偿。这样不会把整个 RN 容器在原生窗口中的偏移带入相交计算。保留页面布局和视口缓存。
relativeToViewport()继续读取所属页面的navigation.layout;缓存记录布局对象,布局对象替换后在下次检测重算。保留原有 margins 与边界公式:左右边界不叠加
layout.left,也不额外测量视口。没有页面布局时沿用原来的 screen 回退。布局更新主动触发检测。 Dimensions 监听在更新布局后调用本页现有 observer 的
throttleMeasure();页面提交渲染后只比较布局的top/width/height,这些值变化时再触发检测,覆盖安全区晚于尺寸事件更新的情况。普通重复渲染,以及仅left/statusBarHeight/bottomVirtualHeight/isLandscape变化,不因此增加测量。固定所属页面。 创建 observer 时通过组件
getPageId()找到其 navigation,避免后台页面检测时误用当前前台页面的布局;无 pageId 的调用沿用创建时的 focused navigation。disconnect 时释放该引用。既有 Promise.all、首次回调与阈值判断、相交公式、滚动触发和节流机制保留。没有加入超时保护、重试、额外初始化等待、collapsable 或共享原生测量缓存。
intersectionRect的边界和宽高计算沿用 master 已有的 #2556,本 PR 不再修改该公式。前后工作流程
例如 iOS Duo 展开横屏:视口横向范围
[0,382],实际目标为[16,256]。旧节点窗口坐标多出宿主偏移 485,返回[501,741],ratio 为 0;修复后返回[16,256],ratio 为 1。微信 demo 由使用者确认:默认原生标题栏不属于页面内容视口,内容坐标
(0,0)位于标题栏下方的内容区左上角。 本 PR 不改微信实现;RN 返回矩形仍以 RN 根容器为原点,默认 MpxNav 的排除通过页面布局的 top/height 表达,不能据此要求 RN 与微信的绝对 top 数值一致。navigationStyle: custom下自绘覆盖层不会自动从几何视口扣除。修改影响与边界
boundingClientRect、节点参照的relativeRect和intersectionRect改用 RN 根容器坐标,不再包含宿主窗口偏移或额外状态栏补偿。普通全屏环境的比例可能保持不变,但依赖旧窗口绝对坐标的业务需要核对。getLayoutData()的高度来源。iOS Duo 展开竖屏的 layout 高度问题按任务约定交由另一个 PR 修复,本 PR 不声称修复该问题。验证与简单证据
当前提交的代码检查
2026-11-18 重新运行通过:
git diff --check通过。设备验证范围
测试基线为本地 master
d82f89fc062e1196de508d4f050c27909ea6f484。iOS 使用模拟器,Android/Harmony 使用 USB 真机;三端均有设备测试记录,但版本覆盖不同,下面分开说明。鸿蒙简单返回值证据:平行视界开+展开时,完整目标实际 left=16,旧版返回 371.84、ratio=0,修复后 left=16、ratio=1;其余三状态旧版多加 35.84 的纵向补偿,上半隐藏 ratio=0.86、下半隐藏=0.14,修复后均为 0.5。动态固定宽度目标的比例在平行视界开时为
0.68 → 0.66 → 0.68,关闭时为1 → 0.66 → 1。静态预期来自独立 native measure,再比较 boundingClientRect、relativeRect、intersectionRect 与 ratio;没有使用待验证 observer 的返回值生成预期。鸿蒙动态测试在每轮内保留同一 runKey、observer 和目标原生引用,物理切换期间不刷新、不滚动、不重新 observe、不手动调用 observer 测量;未跨阈值时核对自动检测记录,允许没有新回调。测试等待仅存在于 demo,没有进入框架。
设备测试后补充的页面归属和布局字段判断已通过上述单元测试,尚未对最终提交再完整重跑设备矩阵。当前 measure 方案未重测 Android;iOS Duo 展开竖屏受独立高度问题影响,默认与无标题栏完整返回值仅各 1 项通过,不计为本 PR 已通过场景。尚未联调高度修复 PR,也未穷举快速连续折叠、键盘、后台恢复及所有低版本。
详细报告保留在本地 demo 的
test-results/intersection-observer-20261117/和test-results/intersection-observer-harmony-20261117/。这里提供脱敏数值,不上传含宿主地图等业务内容的截图。PR 只包含 observer、core 页面包装器、相关测试和对应文档/Skill;不提交 demo、依赖锁文件或本机测试配置。