in sample/src/main/kotlin/org/jetbrains/desktop/sample/linux/SkikoSampleLinux.kt [294:359]
fun draw(canvas: Canvas, y: Float, scale: Float) {
val textLineStats = statsTextLineCreator.makeTextLine(getTextLineStatsString(), 20 * scale)
pastedImage?.let {
Paint().use { paint ->
val imageRect = it.imageInfo.bounds
canvas.drawImageRect(
it,
src = imageRect.toRect(),
dst = Rect(0f, 0f, imageRect.width * scale, imageRect.height * scale),
paint,
)
}
}
val composedTextStartOffset = this.composedTextStartOffset
val cursorOffset = cursorOffset
val stringLine = if (composedText.isEmpty()) {
text
} else {
text.substring(0, cursorOffset) + composedText + text.substring(cursorOffset, text.length)
}
val textLine = textLineCreator.makeTextLine(stringLine.toString(), SkikoCustomTitlebarLinux.CUSTOM_TITLEBAR_HEIGHT * scale)
if (composedText.isNotEmpty()) {
Paint().use { paint ->
paint.color = Color.YELLOW
paint.strokeWidth = 5 * scale
val x0 = textLine.getCoordAtOffset(cursorOffset)
val x1 = textLine.getCoordAtOffset(cursorOffset + composedText.length)
canvas.drawLine(x0 = x0, y0 = y + (5 * scale), x1 = x1, y1 = y + (5 * scale), paint = paint)
}
}
val selectionStartOffset = selectionStartOffset
val selectionEndOffset = selectionEndOffset
if (selectionStartOffset != null && selectionEndOffset != null) {
Paint().use { paint ->
paint.color = Color.BLUE
val x0 = textLine.getCoordAtOffset(selectionStartOffset)
val x1 = textLine.getCoordAtOffset(selectionEndOffset)
canvas.drawRect(r = Rect(left = x0, top = y + textLine.ascent, right = x1, bottom = y + textLine.descent), paint = paint)
}
}
Paint().use { paint ->
paint.color = Color.WHITE
canvas.drawTextLine(textLineStats, 0f, (SkikoCustomTitlebarLinux.CUSTOM_TITLEBAR_HEIGHT * scale) + textLineStats.height, paint)
canvas.drawTextLine(textLine, 0f, y, paint)
}
if (cursorVisible) {
Paint().use { paint ->
val coord = textLine.getCoordAtOffset(cursorOffset + (composedTextStartOffset ?: 0))
cursorRectangle = LogicalRect(
LogicalPoint(x = coord / scale, y = (y + textLine.ascent) / scale),
LogicalSize(width = 5f, height = (textLine.descent - textLine.ascent) / scale),
)
paint.color = Color.GREEN
paint.strokeWidth = cursorRectangle.size.width
canvas.drawLine(
x0 = cursorRectangle.point.x * scale,
y0 = cursorRectangle.point.y * scale,
x1 = cursorRectangle.point.x * scale,
y1 = (cursorRectangle.point.y + cursorRectangle.size.height) * scale,
paint = paint,
)
}
}
}