Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/preferences.scoop

Selected production source · src/text/editor/preferences.scoop

1package scoop.ui.text
3import scoop.graphics.Rect
4import scoop.graphics.text.TextPosition
5import scoop.math.max
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.window.InteractionPreferences
10/** Retained with measurement and Accepted geometry, never read from a later preference snapshot. */
11internal struct EditorCaretStyle(val width: Double, val aspectRatio: Double = 0.0) {
12 fun padding(layout: TextEditLayout): Double {
13 var result = width
14 for (line in layout.lines)
15 result = max(result, line.rect.height * aspectRatio)
16 return result
17 }
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 None
23 return layout.caret(position, geometry.height * aspectRatio)
24 }
27internal 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.interactionPreferences
33 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 }
45internal fun editorBlinkInterval(host: TextEditHost): Duration =
46 host.options.caretBlink ?:
47 host.scope.interactionPreferences.caretBlinkInterval ?:
48 Duration.milliseconds(500L)
50internal fun editorPreferencesChanged(host: TextEditHost, next: InteractionPreferences) {
51 if (host.closed || !host.scope.isMounted)
52 return
53 val previous = host.caretPreferences
54 host.caretPreferences = next
55 val blinkChanged =
56 host.options.caretBlink == None && previous.caretBlinkInterval != next.caretBlinkInterval
57 val widthChanged =
58 host.appearance.caretWidth ==
59 None &&
60 (
61 previous.caretWidthPixels !=
62 next.caretWidthPixels ||
63 previous.caretAspectRatio !=
64 next.caretAspectRatio
65 )
66 if (blinkChanged) {
67 host.blink.cancel()
68 host.caretVisible = true
69 }
70 if (widthChanged)
71 host.scope.requestLayout()
72 else
73 if (blinkChanged)
74 host.scope.requestFrame()