Skip to content

Commit

Permalink
revert flatten reversal, flatten all styles before adding them to the…
Browse files Browse the repository at this point in the history
… annotation
  • Loading branch information
sproctor committed Jan 23, 2025
1 parent 97825d4 commit e62877e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, StyledString>,
Expand All @@ -29,7 +30,6 @@ fun StyledString.toAnnotatedString(
}
}

@OptIn(ExperimentalTextApi::class)
fun StyleDefinition.toSpanStyle(): SpanStyle {
return SpanStyle(
color = textColor.toColor(),
Expand All @@ -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" ->
Expand All @@ -78,8 +79,10 @@ fun StyledStringLeaf.toAnnotatedString(
append(it)
}
}
styles.forEach { style ->
if (style != null) {
pop()
}
styles.forEach { style ->
style.annotation?.let { _ -> pop() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>): StyleDefinition? {
return styles
.reduceOrNull { acc, warlockStyle ->
warlockStyle.mergeWith(acc)
acc.mergeWith(warlockStyle)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class StormfrontClient(

private var currentStyle: WarlockStyle? = null
private val styleStack = Stack<WarlockStyle>()

// Output style gets applied to echoed text as well
private var outputStyle: WarlockStyle? = null

Expand Down Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit e62877e

Please sign in to comment.