scoop:scoop.tray:0.1.0
src/events.scoop
Selected production source · src/events.scoop
1
package scoop.tray3
import scoop.window.*5
internal fun TrayOwner.receive(event: NativeTrayEvent) {6
checkThread()7
if (state == TrayState.Closed)8
return9
if (state == TrayState.Open && resource!!.isClosing) {10
close(TrayMenuCancelReason.ApplicationExit)11
return12
}13
when (event.kind) {14
1 -> {15
val next =16
if (event.action != 0)17
TrayAvailability.Available18
else19
TrayAvailability.Unavailable20
if (next != availability) {21
availability = next22
emit(TrayEvent.AvailabilityChanged(next))23
}24
}25
2 -> if (state == TrayState.Open)26
emit(TrayEvent.Activated(activation(event)))27
3 -> settle()28
4 -> settle()29
5 -> if (state == TrayState.Open && !busy()) {30
when (menuLease) {31
case Some(_) -> showMenu()32
case None -> emit(TrayEvent.Activated(activation(event)))33
}34
}35
6 -> throw WindowException(36
"native tray service",37
WindowErrorKind.NativeFailure,38
Some(event.error)39
)40
else -> throw WindowException("native tray event", WindowErrorKind.NativeFailure)41
}42
}44
private fun TrayOwner.activation(event: NativeTrayEvent): TrayActivation {45
val anchor =46
when (event.anchor) {47
1 -> {48
val bounds =49
DesktopRect(DesktopPosition(event.x, event.y), event.width, event.height)50
Some(51
TrayAnchor.Rectangle(52
TrayGeometry(53
bounds,54
monitorFor(55
bounds.origin.x + bounds.width / 2.0,56
bounds.origin.y + bounds.height / 2.057
)58
)59
)60
)61
}62
2 -> Some(TrayAnchor.PointHint(DesktopPosition(event.x, event.y)))63
else -> None64
}65
return TrayActivation(66
when (event.action) {67
1 -> TrayActivationKind.Primary;68
2 -> TrayActivationKind.Context;69
else -> TrayActivationKind.Secondary70
},71
when (event.source) {72
1 -> TrayActivationSource.Pointer;73
2 -> TrayActivationSource.Keyboard;74
else -> TrayActivationSource.Unknown75
},76
anchor,77
when (event.button) {78
1L -> Some(MouseButton.Left);79
2L -> Some(MouseButton.Right);80
3L -> Some(MouseButton.Middle);81
else -> None82
},83
if (event.clicks > 0)84
Some(event.clicks)85
else86
None87
)88
}90
internal fun TrayOwner.monitorFor(x: Double, y: Double): MonitorId? {91
for (monitor in application.monitors())92
when (monitor.desktopBounds) {93
case Some(val bounds) -> if (containsPoint(bounds, x, y))94
return Some(monitor.id)95
case None -> {}96
}97
return None98
}100
internal fun TrayOwner.geometry(): TrayGeometry? {101
if (!capabilities().iconBounds)102
throw WindowException("tray icon bounds", WindowErrorKind.Unsupported)103
@Unsafe104
{105
val value = nativeTrayGeometry(pointer!!)106
if (value.error == -2L)107
return None108
checkTrayStatus(value.error, "tray icon bounds")109
val bounds = DesktopRect(DesktopPosition(value.x, value.y), value.width, value.height)110
return Some(111
TrayGeometry(112
bounds,113
monitorFor(value.x + value.width / 2.0, value.y + value.height / 2.0)114
)115
)116
}117
}119
internal fun containsPoint(bounds: DesktopRect, x: Double, y: Double): Boolean =120
x >=121
bounds.origin.x &&122
y >=123
bounds.origin.y &&124
x <125
bounds.origin.x +126
bounds.width &&127
y <128
bounds.origin.y +129
bounds.height