-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[JEWEL-735] Implement Markdown tables support (GFM extension) #2913
Open
hamen
wants to merge
3
commits into
JetBrains:master
Choose a base branch
from
rock3r:im-jewel-735
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,615
−92
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
...ewel/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/layout/BasicTableLayout.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
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 | ||
import kotlin.math.max | ||
|
||
/** | ||
* 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 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) | ||
val measurablesByRow = measurables.chunked(columnCount) | ||
for ((rowIndex, rowCells) in rows.withIndex()) { | ||
require(rowCells.size == columnCount) { | ||
"Row $rowIndex contains ${rowCells.size} cells, but it should have $columnCount cells." | ||
} | ||
|
||
for ((columnIndex, cell) in rowCells.withIndex()) { | ||
// Measure each cell individually | ||
val measurable = measurablesByRow[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(height = Int.MAX_VALUE) | ||
intrinsicColumnWidths[columnIndex] = | ||
max(intrinsicColumnWidths[columnIndex].or(0), intrinsicCellWidth) | ||
} | ||
} | ||
|
||
// 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 | ||
measurablesByRow.mapIndexed { rowIndex, rowMeasurables -> | ||
var maxCellHeight = 0 | ||
for ((columnIndex, cellMeasurable) in rowMeasurables.withIndex()) { | ||
val columnWidth = columnWidths[columnIndex] | ||
val cellHeight = cellMeasurable.maxIntrinsicHeight(width = columnWidth) | ||
maxCellHeight = max(maxCellHeight, cellHeight) | ||
} | ||
|
||
tableHeight += maxCellHeight | ||
intrinsicRowHeights[rowIndex] = maxCellHeight | ||
} | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was a bit larger change than this but 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably meant the loop above, with
withIndex
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal tho