scoop:scoop.ui.component:0.1.0
src/menus/root_layout.scoop
Selected production source · src/menus/root_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.window.*8
internal 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: () -> Unit18
)20
internal class MenuRootLayout(21
private val props: Props<MenuRootLayoutProps>,22
scope: ComponentScope23
): Element<Unit>(scope) {24
private var area: InputArea? = None25
private var fallback: FocusTarget? = None26
private var hold: LongPress? = None27
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.value39
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.y58
)59
),60
false61
)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.value78
if (!input.enabled || input.link.containsContent(context.target))79
return@addDefaultAction80
val key =81
when (event) {82
case Keyboard(val value) -> value;83
else -> return@addDefaultAction84
}85
if (!menuKeyAllowed(key, true))86
return@addDefaultAction87
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.width101
else102
bounds.x,103
bounds.y + bounds.height104
)105
),106
false107
)108
}109
case None -> {}110
}111
} else112
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.value128
return LongPressOptions(129
input.enabled && !input.open && input.longPress.enabled,130
input.longPress.delay,131
input.longPress.threshold132
)133
}134
private fun held(value: LongPressSample) {135
val input = props.value136
if (!input.enabled || input.open)137
return138
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
return143
val point = value.contact.pointer.logicalPosition144
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.Layout150
}151
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<152
Unit153
> {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
}166
}