library-ui: signal fully-drawn only when content is interactive
- Commit
c883e70e328b052c1105472a1b8a2eb556da58c4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-05 23:03 UTC (4 hours ago)
- Parents
d8480f2
StartupTiming called Activity.reportFullyDrawn at the first drawn frame, so time-to-fully-drawn always equalled time-to-initial-display and hid the real time until primary content was usable. FirstFrameGate now only records the first-frame (TTID) mark. The sole reportFullyDrawn caller is StartupTiming.onFullyDrawn, reached via the new FullyDrawnWhen(ready) composable or MaActivity/MaFragmentActivity.signalFullyDrawn, both gated one-shot by the pure FullyDrawnGate. Screens drive ready from loaded-content state; apps that cannot know content readiness should not signal at all (TTID only) rather than fake TTFD. Reference adoption (:notes): NotesListPage calls FullyDrawnWhen(ready = hasLoaded), which flips on the first notes DAO emission, i.e. when the list or its loaded-empty state is interactive. Verified: :library:ui testDevUnitTest (including new FullyDrawnGateTest), lint and lintVitalRelease pass; :notes lint, lintVitalRelease, testDevUnitTest and assembleRelease pass.
Diff
library/ui/AGENTS.md
diff --git a/library/ui/AGENTS.md b/library/ui/AGENTS.mdindex f29467e5ab..020c6ae62f 100644--- a/library/ui/AGENTS.md+++ b/library/ui/AGENTS.md@@ -28,7 +28,8 @@ ## Key files ### Shell + scaffolds — start here - com/vayunmathur/library/ui/MaActivity.kt — shared activity shell (incl. `MaFragmentActivity`); installs the custom splash (`installSplashScreen`, never held, no exit animation — the theme in `:library` supplies icon/background) before `super.onCreate`; `deferAfterFirstFrame` queues non-essential startup work past the first drawn frame-- com/vayunmathur/library/ui/StartupTiming.kt — permanent cold-start marks (activityCreate→firstFrame, logged via `Log`) + `FirstFrameGate`/`StartupMarks`+- com/vayunmathur/library/ui/StartupTiming.kt — permanent cold-start marks (activityCreate→firstFrame→fullyDrawn, logged via `Log`) + `FirstFrameGate`/`StartupMarks`/`FullyDrawnGate`; first frame is TTID only, `reportFullyDrawn` fires solely from `StartupTiming.onFullyDrawn`+- com/vayunmathur/library/ui/FullyDrawn.kt — `FullyDrawnWhen(ready)` composable: signals TTFD once primary content is interactive (drive `ready` from loaded-content state, never first frame) - com/vayunmathur/library/ui/AppScaffold.kt - com/vayunmathur/library/ui/DetailScaffold.kt - com/vayunmathur/library/ui/LazyListScaffold.kt
library/ui/src/main/java/com/vayunmathur/library/ui/FullyDrawn.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/FullyDrawn.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/FullyDrawn.ktnew file mode 100644index 0000000000..425880f1de--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/FullyDrawn.kt@@ -0,0 +1,33 @@+package com.vayunmathur.library.ui++import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.remember+import androidx.compose.ui.platform.LocalContext++/**+ * Signals time-to-fully-drawn (TTFD) when [ready] first becomes true.+ *+ * Place this at the screen that owns the app's primary content and drive+ * [ready] from real content state — e.g. the first non-loading emission of+ * the screen's data (`hasLoaded`, a non-empty list state, or an explicit+ * loaded-empty state) — never from composition alone. The first frame stays+ * TTID (measured by [StartupTiming]); this effect is what makes+ * `reportFullyDrawn` mean "interactive with primary content", per the+ * Android performance guidance. One-shot per activity via [FullyDrawnGate]:+ * later recompositions and re-emissions do not re-signal.+ *+ * Apps whose screens cannot know when content is ready should not call+ * this at all: they report TTID only, with no fully-drawn signal, rather+ * than faking TTFD at the first frame.+ */+@Composable+fun FullyDrawnWhen(ready: Boolean) {+ val context = LocalContext.current+ val gate = remember { FullyDrawnGate() }+ LaunchedEffect(ready) {+ if (ready && gate.signal()) {+ context.findActivity()?.let { StartupTiming.onFullyDrawn(it) }+ }+ }+}
library/ui/src/main/java/com/vayunmathur/library/ui/MaActivity.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/MaActivity.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/MaActivity.ktindex 113f14133e..64f2fcc69a 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/MaActivity.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/MaActivity.kt@@ -57,6 +57,15 @@ abstract class MaActivity : ComponentActivity() { firstFrameGate.defer(window.decorView, block) } + /**+ * Signals time-to-fully-drawn for non-Compose readiness (e.g. a view+ * callback). Compose screens should prefer `FullyDrawnWhen(ready)`.+ * Call only once primary content is interactive; never at first frame.+ */+ protected fun signalFullyDrawn() {+ StartupTiming.onFullyDrawn(this)+ }+ /** * Applies edge-to-edge. Override for a custom style — the camera forces a * black navigation bar because its UI is always dark.@@ -123,6 +132,11 @@ abstract class MaFragmentActivity : FragmentActivity() { firstFrameGate.defer(window.decorView, block) } + /** See [MaActivity.signalFullyDrawn]. */+ protected fun signalFullyDrawn() {+ StartupTiming.onFullyDrawn(this)+ }+ /** Applies edge-to-edge; override for a custom style. See [MaActivity.applyEdgeToEdge]. */ protected open fun applyEdgeToEdge() { enableEdgeToEdge()
library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.ktindex f9396422d4..4cd4d2559c 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/StartupTiming.kt@@ -41,6 +41,7 @@ class StartupMarks(private val clock: () -> Long) { const val BEFORE_CONTENT = "beforeContent" const val CONTENT_SET = "contentSet" const val FIRST_FRAME = "firstFrame"+ const val FULLY_DRAWN = "fullyDrawn" } } @@ -61,6 +62,44 @@ object StartupTiming { val total = marks.elapsed(StartupMarks.ACTIVITY_CREATE, StartupMarks.FIRST_FRAME) Log.status(TAG, "${activity.javaClass.simpleName} first frame in ${total}ms marks=${marks.snapshot()}") }++ /**+ * Time-to-fully-drawn: call only when primary content is on screen and+ * the app is interactive. This is the sole caller of+ * [Activity.reportFullyDrawn] in the repo; the first-frame path+ * deliberately does not call it, so TTFD never equals TTID by default.+ * One-shot: later calls are ignored.+ */+ fun onFullyDrawn(activity: Activity) {+ if (marks.snapshot().containsKey(StartupMarks.FULLY_DRAWN)) return+ marks.mark(StartupMarks.FULLY_DRAWN)+ val total = marks.elapsed(StartupMarks.ACTIVITY_CREATE, StartupMarks.FULLY_DRAWN)+ Log.status(TAG, "${activity.javaClass.simpleName} fully drawn in ${total}ms marks=${marks.snapshot()}")+ runCatching { activity.reportFullyDrawn() }+ }++ /** Fully-drawn elapsed ms for tests and callers; null until signalled. */+ fun fullyDrawnElapsed(from: String = StartupMarks.ACTIVITY_CREATE): Long? =+ marks.elapsed(from, StartupMarks.FULLY_DRAWN)+}++/**+ * One-shot gate for the fully-drawn signal: the first [signal] wins, later+ * signals (recompositions, list re-emissions) are no-ops. Pure and+ * clock-free so the one-shot contract is unit-testable on the host; the+ * Android side effect lives in [StartupTiming.onFullyDrawn].+ */+class FullyDrawnGate {+ private var fired = false++ /** Returns true exactly once, on the first call. */+ fun signal(): Boolean {+ if (fired) return false+ fired = true+ return true+ }++ val hasFired: Boolean get() = fired } /**@@ -92,7 +131,9 @@ class FirstFrameGate { // removeOnDrawListener is illegal inside onDraw; detach next frame. view.post { view.viewTreeObserver.removeOnDrawListener(this) } StartupTiming.onFirstFrame(activity)- runCatching { activity.reportFullyDrawn() }+ // No reportFullyDrawn here: first frame is TTID, not TTFD.+ // Apps signal fully-drawn via FullyDrawnWhen/SignalFullyDrawn+ // once primary content is interactive. val blocks = pending.toList() pending.clear() view.post { blocks.forEach { it() } }
library/ui/src/test/java/com/vayunmathur/library/ui/FullyDrawnGateTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/FullyDrawnGateTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/FullyDrawnGateTest.ktnew file mode 100644index 0000000000..c0b5e45784--- /dev/null+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/FullyDrawnGateTest.kt@@ -0,0 +1,18 @@+package com.vayunmathur.library.ui++import kotlin.test.Test+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/** One-shot contract behind the TTFD signal. */+class FullyDrawnGateTest {++ @Test fun firstSignalWinsAndLaterSignalsAreNoOps() {+ val gate = FullyDrawnGate()+ assertFalse(gate.hasFired)+ assertTrue(gate.signal())+ assertTrue(gate.hasFired)+ assertFalse(gate.signal())+ assertFalse(gate.signal())+ }+}
notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.ktindex 0e926639a2..1badb69114 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListPage.kt@@ -52,6 +52,7 @@ import com.vayunmathur.notes.data.noteDbConfigs import com.vayunmathur.notes.platform.NotesListActions import com.vayunmathur.notes.platform.NotesListUiState import com.vayunmathur.notes.platform.NotesViewModel+import com.vayunmathur.library.ui.FullyDrawnWhen import com.vayunmathur.library.ui.ReorderableItem import com.vayunmathur.library.ui.rememberReorderableLazyListState import com.vayunmathur.library.ui.reorderDragHandle@@ -65,6 +66,9 @@ fun NotesListPage(backStack: NavBackStack<Route>, viewModel: NotesViewModel) { val searchQuery by viewModel.searchQuery.collectAsStateWithLifecycle() val selectedTag by viewModel.selectedTag.collectAsStateWithLifecycle() val hasLoaded by viewModel.hasLoadedNotes.collectAsStateWithLifecycle()+ // TTFD: hasLoaded flips on the first DAO emission, i.e. the list (or its+ // loaded-empty state) is on screen and interactive — not first frame.+ FullyDrawnWhen(ready = hasLoaded) val actions = remember(backStack, viewModel) { object : NotesListActions {