mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
fix(agent): keep replies with tables still while the theme color cycles
The Markdown renderer baked the link and code colors into its spans and was rebuilt on every color step, so each step parsed the reply again and reset its table rows, which measure one line high until drawn. Link and code colors now come from the TextView and a live code theme, so a color step only recolors the reply.
This commit is contained in:
1 parent
80fc5dec07
commit
5c8a54ef95
3 files changed
+138
-11
No files matched your search
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Paint
|
||||
import android.net.Uri
|
||||
import android.text.Selection
|
||||
import android.text.Spannable
|
||||
@@ -22,8 +23,11 @@ import com.droiddeck.launcher.R
|
||||
import io.noties.markwon.AbstractMarkwonPlugin
|
||||
import io.noties.markwon.Markwon
|
||||
import io.noties.markwon.MarkwonConfiguration
|
||||
import io.noties.markwon.MarkwonSpansFactory
|
||||
import io.noties.markwon.MarkwonVisitor
|
||||
import io.noties.markwon.core.MarkwonTheme
|
||||
import io.noties.markwon.core.spans.CodeBlockSpan
|
||||
import io.noties.markwon.core.spans.CodeSpan
|
||||
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
|
||||
import io.noties.markwon.ext.tables.TableAwareMovementMethod
|
||||
import io.noties.markwon.ext.tables.TablePlugin
|
||||
@@ -35,7 +39,10 @@ import org.commonmark.ext.gfm.tables.TableCell
|
||||
import org.commonmark.ext.gfm.tables.TableHead
|
||||
import org.commonmark.ext.gfm.tables.TableRow
|
||||
import org.commonmark.node.AbstractVisitor
|
||||
import org.commonmark.node.Code
|
||||
import org.commonmark.node.CustomNode
|
||||
import org.commonmark.node.FencedCodeBlock
|
||||
import org.commonmark.node.IndentedCodeBlock
|
||||
import org.commonmark.node.Node
|
||||
|
||||
@Composable
|
||||
@@ -46,9 +53,8 @@ internal fun AgentMarkdown(text: String, modifier: Modifier) {
|
||||
val density = LocalDensity.current
|
||||
val textSize = with(density) { 15.sp.toPx() }
|
||||
val lineSpacing = with(density) { 3.sp.toPx() }
|
||||
val renderer = remember(context, pal.primary2, pal.surfaceVariant, density) {
|
||||
agentMarkdownRenderer(context, pal.primary2.toArgb(), pal.surfaceVariant.toArgb())
|
||||
}
|
||||
val theme = remember(context, pal.id, density) { AgentMarkdownTheme(context) }
|
||||
val renderer = remember(theme) { agentMarkdownRenderer(context, theme) }
|
||||
val rendered = remember(renderer, text) { renderer.toMarkdown(text) }
|
||||
AndroidView(
|
||||
modifier = modifier,
|
||||
@@ -62,12 +68,25 @@ internal fun AgentMarkdown(text: String, modifier: Modifier) {
|
||||
view.setLinkTextColor(pal.primary2.toArgb())
|
||||
view.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, textSize)
|
||||
view.setLineSpacing(lineSpacing, 1f)
|
||||
val codeBackground = pal.surfaceVariant.toArgb()
|
||||
if (theme.codeBackground != codeBackground) {
|
||||
theme.codeBackground = codeBackground
|
||||
view.invalidate()
|
||||
}
|
||||
setAgentMarkdown(view, renderer, rendered)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
internal fun agentMarkdownRenderer(context: Context, linkColor: Int, codeBackground: Int): Markwon =
|
||||
internal class AgentMarkdownTheme(context: Context) : MarkwonTheme(MarkwonTheme.builderWithDefaults(context)) {
|
||||
var codeBackground = 0
|
||||
|
||||
override fun getCodeBackgroundColor(paint: Paint) = codeBackground
|
||||
|
||||
override fun getCodeBlockBackgroundColor(paint: Paint) = codeBackground
|
||||
}
|
||||
|
||||
internal fun agentMarkdownRenderer(context: Context, theme: AgentMarkdownTheme): Markwon =
|
||||
Markwon.builder(context)
|
||||
.usePlugin(StrikethroughPlugin.create())
|
||||
.usePlugin(TablePlugin.create(context))
|
||||
@@ -104,8 +123,10 @@ internal fun agentMarkdownRenderer(context: Context, linkColor: Int, codeBackgro
|
||||
builder.append(text)
|
||||
}
|
||||
|
||||
override fun configureTheme(builder: MarkwonTheme.Builder) {
|
||||
builder.linkColor(linkColor).codeBackgroundColor(codeBackground)
|
||||
override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) {
|
||||
builder.setFactory(Code::class.java) { _, _ -> CodeSpan(theme) }
|
||||
builder.setFactory(FencedCodeBlock::class.java) { _, _ -> CodeBlockSpan(theme) }
|
||||
builder.setFactory(IndentedCodeBlock::class.java) { _, _ -> CodeBlockSpan(theme) }
|
||||
}
|
||||
|
||||
override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.text.Spanned
|
||||
import android.text.TextPaint
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import io.noties.markwon.core.spans.CodeSpan
|
||||
import io.noties.markwon.ext.tables.TableRowSpan
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotSame
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.ConscryptMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], application = android.app.Application::class)
|
||||
@ConscryptMode(ConscryptMode.Mode.OFF)
|
||||
class AgentMarkdownPaletteTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
private val reply = "Run `build` first\n\n| Step | Result |\n| --- | --- |\n| Build | [PR](https://github.com/owner/repo/pull/1) |\n\nDone"
|
||||
private var palette by mutableStateOf(Themes.innerGamer(0f))
|
||||
|
||||
private fun View.markdownView(): TextView? = when (this) {
|
||||
is TextView -> this
|
||||
is ViewGroup -> (0 until childCount).firstNotNullOfOrNull { getChildAt(it).markdownView() }
|
||||
else -> null
|
||||
}
|
||||
|
||||
private fun showReply(): TextView {
|
||||
lateinit var root: View
|
||||
compose.setContent {
|
||||
root = LocalView.current
|
||||
CompositionLocalProvider(LocalPalette provides palette) { AgentMarkdown(reply, Modifier) }
|
||||
}
|
||||
compose.waitForIdle()
|
||||
return requireNotNull(root.markdownView())
|
||||
}
|
||||
|
||||
private fun assertColors(view: TextView, expected: Palette) {
|
||||
assertEquals(expected.primary2.toArgb(), view.paint.linkColor)
|
||||
val code = TextPaint()
|
||||
(view.text as Spanned).getSpans(0, view.text.length, CodeSpan::class.java).single().updateDrawState(code)
|
||||
assertEquals(expected.surfaceVariant.toArgb(), code.bgColor)
|
||||
}
|
||||
|
||||
@Test fun innerGamerHueStepsRecolorTheReplyWithoutRenderingItAgain() {
|
||||
val view = showReply()
|
||||
val rendered = view.tag as Spanned
|
||||
val rows = rendered.getSpans(0, rendered.length, TableRowSpan::class.java).toSet()
|
||||
assertTrue(rows.isNotEmpty())
|
||||
for (hue in listOf(3f, 90f, 180f, 357f)) {
|
||||
palette = Themes.innerGamer(hue)
|
||||
compose.waitForIdle()
|
||||
assertSame(rendered, view.tag)
|
||||
val current = view.text as Spanned
|
||||
assertEquals(rows, current.getSpans(0, current.length, TableRowSpan::class.java).toSet())
|
||||
assertColors(view, palette)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun switchingThemesRendersTheReplyInTheNewColors() {
|
||||
val view = showReply()
|
||||
for (id in listOf(Themes.GRAPHITE, Themes.WHITE_RED, Themes.INNER_GAMER)) {
|
||||
val before = view.tag
|
||||
palette = Themes.byId(id)
|
||||
compose.waitForIdle()
|
||||
assertNotSame(before, view.tag)
|
||||
assertColors(view, palette)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import android.content.Intent
|
||||
import android.content.ClipboardManager
|
||||
import android.text.Selection
|
||||
import android.text.Spannable
|
||||
import io.noties.markwon.core.spans.CodeBlockSpan
|
||||
import io.noties.markwon.core.spans.CodeSpan
|
||||
import io.noties.markwon.ext.tables.TableRowSpan
|
||||
import android.graphics.Color
|
||||
import android.text.Spanned
|
||||
@@ -23,7 +25,7 @@ import org.robolectric.annotation.Config
|
||||
@ConscryptMode(ConscryptMode.Mode.OFF)
|
||||
class AgentMarkdownTest {
|
||||
private val context get() = RuntimeEnvironment.getApplication()
|
||||
private val renderer get() = agentMarkdownRenderer(context, Color.BLUE, Color.DKGRAY)
|
||||
private val renderer get() = agentMarkdownRenderer(context, AgentMarkdownTheme(context).apply { codeBackground = Color.DKGRAY })
|
||||
private fun Spanned.links() = getSpans(0, length, ClickableSpan::class.java).toList()
|
||||
|
||||
@Test fun namedPullRequestLinksKeepTheirLabelsAndOpenAnExternalBrowser() {
|
||||
@@ -85,27 +87,47 @@ class AgentMarkdownTest {
|
||||
renderer.render(root)
|
||||
}
|
||||
|
||||
@Test fun colorChangesPreserveSelectionAndRefreshLinkSpans() {
|
||||
@Test fun rendererChangesPreserveSelectionAndLinksFollowTheViewLinkColor() {
|
||||
val source = "Read [the PR](https://github.com/owner/repo/pull/1)"
|
||||
val view = TextView(context).apply { setTextIsSelectable(true) }
|
||||
val original = renderer
|
||||
setAgentMarkdown(view, original, original.toMarkdown(source))
|
||||
Selection.setSelection(view.text as Spannable, 5, 11)
|
||||
val updated = agentMarkdownRenderer(context, Color.RED, Color.LTGRAY)
|
||||
val updated = agentMarkdownRenderer(context, AgentMarkdownTheme(context).apply { codeBackground = Color.LTGRAY })
|
||||
val rendered = updated.toMarkdown(source)
|
||||
setAgentMarkdown(view, updated, rendered)
|
||||
assertEquals(5, view.selectionStart)
|
||||
assertEquals(11, view.selectionEnd)
|
||||
assertSame(rendered, view.tag)
|
||||
val paint = android.text.TextPaint()
|
||||
view.setLinkTextColor(Color.RED)
|
||||
val paint = android.text.TextPaint().apply { set(view.paint) }
|
||||
(view.text as Spanned).links().single().updateDrawState(paint)
|
||||
assertEquals(Color.RED, paint.color)
|
||||
}
|
||||
|
||||
@Test fun codeBackgroundsFollowTheThemeWithoutRenderingAgain() {
|
||||
val theme = AgentMarkdownTheme(context).apply { codeBackground = Color.DKGRAY }
|
||||
val rendered = agentMarkdownRenderer(context, theme).toMarkdown("Run `inline`\n\n~~~\nblock\n~~~\n\n indented")
|
||||
theme.codeBackground = Color.RED
|
||||
val inline = android.text.TextPaint()
|
||||
rendered.getSpans(0, rendered.length, CodeSpan::class.java).single().updateDrawState(inline)
|
||||
assertEquals(Color.RED, inline.bgColor)
|
||||
val blocks = rendered.getSpans(0, rendered.length, CodeBlockSpan::class.java)
|
||||
assertEquals(2, blocks.size)
|
||||
for (block in blocks) {
|
||||
var filled = 0
|
||||
val canvas = object : android.graphics.Canvas(android.graphics.Bitmap.createBitmap(10, 10, android.graphics.Bitmap.Config.ARGB_8888)) {
|
||||
override fun drawRect(rect: android.graphics.Rect, paint: android.graphics.Paint) { filled = paint.color }
|
||||
}
|
||||
block.drawLeadingMargin(canvas, android.graphics.Paint(), 0, 1, 0, 5, 10, rendered, 0, 1, true, null)
|
||||
assertEquals(Color.RED, filled)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reversedSelectionsAndCursorsSurviveColorChangesButNotContentChanges() {
|
||||
val view = TextView(context).apply { setTextIsSelectable(true) }
|
||||
val original = renderer
|
||||
val updated = agentMarkdownRenderer(context, Color.RED, Color.LTGRAY)
|
||||
val updated = agentMarkdownRenderer(context, AgentMarkdownTheme(context).apply { codeBackground = Color.LTGRAY })
|
||||
setAgentMarkdown(view, original, original.toMarkdown("A long reply"))
|
||||
for ((start, end) in listOf(8 to 2, 4 to 4)) {
|
||||
Selection.setSelection(view.text as Spannable, start, end)
|
||||
|
||||
Reference in new issue
Block a user