scoop:scoop.ui:0.1.0
src/text/editor/layout.scoop
Selected production source · src/text/editor/layout.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.math.max6
import scoop.surface.*7
import scoop.ui.*9
internal class EditorInputLayout(10
val layout: TextEditLayout,11
val display: TextDisplay,12
val caretStyle: EditorCaretStyle13
)15
internal class EditorFrame(16
private val retained: RetainedEditorLayout,17
val snapshot: ScrollSnapshot,18
val presentationOffset: Point,19
val editor: TextEditorState,20
val projection: TextProjection,21
val revision: Long,22
val display: TextDisplay,23
val cells: TextEditCells?,24
val caretStyle: EditorCaretStyle,25
val caretPadding: Double26
): Closeable {27
val layout: TextEditLayout get() = retained.layout28
public override fun close() {29
retained.close()30
}31
}33
private fun editorLayout(34
host: TextEditHost,35
width: Double?,36
cells: TextEditCells?,37
caretStyle: EditorCaretStyle38
): EditorInputLayout {39
host.checkDocument(host.current.committedText)40
val input = editorSnapshot(host.current.storage.value)41
val cached = host.currentDisplay42
val display =43
when (cached) {44
case Some(val previous) -> if (45
previous.source == input && host.currentProjection === host.options.projection46
)47
previous48
else49
projectEditorText(input, host.options.projection)50
case None -> projectEditorText(input, host.options.projection)51
}52
val text = display.text53
val style = host.appearance.textStyle54
val content =55
when (host.content) {56
case Some(val previous) -> if (previous.text == text && previous.style === style)57
previous58
else59
styledText {60
this.text(text)61
this.style = style62
}63
case None -> styledText {64
this.text(text)65
this.style = style66
}67
}68
host.content = Some(content)69
val paragraph =70
host.body.update(71
host.appearance.fonts,72
content,73
paragraphStyle {74
this.alignment = host.options.alignment75
this.direction =76
host.options.direction ?: host.scope.layoutDirection.textDirection()77
},78
width,79
[]80
)81
val layout = editorGeometry(host, paragraph, cells)82
host.currentProjection = host.options.projection83
host.currentDisplay = Some(display)84
return EditorInputLayout(layout, display, caretStyle)85
}87
private fun editorHint(host: TextEditHost, width: Double?): TextLayout? {88
if (host.current.text.byteLength != 0L || host.placeholder.byteLength == 0L)89
return None90
val text =91
if (host.options.multiline)92
host.placeholder93
else94
mapEditorText(host.placeholder, editorNewlineEdits(host.placeholder)).text95
val style = host.appearance.placeholderStyle96
val content =97
when (host.hintContent) {98
case Some(val previous) -> if (previous.text == text && previous.style === style)99
previous100
else101
styledText {102
this.text(text)103
this.style = style104
}105
case None -> styledText {106
this.text(text)107
this.style = style108
}109
}110
host.hintContent = Some(content)111
return Some(112
host.hint.update(113
host.appearance.fonts,114
content,115
paragraphStyle {116
this.alignment = host.options.alignment117
this.direction =118
host.options.direction ?: host.scope.layoutDirection.textDirection()119
},120
width,121
[]122
)123
)124
}126
private fun editorWidth(host: TextEditHost, viewport: Double?, padding: Double): Double? {127
if (!host.options.multiline || !host.options.wrap)128
return None129
return when (viewport) {130
case Some(val width) -> Some(max(0.0, width - padding))131
case None -> None132
}133
}135
internal fun editorMeasure(136
host: TextEditHost,137
constraints: Constraints,138
cells: TextEditCells?,139
scrollPadding: (Size, Size) -> ScrollPadding140
): TextEditMeasurement {141
constraints.check()142
val caretStyle = editorCaretStyle(host)143
// An unwrapped line bounds the caret height of its wrapped fragments, including fallback fonts.144
val padding =145
if (caretStyle.aspectRatio > 0.0)146
caretStyle.padding(editorLayout(host, None, cells, caretStyle).layout)147
else148
caretStyle.width149
val width = editorWidth(host, constraints.maxWidth, padding)150
val text = editorLayout(host, width, cells, caretStyle)151
val layout = text.layout152
val paragraphMode =153
when (cells) {154
case None -> true;155
case Some(_) -> false156
}157
val hint =158
if (paragraphMode)159
editorHint(host, width)160
else161
None162
val caretPadding =163
if (paragraphMode)164
padding165
else166
0.0167
val naturalWidth = max(layout.size.width, hint?.size?.width ?: 0.0) + caretPadding168
val size = constraints.constrain(Size(naturalWidth, layout.size.height))169
var contentWidth = layout.size.width170
for (line in layout.lines)171
contentWidth = max(contentWidth, line.rect.x + line.rect.width)172
val content =173
Size(max(size.width, contentWidth + caretPadding), max(size.height, layout.size.height))174
val scroll = host.viewport.layout(size, content, padding = scrollPadding(size, content))175
return TextEditMeasurement(176
LayoutMetrics(size, Some(layout.alphabeticBaseline), Some(layout.ideographicBaseline)),177
layout.retain(),178
scroll,179
host.current,180
host.options.projection,181
host.current.revision,182
text.display,183
cells,184
caretStyle,185
caretPadding,186
when (hint) {187
case Some(_) -> Some(host.hint.retainLayout());188
case None -> None189
},190
host.hint.generation191
)192
}194
/** Pointer geometry stays paired with the last successful frame, even after a failed resize. */195
internal fun editorInputText(host: TextEditHost): EditorInputLayout {196
when (host.accepted) {197
case Some(val frame) -> {198
if (frame.display.source.text == host.current.text)199
return EditorInputLayout(frame.layout, frame.display, frame.caretStyle)200
return editorLayout(201
host,202
editorWidth(host, Some(frame.snapshot.viewport.width), frame.caretPadding),203
frame.cells,204
frame.caretStyle205
)206
}207
case None -> return editorLayout(host, None, None, editorCaretStyle(host))208
}209
}211
internal fun editorCaret(host: TextEditHost, text: EditorInputLayout): Rect? =212
text.caretStyle.caret(213
text.layout,214
editorVisualPosition(text.layout, text.display.mapping.forward(host.current.cursor))215
)217
internal fun editorScene(host: TextEditHost, context: SceneContext, measure: TextEditMeasurement) {218
val frame =219
EditorFrame(220
measure.retained.layout.retain(),221
measure.scrollSnapshot,222
measure.scroll.presentationOffset,223
measure.editor,224
measure.projection,225
measure.revision,226
measure.display,227
measure.cells,228
measure.caretStyle,229
measure.caretPadding230
)231
host.staged?.close()232
host.staged = Some(frame)233
val layout = frame.layout234
val appearance = host.appearance235
val input = host.current.inputState236
val caret = editorCaret(host, EditorInputLayout(layout, frame.display, frame.caretStyle))237
val dropCaret =238
when (host.dropTarget) {239
case Some(val target) -> target.caret(frame)240
case None -> None241
}242
val selectedRange = frame.display.displayRange(input.selection.range)243
val selection = layout.selection(selectedRange)244
val marked =245
when (input.markedRange) {246
case Some(val range) -> layout.selection(frame.display.displayRange(range))247
case None -> []248
}249
val active = host.area.isFocusActive250
val color =251
if (active)252
appearance.selectionColor253
else254
appearance.inactiveSelectionColor255
val textColor =256
if (active)257
appearance.selectionTextColor258
else259
appearance.inactiveSelectionTextColor260
val ink =261
when (textColor) {262
case Some(val value) -> if (selection.size > 0L)263
Some(TextSelectionInk(selectedRange, value))264
else265
None266
case None -> None267
}268
val bodyRevision = host.bodyInk.update(layout.generation, ink)269
host.viewport.present(270
context,271
measure.scroll,272
Some {273
content ->274
val snapshot = frame.snapshot275
host.area.setCaret(caret, Some(input.revision))276
val viewport =277
Rect(278
frame.presentationOffset.x - snapshot.padding.left,279
frame.presentationOffset.y - snapshot.padding.top,280
snapshot.viewport.width,281
snapshot.viewport.height282
)283
content.place(host.area, viewport)284
editorSemantics(host, content, frame, viewport)285
content.draw {286
paint -> for (box in selection)287
paint.canvas.drawRect(box.rect, Paint(color))288
}289
val bounds = layout.paintBounds290
content.drawTextLayer(host.bodyKey, editorInkBounds(bounds), bodyRevision) {291
paint -> drawSelectionText(paint.canvas, editorInkBounds(bounds), selection, ink) {292
canvas -> canvas.drawText(layout)293
}294
}295
if (296
frame.layout.cellRects.size ==297
0L &&298
input.surroundingText.byteLength ==299
0L &&300
host.placeholder.byteLength >301
0L302
) {303
val hint = measure.hint!!.layout304
content.drawTextLayer(305
host.hintKey,306
editorInkBounds(hint.paintBounds),307
measure.hintGeneration308
) { paint -> paint.canvas.drawText(hint) }309
}310
content.draw {311
paint ->312
for (box in marked)313
paint.canvas.drawRect(314
Rect(box.rect.x, box.rect.y + box.rect.height - 1.0, box.rect.width, 1.0),315
Paint(appearance.markedColor)316
)317
if (318
active &&319
host.caretVisible &&320
dropCaret ==321
None &&322
input.selection.anchor ==323
input.selection.cursor324
) {325
when (caret) {326
case Some(val rect) -> paint.canvas.drawRect(327
rect,328
Paint(appearance.caretColor)329
)330
case None -> {}331
}332
}333
when (dropCaret) {334
case Some(val rect) -> paint.canvas.drawRect(rect, Paint(appearance.caretColor))335
case None -> {}336
}337
}338
editorSelectionHandles(host, content, frame, viewport)339
}340
)341
context.onFrameComplete {342
completion ->343
when (host.staged) {344
case Some(val staged) -> if (staged === frame)345
host.staged = None346
case None -> {}347
}348
if (host.closed || completion.submission == FrameSubmission.Discarded) {349
frame.close()350
return@onFrameComplete351
}352
val previous = host.accepted353
host.accepted = Some(frame)354
previous?.close()355
host.dropTarget?.accepted()356
host.dataSource?.update()357
host.touchHandles.accepted()358
if (host.closed)359
return@onFrameComplete360
if (host.current.revision == frame.revision) {361
if (362
host.revealNeeded &&363
!editorDragging(host) &&364
!(host.dropTarget?.isHovering ?: false) &&365
host.area.isFocusActive &&366
host.revealRevision ==367
host.focus.node.root.surface.focusRevision368
) {369
host.revealNeeded = false370
when (caret) {371
case Some(val rect) -> host.keyboard.reveal(rect)372
case None -> {}373
}374
}375
editorDragAccepted(host)376
editorScheduleBlink(host)377
}378
}379
}381
private fun editorInkBounds(bounds: Rect): Rect =382
Rect(bounds.x - 1.0, bounds.y - 1.0, bounds.width + 2.0, bounds.height + 2.0)