net.peanuuutz.fork.ui.scene.screen.ScreenOwner.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fork-ui Show documentation
Show all versions of fork-ui Show documentation
Comprehensive API designed for Minecraft modders
The newest version!
package net.peanuuutz.fork.ui.scene.screen
import androidx.compose.runtime.CompositionContext
import net.peanuuutz.fork.ui.ui.context.UIContext
import net.peanuuutz.fork.ui.ui.context.focus.FocusMoveDirection.Down
import net.peanuuutz.fork.ui.ui.context.focus.FocusMoveDirection.Left
import net.peanuuutz.fork.ui.ui.context.focus.FocusMoveDirection.Next
import net.peanuuutz.fork.ui.ui.context.focus.FocusMoveDirection.Previous
import net.peanuuutz.fork.ui.ui.context.focus.FocusMoveDirection.Right
import net.peanuuutz.fork.ui.ui.context.focus.FocusMoveDirection.Up
import net.peanuuutz.fork.ui.ui.context.key.Key
import net.peanuuutz.fork.ui.ui.context.key.isReleased
import net.peanuuutz.fork.ui.ui.context.key.isShiftPressed
import net.peanuuutz.fork.ui.ui.context.owner.AbstractOwner
import net.peanuuutz.fork.ui.ui.context.owner.OwnerContainer
import net.peanuuutz.fork.ui.ui.context.pointer.PointerEvent
import net.peanuuutz.fork.ui.ui.draw.canvas.Canvas
import net.peanuuutz.fork.ui.ui.layout.Constraints
import net.peanuuutz.fork.ui.ui.layout.DefaultMeasurePolicy
import net.peanuuutz.fork.ui.ui.modifier.Modifier
import net.peanuuutz.fork.ui.ui.modifier.input.onKeyOut
import net.peanuuutz.fork.ui.ui.node.LayoutNode
internal class ScreenOwner(
context: CompositionContext,
container: OwnerContainer,
shouldReceiveEvents: Boolean,
canvas: Canvas,
onExitRequest: () -> Unit
) : AbstractOwner(
compositionContext = context,
container = container,
shouldReceiveEvents = shouldReceiveEvents
),
UIContext by ScreenContext(
canvas = canvas,
onExitRequest = onExitRequest
)
{
override var constraints: Constraints = Constraints.Maximum
set(value) {
if (field == value) {
return
}
field = value
root.requestMeasurement()
}
override val root: LayoutNode = LayoutNode().apply {
modifier = Modifier
.onKeyOut { event ->
if (event.isReleased) {
return@onKeyOut false
}
val direction = when (event.key) {
Key.Tab -> if (event.isShiftPressed) Previous else Next
Key.Left -> Left
Key.Up -> Up
Key.Right -> Right
Key.Down -> Down
else -> return@onKeyOut false
}
focusOwner.moveFocus(direction)
}
.then(focusOwner.modifier)
measurePolicy = DefaultMeasurePolicy
}
override fun handlePointerEvent(pointerEvent: PointerEvent): Boolean {
root.outermostCoordinator.propagatePointerEvent(pointerEvent)
return pointerEvent.isConsumed
}
}