Skip to content

Commit 696fcfe

Browse files
authored
Merge pull request #99 from morrisseyai/feature/add-modifier-to-code-block-style
Add Modifier to CodeBlockStyle
2 parents e0470a9 + 06f0a75 commit 696fcfe

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

richtext-ui/src/commonMain/kotlin/com/halilibo/richtext/ui/CodeBlock.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import androidx.compose.ui.unit.sp
2020
* Defines how [CodeBlock]s are rendered.
2121
*
2222
* @param textStyle The [TextStyle] to use for the block.
23-
* @param background The [Color] of a code block, drawn behind the text.
23+
* @param modifier The [Modifier] to use for the block.
2424
* @param padding The amount of space between the edge of the text and the edge of the background.
2525
* @param wordWrap Whether a code block breaks the lines or scrolls horizontally.
2626
*/
2727
@Immutable
2828
public data class CodeBlockStyle(
2929
val textStyle: TextStyle? = null,
30-
// TODO Make background just a modifier instead?
31-
val background: Color? = null,
30+
val modifier: Modifier? = null,
3231
val padding: TextUnit? = null,
3332
val wordWrap: Boolean? = null
3433
) {
@@ -40,13 +39,15 @@ public data class CodeBlockStyle(
4039
private val DefaultCodeBlockTextStyle = TextStyle(
4140
fontFamily = FontFamily.Monospace
4241
)
43-
internal val DefaultCodeBlockBackground: Color = Color.LightGray.copy(alpha = .5f)
42+
internal val DefaultCodeBlockBackgroundColor: Color = Color.LightGray.copy(alpha = .5f)
43+
private val DefaultCodeBlockModifier: Modifier =
44+
Modifier.background(color = DefaultCodeBlockBackgroundColor)
4445
private val DefaultCodeBlockPadding: TextUnit = 16.sp
4546
private const val DefaultCodeWordWrap: Boolean = true
4647

4748
internal fun CodeBlockStyle.resolveDefaults() = CodeBlockStyle(
4849
textStyle = textStyle ?: DefaultCodeBlockTextStyle,
49-
background = background ?: DefaultCodeBlockBackground,
50+
modifier = modifier ?: DefaultCodeBlockModifier,
5051
padding = padding ?: DefaultCodeBlockPadding,
5152
wordWrap = wordWrap ?: DefaultCodeWordWrap
5253
)
@@ -78,17 +79,18 @@ internal fun CodeBlockStyle.resolveDefaults() = CodeBlockStyle(
7879
) {
7980
val codeBlockStyle = currentRichTextStyle.resolveDefaults().codeBlockStyle!!
8081
val textStyle = currentTextStyle.merge(codeBlockStyle.textStyle)
82+
val modifier = codeBlockStyle.modifier!!
8183
val blockPadding = with(LocalDensity.current) {
8284
codeBlockStyle.padding!!.toDp()
8385
}
8486
val resolvedWordWrap = wordWrap ?: codeBlockStyle.wordWrap!!
8587

8688
CodeBlockLayout(
8789
wordWrap = resolvedWordWrap
88-
) { modifier ->
90+
) { layoutModifier ->
8991
Box(
90-
modifier = modifier
91-
.background(color = codeBlockStyle.background!!)
92+
modifier = layoutModifier
93+
.then(modifier)
9294
.padding(blockPadding)
9395
) {
9496
ProvideTextStyle(textStyle) {

richtext-ui/src/commonMain/kotlin/com/halilibo/richtext/ui/string/RichTextString.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import androidx.compose.ui.text.font.FontWeight
1414
import androidx.compose.ui.text.style.BaselineShift
1515
import androidx.compose.ui.text.style.TextDecoration
1616
import androidx.compose.ui.unit.sp
17-
import com.halilibo.richtext.ui.DefaultCodeBlockBackground
17+
import com.halilibo.richtext.ui.DefaultCodeBlockBackgroundColor
1818
import com.halilibo.richtext.ui.string.RichTextString.Builder
1919
import com.halilibo.richtext.ui.string.RichTextString.Format
2020
import com.halilibo.richtext.ui.string.RichTextString.Format.Bold
@@ -206,7 +206,7 @@ public data class RichTextString internal constructor(
206206
internal val DefaultStyle = SpanStyle(
207207
fontFamily = FontFamily.Monospace,
208208
fontWeight = FontWeight.Medium,
209-
background = DefaultCodeBlockBackground
209+
background = DefaultCodeBlockBackgroundColor
210210
)
211211

212212
override fun getStyle(

0 commit comments

Comments
 (0)