scoop:scoop.ui.component:0.1.0
src/sidebar/rail.scoop
Selected production source · src/sidebar/rail.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
private class SidebarRailInput(val props: SidebarProps, val theme: Theme, val toggle: () -> Unit)11
internal fun sidebarRail(input: SidebarProps, theme: Theme, toggle: () -> Unit): View =12
element(13
SidebarRailInput(input, theme, toggle),14
{ current, mount -> SidebarRail(current, scope = mount) }15
)17
private class SidebarRail(18
private val props: Props<SidebarRailInput>,19
scope: ComponentScope20
): Element<Unit>(scope) {21
private var area: InputArea? = None22
private var press: Press? = None23
private var cursor = false24
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.isHovered46
if (hover != cursor) {47
resizeCursor?.invoke(scope.nativeWindow, if (hover) Some(Orientation.Horizontal) else None)48
cursor = hover49
}50
}51
public override fun update(): UiInvalidation {52
area!!.scope.enabled = props.value.props.enabled53
press!!.update(PressOptions(props.value.props.enabled)) { props.value.toggle() }54
refreshCursor()55
val input = props.value.props56
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.relations67
)68
return UiInvalidation.Layout69
}70
private fun expand(value: Boolean): SemanticActionResult {71
val input = props.value.props72
if (!input.enabled)73
return SemanticActionResult.Rejected74
if (input.open.value != value)75
props.value.toggle()76
return if (input.open.value == value)77
SemanticActionResult.Completed78
else79
SemanticActionResult.Rejected80
}81
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<82
Unit83
> =84
LayoutResult(85
LayoutMetrics(constraints.constrain(Size(16.0, constraints.maxHeight ?: 0.0))),86
Unit87
)88
public override fun scene(context: SceneContext, measured: Unit) {89
val size = context.size90
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.098
val color = props.value.theme.tokens.palette.sidebar.border99
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
}111
}