scoop:scoop.ui.component:0.1.0
platform/desktop/native/chrome/interaction.scoop
Selected production source · platform/desktop/native/chrome/interaction.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.window.*8
internal enum WindowInteraction {9
Move, Resize(WindowResizeEdge)10
}12
internal class WindowInteractionProps(13
val content: View,14
val kind: WindowInteraction,15
val enabled: Boolean,16
val doubleClickMaximize: Boolean = false,17
val windowMenu: Boolean = false18
)20
private class WindowInteractionLayout(21
private val props: Props<WindowInteractionProps>,22
scope: ComponentScope23
): Element<Unit>(scope) {24
private var area: InputArea? = None25
private var region: WindowRegionHandle? = None26
private var previous: WindowInteraction? = None27
public override fun onMount() {28
val target = scope.createArea()29
area = Some(target)30
scope.routeChildren(target)31
target.scope.defaultAction =32
{33
context, event ->34
val input = props.value35
if (input.enabled)36
when (event) {37
case Pointer(38
InputPointerEvent.Button(_, val button, ElementState.Pressed, val count)39
) -> {40
val capabilities = scope.application.desktopCapabilities().frame41
if (button == MouseButton.Left) {42
when (input.kind) {43
case Move -> if (count >= 2 && input.doubleClickMaximize) {44
requireChromeRequest(45
scope.nativeWindow.requestMaximized(46
!(scope.nativeWindow.isMaximized ?: false)47
),48
"maximize window"49
)50
} else51
requireChromeRequest(52
scope.nativeWindow.beginMove(),53
"move window"54
)55
case Resize(val edge) -> requireChromeRequest(56
scope.nativeWindow.beginResize(edge),57
"resize window"58
)59
}60
context.consume()61
} else62
if (63
button ==64
MouseButton.Right &&65
input.windowMenu &&66
capabilities.windowMenu67
) {68
requireChromeRequest(69
scope.nativeWindow.showWindowMenu(),70
"window menu"71
)72
context.consume()73
}74
}75
else -> {}76
}77
}78
update()79
}80
public override fun update(): UiInvalidation {81
val value = props.value82
val capabilities = scope.application.desktopCapabilities().frame83
if (value.enabled) {84
val supported =85
when (value.kind) {86
case Move -> capabilities.beginMove;87
case Resize(_) -> capabilities.beginResize || capabilities.resizeHitRegions88
}89
if (!supported)90
throw WindowException("window interaction", WindowErrorKind.Unsupported)91
}92
if (previous != Some(value.kind)) {93
region?.close()94
region =95
when (value.kind) {96
case Resize(val edge) -> if (capabilities.resizeHitRegions)97
Some(98
scope.createWindowRegion(99
WindowRegionKind.Hit(WindowHitRole.Resize(edge))100
)101
)102
else103
None104
case Move -> None105
}106
previous = Some(value.kind)107
}108
when (region) {109
case Some(val value) -> value.enabled = props.value.enabled;110
case None -> {}111
}112
return UiInvalidation.Scene113
}114
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<115
Unit116
> {117
val size = context.measureChild(0L, constraints)118
context.placeChild(0L, Point(0.0, 0.0))119
return LayoutResult(size, Unit)120
}121
public override fun scene(context: SceneContext, measured: Unit) {122
val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)123
context.place(area!!, bounds)124
when (region) {125
case Some(val value) -> context.place(value, bounds);126
case None -> {}127
}128
context.children()129
}130
}132
public class WindowDragRegionBuilder internal constructor() {133
public var enabled: Boolean = true134
public var doubleClickMaximize: Boolean = true135
public var windowMenu: Boolean = true136
public var key: ViewKey = ViewKey.Position137
}139
public fun windowDragRegion(140
content: View,141
configure: WindowDragRegionBuilder.() -> Unit = {}142
): View {143
val builder = WindowDragRegionBuilder()144
builder.configure()145
return element(146
WindowInteractionProps(147
content,148
WindowInteraction.Move,149
builder.enabled,150
builder.doubleClickMaximize,151
builder.windowMenu152
),153
{ current, mount -> WindowInteractionLayout(current, scope = mount) },154
[content],155
builder.key156
)157
}159
public class WindowResizeRegionBuilder internal constructor() {160
public var enabled: Boolean = true161
public var key: ViewKey = ViewKey.Position162
}164
public fun windowResizeRegion(165
edge: WindowResizeEdge,166
content: View,167
configure: WindowResizeRegionBuilder.() -> Unit = {}168
): View {169
val builder = WindowResizeRegionBuilder()170
builder.configure()171
return element(172
WindowInteractionProps(content, WindowInteraction.Resize(edge), builder.enabled),173
{ current, mount -> WindowInteractionLayout(current, scope = mount) },174
[content],175
builder.key176
)177
}