Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/rail.scoop

Selected production source · src/sidebar/rail.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9private class SidebarRailInput(val props: SidebarProps, val theme: Theme, val toggle: () -> Unit)
11internal fun sidebarRail(input: SidebarProps, theme: Theme, toggle: () -> Unit): View =
12 element(
13 SidebarRailInput(input, theme, toggle),
14 { current, mount -> SidebarRail(current, scope = mount) }
15 )
17private class SidebarRail(
18 private val props: Props<SidebarRailInput>,
19 scope: ComponentScope
20): Element<Unit>(scope) {
21 private var area: InputArea? = None
22 private var press: Press? = None
23 private var cursor = false
24 private val semantics = createComponentSemantics(scope, SemanticRole.Disclosure)
25 public override fun onMount() {
26 area =
27 Some(
28 scope.createArea(focusable = true) {
29 context,
30 _ -> if (context.phase == RoutedPhase.Notification && scope.isMounted) {
31 refreshCursor();
32 scope.requestFrame()
33 }
34 }
35 )
36 semantics?.bindFocus(Some(scope.focusTarget(area!!, FocusOptions(tabStop = false))))
37 press =
38 Some(
39 scope.press(area!!, PressOptions(props.value.props.enabled)) {
40 props.value.toggle()
41 }
42 )
43 }
44 private fun refreshCursor() {
45 val hover = props.value.props.enabled && area!!.scope.isHovered
46 if (hover != cursor) {
47 resizeCursor?.invoke(scope.nativeWindow, if (hover) Some(Orientation.Horizontal) else None)
48 cursor = hover
49 }
50 }
51 public override fun update(): UiInvalidation {
52 area!!.scope.enabled = props.value.props.enabled
53 press!!.update(PressOptions(props.value.props.enabled)) { props.value.toggle() }
54 refreshCursor()
55 val input = props.value.props
56 semantics?.bindReference(input.railSemantics.reference)
57 semantics?.update(
58 input.railSemantics.properties(
59 enabled = input.enabled,
60 expanded = Some(input.open.value)
61 ),
62 SemanticActions(
63 invoke = Some { expand(!props.value.props.open.value) },
64 setExpanded = Some { value -> expand(value) }
65 ),
66 input.railSemantics.relations
67 )
68 return UiInvalidation.Layout
69 }
70 private fun expand(value: Boolean): SemanticActionResult {
71 val input = props.value.props
72 if (!input.enabled)
73 return SemanticActionResult.Rejected
74 if (input.open.value != value)
75 props.value.toggle()
76 return if (input.open.value == value)
77 SemanticActionResult.Completed
78 else
79 SemanticActionResult.Rejected
80 }
81 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
82 Unit
83 > =
84 LayoutResult(
85 LayoutMetrics(constraints.constrain(Size(16.0, constraints.maxHeight ?: 0.0))),
86 Unit
87 )
88 public override fun scene(context: SceneContext, measured: Unit) {
89 val size = context.size
90 val bounds = Rect(0.0, 0.0, size.width, size.height)
91 context.place(area!!, bounds)
92 when (semantics) {
93 case Some(val handle) -> context.semantics(handle, bounds)
94 case None -> {}
95 }
96 if (area!!.scope.isHovered && props.value.props.enabled) {
97 val density = props.value.theme.tokens.typography.body.fontSize / 14.0
98 val color = props.value.theme.tokens.palette.sidebar.border
99 context.draw {
100 paint -> paint.canvas.drawRect(
101 Rect(size.width / 2.0 - density, 0.0, density * 2.0, size.height),
102 Paint(color)
103 )
104 }
105 }
106 }
107 public override fun onUnmount() {
108 if (cursor && scope.isMounted)
109 resizeCursor?.invoke(scope.nativeWindow, None)
110 }