Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dedicated test-only path with different responsibility split #745

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.jakewharton.mosaic.ui.BoxMeasurePolicy
import com.jakewharton.mosaic.ui.unit.IntSize
import kotlin.concurrent.Volatile
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -76,21 +77,17 @@ public fun runMosaicBlocking(content: @Composable () -> Unit) {
}
}

public suspend fun runMosaic(content: @Composable () -> Unit) {
runMosaic(isTest = false, content)
}

private const val StageDeviceAttributes = 3
private const val StageCapabilityQueries = 2
private const val StageDefaultQueries = 1
private const val StageNormalOperation = 0

internal suspend fun runMosaic(isTest: Boolean, content: @Composable () -> Unit) {
public suspend fun runMosaic(content: @Composable () -> Unit) {
val reader = TerminalReader()

// Entering raw mode can fail, so perform it before any additional control sequences which change
// settings. We also need to be in character mode to query capabilities with control sequences.
if (!isTest && env("MOSAIC_RAW_MODE") != "false") {
if (env("MOSAIC_RAW_MODE") != "false") {
reader.enableRawMode()
}

Expand Down Expand Up @@ -279,7 +276,7 @@ internal suspend fun runMosaic(isTest: Boolean, content: @Composable () -> Unit)
}
print("\r\n")

if (!toggleInBandResize && !isTest) {
if (!toggleInBandResize) {
terminalState.update {
copy(
size = reader.currentSize().let { size ->
Expand All @@ -295,36 +292,46 @@ internal suspend fun runMosaic(isTest: Boolean, content: @Composable () -> Unit)
synchronizedRendering = supportsSynchronizedRendering,
)

val clock = BroadcastFrameClock()
val mosaicComposition = MosaicComposition(
coroutineContext = coroutineContext + clock,
onDraw = { rootNode ->
print(rendering.render(rootNode).toString())
},
keyEvents = keyEvents,
terminalState = terminalState,
)

mosaicComposition.setContent(content)
runMosaicComposition(rendering, keyEvents, terminalState, content)

mosaicComposition.scope.launch {
while (true) {
clock.sendFrame(nanoTime())

// "1000 FPS should be enough for anybody"
// We need to yield in order for other coroutines on this dispatcher to run, otherwise
// this is effectively a spin loop. We do a delay instead of a yield since dispatchers
// are not required to support yield, but reasonable delay support is almost a guarantee.
delay(1)
}
}

mosaicComposition.awaitComplete()
eventJob.cancel()
},
)
}

internal suspend fun runMosaicComposition(
rendering: Rendering,
keyEvents: Channel<KeyEvent>,
terminalState: MutableState<Terminal>,
content: @Composable (() -> Unit),
) {
val clock = BroadcastFrameClock()
val mosaicComposition = MosaicComposition(
coroutineContext = coroutineContext + clock,
onDraw = { rootNode ->
print(rendering.render(rootNode).toString())
},
keyEvents = keyEvents,
terminalState = terminalState,
)

mosaicComposition.setContent(content)

mosaicComposition.scope.launch {
while (true) {
clock.sendFrame(nanoTime())

// "1000 FPS should be enough for anybody"
// We need to yield in order for other coroutines on this dispatcher to run, otherwise
// this is effectively a spin loop. We do a delay instead of a yield since dispatchers
// are not required to support yield, but reasonable delay support is almost a guarantee.
delay(1)
}
}

mosaicComposition.awaitComplete()
}

internal inline fun <T> MutableState<T>.update(updater: T.() -> T) {
value = value.updater()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -276,7 +277,11 @@ class MosaicTest {
var frameTimeA = 0L
var frameTimeB = 0L

runMosaic(isTest = true) {
runMosaicComposition(
rendering = AnsiRendering(),
keyEvents = Channel(),
terminalState = mutableStateOf(Terminal.Default),
) {
LaunchedEffect(Unit) {
withFrameNanos { frameTimeNanos ->
frameTimeA = frameTimeNanos
Expand Down
Loading