Skip to content

fix(floating): clamp window bounds on screen and sync delegate close state - #1501

Open
zou-handy wants to merge 1 commit into
AAswordman:devfrom
zou-handy:fix/floating-window-bounds-and-delegate-sync
Open

zou-handy wants to merge 1 commit into
AAswordman:devfrom
zou-handy:fix/floating-window-bounds-and-delegate-sync

Conversation

@zou-handy

Copy link
Copy Markdown
Contributor

变更说明 / Description

修复悬浮窗窗口模式(FloatingMode.WINDOW)在贴边切换或拖拽时滑入负向/越界 X 坐标后无法移动、无法收起也无法从主界面关闭的卡死问题。

背景与动机 / Context and motivation

在 Android 16(ColorOS 16,1080×2400,系统左右侧滑返回手势区 0..72px / 1008..1080px)真机复现到悬浮窗完全锁死在屏幕左上角的现场(dumpsys window windows 实测 ty=APPLICATION_OVERLAY,frame=[-231,120][460,888],operit.log 记录 Floating view added at (-231, 0)):

  1. 窗口模式允许负向与越界 X 坐标导致控制区全部失联:
    • FloatingWindowManager 在 createLayoutParams()、switchMode() 非球分支以及 onMove() 中对 FloatingMode.WINDOW 使用了 -(width - minVisibleWidth)(即 -width / 3,691px 宽窗口下可达 -231px)与 screenWidth - minVisibleWidth / 2。
    • 但 FloatingChatWindowScreen 的 TitleBar 左侧放置了 History / Fullscreen / Minimize 三个按钮、右侧放置了 Home / Close 按钮,仅中间空白段挂了 detectDragGestures。一旦窗口落到 x = -231(或右侧越界),左侧按钮直接移出屏幕外,残留在屏幕最左缘的按钮落入 Android 24dp(72px)系统侧滑返回手势拦截区(ColorSideGestureLeft),每次触摸都会被系统手势抢走并下发 ACTION_CANCEL,导致既拖不动也点不到最小化。
    • 同时 updateWindowSizeInLayoutParams() 在缩放/拉宽窗口时未重新钳制坐标,靠右缩放会把右侧 Home / Close 按钮顶出屏幕外。
    • switchMode() 在更新 state.currentMode 时过早调用了 callback.saveState(),而后续 postDelayed 真正写入目标 state.x / state.y 后未再次持久化,导致 floating_chat_prefs 保存的是切换前的旧坐标(如贴边悬浮球的负坐标)。
  2. ACTION_CANCEL 导致拖拽/缩放状态锁死:
    • FloatingChatWindowScreen 中 TitleBar、RightEdgeScaleHandle、CornerScaleHandle、BottomResizeHandle 四个 detectDragGestures 均未提供 onDragCancel。当触摸靠近屏幕边缘被系统侧滑手势以 ACTION_CANCEL 打断时,viewModel.isDragging、floatContext.isEdgeResizing 及缩放态会一直停留在 true,后续 syncWindowState() 因 if (!isDragging) 条件永远不再同步窗口状态,且不会触发 saveWindowState。
  3. 主界面 FloatingWindowDelegate 状态失步导致无法兜底关闭:
    • 当 FloatingChatService 已在运行而主界面通过 tryBindToRunningService() 绑定时,onServiceConnected 未将 _isFloatingMode.value 置为 true,主界面点击悬浮窗按钮不会走关闭分支;
    • 当服务由外部停止并广播 ACTION_FLOATING_CHAT_SERVICE_STOPPED 时,disconnectFromService(updateFloatingMode = false) 未复位 _isFloatingMode;
    • toggleFloatingMode() 关闭分支仅调用 floatingService?.onClose(),若尚未完成绑定或连接已断开则变成空操作,既关不掉残留的 TYPE_APPLICATION_OVERLAY 窗口也无法复位按钮状态。

改动范围 / Changes

  • FloatingWindowManager.kt:
    • 新增纯计算辅助方法 clampWindowPosition(...),统一约束 FloatingMode.WINDOW 横向坐标在 0..(screenWidth - width).coerceAtLeast(0) 内、纵向顶部不小于 0 且保留可见高度(窗口尺寸极端大于屏幕时退化为 0,避免 coerceIn 抛空区间异常)。
    • 在 createLayoutParams()、updateWindowSizeInLayoutParams()、switchMode()(FloatingMode.WINDOW 分支)与 onMove() 统一接入 clampWindowPosition(...)。
    • 在 switchMode() 各分支完成 state.x / state.y 同步后补充 callback.saveState(),确保持久化的是切换完成后的真实窗口坐标。
  • FloatingChatWindowScreen.kt:
    • 为 TitleBar、RightEdgeScaleHandle、CornerScaleHandle、BottomResizeHandle 全部四个 detectDragGestures 补齐 onDragCancel 收尾逻辑;在 CornerScaleHandle 结束/取消缩放时同步触发 saveWindowState。
  • FloatingWindowDelegate.kt:
    • onServiceConnected 在仍处于绑定态时同步 _isFloatingMode.value = true;
    • ACTION_FLOATING_CHAT_SERVICE_STOPPED 调用 disconnectFromService(updateFloatingMode = true);
    • toggleFloatingMode() 关闭分支在 floatingService 为空时回退尝试 FloatingChatService.getInstance()?.onClose() 或 context.stopService(...) 并复位状态。
  • 新增 JVM 单元测试 FloatingWindowBoundsTest.kt,覆盖左越界(-231 -> 0)、右越界、上下越界及超大窗口尺寸防空区间异常。

明确不包含:

  • 不改动悬浮球模式(FloatingMode.BALL / VOICE_BALL)原有的半贴边隐藏边界(-ballSize + ballSize / 2)。
  • 不改动全屏语音(FULLSCREEN)、屏幕识图(SCREEN_OCR)和结果气泡(RESULT_DISPLAY)的布局策略,不涉及字符串资源与持久化字段格式变更。

兼容性与风险 / Compatibility and risks

  • 窗口模式(FloatingMode.WINDOW)将不再允许左右拖出屏幕边界外;此前历史 floating_chat_prefs 中已写入负向或越界 window_x / window_y 的用户在升级后首次启动悬浮窗时会自动被钳制回屏幕安全区内,无需手动清应用数据。
  • 持久化键名与类型完全不变,无数据迁移风险。

关联 Issue / Related issue

N/A(真机使用过程中复现并定位的悬浮窗交互缺陷,未检索到已有重复 Issue;背景与真机窗口现场见上文及下方证据段。)

验证方式 / Verification

检查或命令:
python3 -B ci/script/check_repo_hygiene.py --base origin/dev --candidate HEAD
git diff --check origin/dev HEAD

环境与变体:
- 真机复现与日志取证环境:OPPO Find X9 (PLJ110),Android 16 / ColorOS 16,Operit 1.12.2+2,1080×2400 (density=3.0)
- 本地仓库卫生检查环境:Ubuntu 24.04 (Python 3.12),基线 origin/dev (aeb569bfb166aa6ad8e9a2040856dd88bd5aea0a)

结果:
- check_repo_hygiene.py:Repository hygiene: errors=0 warnings=0(检查全部 4 个新增/修改文件通过)
- git diff --check:通过,无空白字符或冲突标记问题
- 未在本地运行完整 Gradle 编译与 testDebugUnitTest 原因:当前移动侧开发容器未安装完整 Android SDK/NDK 工具链,编译与 FloatingWindowBoundsTest 单测由 PR 触发的 GitHub Actions Candidate checks (android_build / android_tests) 执行验证

证据 / Evidence

真机卡死现场 dumpsys window windows 与 operit.log 关键取证记录(windowWidth = 288dp * 3.0 * 0.8 = 691px,x = -(691 - 460) = -231px):

Window #11 Window{... u0 com.ai.assistance.operit}:
  mOwnerUid=10029 package=com.ai.assistance.operit
  mAttrs={( -231,0 )(691x768) gr=TOP START sim={state=unspecified} ty=APPLICATION_OVERLAY fmt=TRANSLUCENT
    fl=NOT_FOCUSABLE LAYOUT_NO_LIMITS HARDWARE_ACCELERATED}
  Frames: parent=[0,120][1080,2400] display=[-100000,-100000][100000,100000]
          frame=[-231,120][460,888]

operit.log:
D/FloatingWindowManager: Floating view added at (-231, 0)

floating_chat_prefs.xml:
<int name="window_x" value="-231" />
<int name="window_y" value="0" />
<string name="current_mode">WINDOW</string>

修复后对同一参数(x = -231, y = 0, width = 691, height = 768, screenWidth = 1080, screenHeight = 2280),FloatingWindowManager.clampWindowPosition 将坐标钳制为 (0, 0),TitleBar 左侧控制按钮与中间拖拽热区、右侧关闭按钮均完整保留在屏幕可见区域内。

检查清单 / Checklist

  • 我已记录可复现验证和未运行项原因 / Reproducible verification and reasons for unrun checks are recorded
  • 日常开发 PR 的目标分支为 dev;如目标为 main,我已说明这是维护者发布同步 / The target branch is dev for regular work; if it is main, I explained why this is a maintainer-led release sync
  • 我已确认 Candidate checks 覆盖改动范围,并会处理技术失败项 / Candidate checks covers the change scope and technical failures will be addressed
  • 最终 diff 无无关、临时、生成、二进制或敏感内容 / Final diff has no unrelated, temporary, generated, binary, or secret content
  • 已提供对应的回归、UI、文档/字符串或兼容性证据 / Relevant regression, UI, docs/strings, or compatibility evidence is provided

Copilot AI balanced review requested due to automatic review settings November 13, 2026 16:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@CATMIAOZHI
CATMIAOZHI self-requested a review November 13, 2026 19:37

@CATMIAOZHI CATMIAOZHI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审计结论:✅ 批准合并

改动(4 个文件,+204/−42):悬浮窗窗口模式(FloatingMode.WINDOW)坐标统一钳制在屏幕内,补齐四个拖拽手势的 onDragCancel 收尾,FloatingWindowDelegate 关闭分支状态同步;新增 FloatingWindowBoundsTest 单元测试。

通过的检查:

  • 目标分支为 dev,符合要求。
  • 根因定位与修复一致:dumpsys 取证的 x = -231 越界坐标,源头是 createLayoutParams() / switchMode() / onMove() 三处允许负向 X 的钳制区间,新 clampWindowPosition() 在四处调用点统一收口为 0..(screenWidth - width),且 updateWindowSizeInLayoutParams() 缩放时也会重新钳制,旧坐标的持久化来源已堵住。
  • clampWindowPosition() 边界处理正确:窗口大于屏幕时 maxX / maxY 经 coerceAtLeast(0) 退化为 0,不会触发 coerceIn 空区间异常;isFromBall 分支传 minVisibleHeight = height,与旧逻辑(整窗可见)一致;纵向保留 height * 2 / 3 可见高度,顶部 TitleBar 始终可达。
  • switchMode() 四个分支在 state.x / state.y 同步后都补了 callback.saveState(),持久化的是切换完成后的真实坐标,修复了"先保存旧坐标、后写新坐标"的失步问题;switchMode 开头已有的 saveState()(模式切换时)不受影响。
  • FloatingChatWindowScreen 四处 detectDragGestures(TitleBar、右缘、角落、底部)均补齐 onDragCancel,与 onDragEnd 对称收尾,CornerScaleHandle 结束/取消时都会触发 saveWindowState;ACTION_CANCEL 导致拖拽态锁死的问题已覆盖。
  • FloatingWindowDelegate 三处失步均已修:onServiceConnected 增加 isBoundToService 守卫并同步 _isFloatingMode = true;ACTION_FLOATING_CHAT_SERVICE_STOPPED 改为 disconnectFromService(updateFloatingMode = true);关闭分支在 floatingService 为空时回退 FloatingChatService.getInstance()?.onClose() 或 stopService() 并复位状态。getInstance() 在 FloatingChatService 伴生对象中存在,disconnectFromService 幂等(重复调用无害)。
  • 球模式(BALL / VOICE_BALL)的半贴边边界、全屏/识图/结果气泡模式均未改动,与 PR 描述的"明确不包含"一致。
  • 新增单测覆盖左越界 -231 -> 0、右越界、上下越界及超大窗口防空区间四种场景,断言与 clampWindowPosition 的实现一致。
  • PR 描述与 diff 一致,验证方式与未运行项(本地未跑 Gradle 编译/单测,原因已说明,由 CI 验证)已如实记录。

问题:

  1. [P3] switchMode() 球模式切换的两条 postDelayed 回调(150ms / 100ms)在执行时不校验当前模式:若用户在 150ms 内连续切换两次,先发出的回调可能在后一次切换已完成持久化之后才执行,把中间过渡态的坐标写回 state.x / state.y 并 saveState()。后果有限(下次启动时 createLayoutParams 会再钳制到屏内,不会重现卡死),但恢复的位置可能不是用户最后停留的位置。建议回调执行时校验当前 state.currentMode 是否仍为本次切换的目标模式,或在新一次 switchMode 时取消旧的延迟回调。
  2. [P3] FloatingWindowDelegate.toggleFloatingMode() 关闭回退分支里 context.stopService(...) 的 catch (_: Exception) {} 为空吞异常,出错时无日志,排障困难;建议至少记一条日志(注:同文件 disconnectFromService / cleanup 等处已有同类空 catch,可顺手统一处理,不阻塞)。

后续可跟进(不阻塞):本地未跑 Gradle 编译与 FloatingWindowBoundsTest,以 CI 的 android_build / android_tests 结果为准;若 CI 单测失败请按失败信息处理。

由 水晴喵的muse 审计

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants