From e62877e63c36a135b5e374433c99b5b5a96c9bdb Mon Sep 17 00:00:00 2001 From: Sean Proctor Date: Thu, 23 Jan 2025 17:20:43 -0500 Subject: [PATCH] revert flatten reversal, flatten all styles before adding them to the annotation --- .../warlock3/compose/util/StyledStringHelpers.kt | 11 +++++++---- .../warlockfe/warlock3/core/text/StyleDefinition.kt | 5 ++--- .../warlock3/stormfront/network/StormfrontClient.kt | 4 +++- .../stormfront/protocol/elements/OutputHandler.kt | 10 +--------- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/compose/src/commonMain/kotlin/warlockfe/warlock3/compose/util/StyledStringHelpers.kt b/compose/src/commonMain/kotlin/warlockfe/warlock3/compose/util/StyledStringHelpers.kt index 982f294e..98481066 100644 --- a/compose/src/commonMain/kotlin/warlockfe/warlock3/compose/util/StyledStringHelpers.kt +++ b/compose/src/commonMain/kotlin/warlockfe/warlock3/compose/util/StyledStringHelpers.kt @@ -16,6 +16,7 @@ import warlockfe.warlock3.core.text.StyledStringLeaf import warlockfe.warlock3.core.text.StyledStringSubstring import warlockfe.warlock3.core.text.StyledStringVariable import warlockfe.warlock3.core.text.WarlockStyle +import warlockfe.warlock3.core.text.flattenStyles fun StyledString.toAnnotatedString( variables: Map, @@ -29,7 +30,6 @@ fun StyledString.toAnnotatedString( } } -@OptIn(ExperimentalTextApi::class) fun StyleDefinition.toSpanStyle(): SpanStyle { return SpanStyle( color = textColor.toColor(), @@ -52,9 +52,10 @@ fun StyledStringLeaf.toAnnotatedString( actionHandler: (String) -> Unit, ): AnnotatedString { return buildAnnotatedString { + val style = flattenStyles(styles.map { it.toStyleDefinition(styleMap) }) + ?.also { pushStyle(it.toSpanStyle()) } + styles.forEach { style -> - val styleDef = style.toStyleDefinition(styleMap) - pushStyle(styleDef.toSpanStyle()) style.annotation?.let { annotation -> when (val tag = annotation.first) { "action" -> @@ -78,8 +79,10 @@ fun StyledStringLeaf.toAnnotatedString( append(it) } } - styles.forEach { style -> + if (style != null) { pop() + } + styles.forEach { style -> style.annotation?.let { _ -> pop() } } } diff --git a/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/StyleDefinition.kt b/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/StyleDefinition.kt index 9f0140d3..bec04473 100644 --- a/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/StyleDefinition.kt +++ b/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/StyleDefinition.kt @@ -25,12 +25,11 @@ data class StyleDefinition( } /* - * Priority goes to later styles in the list - * Used to be opposite changing for 3.0.43 + * Priority goes to earlier styles in the list */ fun flattenStyles(styles: List): StyleDefinition? { return styles .reduceOrNull { acc, warlockStyle -> - warlockStyle.mergeWith(acc) + acc.mergeWith(warlockStyle) } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt index 0dea578b..032caf11 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt @@ -247,6 +247,7 @@ class StormfrontClient( private var currentStyle: WarlockStyle? = null private val styleStack = Stack() + // Output style gets applied to echoed text as well private var outputStyle: WarlockStyle? = null @@ -350,7 +351,8 @@ class StormfrontClient( styleStack.push(event.style) StormfrontPopStyleEvent -> - styleStack.pop() + if (styleStack.isNotEmpty()) + styleStack.pop() is StormfrontPromptEvent -> { currentTypeAhead.update { max(0, it - 1) } diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/OutputHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/OutputHandler.kt index 95c1acae..b72d587e 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/OutputHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/OutputHandler.kt @@ -8,14 +8,6 @@ import warlockfe.warlock3.stormfront.protocol.StormfrontOutputEvent class OutputHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontOutputEvent { val className = element.attributes["class"] - return StormfrontOutputEvent(style = getStyleByClass(className)) + return StormfrontOutputEvent(style = className?.ifBlank { null }?.let { WarlockStyle(it) }) } } - -fun getStyleByClass(name: String?): WarlockStyle? { - return if (name?.isNotBlank() == true) { - WarlockStyle(name) - } else { - null - } -} \ No newline at end of file