Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Markdown tables support (GFM extension) #735

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions .idea/ktlint-plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified art/docs/custom-chrome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/docs/gfm-table-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/docs/gfm-table-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/docs/markdown-renderer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified art/docs/merge-dialog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions foundation/api/foundation.api
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ public final class org/jetbrains/jewel/foundation/code/highlighting/NoOpCodeHigh
public fun highlight-C7ITchA (Ljava/lang/String;Ljava/lang/String;)Lkotlinx/coroutines/flow/Flow;
}

public final class org/jetbrains/jewel/foundation/layout/BasicTableLayoutKt {
public static final fun BasicTableLayout-yE4rkUQ (IIJLandroidx/compose/ui/Modifier;FLjava/util/List;Landroidx/compose/runtime/Composer;II)V
}

public class org/jetbrains/jewel/foundation/lazy/DefaultMacOsSelectableColumnKeybindings : org/jetbrains/jewel/foundation/lazy/DefaultSelectableColumnKeybindings {
public static final field $stable I
public static final field Companion Lorg/jetbrains/jewel/foundation/lazy/DefaultMacOsSelectableColumnKeybindings$Companion;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package org.jetbrains.jewel.foundation.layout

import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
* A simple table that sizes columns to take as much room as they need. If the horizontal space available is less than
* what the cells would take, all columns are sized proportionally to their intrinsic width so that they still can fit
* the available width.
*
* Cells **must** only contain one top-level component. If you need your cells to contain more than one, wrap your cell
* content in a [`Box`][Box], [`Column`][androidx.compose.foundation.layout.Column],
* [`Row`][androidx.compose.foundation.layout.Row], etc.
*
* Incoming height constraints are ignored. The table will always take up as much vertical room as it needs. If you want
* to constrain the table height consider wrapping it in a
* [`VerticallyScrollableContainer`][org.jetbrains.jewel.ui.component.VerticallyScrollableContainer].
*
* @param rowCount The number of rows this table has.
* @param columnCount The number of columns this table has.
* @param cellBorderColor The color of the cell borders. Set to [Color.Unspecified] to avoid drawing the borders — in
* which case, the [cellBorderWidth] acts as a padding.
* @param modifier Modifier to apply to the table.
* @param cellBorderWidth The width of the table's borders.
* @param rows The rows that make up the table. Each row is a list of composables, one per row cell.
*/
@Suppress("KDocUnresolvedReference")
@Composable
public fun BasicTableLayout(
rowCount: Int,
columnCount: Int,
cellBorderColor: Color,
modifier: Modifier = Modifier,
cellBorderWidth: Dp = 1.dp,
rows: List<List<@Composable () -> Unit>>,
) {
var rowHeights by remember { mutableStateOf(emptyList<Int>()) }
var columnWidths by remember { mutableStateOf(emptyList<Int>()) }

Layout(
modifier =
modifier.thenIf(rowHeights.size == rowCount && columnWidths.size == columnCount) {
drawTableBorders(cellBorderColor, cellBorderWidth, rowHeights, columnWidths)
},
content = { rows.forEach { row -> row.forEach { cell -> cell() } } },
measurePolicy = { measurables, incomingConstraints ->
require(rows.size == rowCount) { "Found ${rows.size} rows, but expected $rowCount." }
require(measurables.size == rowCount * columnCount) {
"Found ${measurables.size} cells, but expected ${rowCount * columnCount}."
}

val intrinsicColumnWidths = IntArray(columnCount)
rows.forEachIndexed { rowIndex, row ->
require(row.size == columnCount) {
"Row $rowIndex contains ${row.size} cells, but it should have $columnCount cells."
}

row.forEachIndexed { columnIndex, cell ->
// Subcompose each cell individually
val measurable = measurables[rowIndex * columnIndex]

// Store the intrinsic width for each column, assuming we have infinite
// vertical space available to display each cell (which we do)
val intrinsicCellWidth = measurable.maxIntrinsicWidth(Int.MAX_VALUE)
intrinsicColumnWidths[columnIndex] =
intrinsicColumnWidths[columnIndex].coerceAtLeast(intrinsicCellWidth)

measurable
}
}

// The available width we can assign to cells is equal to the max width from the
// incoming
// constraints, minus the vertical borders applied between columns and to the sides of
// the
// table
val cellBorderWidthPx = cellBorderWidth.roundToPx()
val totalHorizontalBordersWidth = cellBorderWidthPx * (columnCount + 1)
val minTableIntrinsicWidth = intrinsicColumnWidths.sum() + totalHorizontalBordersWidth
val availableWidth = incomingConstraints.maxWidth

// We want to size the columns as a ratio of their intrinsic size to the available width
// if there is not enough room to show them all, or as their intrinsic width if they all
// fit
var tableWidth = 0

if (minTableIntrinsicWidth <= availableWidth) {
// We have enough room for all columns, use intrinsic column sizes
tableWidth = minTableIntrinsicWidth
} else {
// We can't fit all columns in the available width; set their size proportionally
// to the intrinsic width, so they all fit within the available horizontal space
val scaleRatio = availableWidth.toFloat() / minTableIntrinsicWidth
for (i in 0 until columnCount) {
// By truncating the decimal side, we may end up a few pixels short than the
// available width, but at least we're never exceeding it.
intrinsicColumnWidths[i] = (intrinsicColumnWidths[i] * scaleRatio).toInt()
tableWidth += intrinsicColumnWidths[i]
}
tableWidth += totalHorizontalBordersWidth
}
columnWidths = intrinsicColumnWidths.toList()

// The height of each row is the maximum intrinsic height of their cells, calculated
// from
// the (possibly scaled) intrinsic column widths we just computed
val intrinsicRowHeights = IntArray(rowCount)
var tableHeight = 0
measurables.chunked(columnCount).mapIndexed { rowIndex, rowMeasurables ->
val rowHeight =
rowMeasurables
.mapIndexed { columnIndex, cellMeasurable ->
val columnWidth = columnWidths[columnIndex]
cellMeasurable.maxIntrinsicHeight(columnWidth)
}
.max()

tableHeight += rowHeight
intrinsicRowHeights[rowIndex] = rowHeight
}
rowHeights = intrinsicRowHeights.toList()

// Add the horizontal borders drawn between rows and on top and bottom of the table
tableHeight += cellBorderWidthPx * (rowCount + 1)

// Measure all cells, using the fixed constraints we calculated for each row and column
val placeables =
measurables.chunked(columnCount).mapIndexed { rowIndex, cellMeasurables ->
cellMeasurables.mapIndexed { columnIndex, cellMeasurable ->
val cellConstraints = Constraints.fixed(columnWidths[columnIndex], rowHeights[rowIndex])
cellMeasurable.measure(cellConstraints)
}
}

layout(tableWidth, tableHeight) {
// Place cells. We start by leaving space for the top and start-side borders
var y = cellBorderWidthPx

placeables.forEachIndexed { rowIndex, cellPlaceables ->
var x = cellBorderWidthPx

var rowHeight = 0
cellPlaceables.forEach { cellPlaceable ->
cellPlaceable.placeRelative(x, y)
x += cellBorderWidthPx
x += cellPlaceable.width
rowHeight = cellPlaceable.height.coerceAtLeast(rowHeight)
}

y += cellBorderWidthPx
y += rowHeight
}
}
},
)
}

private fun Modifier.drawTableBorders(
cellBorderColor: Color,
cellBorderWidth: Dp,
rowHeights: List<Int>,
columnWidths: List<Int>,
) = drawBehind {
val borderWidthPx = cellBorderWidth.toPx()
val halfBorderWidthPx = borderWidthPx / 2f

// First, draw the outer border
drawRect(
color = cellBorderColor,
topLeft = Offset(halfBorderWidthPx, halfBorderWidthPx),
size = Size(size.width - borderWidthPx, size.height - borderWidthPx),
style = Stroke(width = borderWidthPx),
)

// Then, draw all horizontal borders below rows.
// No need to draw the last horizontal border as it's covered by the border rect
var y = halfBorderWidthPx
val endX = size.width - borderWidthPx

for (i in 0 until rowHeights.lastIndex) {
y += rowHeights[i].toFloat() + borderWidthPx
drawLine(
color = cellBorderColor,
start = Offset(halfBorderWidthPx, y),
end = Offset(endX, y),
strokeWidth = borderWidthPx,
)
}

// Lastly, draw all vertical borders to the end of columns
// (minus the last one, as before)
var x = halfBorderWidthPx
val endY = size.height - borderWidthPx

for (i in 0 until columnWidths.lastIndex) {
x += columnWidths[i].toFloat() + borderWidthPx
drawLine(
color = cellBorderColor,
start = Offset(x, halfBorderWidthPx),
end = Offset(x, endY),
strokeWidth = borderWidthPx,
)
}
}

// TODO remove this once thenIf is moved to foundation
private inline fun Modifier.thenIf(precondition: Boolean, action: Modifier.() -> Modifier): Modifier =
if (precondition) action() else this
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ poko = "0.17.1"
[libraries]
commonmark-core = { module = "org.commonmark:commonmark", version.ref = "commonmark" }
commonmark-ext-autolink = { module = "org.commonmark:commonmark-ext-autolink", version.ref = "commonmark" }
commonmark-ext-gfm-tables = { module = "org.commonmark:commonmark-ext-gfm-tables", version.ref = "commonmark" }

filePicker = { module = "com.darkrockstudios:mpfilepicker", version = "3.1.0" }

Expand Down
24 changes: 16 additions & 8 deletions int-ui/int-ui-standalone/api/int-ui-standalone.api
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,22 @@ public final class org/jetbrains/jewel/intui/standalone/styling/IntUiUndecorated
}

public final class org/jetbrains/jewel/intui/standalone/theme/IntUiGlobalColorsKt {
public static final fun dark-GyCwops (Lorg/jetbrains/jewel/foundation/BorderColors$Companion;JJJLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/BorderColors;
public static final fun dark-Hformbs (Lorg/jetbrains/jewel/foundation/OutlineColors$Companion;JJJJJLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/OutlineColors;
public static final fun dark-Hformbs (Lorg/jetbrains/jewel/foundation/TextColors$Companion;JJJJJLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/TextColors;
public static final fun dark-yrwZFoE (Lorg/jetbrains/jewel/foundation/GlobalColors$Companion;Lorg/jetbrains/jewel/foundation/BorderColors;Lorg/jetbrains/jewel/foundation/OutlineColors;Lorg/jetbrains/jewel/foundation/TextColors;JLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/GlobalColors;
public static final fun light-GyCwops (Lorg/jetbrains/jewel/foundation/BorderColors$Companion;JJJLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/BorderColors;
public static final fun light-Hformbs (Lorg/jetbrains/jewel/foundation/OutlineColors$Companion;JJJJJLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/OutlineColors;
public static final fun light-Hformbs (Lorg/jetbrains/jewel/foundation/TextColors$Companion;JJJJJLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/TextColors;
public static final fun light-yrwZFoE (Lorg/jetbrains/jewel/foundation/GlobalColors$Companion;Lorg/jetbrains/jewel/foundation/BorderColors;Lorg/jetbrains/jewel/foundation/OutlineColors;Lorg/jetbrains/jewel/foundation/TextColors;JLandroidx/compose/runtime/Composer;II)Lorg/jetbrains/jewel/foundation/GlobalColors;
public static final fun dark-jdqzblg (Lorg/jetbrains/jewel/foundation/OutlineColors$Companion;JJJJJ)Lorg/jetbrains/jewel/foundation/OutlineColors;
public static final fun dark-jdqzblg (Lorg/jetbrains/jewel/foundation/TextColors$Companion;JJJJJ)Lorg/jetbrains/jewel/foundation/TextColors;
public static synthetic fun dark-jdqzblg$default (Lorg/jetbrains/jewel/foundation/OutlineColors$Companion;JJJJJILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/OutlineColors;
public static synthetic fun dark-jdqzblg$default (Lorg/jetbrains/jewel/foundation/TextColors$Companion;JJJJJILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/TextColors;
public static final fun dark-xwkQ0AY (Lorg/jetbrains/jewel/foundation/GlobalColors$Companion;Lorg/jetbrains/jewel/foundation/BorderColors;Lorg/jetbrains/jewel/foundation/OutlineColors;Lorg/jetbrains/jewel/foundation/TextColors;J)Lorg/jetbrains/jewel/foundation/GlobalColors;
public static synthetic fun dark-xwkQ0AY$default (Lorg/jetbrains/jewel/foundation/GlobalColors$Companion;Lorg/jetbrains/jewel/foundation/BorderColors;Lorg/jetbrains/jewel/foundation/OutlineColors;Lorg/jetbrains/jewel/foundation/TextColors;JILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/GlobalColors;
public static final fun dark-zSO0fhY (Lorg/jetbrains/jewel/foundation/BorderColors$Companion;JJJ)Lorg/jetbrains/jewel/foundation/BorderColors;
public static synthetic fun dark-zSO0fhY$default (Lorg/jetbrains/jewel/foundation/BorderColors$Companion;JJJILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/BorderColors;
public static final fun light-jdqzblg (Lorg/jetbrains/jewel/foundation/OutlineColors$Companion;JJJJJ)Lorg/jetbrains/jewel/foundation/OutlineColors;
public static final fun light-jdqzblg (Lorg/jetbrains/jewel/foundation/TextColors$Companion;JJJJJ)Lorg/jetbrains/jewel/foundation/TextColors;
public static synthetic fun light-jdqzblg$default (Lorg/jetbrains/jewel/foundation/OutlineColors$Companion;JJJJJILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/OutlineColors;
public static synthetic fun light-jdqzblg$default (Lorg/jetbrains/jewel/foundation/TextColors$Companion;JJJJJILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/TextColors;
public static final fun light-xwkQ0AY (Lorg/jetbrains/jewel/foundation/GlobalColors$Companion;Lorg/jetbrains/jewel/foundation/BorderColors;Lorg/jetbrains/jewel/foundation/OutlineColors;Lorg/jetbrains/jewel/foundation/TextColors;J)Lorg/jetbrains/jewel/foundation/GlobalColors;
public static synthetic fun light-xwkQ0AY$default (Lorg/jetbrains/jewel/foundation/GlobalColors$Companion;Lorg/jetbrains/jewel/foundation/BorderColors;Lorg/jetbrains/jewel/foundation/OutlineColors;Lorg/jetbrains/jewel/foundation/TextColors;JILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/GlobalColors;
public static final fun light-zSO0fhY (Lorg/jetbrains/jewel/foundation/BorderColors$Companion;JJJ)Lorg/jetbrains/jewel/foundation/BorderColors;
public static synthetic fun light-zSO0fhY$default (Lorg/jetbrains/jewel/foundation/BorderColors$Companion;JJJILjava/lang/Object;)Lorg/jetbrains/jewel/foundation/BorderColors;
}

public final class org/jetbrains/jewel/intui/standalone/theme/IntUiGlobalMetricsKt {
Expand Down
Loading
Loading