scoop:scoop.tray:0.1.0
src/placement.scoop
Selected production source · src/placement.scoop
1
package scoop.tray3
import scoop.window.*5
internal fun TrayOwner.placeWindow(window: Window, options: TrayWindowPlacement): RequestStatus {6
requireOpen()7
if (!positioning.globalWindowPosition)8
throw WindowException("tray window placement", WindowErrorKind.Unsupported)9
@Unsafe10
{11
val lease = resource!!.acquireWindow(window)12
try {13
val anchor = placementAnchor(options)14
val centerX = anchor.origin.x + anchor.width / 2.015
val centerY = anchor.origin.y + anchor.height / 2.016
var target: Monitor? = None17
for (monitor in application.monitors())18
when (monitor.desktopBounds) {19
case Some(val bounds) -> if (containsPoint(bounds, centerX, centerY))20
target = Some(monitor)21
case None -> {}22
}23
val monitor =24
target ?: throw WindowException("tray anchor monitor", WindowErrorKind.Unavailable)25
val work =26
monitor.workArea ?:27
if (options.allowMonitorBounds)28
monitor.desktopBounds ?:29
throw WindowException("monitor bounds", WindowErrorKind.Unavailable)30
else31
throw WindowException("monitor work area", WindowErrorKind.Unavailable)32
val metrics =33
window.metrics ?:34
throw WindowException("window geometry", WindowErrorKind.Unavailable)35
val outer =36
window.outerBounds ?:37
throw WindowException("window outer bounds", WindowErrorKind.Unavailable)38
val fromUnits = desktopUnits(metrics.scaleFactor)39
val targetUnits = desktopUnits(monitor.scaleFactor)40
val width = outer.width * targetUnits / fromUnits41
val height = outer.height * targetUnits / fromUnits42
val plan =43
planPlacement(anchor, work, width, height, options.gap * targetUnits, options)44
if (plan.width != width || plan.height != height) {45
val frameWidth = outer.width / fromUnits - metrics.logicalSize.width46
val frameHeight = outer.height / fromUnits - metrics.logicalSize.height47
val contentWidth = plan.width / targetUnits - frameWidth48
val contentHeight = plan.height / targetUnits - frameHeight49
if (contentWidth <= 0.0 || contentHeight <= 0.0)50
throw WindowException("tray window does not fit", WindowErrorKind.Unavailable)51
window.requestSize(LogicalSize(contentWidth, contentHeight))52
}53
val status = window.requestPosition(plan.origin)54
// Win32 delivers WM_DPICHANGED during SetWindowPos. Use the actual55
// new frame without waiting for a managed metrics event to arrive.56
when (window.outerBounds) {57
case Some(val actual) -> if (58
actual.width != plan.width || actual.height != plan.height59
) {60
val adjusted =61
planPlacement(62
anchor,63
work,64
actual.width,65
actual.height,66
options.gap * targetUnits,67
options68
)69
window.requestPosition(adjusted.origin)70
}71
case None -> {}72
}73
return status74
} finally {75
lease.close()76
}77
}78
}80
private fun TrayOwner.placementAnchor(options: TrayWindowPlacement): DesktopRect {81
if (capabilities().iconBounds)82
when (geometry()) {83
case Some(val value) -> return value.bounds84
case None -> {}85
}86
return when (options.anchor) {87
case Some(TrayAnchor.Rectangle(val value)) -> {88
validBounds(value.bounds, false)89
value.bounds90
}91
case Some(TrayAnchor.PointHint(val point)) -> {92
if (!options.allowPointHint)93
throw WindowException(94
"tray point hint requires explicit opt-in",95
WindowErrorKind.Unavailable96
)97
val value = DesktopRect(point, 0.0, 0.0)98
validBounds(value, true)99
value100
}101
case None -> throw WindowException("tray anchor", WindowErrorKind.Unavailable)102
}103
}105
private fun validBounds(rect: DesktopRect, point: Boolean) {106
if (107
!rect.origin.x.isFinite() ||108
!rect.origin.y.isFinite() ||109
!rect.width.isFinite() ||110
!rect.height.isFinite() ||111
!(rect.origin.x + rect.width).isFinite() ||112
!(rect.origin.y + rect.height).isFinite() ||113
rect.width <114
0.0 ||115
rect.height <116
0.0 ||117
(!point && (rect.width == 0.0 || rect.height == 0.0))118
)119
throw IllegalArgumentException(Some("invalid tray placement bounds"))120
}122
private fun smaller(a: Double, b: Double): Double =123
if (a < b)124
a125
else126
b128
private fun larger(a: Double, b: Double): Double =129
if (a > b)130
a131
else132
b134
private fun automaticSide(anchor: DesktopRect, work: DesktopRect): TrayPlacementSide {135
val x = anchor.origin.x + anchor.width / 2.0136
val y = anchor.origin.y + anchor.height / 2.0137
val top = y - work.origin.y138
val bottom = work.origin.y + work.height - y139
val left = x - work.origin.x140
val right = work.origin.x + work.width - x141
if (top <= bottom && top <= left && top <= right)142
return TrayPlacementSide.Below143
if (bottom <= left && bottom <= right)144
return TrayPlacementSide.Above145
return if (left <= right)146
TrayPlacementSide.Right147
else148
TrayPlacementSide.Left149
}151
private fun opposite(side: TrayPlacementSide): TrayPlacementSide =152
when (side) {153
case Above -> TrayPlacementSide.Below154
case Below -> TrayPlacementSide.Above155
case Left -> TrayPlacementSide.Right156
case Right -> TrayPlacementSide.Left157
case Automatic -> throw IllegalArgumentException(Some("placement side was not resolved"))158
}160
private fun vertical(side: TrayPlacementSide): Boolean =161
side == TrayPlacementSide.Above || side == TrayPlacementSide.Below163
private fun availableSpace(164
side: TrayPlacementSide,165
anchor: DesktopRect,166
work: DesktopRect,167
gap: Double168
): Double =169
when (side) {170
case Above -> smaller(anchor.origin.y - gap, work.origin.y + work.height) - work.origin.y171
case Below -> work.origin.y +172
work.height -173
larger(anchor.origin.y + anchor.height + gap, work.origin.y)174
case Left -> smaller(anchor.origin.x - gap, work.origin.x + work.width) - work.origin.x175
case Right -> work.origin.x +176
work.width -177
larger(anchor.origin.x + anchor.width + gap, work.origin.x)178
case Automatic -> throw IllegalArgumentException(Some("placement side was not resolved"))179
}181
private fun aligned(182
origin: Double,183
extent: Double,184
size: Double,185
alignment: TrayPlacementAlignment186
): Double =187
when (alignment) {188
case Start -> origin189
case Center -> origin + (extent - size) / 2.0190
case End -> origin + extent - size191
}193
/** Pure placement policy; it cannot turn a hint or a full monitor into a work area. */194
internal fun planPlacement(195
anchor: DesktopRect,196
work: DesktopRect,197
requestedWidth: Double,198
requestedHeight: Double,199
gap: Double,200
options: TrayWindowPlacement201
): DesktopRect {202
validBounds(anchor, true)203
validBounds(work, false)204
if (205
!requestedWidth.isFinite() ||206
!requestedHeight.isFinite() ||207
requestedWidth <=208
0.0 ||209
requestedHeight <=210
0.0 ||211
!gap.isFinite() ||212
gap <213
0.0214
)215
throw IllegalArgumentException(Some("invalid tray placement size or gap"))216
var side =217
if (options.side == TrayPlacementSide.Automatic)218
automaticSide(anchor, work)219
else220
options.side221
var space = availableSpace(side, anchor, work, gap)222
val extent =223
if (vertical(side))224
requestedHeight225
else226
requestedWidth227
if (options.flip && extent > space) {228
val flipped = opposite(side)229
val other = availableSpace(flipped, anchor, work, gap)230
if (other > space) {231
side = flipped;232
space = other233
}234
}235
val maxWidth =236
if (vertical(side))237
work.width238
else239
smaller(work.width, space)240
val maxHeight =241
if (vertical(side))242
smaller(work.height, space)243
else244
work.height245
if (246
maxWidth <=247
0.0 ||248
maxHeight <=249
0.0 ||250
(!options.allowResize && (requestedWidth > maxWidth || requestedHeight > maxHeight))251
)252
throw WindowException(253
"tray window does not fit beside the anchor",254
WindowErrorKind.Unavailable255
)256
val width = smaller(requestedWidth, maxWidth)257
val height = smaller(requestedHeight, maxHeight)258
var x = aligned(anchor.origin.x, anchor.width, width, options.alignment)259
var y = aligned(anchor.origin.y, anchor.height, height, options.alignment)260
when (side) {261
case Above -> y = smaller(anchor.origin.y - gap, work.origin.y + work.height) - height262
case Below -> y = larger(anchor.origin.y + anchor.height + gap, work.origin.y)263
case Left -> x = smaller(anchor.origin.x - gap, work.origin.x + work.width) - width264
case Right -> x = larger(anchor.origin.x + anchor.width + gap, work.origin.x)265
case Automatic -> throw IllegalArgumentException(Some("placement side was not resolved"))266
}267
if (options.slide) {268
if (vertical(side))269
x = larger(work.origin.x, smaller(x, work.origin.x + work.width - width))270
else271
y = larger(work.origin.y, smaller(y, work.origin.y + work.height - height))272
}273
if (274
x <275
work.origin.x ||276
y <277
work.origin.y ||278
x +279
width >280
work.origin.x +281
work.width ||282
y +283
height >284
work.origin.y +285
work.height286
)287
throw WindowException(288
"tray window does not fit without sliding",289
WindowErrorKind.Unavailable290
)291
return DesktopRect(DesktopPosition(x, y), width, height)292
}