Scoop API

scoop:scoop.ui.component:0.1.0

src/scroll/viewport_bars.scoop

Selected production source · src/scroll/viewport_bars.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
9/** Themed scrollbar presentation for a custom viewport. */
10public class ViewportScrollbarOptions public constructor(
11 public val theme: Theme,
12 public val axes: ScrollAxes = ScrollAxes.Both,
13 public val visibility: ScrollbarVisibility = ScrollbarVisibility.Auto,
14 public val scrollbar: ScrollbarOptions = scrollbarOptions(),
15 public val scrollbarBehavior: ScrollbarBehavior = scrollbarBehavior(),
16 public val lineStep: Double = 40.0,
17 public val enabled: Boolean = true
18) {
19 init {
20 requireComponent(lineStep.isFinite() && lineStep > 0.0, "invalid scroll line step")
21 }
24/** Draw after the owner's content. Its input area, focus and ScrollViewport remain the owner's. */
25public class ViewportScrollbars public constructor(
26 private val scope: ComponentScope,
27 private val viewport: ScrollViewport,
28 parent: InputArea,
29 parentSemantics: SemanticsHandle? = None,
30 private val options: () -> ViewportScrollbarOptions
31) {
32 private val painters = scope.resource { ThemePainterCache() }
33 private val feedback = scope
34 .resource {
35 ScrollbarFeedback(scope, { viewport.state }, options, parent) { feedbackChanged() }
36 }
37 private val horizontal = bar(parent, parentSemantics, Orientation.Horizontal)
38 private val vertical = bar(parent, parentSemantics, Orientation.Vertical)
39 private var direction = scope.layoutDirection
40 private var reserved = Size(0.0, 0.0)
41 public val gutter: Size get() = reserved
43 private fun bar(
44 parent: InputArea,
45 semantics: SemanticsHandle?,
46 orientation: Orientation
47 ): ScrollBarControl = ScrollBarControl(
48 scope,
49 { viewport.state },
50 options,
51 parent,
52 orientation,
53 feedback,
54 semantics
55 ) { handle, axis ->
56 val value = options()
57 handle
58 .updateScrollBar(
59 viewport,
60 scrollSemanticOrientation(axis),
61 value.lineStep,
62 scrollBarProperties(value, axis)
63 )
64 }
66 private fun feedbackChanged() {
67 horizontal.feedbackChanged()
68 vertical.feedbackChanged()
69 if (scope.isMounted) scope.requestFrame()
70 }
72 internal fun painter(): ThemePainter = painters.get(options().theme)
74 /** Returns whether this configuration changes the reserved viewport size. */
75 public fun update(): Boolean {
76 feedback.update()
77 horizontal.update()
78 vertical.update()
79 val value = options()
80 painters.get(value.theme)
81 val next = scrollbarGutter(
82 value.axes,
83 value.visibility,
84 feedback.options.thickness,
85 value.scrollbarBehavior.placement
86 )
87 val changed = next != gutter
88 reserved = next
89 return changed
90 }
92 public fun contentViewport(outer: Size, direction: LayoutDirection): Rect = scrollbarViewport(
93 outer,
94 gutter,
95 direction
96 )
98 public fun layout(
99 viewportSize: Size,
100 contentSize: Size,
101 adjustment: ScrollAdjustment? = None
102 ): ScrollLayout {
103 val value = options()
104 val padding = scrollbarPadding(
105 viewportSize,
106 contentSize,
107 value.axes,
108 value.visibility,
109 feedback.options.thickness,
110 value.scrollbarBehavior.placement,
111 scope.layoutDirection
112 )
113 return viewport.layout(viewportSize, contentSize, adjustment, padding)
114 }
116 public fun scene(context: SceneContext, layout: ScrollLayout) {
117 val size = context.size
118 val presentedDirection = context.layoutDirection
119 context
120 .onFrameComplete { frame ->
121 if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {
122 if (direction != presentedDirection) {
123 horizontal.cancel()
124 vertical.cancel()
125 }
126 direction = presentedDirection
127 }
128 }
129 val value = options()
130 val snapshot = layout.snapshot
131 val bars = ScrollBarsLayout(
132 size,
133 snapshot,
134 value.axes,
135 value.visibility,
136 feedback.options.thickness,
137 presentedDirection,
138 value.scrollbarBehavior.placement
139 )
140 val bounds = Rect(0.0, 0.0, size.width, size.height)
141 val painter = painters.get(value.theme)
142 feedback.scene(context, bounds, bars.horizontal, bars.vertical)
143 horizontal.scene(context, snapshot, bars.horizontal, painter)
144 vertical.scene(context, snapshot, bars.vertical, painter)
145 }