Skip to content

Commit

Permalink
- remove SurfaceWrapper and replace with specific `MarkdownCodeBackgr…
Browse files Browse the repository at this point in the history
…ound`

- rename `Text` to `MarkdownBasicText`
  • Loading branch information
mikepenz committed Jan 13, 2024
1 parent f6afed9 commit bdd1d16
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,7 +61,7 @@ fun MarkdownText(
}
} else modifier

Text(
MarkdownBasicText(
text = content,
modifier = textModifier
.onPlaced {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -85,7 +70,7 @@ internal fun Text(
}

@Composable
internal fun Text(
internal fun MarkdownBasicText(
text: AnnotatedString,
style: TextStyle,
modifier: Modifier = Modifier,
Expand All @@ -105,30 +90,13 @@ internal fun Text(
inlineContent: Map<String, InlineTextContent> = 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit bdd1d16

Please sign in to comment.