Skip to content

Commit 83a7d0c

Browse files
authored
Create Terminal.Theme, and use Terminal.Size more (#815)
The ternary theme more accurately models our knowledge, and the size propagates both cell and pixel values.
1 parent cbcdc34 commit 83a7d0c

File tree

15 files changed

+68
-38
lines changed

15 files changed

+68
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Changed:
1414
- Switched to our own terminal integration library. Report any issues with keyboard input, incorrect size reporting, or garbled output.
1515
- Only disable the cursor and emit synchronized rendering markers if the terminal reports support for those features.
1616
- `Static` function is now called `StaticEffect` to better indicate that it only renders its content once.
17-
- The runtime's `Terminal` was renamed to `TerminalState` to avoid conflict with new, lower-level `Terminal` type.
17+
- The runtime's `Terminal` was renamed to `TerminalState` to avoid conflict with new, lower-level `Terminal` type. Additionally, `TerminalState` now uses `Terminal.Theme` and `Terminal.Size` for exposing the theme and size, respectively.
1818
- `runMosaic` and `runMosaicBlocking` now accept a `NonInteractivePolicy` argument which dictates the behavior when Mosaic cannot connect directly to the TTY.
1919

2020
Fixed:

mosaic-runtime/api/mosaic-runtime.api

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public final class com/jakewharton/mosaic/TerminalKt {
4646

4747
public final class com/jakewharton/mosaic/TerminalState {
4848
public static final field $stable I
49-
public synthetic fun <init> (ZZJLkotlin/jvm/internal/DefaultConstructorMarker;)V
49+
public fun <init> (ZLcom/jakewharton/mosaic/terminal/Terminal$Theme;Lcom/jakewharton/mosaic/terminal/Terminal$Size;)V
5050
public fun equals (Ljava/lang/Object;)Z
51-
public final fun getDarkTheme ()Z
5251
public final fun getFocused ()Z
53-
public final fun getSize-Usd9Mdw ()J
52+
public final fun getSize ()Lcom/jakewharton/mosaic/terminal/Terminal$Size;
53+
public final fun getTheme ()Lcom/jakewharton/mosaic/terminal/Terminal$Theme;
5454
public fun hashCode ()I
5555
public fun toString ()Ljava/lang/String;
5656
}

mosaic-runtime/api/mosaic-runtime.klib.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,14 @@ final class com.jakewharton.mosaic/StaticLogger { // com.jakewharton.mosaic/Stat
402402
}
403403

404404
final class com.jakewharton.mosaic/TerminalState { // com.jakewharton.mosaic/TerminalState|null[0]
405-
constructor <init>(kotlin/Boolean, kotlin/Boolean, com.jakewharton.mosaic.ui.unit/IntSize) // com.jakewharton.mosaic/TerminalState.<init>|<init>(kotlin.Boolean;kotlin.Boolean;com.jakewharton.mosaic.ui.unit.IntSize){}[0]
405+
constructor <init>(kotlin/Boolean, com.jakewharton.mosaic.terminal/Terminal.Theme, com.jakewharton.mosaic.terminal/Terminal.Size) // com.jakewharton.mosaic/TerminalState.<init>|<init>(kotlin.Boolean;com.jakewharton.mosaic.terminal.Terminal.Theme;com.jakewharton.mosaic.terminal.Terminal.Size){}[0]
406406

407-
final val darkTheme // com.jakewharton.mosaic/TerminalState.darkTheme|{}darkTheme[0]
408-
final fun <get-darkTheme>(): kotlin/Boolean // com.jakewharton.mosaic/TerminalState.darkTheme.<get-darkTheme>|<get-darkTheme>(){}[0]
409407
final val focused // com.jakewharton.mosaic/TerminalState.focused|{}focused[0]
410408
final fun <get-focused>(): kotlin/Boolean // com.jakewharton.mosaic/TerminalState.focused.<get-focused>|<get-focused>(){}[0]
411409
final val size // com.jakewharton.mosaic/TerminalState.size|{}size[0]
412-
final fun <get-size>(): com.jakewharton.mosaic.ui.unit/IntSize // com.jakewharton.mosaic/TerminalState.size.<get-size>|<get-size>(){}[0]
410+
final fun <get-size>(): com.jakewharton.mosaic.terminal/Terminal.Size // com.jakewharton.mosaic/TerminalState.size.<get-size>|<get-size>(){}[0]
411+
final val theme // com.jakewharton.mosaic/TerminalState.theme|{}theme[0]
412+
final fun <get-theme>(): com.jakewharton.mosaic.terminal/Terminal.Theme // com.jakewharton.mosaic/TerminalState.theme.<get-theme>|<get-theme>(){}[0]
413413

414414
final fun equals(kotlin/Any?): kotlin/Boolean // com.jakewharton.mosaic/TerminalState.equals|equals(kotlin.Any?){}[0]
415415
final fun hashCode(): kotlin/Int // com.jakewharton.mosaic/TerminalState.hashCode|hashCode(){}[0]

mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/mosaic.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import com.jakewharton.mosaic.terminal.Terminal
2727
import com.jakewharton.mosaic.tty.Tty
2828
import com.jakewharton.mosaic.tty.terminal.asTerminalIn
2929
import com.jakewharton.mosaic.ui.BoxMeasurePolicy
30-
import com.jakewharton.mosaic.ui.unit.IntSize
3130
import kotlin.concurrent.Volatile
3231
import kotlin.coroutines.CoroutineContext
3332
import kotlin.coroutines.coroutineContext
@@ -176,8 +175,8 @@ internal class MosaicComposition(
176175
private var terminalState = mutableStateOf(
177176
TerminalState(
178177
terminal.state.focused.value,
179-
terminal.state.systemTheme.value,
180-
terminal.state.size.value.let { IntSize(it.columns, it.rows) },
178+
terminal.state.theme.value,
179+
terminal.state.size.value,
181180
),
182181
).also { state ->
183182
scope.launch(Unconfined, start = UNDISPATCHED) {
@@ -190,13 +189,13 @@ internal class MosaicComposition(
190189
}
191190
}
192191
scope.launch(Unconfined, start = UNDISPATCHED) {
193-
terminal.state.systemTheme.collect { systemTheme ->
194-
state.value = state.value.copy(darkTheme = systemTheme)
192+
terminal.state.theme.collect { theme ->
193+
state.value = state.value.copy(theme = theme)
195194
}
196195
}
197196
scope.launch(Unconfined, start = UNDISPATCHED) {
198197
terminal.state.size.collect { size ->
199-
state.value = state.value.copy(size = IntSize(size.columns, size.rows))
198+
state.value = state.value.copy(size = size)
200199
}
201200
}
202201
}

mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/nonInteractive.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal object NonInteractiveTerminal : Terminal, Terminal.State, Terminal.Capa
3232
override val events: ReceiveChannel<Event> = Channel<Event>().apply { close() }
3333

3434
override val focused: StateFlow<Boolean> = MutableStateFlow(true)
35-
override val systemTheme: StateFlow<Boolean> = MutableStateFlow(false)
35+
override val theme: StateFlow<Terminal.Theme> = MutableStateFlow(Terminal.Theme.Unknown)
3636
override val size: StateFlow<Terminal.Size> = MutableStateFlow(Terminal.Size.Default)
3737

3838
override val interactive: Boolean get() = false

mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/terminal.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.jakewharton.mosaic
33
import androidx.compose.runtime.Immutable
44
import androidx.compose.runtime.ProvidableCompositionLocal
55
import androidx.compose.runtime.compositionLocalOf
6-
import com.jakewharton.mosaic.ui.unit.IntSize
6+
import com.jakewharton.mosaic.terminal.Terminal
77
import dev.drewhamilton.poko.Poko
88

99
public val LocalTerminalState: ProvidableCompositionLocal<TerminalState> = compositionLocalOf {
@@ -13,13 +13,13 @@ public val LocalTerminalState: ProvidableCompositionLocal<TerminalState> = compo
1313
@[Immutable Poko]
1414
public class TerminalState(
1515
public val focused: Boolean,
16-
public val darkTheme: Boolean,
17-
public val size: IntSize,
16+
public val theme: Terminal.Theme,
17+
public val size: Terminal.Size,
1818
)
1919

2020
@Suppress("NOTHING_TO_INLINE")
2121
internal inline fun TerminalState.copy(
2222
focused: Boolean = this.focused,
23-
darkTheme: Boolean = this.darkTheme,
24-
size: IntSize = this.size,
25-
) = TerminalState(focused, darkTheme, size)
23+
theme: Terminal.Theme = this.theme,
24+
size: Terminal.Size = this.size,
25+
) = TerminalState(focused, theme, size)

mosaic-runtime/src/commonTest/kotlin/com/jakewharton/mosaic/CounterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CounterTest {
8888
var count by remember { mutableIntStateOf(0) }
8989

9090
Box(
91-
modifier = Modifier.width(LocalTerminalState.current.size.width),
91+
modifier = Modifier.width(LocalTerminalState.current.size.columns),
9292
contentAlignment = Alignment.Center,
9393
) {
9494
Text("The count is: $count")

mosaic-terminal/api/mosaic-terminal.api

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,16 @@ public final class com/jakewharton/mosaic/terminal/Terminal$Size$Companion {
298298
public abstract interface class com/jakewharton/mosaic/terminal/Terminal$State {
299299
public abstract fun getFocused ()Lkotlinx/coroutines/flow/StateFlow;
300300
public abstract fun getSize ()Lkotlinx/coroutines/flow/StateFlow;
301-
public abstract fun getSystemTheme ()Lkotlinx/coroutines/flow/StateFlow;
301+
public abstract fun getTheme ()Lkotlinx/coroutines/flow/StateFlow;
302+
}
303+
304+
public final class com/jakewharton/mosaic/terminal/Terminal$Theme : java/lang/Enum {
305+
public static final field Dark Lcom/jakewharton/mosaic/terminal/Terminal$Theme;
306+
public static final field Light Lcom/jakewharton/mosaic/terminal/Terminal$Theme;
307+
public static final field Unknown Lcom/jakewharton/mosaic/terminal/Terminal$Theme;
308+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
309+
public static fun valueOf (Ljava/lang/String;)Lcom/jakewharton/mosaic/terminal/Terminal$Theme;
310+
public static fun values ()[Lcom/jakewharton/mosaic/terminal/Terminal$Theme;
302311
}
303312

304313
public final class com/jakewharton/mosaic/terminal/TerminalColorEvent : com/jakewharton/mosaic/terminal/Event {

mosaic-terminal/api/mosaic-terminal.klib.api

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ abstract interface com.jakewharton.mosaic.terminal/Terminal : kotlin/AutoCloseab
2727
abstract val state // com.jakewharton.mosaic.terminal/Terminal.state|{}state[0]
2828
abstract fun <get-state>(): com.jakewharton.mosaic.terminal/Terminal.State // com.jakewharton.mosaic.terminal/Terminal.state.<get-state>|<get-state>(){}[0]
2929

30+
final enum class Theme : kotlin/Enum<com.jakewharton.mosaic.terminal/Terminal.Theme> { // com.jakewharton.mosaic.terminal/Terminal.Theme|null[0]
31+
enum entry Dark // com.jakewharton.mosaic.terminal/Terminal.Theme.Dark|null[0]
32+
enum entry Light // com.jakewharton.mosaic.terminal/Terminal.Theme.Light|null[0]
33+
enum entry Unknown // com.jakewharton.mosaic.terminal/Terminal.Theme.Unknown|null[0]
34+
35+
final val entries // com.jakewharton.mosaic.terminal/Terminal.Theme.entries|#static{}entries[0]
36+
final fun <get-entries>(): kotlin.enums/EnumEntries<com.jakewharton.mosaic.terminal/Terminal.Theme> // com.jakewharton.mosaic.terminal/Terminal.Theme.entries.<get-entries>|<get-entries>#static(){}[0]
37+
38+
final fun valueOf(kotlin/String): com.jakewharton.mosaic.terminal/Terminal.Theme // com.jakewharton.mosaic.terminal/Terminal.Theme.valueOf|valueOf#static(kotlin.String){}[0]
39+
final fun values(): kotlin/Array<com.jakewharton.mosaic.terminal/Terminal.Theme> // com.jakewharton.mosaic.terminal/Terminal.Theme.values|values#static(){}[0]
40+
}
41+
3042
abstract interface Capabilities { // com.jakewharton.mosaic.terminal/Terminal.Capabilities|null[0]
3143
abstract val ansiLevel // com.jakewharton.mosaic.terminal/Terminal.Capabilities.ansiLevel|{}ansiLevel[0]
3244
abstract fun <get-ansiLevel>(): com.jakewharton.mosaic.terminal/AnsiLevel // com.jakewharton.mosaic.terminal/Terminal.Capabilities.ansiLevel.<get-ansiLevel>|<get-ansiLevel>(){}[0]
@@ -51,8 +63,8 @@ abstract interface com.jakewharton.mosaic.terminal/Terminal : kotlin/AutoCloseab
5163
abstract fun <get-focused>(): kotlinx.coroutines.flow/StateFlow<kotlin/Boolean> // com.jakewharton.mosaic.terminal/Terminal.State.focused.<get-focused>|<get-focused>(){}[0]
5264
abstract val size // com.jakewharton.mosaic.terminal/Terminal.State.size|{}size[0]
5365
abstract fun <get-size>(): kotlinx.coroutines.flow/StateFlow<com.jakewharton.mosaic.terminal/Terminal.Size> // com.jakewharton.mosaic.terminal/Terminal.State.size.<get-size>|<get-size>(){}[0]
54-
abstract val systemTheme // com.jakewharton.mosaic.terminal/Terminal.State.systemTheme|{}systemTheme[0]
55-
abstract fun <get-systemTheme>(): kotlinx.coroutines.flow/StateFlow<kotlin/Boolean> // com.jakewharton.mosaic.terminal/Terminal.State.systemTheme.<get-systemTheme>|<get-systemTheme>(){}[0]
66+
abstract val theme // com.jakewharton.mosaic.terminal/Terminal.State.theme|{}theme[0]
67+
abstract fun <get-theme>(): kotlinx.coroutines.flow/StateFlow<com.jakewharton.mosaic.terminal/Terminal.Theme> // com.jakewharton.mosaic.terminal/Terminal.State.theme.<get-theme>|<get-theme>(){}[0]
5668
}
5769

5870
final class Size { // com.jakewharton.mosaic.terminal/Terminal.Size|null[0]

mosaic-terminal/src/commonMain/kotlin/com/jakewharton/mosaic/terminal/Terminal.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface Terminal : AutoCloseable {
1111

1212
public interface State {
1313
public val focused: StateFlow<Boolean>
14-
public val systemTheme: StateFlow<Boolean>
14+
public val theme: StateFlow<Theme>
1515
public val size: StateFlow<Size>
1616
}
1717

@@ -37,4 +37,10 @@ public interface Terminal : AutoCloseable {
3737
public val Default: Size = Size(80, 24)
3838
}
3939
}
40+
41+
public enum class Theme {
42+
Unknown,
43+
Light,
44+
Dark,
45+
}
4046
}

0 commit comments

Comments
 (0)