Scoop API

scoop:scoop.ui.component:0.1.0

src/menus/root_layout.scoop

Selected production source · src/menus/root_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
8internal class MenuRootLayoutProps(
9 val anchor: LayoutAnchor,
10 val link: HoverPopupLink,
11 val contextMenu: Boolean,
12 val enabled: Boolean,
13 val open: Boolean,
14 val longPress: LongPressOptions,
15 val show: (Point?, Boolean) -> Unit,
16 val hovered: () -> Unit,
17 val updated: () -> Unit
20internal class MenuRootLayout(
21 private val props: Props<MenuRootLayoutProps>,
22 scope: ComponentScope
23): Element<Unit>(scope) {
24 private var area: InputArea? = None
25 private var fallback: FocusTarget? = None
26 private var hold: LongPress? = None
27 public override fun onMount() {
28 val target = scope.createArea(focusable = props.value.contextMenu)
29 area = Some(target)
30 if (props.value.contextMenu) {
31 fallback = Some(scope.focusTarget(target, FocusOptions(tabStop = false)))
32 hold = Some(scope.longPress(target, holdOptions()) { value -> held(value) })
33 }
34 scope.routeChildren(target)
35 scope.layoutAnchor(props.value.anchor)
36 target.scope.addDefaultAction {
37 context, event ->
38 val input = props.value
39 if (input.enabled && !input.link.containsContent(context.target))
40 when (event) {
41 case Pointer(
42 InputPointerEvent.Button(
43 val pointer,
44 MouseButton.Right,
45 ElementState.Pressed,
46 _
47 )
48 ) -> {
49 if (input.contextMenu && target.visibleBounds != None) {
50 context.consume()
51 if (!target.scope.activity.hasFocus)
52 fallback!!.handle.requestFocus(FocusOrigin.Pointer)
53 input.show(
54 Some(
55 Point(
56 pointer.state.logicalPosition.x,
57 pointer.state.logicalPosition.y
58 )
59 ),
60 false
61 )
62 }
63 }
64 else -> {}
65 }
66 }
67 target.scope.addEventHandler {
68 context,
69 event -> if (props.value.enabled && !props.value.link.containsContent(context.target))
70 when (event) {
71 case Pointer(InputPointerEvent.Entered(_)) -> props.value.hovered()
72 else -> {}
73 }
74 }
75 target.scope.addDefaultAction {
76 context, event ->
77 val input = props.value
78 if (!input.enabled || input.link.containsContent(context.target))
79 return@addDefaultAction
80 val key =
81 when (event) {
82 case Keyboard(val value) -> value;
83 else -> return@addDefaultAction
84 }
85 if (!menuKeyAllowed(key, true))
86 return@addDefaultAction
87 if (input.contextMenu) {
88 val request =
89 key.logicalKey ==
90 LogicalKey.Named(NamedKey.ContextMenu) ||
91 (key.logicalKey == LogicalKey.Named(NamedKey.F10) && key.modifiers.shift)
92 if (request)
93 when (target.visibleBounds) {
94 case Some(val bounds) -> {
95 context.consume()
96 input.show(
97 Some(
98 Point(
99 if (scope.layoutDirection == LayoutDirection.Rtl)
100 bounds.x + bounds.width
101 else
102 bounds.x,
103 bounds.y + bounds.height
104 )
105 ),
106 false
107 )
108 }
109 case None -> {}
110 }
111 } else
112 if (
113 !key.modifiers.shift &&
114 (
115 key.logicalKey ==
116 LogicalKey.Named(NamedKey.ArrowDown) ||
117 key.logicalKey ==
118 LogicalKey.Named(NamedKey.ArrowUp)
119 )
120 ) {
121 context.consume()
122 input.show(None, key.logicalKey == LogicalKey.Named(NamedKey.ArrowUp))
123 }
124 }
125 }
126 private fun holdOptions(): LongPressOptions {
127 val input = props.value
128 return LongPressOptions(
129 input.enabled && !input.open && input.longPress.enabled,
130 input.longPress.delay,
131 input.longPress.threshold
132 )
133 }
134 private fun held(value: LongPressSample) {
135 val input = props.value
136 if (!input.enabled || input.open)
137 return
138 val target = area!!
139 if (!target.scope.activity.hasFocus)
140 fallback!!.handle.requestFocus(FocusOrigin.Pointer)
141 if (!scope.isMounted || target.isClosed || !target.scope.isInputAllowed)
142 return
143 val point = value.contact.pointer.logicalPosition
144 input.show(Some(Point(point.x, point.y)), false)
145 }
146 public override fun update(): UiInvalidation {
147 hold?.update(holdOptions()) { value -> held(value) }
148 props.value.updated()
149 return UiInvalidation.Layout
150 }
151 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
152 Unit
153 > {
154 val result = context.measureChild(0L, constraints)
155 context.placeChild(0L, Point(0.0, 0.0))
156 context.measureChild(1L, Constraints())
157 context.placeChild(1L, Point(0.0, 0.0))
158 return LayoutResult(result, Unit)
159 }
160 public override fun scene(context: SceneContext, measured: Unit) {
161 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
162 context.anchor(props.value.anchor, bounds)
163 context.place(area!!, bounds)
164 context.children()
165 }