Scoop API

scoop:scoop.ui.component:0.1.0

src/menus/row_layout.scoop

Selected production source · src/menus/row_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
8internal class MenuRowLayoutProps(
9 val anchor: LayoutAnchor,
10 val link: HoverPopupLink,
11 val open: Binding<Boolean>,
12 val focus: FocusHandle,
13 val hover: HoverOptions,
14 val highlight: (Boolean) -> Unit,
15 val enter: () -> Unit,
16 val submenu: Boolean,
17 val pointerOpen: () -> Unit
20internal class MenuRowLayout(
21 private val props: Props<MenuRowLayoutProps>,
22 scope: ComponentScope
23): Element<Unit>(scope) {
24 private var area: InputArea? = None
25 private var hover: Hover? = None
26 private fun refresh() {
27 val input = area!!.scope
28 props.value.highlight(input.activity.hasFocus)
29 }
30 public override fun onMount() {
31 val target = scope.createArea()
32 area = Some(target)
33 scope.routeChildren(target)
34 scope.layoutAnchor(props.value.anchor)
35 val open =
36 binding(
37 { props.value.open.value },
38 {
39 value ->
40 if (value)
41 props.value.pointerOpen()
42 props.value.open.value = value
43 }
44 )
45 hover = Some(scope.hover(target, open, props.value.hover))
46 props.value.link.bind(hover!!)
47 target.scope.observeActivity { refresh() }
48 target.scope.addEventHandler {
49 context, event ->
50 if (props.value.link.containsContent(context.target))
51 return@addEventHandler
52 when (event) {
53 case Pointer(InputPointerEvent.Entered(_)) -> {
54 props.value.focus.requestFocus(FocusOrigin.Pointer);
55 refresh()
56 }
57 case Pointer(InputPointerEvent.Moved(_)) -> props.value.focus.requestFocus(
58 FocusOrigin.Pointer
59 )
60 case Pointer(InputPointerEvent.Left(_)) -> refresh()
61 else -> {}
62 }
63 }
64 target.scope.addDefaultAction {
65 context, event ->
66 if (!props.value.submenu || props.value.link.containsContent(context.target))
67 return@addDefaultAction
68 val key =
69 when (event) {
70 case Keyboard(val value) -> value;
71 else -> return@addDefaultAction
72 }
73 if (menuKeyAllowed(key) && menuBackward(key, scope.layoutDirection) == Some(false)) {
74 context.consume();
75 props.value.enter()
76 }
77 }
78 }
79 public override fun update(): UiInvalidation {
80 val input = props.value
81 hover!!.update(
82 binding(
83 { props.value.open.value },
84 {
85 value ->
86 if (value)
87 props.value.pointerOpen()
88 props.value.open.value = value
89 }
90 ),
91 Some(input.hover)
92 )
93 refresh()
94 return UiInvalidation.Layout
95 }
96 public override fun onUnmount() {
97 props.value.link.unbind(hover!!)
98 }
99 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
100 Unit
101 > {
102 val result = context.measureChild(0L, constraints)
103 context.placeChild(0L, Point(0.0, 0.0))
104 context.measureChild(1L, Constraints())
105 context.placeChild(1L, Point(0.0, 0.0))
106 return LayoutResult(result, Unit)
107 }
108 public override fun scene(context: SceneContext, measured: Unit) {
109 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
110 context.anchor(props.value.anchor, bounds)
111 context.place(area!!, bounds)
112 context.children()
113 }