Compare commits

...
8 changed files with 276 additions and 17 deletions
@@ -15,5 +15,6 @@ android {
}
dependencies {
implementation 'com.facebook.react:react-android'
testImplementation "junit:junit:4.13.2"
}
@@ -1,26 +1,48 @@
package sh.paseo.trace
import android.os.Trace
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.bridge.ReactContext
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class PaseoNativeTraceModule : Module() {
private var recorder: DiagnosticRecorder? = null
private var touches: TouchDiagnostics? = null
override fun definition() = ModuleDefinition {
Name("PaseoNativeTrace")
Function("startRecorder") { metadata: Map<String, String> ->
if (recorder == null) {
appContext.reactContext?.applicationContext?.let { recorder = DiagnosticRecorder(it, metadata) }
(appContext.reactContext as? ReactContext)?.let { context ->
val sink = DiagnosticRecorder(context.applicationContext, metadata)
recorder = sink
UiThreadUtil.runOnUiThread {
if (recorder === sink) {
touches = TouchDiagnostics(context, sink::record).also { it.start(appContext.currentActivity?.window) }
}
}
}
}
HeartbeatTiming.intervalMs
}
Function("record") { name: String, data: Map<String, String> -> recorder?.record(name, data) }
Function("heartbeat") { recorder?.heartbeat() }
OnActivityEntersForeground { recorder?.active(true) }
OnActivityEntersForeground {
recorder?.active(true)
UiThreadUtil.runOnUiThread { touches?.activity(appContext.currentActivity?.window) }
}
OnActivityEntersBackground { recorder?.active(false) }
OnDestroy { recorder?.close(); recorder = null }
OnDestroy {
val sink = recorder
recorder = null
UiThreadUtil.runOnUiThread {
touches?.close()
touches = null
sink?.close()
}
}
Function("beginSection") { name: String ->
Trace.beginSection(name.take(127))
@@ -0,0 +1,141 @@
package sh.paseo.trace
import android.graphics.Rect
import android.os.Build
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowInsets
import com.facebook.react.R
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.interfaces.ExtraWindowEventListener
import com.facebook.react.uimanager.ReactPointerEventsView
import com.facebook.react.uimanager.TouchTargetHelper
import com.facebook.react.uimanager.RootView
/** Temporary touch evidence. Only attached while the diagnostic recorder is running. */
internal class TouchDiagnostics(
private val context: ReactContext,
private val write: (String, Map<String, String>) -> Unit,
) : ExtraWindowEventListener {
private val windows = mutableMapOf<Window, TouchCallback>()
private var nextWindow = 0
private var activityWindow: Window? = null
fun start(window: Window?) {
UiThreadUtil.assertOnUiThread()
context.addExtraWindowEventListener(this)
activity(window)
}
fun activity(window: Window?) {
if (activityWindow === window) return
activityWindow?.let { release(it) }
activityWindow = window
window?.let { observe(it) }
}
override fun onExtraWindowCreate(window: Window) = observe(window)
override fun onExtraWindowDestroy(window: Window) = release(window)
private fun observe(window: Window) {
if (windows.containsKey(window)) return
val callback = TouchCallback(window, window.callback, ++nextWindow, write)
windows[window] = callback
window.callback = callback
write("touch.window.open", mapOf("window" to "$nextWindow"))
}
private fun release(window: Window) {
val callback = windows.remove(window) ?: return
// Another observer may wrap ours. Disable it even when we cannot unwrap its owner.
callback.active = false
if (window.callback === callback) window.callback = callback.previous
write("touch.window.close", mapOf("window" to "${callback.id}"))
}
fun close() {
context.removeExtraWindowEventListener(this)
windows.keys.toList().forEach { release(it) }
activityWindow = null
}
}
private class TouchCallback(
private val window: Window,
val previous: Window.Callback,
val id: Int,
private val write: (String, Map<String, String>) -> Unit,
) : Window.Callback by previous {
var active = true
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
val boundary = event.actionMasked == MotionEvent.ACTION_DOWN ||
event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL
if (!active || !boundary) return previous.dispatchTouchEvent(event)
val fields = mutableMapOf(
"window" to "$id", "gesture" to "${event.downTime}",
"action" to "${event.actionMasked}", "eventMs" to "${event.eventTime}",
"x" to "${event.x}", "y" to "${event.y}",
"imeVisible" to if (Build.VERSION.SDK_INT >= 30) "${window.decorView.rootWindowInsets?.isVisible(WindowInsets.Type.ime())}" else "unsupported",
"imeBottom" to if (Build.VERSION.SDK_INT >= 30) "${window.decorView.rootWindowInsets?.getInsets(WindowInsets.Type.ime())?.bottom}" else "unsupported",
"focus" to describe(window.currentFocus),
)
// This is a hit-test prediction, not proof that the eventual responder accepted the press.
// Use RN's own pointer-events and transformed-coordinate rules, rather than approximating them.
val root = reactRootAt(window.decorView, event.x.toInt(), event.y.toInt())
if (root != null) {
try {
val origin = IntArray(2)
root.getLocationOnScreen(origin)
val path = TouchTargetHelper.findTargetPathAndCoordinatesForTouch(event.rawX - origin[0], event.rawY - origin[1], root, FloatArray(2))
path.take(10).forEachIndexed { index, target ->
fields["hit$index"] = "${target.getViewId()}:${describe(target.getView())}"
}
} catch (error: RuntimeException) {
fields["hitError"] = error.javaClass.simpleName
}
}
write("touch.window.before", fields)
val handled = previous.dispatchTouchEvent(event)
write("touch.window.after", mapOf(
"window" to "$id", "gesture" to "${event.downTime}",
"action" to "${event.actionMasked}", "handled" to "$handled",
"focus" to describe(window.currentFocus),
))
return handled
}
}
private fun describe(view: View?): String {
if (view == null) return "none"
// Deliberately omit text, accessibility labels, nativeID, and content descriptions.
val pointer = (view as? ReactPointerEventsView)?.pointerEvents
val testId = (view.getTag(R.id.react_test_id) as? String)?.takeIf { it in diagnosticTestIds } ?: ""
return "${view.javaClass.simpleName}#$testId/${view.id};p=$pointer;a=${view.alpha};enabled=${view.isEnabled};frame=${view.left},${view.top},${view.width},${view.height};translation=${view.translationX},${view.translationY}"
}
// Other test IDs include user-controlled entity IDs. Keep only fixed surface names.
private val diagnosticTestIds = setOf(
"message-input-root", "composer-fullscreen", "composer-viewport", "composer-viewport-content",
"composer-dock-content", "composer-track-scroll", "agent-list-backdrop", "file-explorer-backdrop",
"workspace-create-submit", "new-workspace-launch-submit", "compact-provider-list",
)
// Expo's developer menu adds a full-window Compose host above the React root. It passes ordinary
// touches through, but RN's helper cannot traverse that host. Start at the actual React surface;
// native modal windows expose a separate RootView and receive their own observation.
private fun reactRootAt(view: View, x: Int, y: Int): ViewGroup? {
if (view.visibility != View.VISIBLE || view.alpha == 0f) return null
// getGlobalVisibleRect is relative to the window root, including for offset/freeform windows.
val bounds = Rect()
if (!view.getGlobalVisibleRect(bounds) || !bounds.contains(x, y)) return null
if (view is RootView && view is ViewGroup) return view
if (view is ViewGroup) {
for (index in view.childCount - 1 downTo 0) {
reactRootAt(view.getChildAt(index), x, y)?.let { return it }
}
}
return null
}
@@ -1,3 +1,4 @@
import { recordComposerCommand } from "@/composer/touch-diagnostics";
import { forwardRef, useCallback, type ComponentProps } from "react";
import { Text, View, type PressableStateCallbackType } from "react-native";
import { StyleSheet } from "react-native-unistyles";
@@ -61,13 +62,18 @@ export const AgentControlTrigger = forwardRef<View, AgentControlTriggerProps>(
[disabled, isSheet, open, showToolbarLabel],
);
const activate = useCallback(() => {
recordComposerCommand("control");
onPress();
}, [onPress]);
return (
<ComboboxTrigger
{...triggerProps}
ref={ref}
collapsable={false}
disabled={disabled}
onPress={onPress}
onPress={activate}
style={triggerStyle}
accessibilityRole="button"
accessibilityLabel={accessibilityLabel}
@@ -1,3 +1,4 @@
import { recordComposerCommand } from "@/composer/touch-diagnostics";
import { useCallback, useMemo, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import {
@@ -206,6 +207,7 @@ export function CompactModelSheet({
);
const open = useCallback(() => {
recordComposerCommand("model");
Keyboard.dismiss();
rootBrowser.showAll();
setIsOpen(true);
+23 -13
View File
@@ -1,3 +1,4 @@
import { useComposerTouchDiagnostics, recordComposerCommand } from "@/composer/touch-diagnostics";
import { useKeyboardFocusSurface } from "@/keyboard";
import type { SendShortcut } from "@/hooks/use-settings";
import {
@@ -1761,6 +1762,7 @@ export const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
}, [resetComposerHeight]);
const handleSendMessage = useCallback(() => {
recordComposerCommand("send");
const liveValue = textInputRef.current?.getText() ?? valueRef.current;
if (!preserveHeightOnSubmit) {
updateLiveTextPresence("");
@@ -1790,19 +1792,18 @@ export const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
exitFullscreen,
]);
const handleQueueMessage = useCallback(
() =>
queueMessageImpl({
value: textInputRef.current?.getText() ?? valueRef.current,
attachments,
cwd,
onQueue,
replaceText,
onMinimizeHeight: minimizeInputHeight,
exitFullscreen,
}),
[attachments, cwd, onQueue, replaceText, minimizeInputHeight, exitFullscreen],
);
const handleQueueMessage = useCallback(() => {
recordComposerCommand("queue");
return queueMessageImpl({
value: textInputRef.current?.getText() ?? valueRef.current,
attachments,
cwd,
onQueue,
replaceText,
onMinimizeHeight: minimizeInputHeight,
exitFullscreen,
});
}, [attachments, cwd, onQueue, replaceText, minimizeInputHeight, exitFullscreen]);
const handleDefaultSendAction = useCallback(() => {
runDefaultSendAction({
@@ -2327,8 +2328,17 @@ export const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
handleRealtimeVoiceStop,
],
);
const touchDiagnostics = useComposerTouchDiagnostics({
active: isPaneFocused,
disabled,
sendDisabled: isSendButtonDisabled,
focused: isInputFocused,
fullscreen: isFullscreen,
voiceOverlay: showOverlay,
});
return (
<View
{...touchDiagnostics}
ref={rootRef}
style={
isFullscreen
@@ -0,0 +1,63 @@
import { useEffect } from "react";
import type { ComposerTouchState } from "./index";
import { Keyboard, type GestureResponderEvent } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { watchdog } from "@/watchdog";
/** Temporary companion to Android TouchDiagnostics. No draft text or identifiers are recorded. */
export function recordComposerCommand(command: "send" | "queue" | "control" | "model"): void {
watchdog?.record("touch.composer.command", { command });
}
function useRecordedComposerTouches(state: ComposerTouchState) {
const insets = useSafeAreaInsets();
const { active, disabled, sendDisabled, focused, fullscreen, voiceOverlay } = state;
useEffect(() => {
if (!active) return;
watchdog?.record("touch.composer.state", {
disabled: String(disabled),
sendDisabled: String(sendDisabled),
focused: String(focused),
fullscreen: String(fullscreen),
voiceOverlay: String(voiceOverlay),
});
}, [active, disabled, sendDisabled, focused, fullscreen, voiceOverlay]);
const record = (phase: string, event: GestureResponderEvent) => {
if (!watchdog) return;
const touch = event.nativeEvent;
watchdog.record("touch.composer", {
phase,
target: String(touch.target),
timestamp: String(touch.timestamp),
x: String(touch.pageX),
y: String(touch.pageY),
disabled: String(state.disabled),
sendDisabled: String(state.sendDisabled),
focused: String(state.focused),
fullscreen: String(state.fullscreen),
voiceOverlay: String(state.voiceOverlay),
keyboardVisible: String(Keyboard.isVisible()),
keyboardHeight: String(Keyboard.metrics()?.height),
safeBottom: String(insets.bottom),
});
};
return watchdog
? {
onStartShouldSetResponderCapture(event: GestureResponderEvent) {
record("capture", event);
return false;
},
onTouchEnd: (event: GestureResponderEvent) => record("end", event),
onTouchCancel: (event: GestureResponderEvent) => record("cancel", event),
}
: {};
}
function useNoComposerTouches(_state: ComposerTouchState) {
return {};
}
// Build configuration is fixed for the module lifetime. Ordinary builds subscribe to nothing.
export const useComposerTouchDiagnostics = watchdog
? useRecordedComposerTouches
: useNoComposerTouches;
@@ -0,0 +1,14 @@
/** Temporary diagnostics are Android-only. Metro selects index.android.ts on Android. */
export interface ComposerTouchState {
active: boolean;
disabled: boolean;
sendDisabled: boolean;
focused: boolean;
fullscreen: boolean;
voiceOverlay: boolean;
}
export function recordComposerCommand(_command: "send" | "queue" | "control" | "model"): void {}
export function useComposerTouchDiagnostics(_state: ComposerTouchState) {
return {};
}