Skip to content

Commit

Permalink
Add onTextClick to MarkdownBlockRenderer (#366)
Browse files Browse the repository at this point in the history
* Fix text colour in Markdown (bridge)

We were not setting the right text colour when creating the bridge
Markdown Styling. Now we make sure we do respect the colour
set in the various InlinesStyle's TextStyles when rendering inlines.

In addition, I've added a simple API to be able to initialize a
MarkdownStyling in the bridge based only on a base TextStyle. This is
useful, e.g., when you want to make smaller text such as comments.

* Add onTextClick to MarkdownBlockRenderer

This is a workaround to the fact that ClickableText swallows all clicks,
since its onClick lambda has no way of saying if it did handle a click.

Hopefully this won't be needed anymore when we move to Compose 1.7,
which brings in a proper LinkAnnotation.
  • Loading branch information
rock3r authored Apr 26, 2024
1 parent f5e87a4 commit 8591713
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion markdown/core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public final class org/jetbrains/jewel/markdown/rendering/DefaultInlineMarkdownR

public class org/jetbrains/jewel/markdown/rendering/DefaultMarkdownBlockRenderer : org/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer {
public static final field $stable I
public fun <init> (Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;)V
public fun <init> (Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)V
public fun render (Ljava/util/List;Landroidx/compose/runtime/Composer;I)V
public fun render (Lorg/jetbrains/jewel/markdown/MarkdownBlock$BlockQuote;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling$BlockQuote;Landroidx/compose/runtime/Composer;I)V
public fun render (Lorg/jetbrains/jewel/markdown/MarkdownBlock$CodeBlock$FencedCodeBlock;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling$Code$Fenced;Landroidx/compose/runtime/Composer;I)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public open class DefaultMarkdownBlockRenderer(
private val rendererExtensions: List<MarkdownRendererExtension>,
private val inlineRenderer: InlineMarkdownRenderer,
private val onUrlClick: (String) -> Unit,
private val onTextClick: () -> Unit,
) : MarkdownBlockRenderer {

@Composable
Expand Down Expand Up @@ -115,6 +116,7 @@ public open class DefaultMarkdownBlockRenderer(
text = renderedContent,
textStyle = styling.inlinesStyling.textStyle,
color = styling.inlinesStyling.textStyle.color.takeOrElse { LocalContentColor.current },
onUnhandledClick = onTextClick,
)
}

Expand Down Expand Up @@ -158,6 +160,7 @@ public open class DefaultMarkdownBlockRenderer(
text = renderedContent,
textStyle = textStyle,
color = textStyle.color.takeOrElse { LocalContentColor.current },
onUnhandledClick = onTextClick,
)

if (underlineWidth > 0.dp && underlineColor.isSpecified) {
Expand Down Expand Up @@ -380,6 +383,7 @@ public open class DefaultMarkdownBlockRenderer(
textStyle: TextStyle,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
onUnhandledClick: () -> Unit,
) {
var pointerIcon by remember { mutableStateOf(PointerIcon.Default) }

Expand All @@ -400,8 +404,12 @@ public open class DefaultMarkdownBlockRenderer(
}
},
) { offset ->
val span = text.getUrlAnnotations(offset, offset).firstOrNull() ?: return@ClickableText
onUrlClick(span.item.url)
val span = text.getUrlAnnotations(offset, offset).firstOrNull()
if (span != null) {
onUrlClick(span.item.url)
} else {
onUnhandledClick()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public final class org/jetbrains/jewel/intui/markdown/MarkdownBlockRendererExtensionsKt {
public static final fun create (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static synthetic fun create$default (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static final fun create (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static synthetic fun create$default (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
}

public final class org/jetbrains/jewel/intui/markdown/styling/MarkdownBridgeStylingKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public fun MarkdownBlockRenderer.Companion.create(
rendererExtensions: List<MarkdownRendererExtension> = emptyList(),
inlineRenderer: InlineMarkdownRenderer = InlineMarkdownRenderer.default(),
onUrlClick: (String) -> Unit = {},
): MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick)
onTextClick: () -> Unit = {},
): MarkdownBlockRenderer =
DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick, onTextClick)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public final class org/jetbrains/jewel/intui/markdown/MarkdownBlockRendererExtensionsKt {
public static final fun dark (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static synthetic fun dark$default (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static final fun light (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static synthetic fun light$default (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static final fun dark (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static synthetic fun dark$default (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static final fun light (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
public static synthetic fun light$default (Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer$Companion;Lorg/jetbrains/jewel/markdown/rendering/MarkdownStyling;Ljava/util/List;Lorg/jetbrains/jewel/markdown/rendering/InlineMarkdownRenderer;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lorg/jetbrains/jewel/markdown/rendering/MarkdownBlockRenderer;
}

public final class org/jetbrains/jewel/intui/markdown/styling/MarkdownIntUiStylingKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public fun MarkdownBlockRenderer.Companion.light(
rendererExtensions: List<MarkdownRendererExtension> = emptyList(),
inlineRenderer: InlineMarkdownRenderer = InlineMarkdownRenderer.default(),
onUrlClick: (String) -> Unit = {},
): MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick)
onTextClick: () -> Unit = {},
): MarkdownBlockRenderer =
DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick, onTextClick)

public fun MarkdownBlockRenderer.Companion.dark(
styling: MarkdownStyling = MarkdownStyling.dark(),
rendererExtensions: List<MarkdownRendererExtension> = emptyList(),
inlineRenderer: InlineMarkdownRenderer = InlineMarkdownRenderer.default(),
onUrlClick: (String) -> Unit = {},
): MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick)
onTextClick: () -> Unit = {},
): MarkdownBlockRenderer =
DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick, onTextClick)
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ internal fun MarkdownPreview(@Language("Markdown") rawMarkdown: String, modifier
MarkdownBlockRenderer.create(
styling = markdownStyling,
inlineRenderer = InlineMarkdownRenderer.default(),
) { url ->
Desktop.getDesktop().browse(URI.create(url))
}
onUrlClick = { url -> Desktop.getDesktop().browse(URI.create(url)) },
)
}

SelectionContainer(modifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ internal fun MarkdownPreview(
styling = markdownStyling,
rendererExtensions = listOf(GitHubAlertRendererExtension(AlertStyling.dark(), markdownStyling)),
inlineRenderer = InlineMarkdownRenderer.default(extensions),
) { url ->
Desktop.getDesktop().browse(URI.create(url))
}
onUrlClick = { url -> Desktop.getDesktop().browse(URI.create(url)) },
)
} else {
MarkdownBlockRenderer.light(
styling = markdownStyling,
rendererExtensions = listOf(GitHubAlertRendererExtension(AlertStyling.light(), markdownStyling)),
inlineRenderer = InlineMarkdownRenderer.default(extensions),
) { url ->
Desktop.getDesktop().browse(URI.create(url))
}
onUrlClick = { url -> Desktop.getDesktop().browse(URI.create(url)) },
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions ui/api/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ public final class org/jetbrains/jewel/ui/component/ButtonState$Companion {

public final class org/jetbrains/jewel/ui/component/CheckboxKt {
public static final fun Checkbox (ZLkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Landroidx/compose/runtime/Composer;III)V
public static final fun CheckboxRow (Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Landroidx/compose/runtime/Composer;III)V
public static final fun CheckboxRow (Ljava/lang/String;ZLkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Landroidx/compose/runtime/Composer;III)V
public static final fun CheckboxRow (ZLkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V
public static final fun TriStateCheckbox (Landroidx/compose/ui/state/ToggleableState;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Landroidx/compose/runtime/Composer;III)V
public static final fun TriStateCheckboxRow (Landroidx/compose/ui/state/ToggleableState;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V
public static final fun TriStateCheckboxRow (Ljava/lang/String;Landroidx/compose/ui/state/ToggleableState;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Landroidx/compose/runtime/Composer;III)V
public static final fun TriStateCheckboxRow (Ljava/lang/String;Landroidx/compose/ui/state/ToggleableState;Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;ZLorg/jetbrains/jewel/ui/Outline;Landroidx/compose/foundation/interaction/MutableInteractionSource;Lorg/jetbrains/jewel/ui/component/styling/CheckboxColors;Lorg/jetbrains/jewel/ui/component/styling/CheckboxMetrics;Lorg/jetbrains/jewel/ui/component/styling/CheckboxIcons;Landroidx/compose/ui/text/TextStyle;Landroidx/compose/ui/Alignment$Vertical;Landroidx/compose/runtime/Composer;III)V
}

public final class org/jetbrains/jewel/ui/component/CheckboxState : org/jetbrains/jewel/foundation/state/FocusableComponentState, org/jetbrains/jewel/foundation/state/ToggleableComponentState {
Expand Down
23 changes: 17 additions & 6 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Checkbox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public fun Checkbox(
state = state,
onClick = { onCheckedChange.invoke(!checked) },
modifier = modifier,
contentModifier = Modifier,
enabled = enabled,
outline = outline,
interactionSource = interactionSource,
Expand Down Expand Up @@ -103,6 +104,7 @@ public fun TriStateCheckbox(
state = state,
onClick = onClick,
modifier = modifier,
contentModifier = Modifier,
enabled = enabled,
outline = outline,
interactionSource = interactionSource,
Expand All @@ -121,6 +123,7 @@ public fun TriStateCheckboxRow(
state: ToggleableState,
onClick: () -> Unit,
modifier: Modifier = Modifier,
textModifier: Modifier = Modifier,
enabled: Boolean = true,
outline: Outline = Outline.None,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
Expand All @@ -134,6 +137,7 @@ public fun TriStateCheckboxRow(
state = state,
onClick = onClick,
modifier = modifier,
contentModifier = textModifier,
enabled = enabled,
outline = outline,
interactionSource = interactionSource,
Expand All @@ -151,8 +155,9 @@ public fun TriStateCheckboxRow(
public fun CheckboxRow(
text: String,
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit)?,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
textModifier: Modifier = Modifier,
enabled: Boolean = true,
outline: Outline = Outline.None,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
Expand All @@ -166,8 +171,9 @@ public fun CheckboxRow(

CheckboxImpl(
state = state,
onClick = { onCheckedChange?.invoke(!checked) },
onClick = { onCheckedChange(!checked) },
modifier = modifier,
contentModifier = textModifier,
enabled = enabled,
outline = outline,
interactionSource = interactionSource,
Expand All @@ -184,7 +190,7 @@ public fun CheckboxRow(
@Composable
public fun CheckboxRow(
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit)?,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
outline: Outline = Outline.None,
Expand All @@ -198,8 +204,9 @@ public fun CheckboxRow(
) {
CheckboxImpl(
state = ToggleableState(checked),
onClick = { onCheckedChange?.invoke(!checked) },
onClick = { onCheckedChange(!checked) },
modifier = modifier,
contentModifier = Modifier,
enabled = enabled,
outline = outline,
interactionSource = interactionSource,
Expand Down Expand Up @@ -231,6 +238,7 @@ public fun TriStateCheckboxRow(
state = state,
onClick = onClick,
modifier = modifier,
contentModifier = Modifier,
enabled = enabled,
outline = outline,
interactionSource = interactionSource,
Expand All @@ -251,6 +259,7 @@ private fun CheckboxImpl(
metrics: CheckboxMetrics,
icons: CheckboxIcons,
modifier: Modifier,
contentModifier: Modifier,
enabled: Boolean,
outline: Outline,
interactionSource: MutableInteractionSource,
Expand Down Expand Up @@ -315,7 +324,7 @@ private fun CheckboxImpl(
val checkboxBoxModifier = Modifier.size(metrics.checkboxSize)

if (content == null) {
Box(checkboxBoxModifier, contentAlignment = Alignment.TopStart) {
Box(wrapperModifier.then(checkboxBoxModifier), contentAlignment = Alignment.TopStart) {
CheckBoxImage(checkboxPainter)
Box(outlineModifier.align(Alignment.Center))
}
Expand All @@ -335,7 +344,9 @@ private fun CheckboxImpl(
LocalTextStyle provides textStyle.copy(color = contentColor.takeOrElse { textStyle.color }),
LocalContentColor provides contentColor.takeOrElse { LocalContentColor.current },
) {
content()
Row(contentModifier) {
content()
}
}
}
}
Expand Down

0 comments on commit 8591713

Please sign in to comment.