Scoop API

scoop:scoop.ui:0.1.0

src/runtime/direction.scoop

Selected production source · src/runtime/direction.scoop

1package scoop.ui
3import scoop.graphics.text.TextDirection
5public enum LayoutDirection {
6 Ltr, Rtl
7}
9/** Layout inheritance is independent from a paragraph's explicit bidi direction. */
10public fun LayoutDirection.textDirection(): TextDirection =
11 if (this == LayoutDirection.Rtl)
12 TextDirection.Rtl
13 else
14 TextDirection.Ltr
16public fun direction(content: View, value: LayoutDirection, key: ViewKey = ViewKey.Position): View =
17 View(key, ViewBody.Direction(content, value))
19internal val View.directionOverride: LayoutDirection? get() =
20 when (body) {
21 case Direction(_, val value) -> Some(value)
22 else -> None
23 }
25/** Only a direction change reaches this path; ordinary Props still use reconciliation. */
26internal fun MountedNode.refreshDirection(turn: UpdateTurn) {
27 if (!active || !root.isOpen)
28 return
29 val next = view.directionOverride ?: parent?.layoutDirection ?: LayoutDirection.Ltr
30 if (next == layoutDirection)
31 return
32 layoutDirection = next
33 request(UiInvalidation.Layout)
34 updated = Some(turn)
35 inputChanged = false
36 stateDirty = false
37 when (body) {
38 case Component(val instance) -> rebuildComponent(instance, turn, true)
39 case Element(val instance) -> {
40 for (child in children)
41 child.refreshDirection(turn)
42 if (mounted && active && root.isOpen)
43 request(updateInputs { instance.update() })
44 }
45 case Lazy(val instance, _) -> {
46 for (child in children)
47 child.refreshDirection(turn)
48 if (mounted && active && root.isOpen)
49 request(updateInputs { instance.update() })
50 }
51 else -> for (child in children)
52 child.refreshDirection(turn)
53 }
56internal fun MountedNode.rebuildComponent(
57 instance: ComponentInstance,
58 turn: UpdateTurn,
59 directionOnly: Boolean
60) {
61 val collector = DependencyCollector()
62 val next = evaluateUi(Some(collector)) { instance.value.render() }
63 if (!active || !root.isOpen)
64 return
65 replaceDependencies(collector)
66 if (directionOnly && children.size == 1L && children[0L].view === next)
67 children[0L].refreshDirection(turn)
68 else
69 reconcile([next], turn)
72internal fun MountedNode.replaceDependencies(collector: DependencyCollector) {
73 replaceStateDependencies(this, dependencies, collector)
76/** Only root display inputs are observed here; update retains its ordinary mutation rules. */
77internal fun MountedNode.updateInputs(action: () -> UiInvalidation): UiInvalidation {
78 val collector = DependencyCollector()
79 scope.displayReads = Some(collector)
80 try {
81 val invalidation = action()
82 if (active && root.isOpen)
83 replaceDependencies(collector)
84 return invalidation
85 } finally {
86 scope.displayReads = None
87 }