Scoop API

scoop:scoop.tray:0.1.0

src/events.scoop

Selected production source · src/events.scoop

1package scoop.tray
3import scoop.window.*
5internal fun TrayOwner.receive(event: NativeTrayEvent) {
6 checkThread()
7 if (state == TrayState.Closed)
8 return
9 if (state == TrayState.Open && resource!!.isClosing) {
10 close(TrayMenuCancelReason.ApplicationExit)
11 return
12 }
13 when (event.kind) {
14 1 -> {
15 val next =
16 if (event.action != 0)
17 TrayAvailability.Available
18 else
19 TrayAvailability.Unavailable
20 if (next != availability) {
21 availability = next
22 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 }
44private 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.0
57 )
58 )
59 )
60 )
61 }
62 2 -> Some(TrayAnchor.PointHint(DesktopPosition(event.x, event.y)))
63 else -> None
64 }
65 return TrayActivation(
66 when (event.action) {
67 1 -> TrayActivationKind.Primary;
68 2 -> TrayActivationKind.Context;
69 else -> TrayActivationKind.Secondary
70 },
71 when (event.source) {
72 1 -> TrayActivationSource.Pointer;
73 2 -> TrayActivationSource.Keyboard;
74 else -> TrayActivationSource.Unknown
75 },
76 anchor,
77 when (event.button) {
78 1L -> Some(MouseButton.Left);
79 2L -> Some(MouseButton.Right);
80 3L -> Some(MouseButton.Middle);
81 else -> None
82 },
83 if (event.clicks > 0)
84 Some(event.clicks)
85 else
86 None
87 )
90internal 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 None
100internal fun TrayOwner.geometry(): TrayGeometry? {
101 if (!capabilities().iconBounds)
102 throw WindowException("tray icon bounds", WindowErrorKind.Unsupported)
103 @Unsafe
104 {
105 val value = nativeTrayGeometry(pointer!!)
106 if (value.error == -2L)
107 return None
108 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 }
119internal 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