diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownCode.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownCode.kt index 2915e76..9e973a0 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownCode.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownCode.kt @@ -1,20 +1,28 @@ package com.mikepenz.markdown.compose.elements -import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.* +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.draw.shadow +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.RectangleShape +import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.semantics.isTraversalGroup +import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import com.mikepenz.markdown.compose.LocalMarkdownColors import com.mikepenz.markdown.compose.LocalMarkdownDimens import com.mikepenz.markdown.compose.LocalMarkdownPadding import com.mikepenz.markdown.compose.LocalMarkdownTypography -import com.mikepenz.markdown.compose.elements.material.Surface -import com.mikepenz.markdown.compose.elements.material.Text +import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText import org.intellij.markdown.ast.ASTNode @Composable @@ -25,12 +33,12 @@ private fun MarkdownCode( val backgroundCodeColor = LocalMarkdownColors.current.codeBackground val codeBackgroundCornerSize = LocalMarkdownDimens.current.codeBackgroundCornerSize val codeBlockPadding = LocalMarkdownPadding.current.codeBlock - Surface( + MarkdownCodeBackground( color = backgroundCodeColor, shape = RoundedCornerShape(codeBackgroundCornerSize), modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp) ) { - Text( + MarkdownBasicText( code, color = LocalMarkdownColors.current.codeText, modifier = Modifier.horizontalScroll(rememberScrollState()).padding(codeBlockPadding), @@ -63,3 +71,29 @@ fun MarkdownCodeBlock( val end = node.children[node.children.size - 1].endOffset MarkdownCode(content.subSequence(start, end).toString().replaceIndent()) } + + +@Composable +internal fun MarkdownCodeBackground( + color: Color, + modifier: Modifier = Modifier, + shape: Shape = RectangleShape, + border: BorderStroke? = null, + elevation: Dp = 0.dp, + content: @Composable () -> Unit +) { + Box( + modifier = modifier + .shadow(elevation, shape, clip = false) + .then(if (border != null) Modifier.border(border, shape) else Modifier) + .background(color = color, shape = shape) + .clip(shape) + .semantics(mergeDescendants = false) { + isTraversalGroup = true + } + .pointerInput(Unit) {}, + propagateMinConstraints = true + ) { + content() + } +} diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt index 25a062c..ada0fae 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownList.kt @@ -9,7 +9,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.buildAnnotatedString import com.mikepenz.markdown.compose.* -import com.mikepenz.markdown.compose.elements.material.Text +import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText import com.mikepenz.markdown.utils.buildMarkdownAnnotatedString import com.mikepenz.markdown.utils.filterNonListTypes import org.intellij.markdown.MarkdownElementTypes @@ -69,7 +69,7 @@ fun MarkdownOrderedList( val listItemBottom = LocalMarkdownPadding.current.listItemBottom MarkdownListItems(content, node, style, level) { index, child -> Row(Modifier.fillMaxWidth()) { - Text( + MarkdownBasicText( text = orderedListHandler.transform(LIST_NUMBER, child.findChildOfType(LIST_NUMBER)?.getTextInNode(content), index), style = style, color = LocalMarkdownColors.current.text @@ -95,7 +95,7 @@ fun MarkdownBulletList( val listItemBottom = LocalMarkdownPadding.current.listItemBottom MarkdownListItems(content, node, style, level) { index, child -> Row(Modifier.fillMaxWidth()) { - Text( + MarkdownBasicText( bulletHandler.transform(LIST_BULLET, child.findChildOfType(LIST_BULLET)?.getTextInNode(content), index), style = style, color = LocalMarkdownColors.current.text diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt index 1b6b389..32cd2f3 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt @@ -18,7 +18,7 @@ import com.mikepenz.markdown.compose.LocalImageTransformer import com.mikepenz.markdown.compose.LocalMarkdownColors import com.mikepenz.markdown.compose.LocalMarkdownTypography import com.mikepenz.markdown.compose.LocalReferenceLinkHandler -import com.mikepenz.markdown.compose.elements.material.Text +import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText import com.mikepenz.markdown.model.rememberMarkdownImageState import com.mikepenz.markdown.utils.TAG_IMAGE_URL import com.mikepenz.markdown.utils.TAG_URL @@ -61,7 +61,7 @@ fun MarkdownText( } } else modifier - Text( + MarkdownBasicText( text = content, modifier = textModifier .onPlaced { diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/SurfaceWrapper.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/SurfaceWrapper.kt deleted file mode 100644 index e03f941..0000000 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/SurfaceWrapper.kt +++ /dev/null @@ -1,56 +0,0 @@ -package com.mikepenz.markdown.compose.elements.material - -import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.background -import androidx.compose.foundation.border -import androidx.compose.foundation.layout.Box -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.draw.shadow -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.RectangleShape -import androidx.compose.ui.graphics.Shape -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.semantics.isContainer -import androidx.compose.ui.semantics.semantics -import androidx.compose.ui.unit.Dp -import androidx.compose.ui.unit.dp - -@Composable -internal fun Surface( - color: Color, - modifier: Modifier = Modifier, - shape: Shape = RectangleShape, - border: BorderStroke? = null, - elevation: Dp = 0.dp, - content: @Composable () -> Unit -) { - Box( - modifier = modifier - .surface( - shape = shape, - backgroundColor = color, - border = border, - elevation = elevation - ) - .semantics(mergeDescendants = false) { - @Suppress("DEPRECATION") - isContainer = true - } - .pointerInput(Unit) {}, - propagateMinConstraints = true - ) { - content() - } -} - -private fun Modifier.surface( - shape: Shape, - backgroundColor: Color, - border: BorderStroke?, - elevation: Dp -) = this.shadow(elevation, shape, clip = false) - .then(if (border != null) Modifier.border(border, shape) else Modifier) - .background(color = backgroundColor, shape = shape) - .clip(shape) \ No newline at end of file diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/TextWrapper.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/TextWrapper.kt index 7fa96d9..ae7bfda 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/TextWrapper.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/material/TextWrapper.kt @@ -16,9 +16,10 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.TextUnit +import com.mikepenz.markdown.compose.LocalMarkdownColors @Composable -internal fun Text( +internal fun MarkdownBasicText( text: String, style: TextStyle, modifier: Modifier = Modifier, @@ -37,29 +38,13 @@ internal fun Text( minLines: Int = 1, onTextLayout: ((TextLayoutResult) -> Unit)? = null, ) { - // TL:DR: profile before you change any line of code in this method - // - // The call to LocalContentAlpha.current looks like it can be avoided by only calling it in the - // last else block but, in 1.5, this causes a control flow group to be created because it would - // be a conditional call to a composable function. The call is currently made unconditionally - // since the call to LocalContentAlpha.current does not create a group (it is a read-only - // composable) and looking up the value in the composition locals map is currently faster than - // creating a group to avoid it. - // - // Similar notes regarding lambda allocations. It appears there's a path to optimize for - // zero-allocations in the style-provided color route, but this either introduces a group or a - // box depending on how it's coded. It's also possible that allocating a final ColorProducer - // subclass with no capture may be a successful optimization, but it appeared slower in initial - // profiling. - // - // If changing ANY LINE OF CODE, please confirm that it's faster or the same speed using - // profilers and benchmarks. + // Note: This component is ported over from Material2 Text - to remove the dependency on Material val overrideColorOrUnspecified: Color = if (color.isSpecified) { color } else if (style.color.isSpecified) { style.color } else { - Color.Black // TODO + LocalMarkdownColors.current.text } BasicText( @@ -85,7 +70,7 @@ internal fun Text( } @Composable -internal fun Text( +internal fun MarkdownBasicText( text: AnnotatedString, style: TextStyle, modifier: Modifier = Modifier, @@ -105,30 +90,13 @@ internal fun Text( inlineContent: Map = mapOf(), onTextLayout: (TextLayoutResult) -> Unit = {}, ) { - // TL:DR: profile before you change any line of code in this method - // - // The call to LocalContentAlpha.current looks like it can be avoided by only calling it in the - // last else block but, in 1.5, this causes a control flow group to be created because it would - // be a conditional call to a composable function. The call is currently made unconditionally - // since the call to LocalContentAlpha.current does not create a group (it is a read-only - // composable) and looking up the value in the composition locals map is currently faster than - // creating a group to avoid it. - // - // Similar notes regarding lambda allocations. It appears there's a path to optimize for - // zero-allocations in the style-provided color route, but this either introduces a group or a - // box depending on how it's coded. It's also possible that allocating a final ColorProducer - // subclass with no capture may be a successful optimization, but it appeared slower in initial - // profiling. - // - // If changing ANY LINE OF CODE, please confirm that it's faster or the same speed using - // profilers and benchmarks. - + // Note: This component is ported over from Material2 Text - to remove the dependency on Material val overrideColorOrUnspecified = if (color.isSpecified) { color } else if (style.color.isSpecified) { style.color } else { - Color.Black // TODO + LocalMarkdownColors.current.text } BasicText( diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownImageState.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownImageState.kt index f6a3e06..e56ed15 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownImageState.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownImageState.kt @@ -1,11 +1,6 @@ package com.mikepenz.markdown.model -import androidx.compose.runtime.Composable -import androidx.compose.runtime.derivedStateOf -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue +import androidx.compose.runtime.* import androidx.compose.ui.geometry.Size import androidx.compose.ui.geometry.isUnspecified import androidx.compose.ui.platform.LocalDensity