scoop:scoop.ui:0.1.0
src/overlay/options.scoop
Selected production source · src/overlay/options.scoop
1
package scoop.ui3
import scoop.graphics.Rect4
import scoop.graphics.Point6
public enum OverlayAlignment {7
Start, Center, End8
}10
public enum OverlaySide {11
Top, Right, Bottom, Left, Start, End12
}14
public enum AnchorVisibility {15
Close, Hide, KeepVisible16
}18
public enum OverlayCloseReason {19
Explicit, Escape, OutsidePress, AnchorUnavailable, OwnerClosed20
}22
public enum OutsidePress {23
Ignore, CloseAndConsume, CloseAndContinue24
}26
public struct OverlayDismiss(27
val escape: Boolean = false,28
val outsidePress: OutsidePress = OutsidePress.Ignore,29
val includeAnchor: Boolean = false30
)32
public enum OverlayMode {33
NonModal, FocusContained, Modal34
}36
public struct OverlayFocusOptions(37
val initialFocus: FocusHandle? = None,38
val restoreFocus: Boolean = true,39
val autoFocus: Boolean = true,40
val restoreTarget: FocusHandle? = None41
)43
public enum OverlayPlacement {44
Viewport(45
val horizontal: OverlayAlignment = OverlayAlignment.Center,46
val vertical: OverlayAlignment = OverlayAlignment.Center,47
val viewportPadding: Double = 8.048
),49
Anchored(50
val anchor: LayoutAnchor,51
val preferredSide: OverlaySide = OverlaySide.Bottom,52
val alignment: OverlayAlignment = OverlayAlignment.Start,53
val gap: Double = 4.0,54
val viewportPadding: Double = 8.0,55
val matchAnchorWidth: Boolean = false56
),57
ContentAligned(58
val anchor: LayoutAnchor,59
val preferredSide: OverlaySide = OverlaySide.Bottom,60
val alignment: OverlayAlignment = OverlayAlignment.Center,61
val gap: Double = 4.0,62
val viewportPadding: Double = 8.0,63
val matchAnchorWidth: Boolean = true64
),65
AtPoint(66
val point: Point,67
val preferredSide: OverlaySide = OverlaySide.Bottom,68
val alignment: OverlayAlignment = OverlayAlignment.Start,69
val gap: Double = 0.0,70
val viewportPadding: Double = 8.071
)72
}74
public class PortalOptions internal constructor(75
public val placement: OverlayPlacement = OverlayPlacement.Viewport(),76
public val handle: OverlayHandle? = None,77
public val anchorVisibility: AnchorVisibility = AnchorVisibility.Close,78
public val mode: OverlayMode = OverlayMode.NonModal,79
public val focus: OverlayFocusOptions? = None,80
public val dismiss: OverlayDismiss = OverlayDismiss(),81
public val transition: PresenceOptions = PresenceOptions(),82
public val onCloseRequest: (OverlayCloseReason) -> Boolean = { _ -> true },83
public val onClosed: (OverlayCloseReason) -> Unit = { _ -> Unit }84
) {85
init {86
requireUi(87
mode == OverlayMode.NonModal || (focus?.autoFocus ?: true),88
"focus-constrained overlays require initial focus"89
)90
requireUi(91
mode != OverlayMode.Modal || dismiss.outsidePress != OutsidePress.CloseAndContinue,92
"modal outside presses cannot continue to the background"93
)94
when (placement) {95
case Viewport(_, _, val padding) -> checkOverlayDistance(padding)96
case AtPoint(val point, _, _, val gap, val padding) -> {97
requireUi(point.x.isFinite() && point.y.isFinite(), "non-finite overlay point")98
checkOverlayDistance(gap)99
checkOverlayDistance(padding)100
}101
case ContentAligned(_, _, _, val gap, val padding, _) -> {102
checkOverlayDistance(gap)103
checkOverlayDistance(padding)104
}105
case Anchored(_, _, _, val gap, val padding, _) -> {106
checkOverlayDistance(gap)107
checkOverlayDistance(padding)108
}109
}110
}111
internal val focusOptions: OverlayFocusOptions? get() =112
if (mode == OverlayMode.NonModal)113
focus114
else115
Some(focus ?: OverlayFocusOptions())116
}118
/** Value geometry for the nearest portal in the current scene pass. */119
public class OverlayLayout internal constructor(120
public val bounds: Rect,121
public val available: Rect,122
public val anchor: Rect?,123
public val side: OverlaySide?,124
public val presence: PresenceSnapshot = PresenceSnapshot(PresencePhase.Present, 1.0)125
)127
private fun checkOverlayDistance(value: Double) {128
requireUi(value.isFinite() && value >= 0.0, "invalid overlay gap or viewport padding")129
}131
internal val OverlayPlacement.anchor: LayoutAnchor? get() =132
when (this) {133
case Viewport(_, _, _) -> None134
case AtPoint(_, _, _, _, _) -> None135
case Anchored(val value, _, _, _, _, _) -> Some(value)136
case ContentAligned(val value, _, _, _, _, _) -> Some(value)137
}139
internal fun checkOverlayMutation() {140
when (currentEvaluation()) {141
case Some(_) -> throw IllegalStateException(142
Some("overlay bindings cannot change during render, measure, scene or draw")143
)144
case None -> {}145
}146
}