scoop:scoop.ui:0.1.0
src/text/editor/preferences.scoop
Selected production source · src/text/editor/preferences.scoop
1
package scoop.ui.text3
import scoop.graphics.Rect4
import scoop.graphics.text.TextPosition5
import scoop.math.max6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.window.InteractionPreferences10
/** Retained with measurement and Accepted geometry, never read from a later preference snapshot. */11
internal struct EditorCaretStyle(val width: Double, val aspectRatio: Double = 0.0) {12
fun padding(layout: TextEditLayout): Double {13
var result = width14
for (line in layout.lines)15
result = max(result, line.rect.height * aspectRatio)16
return result17
}19
fun caret(layout: TextEditLayout, position: TextPosition): Rect? {20
if (aspectRatio == 0.0)21
return layout.caret(position, width)22
val geometry = layout.caret(position, 0.0) ?: return None23
return layout.caret(position, geometry.height * aspectRatio)24
}25
}27
internal fun editorCaretStyle(host: TextEditHost): EditorCaretStyle {28
when (host.appearance.caretWidth) {29
case Some(val width) -> return EditorCaretStyle(width)30
case None -> {}31
}32
val preferences = host.scope.interactionPreferences33
when (preferences.caretWidthPixels) {34
case Some(val pixels) -> return EditorCaretStyle(35
pixels.toDouble() / (host.window.metrics?.scaleFactor ?: 1.0)36
)37
case None -> {}38
}39
return when (preferences.caretAspectRatio) {40
case Some(val ratio) -> EditorCaretStyle(0.0, ratio)41
case None -> EditorCaretStyle(1.0)42
}43
}45
internal fun editorBlinkInterval(host: TextEditHost): Duration =46
host.options.caretBlink ?:47
host.scope.interactionPreferences.caretBlinkInterval ?:48
Duration.milliseconds(500L)50
internal fun editorPreferencesChanged(host: TextEditHost, next: InteractionPreferences) {51
if (host.closed || !host.scope.isMounted)52
return53
val previous = host.caretPreferences54
host.caretPreferences = next55
val blinkChanged =56
host.options.caretBlink == None && previous.caretBlinkInterval != next.caretBlinkInterval57
val widthChanged =58
host.appearance.caretWidth ==59
None &&60
(61
previous.caretWidthPixels !=62
next.caretWidthPixels ||63
previous.caretAspectRatio !=64
next.caretAspectRatio65
)66
if (blinkChanged) {67
host.blink.cancel()68
host.caretVisible = true69
}70
if (widthChanged)71
host.scope.requestLayout()72
else73
if (blinkChanged)74
host.scope.requestFrame()75
}