From 033ddce290926e7acaef1b33402e47f2e2e23ec6 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Thu, 21 Nov 2024 11:33:49 -0500 Subject: [PATCH] Re-expose FrameClock in guest code (#2475) It can be used for other things. --- redwood-treehouse-guest/api/redwood-treehouse-guest.api | 1 + .../api/redwood-treehouse-guest.klib.api | 2 ++ .../app/cash/redwood/treehouse/StandardAppLifecycle.kt | 6 +++++- .../kotlin/app/cash/redwood/treehouse/treehouseCompose.kt | 8 +------- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/redwood-treehouse-guest/api/redwood-treehouse-guest.api b/redwood-treehouse-guest/api/redwood-treehouse-guest.api index 1b2c0092cb..7cefff4c04 100644 --- a/redwood-treehouse-guest/api/redwood-treehouse-guest.api +++ b/redwood-treehouse-guest/api/redwood-treehouse-guest.api @@ -1,6 +1,7 @@ public final class app/cash/redwood/treehouse/StandardAppLifecycle : app/cash/redwood/treehouse/AppLifecycle { public synthetic fun (Lapp/cash/redwood/protocol/guest/ProtocolWidgetSystemFactory;Lkotlinx/serialization/json/Json;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun close ()V + public final fun getFrameClock ()Landroidx/compose/runtime/MonotonicFrameClock; public fun getGuestProtocolVersion-7jYel6c ()Ljava/lang/String; public fun sendFrame (J)V public fun start (Lapp/cash/redwood/treehouse/AppLifecycle$Host;)V diff --git a/redwood-treehouse-guest/api/redwood-treehouse-guest.klib.api b/redwood-treehouse-guest/api/redwood-treehouse-guest.klib.api index 2ecd8dc684..34ff8047eb 100644 --- a/redwood-treehouse-guest/api/redwood-treehouse-guest.klib.api +++ b/redwood-treehouse-guest/api/redwood-treehouse-guest.klib.api @@ -9,6 +9,8 @@ final class app.cash.redwood.treehouse/StandardAppLifecycle : app.cash.redwood.treehouse/AppLifecycle { // app.cash.redwood.treehouse/StandardAppLifecycle|null[0] constructor (app.cash.redwood.protocol.guest/ProtocolWidgetSystemFactory, kotlinx.serialization.json/Json, kotlin/UInt) // app.cash.redwood.treehouse/StandardAppLifecycle.|(app.cash.redwood.protocol.guest.ProtocolWidgetSystemFactory;kotlinx.serialization.json.Json;kotlin.UInt){}[0] + final val frameClock // app.cash.redwood.treehouse/StandardAppLifecycle.frameClock|{}frameClock[0] + final fun (): androidx.compose.runtime/MonotonicFrameClock // app.cash.redwood.treehouse/StandardAppLifecycle.frameClock.|(){}[0] final val guestProtocolVersion // app.cash.redwood.treehouse/StandardAppLifecycle.guestProtocolVersion|{}guestProtocolVersion[0] final fun (): app.cash.redwood.protocol/RedwoodVersion // app.cash.redwood.treehouse/StandardAppLifecycle.guestProtocolVersion.|(){}[0] diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt index 0725962da3..bb4a33f5e1 100644 --- a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt @@ -15,6 +15,8 @@ */ package app.cash.redwood.treehouse +import androidx.compose.runtime.BroadcastFrameClock +import androidx.compose.runtime.MonotonicFrameClock import app.cash.redwood.protocol.EventTag import app.cash.redwood.protocol.Id import app.cash.redwood.protocol.RedwoodVersion @@ -49,11 +51,12 @@ public class StandardAppLifecycle( } } - internal fun requestHostFrame() { + private val broadcastFrameClock = BroadcastFrameClock { if (started) { host.requestFrame() } } + public val frameClock: MonotonicFrameClock get() = broadcastFrameClock internal val mismatchHandler: ProtocolMismatchHandler = object : ProtocolMismatchHandler { override fun onUnknownEvent(widgetTag: WidgetTag, tag: EventTag) { @@ -85,6 +88,7 @@ public class StandardAppLifecycle( } override fun sendFrame(timeNanos: Long) { + broadcastFrameClock.sendFrame(timeNanos) for (frameListener in frameListeners) { frameListener.onFrame(timeNanos) } diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/treehouseCompose.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/treehouseCompose.kt index 6382c8e3af..8d1663fef0 100644 --- a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/treehouseCompose.kt +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/treehouseCompose.kt @@ -15,7 +15,6 @@ */ package app.cash.redwood.treehouse -import androidx.compose.runtime.BroadcastFrameClock import androidx.compose.runtime.saveable.SaveableStateRegistry import app.cash.redwood.compose.RedwoodComposition import app.cash.redwood.protocol.Change @@ -60,12 +59,7 @@ private class RedwoodZiplineTreehouseUi( EventSink by guestAdapter, StandardAppLifecycle.FrameListener { - private val clock = BroadcastFrameClock { - appLifecycle.requestHostFrame() - } - override fun onFrame(timeNanos: Long) { - clock.sendFrame(timeNanos) guestAdapter.emitChanges() } @@ -139,7 +133,7 @@ private class RedwoodZiplineTreehouseUi( appLifecycle.addFrameListener(this) val composition = ProtocolRedwoodComposition( - scope = coroutineScope + clock, + scope = coroutineScope + appLifecycle.frameClock, guestAdapter = guestAdapter, widgetVersion = appLifecycle.widgetVersion, onBackPressedDispatcher = host.asOnBackPressedDispatcher(),