Skip to content

Commit 3ea9c57

Browse files
committed
feature: add getDefaultViewModelInfo to experimental API
Add the ability to query the default view model name and instance name for an artboard through the experimental CommandQueue API. This mirrors the iOS experimental SDK's File.getDefaultViewModelInfo(for:) which already exists. The name is needed for file-level queries like getViewModelProperties() and getViewModelInstanceNames() which are keyed by view model name — without it, ViewModelSource.DefaultForArtboard users cannot access those APIs. Changes: - CommandQueueBridge: add cppGetDefaultViewModelInfo JNI declaration - CommandQueue: add getDefaultViewModelInfo suspend function + callback - RiveFile: add public getDefaultViewModelInfo(artboard) API - DefaultViewModelInfo: new data class for the result - bindings_command_queue.cpp: JNI binding + ArtboardListener callback
1 parent 6ee8e8d commit 3ea9c57

4 files changed

Lines changed: 128 additions & 0 deletions

File tree

kotlin/src/main/cpp/src/bindings/bindings_command_queue.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ class ArtboardListener : public rive::CommandQueue::ArtboardListener
291291
jList.get());
292292
}
293293

294+
void onDefaultViewModelInfoReceived(
295+
const rive::ArtboardHandle,
296+
uint64_t requestID,
297+
std::string viewModelName,
298+
std::string instanceName) override
299+
{
300+
auto env = GetJNIEnv();
301+
auto jViewModelName = MakeJString(env, viewModelName);
302+
auto jInstanceName = MakeJString(env, instanceName);
303+
m_queue.call("onDefaultViewModelInfoReceived",
304+
"(JLjava/lang/String;Ljava/lang/String;)V",
305+
requestID,
306+
jViewModelName.get(),
307+
jInstanceName.get());
308+
}
309+
294310
private:
295311
JCommandQueue m_queue;
296312
};
@@ -985,6 +1001,25 @@ extern "C"
9851001
commandQueue->requestStateMachineNames(artboardHandle, requestID);
9861002
}
9871003

1004+
JNIEXPORT void JNICALL
1005+
Java_app_rive_core_CommandQueueJNIBridge_cppGetDefaultViewModelInfo(
1006+
JNIEnv*,
1007+
jobject,
1008+
jlong ref,
1009+
jlong requestID,
1010+
jlong jFileHandle,
1011+
jlong jArtboardHandle)
1012+
{
1013+
auto commandQueue = reinterpret_cast<rive::CommandQueue*>(ref);
1014+
auto fileHandle = handleFromLong<rive::FileHandle>(jFileHandle);
1015+
auto artboardHandle =
1016+
handleFromLong<rive::ArtboardHandle>(jArtboardHandle);
1017+
1018+
commandQueue->requestDefaultViewModelInfo(artboardHandle,
1019+
fileHandle,
1020+
requestID);
1021+
}
1022+
9881023
JNIEXPORT void JNICALL
9891024
Java_app_rive_core_CommandQueueJNIBridge_cppGetViewModelNames(
9901025
JNIEnv*,

kotlin/src/main/kotlin/app/rive/RiveFile.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.runtime.Stable
66
import androidx.compose.runtime.produceState
77
import androidx.compose.ui.platform.LocalContext
88
import app.rive.core.CloseOnce
9+
import app.rive.core.DefaultViewModelInfo
910
import app.rive.core.FileHandle
1011
import app.rive.core.RiveWorker
1112
import app.rive.core.SuspendLazy
@@ -144,6 +145,25 @@ class RiveFile internal constructor(
144145
private val enumsCache = SuspendLazy {
145146
riveWorker.getEnums(fileHandle)
146147
}
148+
149+
/**
150+
* Retrieves the default view model information for an artboard.
151+
*
152+
* View models define the data structure and bindings for an artboard. This method allows
153+
* discovery of which view model and instance are associated with the artboard by default,
154+
* as assigned in the Rive editor.
155+
*
156+
* This is useful when using [ViewModelSource.DefaultForArtboard] to create a view model
157+
* instance, since that path does not reveal the view model name. The name is required for
158+
* file-level queries like [getViewModelProperties] and [getViewModelInstanceNames].
159+
*
160+
* @param artboard The artboard to query for default view model information.
161+
* @return A [DefaultViewModelInfo] containing the view model name and instance name.
162+
* @throws RuntimeException If no default view model is found for the artboard.
163+
* @throws CancellationException If the coroutine is cancelled before the operation completes.
164+
*/
165+
suspend fun getDefaultViewModelInfo(artboard: Artboard): DefaultViewModelInfo =
166+
riveWorker.getDefaultViewModelInfo(fileHandle, artboard.artboardHandle)
147167
}
148168

149169
/**

kotlin/src/main/kotlin/app/rive/core/CommandQueue.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,51 @@ class CommandQueue(
560560
(pendingContinuations.remove(requestID) as? Continuation<List<String>>)?.resume(names)
561561
}
562562

563+
/**
564+
* Query the default view model info for an artboard. Returns on
565+
* [onDefaultViewModelInfoReceived].
566+
*
567+
* @param fileHandle The handle of the file containing the artboard.
568+
* @param artboardHandle The handle of the artboard to query.
569+
* @return A [DefaultViewModelInfo] containing the view model name and instance name.
570+
* @throws RuntimeException If the artboard or file handle is invalid, or no default view model
571+
* is found.
572+
* @throws IllegalStateException If the CommandQueue has been released.
573+
* @throws CancellationException If the coroutine is cancelled before the operation completes.
574+
*/
575+
@Throws(RuntimeException::class, IllegalStateException::class, CancellationException::class)
576+
suspend fun getDefaultViewModelInfo(
577+
fileHandle: FileHandle,
578+
artboardHandle: ArtboardHandle
579+
): DefaultViewModelInfo =
580+
suspendNativeRequest { requestID ->
581+
bridge.cppGetDefaultViewModelInfo(
582+
cppPointer.pointer,
583+
requestID,
584+
fileHandle.handle,
585+
artboardHandle.handle
586+
)
587+
}
588+
589+
/**
590+
* Callback when the default view model info is received, from [getDefaultViewModelInfo].
591+
*
592+
* @param requestID The request ID used when querying, used to complete the continuation.
593+
* @param viewModelName The name of the default view model for the artboard.
594+
* @param instanceName The name of the default view model instance for the artboard.
595+
*/
596+
@Keep // Called from JNI
597+
@Suppress("Unused")
598+
@JvmName("onDefaultViewModelInfoReceived")
599+
internal fun onDefaultViewModelInfoReceived(
600+
requestID: Long,
601+
viewModelName: String,
602+
instanceName: String
603+
) {
604+
(pendingContinuations.remove(requestID) as? Continuation<DefaultViewModelInfo>)
605+
?.resume(DefaultViewModelInfo(viewModelName, instanceName))
606+
}
607+
563608
/**
564609
* Query the file for available view model names. Returns on [onViewModelsListed].
565610
*
@@ -2347,6 +2392,20 @@ value class FontHandle(val handle: Long) {
23472392
override fun toString(): String = "FontHandle($handle)"
23482393
}
23492394

2395+
/**
2396+
* Information about the default view model for an artboard.
2397+
*
2398+
* Returned by [CommandQueue.getDefaultViewModelInfo]. Contains the name of the default view model
2399+
* and the name of its default instance, as assigned in the Rive editor.
2400+
*
2401+
* @param viewModelName The name of the default view model type for the artboard.
2402+
* @param instanceName The name of the default view model instance for the artboard.
2403+
*/
2404+
data class DefaultViewModelInfo(
2405+
val viewModelName: String,
2406+
val instanceName: String
2407+
)
2408+
23502409
/**
23512410
* A key used to uniquely identify a draw operation in the CommandQueue. This is useful when the
23522411
* same CommandQueue issues multiple draw calls. If the same key is used before the render loop

kotlin/src/main/kotlin/app/rive/core/CommandQueueBridge.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ interface CommandQueueBridge {
3030
artboardHandle: Long
3131
)
3232

33+
fun cppGetDefaultViewModelInfo(
34+
pointer: Long,
35+
requestID: Long,
36+
fileHandle: Long,
37+
artboardHandle: Long
38+
)
39+
3340
fun cppGetViewModelNames(
3441
pointer: Long,
3542
requestID: Long,
@@ -466,6 +473,13 @@ internal class CommandQueueJNIBridge : CommandQueueBridge {
466473
artboardHandle: Long
467474
)
468475

476+
external override fun cppGetDefaultViewModelInfo(
477+
pointer: Long,
478+
requestID: Long,
479+
fileHandle: Long,
480+
artboardHandle: Long
481+
)
482+
469483
external override fun cppGetViewModelNames(
470484
pointer: Long,
471485
requestID: Long,

0 commit comments

Comments
 (0)