Scoop API

scoop:scoop.tray:0.1.0

src/placement.scoop

Selected production source · src/placement.scoop

1package scoop.tray
3import scoop.window.*
5internal fun TrayOwner.placeWindow(window: Window, options: TrayWindowPlacement): RequestStatus {
6 requireOpen()
7 if (!positioning.globalWindowPosition)
8 throw WindowException("tray window placement", WindowErrorKind.Unsupported)
9 @Unsafe
10 {
11 val lease = resource!!.acquireWindow(window)
12 try {
13 val anchor = placementAnchor(options)
14 val centerX = anchor.origin.x + anchor.width / 2.0
15 val centerY = anchor.origin.y + anchor.height / 2.0
16 var target: Monitor? = None
17 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 else
31 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 / fromUnits
41 val height = outer.height * targetUnits / fromUnits
42 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.width
46 val frameHeight = outer.height / fromUnits - metrics.logicalSize.height
47 val contentWidth = plan.width / targetUnits - frameWidth
48 val contentHeight = plan.height / targetUnits - frameHeight
49 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 actual
55 // 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.height
59 ) {
60 val adjusted =
61 planPlacement(
62 anchor,
63 work,
64 actual.width,
65 actual.height,
66 options.gap * targetUnits,
67 options
68 )
69 window.requestPosition(adjusted.origin)
70 }
71 case None -> {}
72 }
73 return status
74 } finally {
75 lease.close()
76 }
77 }
80private fun TrayOwner.placementAnchor(options: TrayWindowPlacement): DesktopRect {
81 if (capabilities().iconBounds)
82 when (geometry()) {
83 case Some(val value) -> return value.bounds
84 case None -> {}
85 }
86 return when (options.anchor) {
87 case Some(TrayAnchor.Rectangle(val value)) -> {
88 validBounds(value.bounds, false)
89 value.bounds
90 }
91 case Some(TrayAnchor.PointHint(val point)) -> {
92 if (!options.allowPointHint)
93 throw WindowException(
94 "tray point hint requires explicit opt-in",
95 WindowErrorKind.Unavailable
96 )
97 val value = DesktopRect(point, 0.0, 0.0)
98 validBounds(value, true)
99 value
100 }
101 case None -> throw WindowException("tray anchor", WindowErrorKind.Unavailable)
102 }
105private 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"))
122private fun smaller(a: Double, b: Double): Double =
123 if (a < b)
124 a
125 else
126 b
128private fun larger(a: Double, b: Double): Double =
129 if (a > b)
130 a
131 else
132 b
134private fun automaticSide(anchor: DesktopRect, work: DesktopRect): TrayPlacementSide {
135 val x = anchor.origin.x + anchor.width / 2.0
136 val y = anchor.origin.y + anchor.height / 2.0
137 val top = y - work.origin.y
138 val bottom = work.origin.y + work.height - y
139 val left = x - work.origin.x
140 val right = work.origin.x + work.width - x
141 if (top <= bottom && top <= left && top <= right)
142 return TrayPlacementSide.Below
143 if (bottom <= left && bottom <= right)
144 return TrayPlacementSide.Above
145 return if (left <= right)
146 TrayPlacementSide.Right
147 else
148 TrayPlacementSide.Left
151private fun opposite(side: TrayPlacementSide): TrayPlacementSide =
152 when (side) {
153 case Above -> TrayPlacementSide.Below
154 case Below -> TrayPlacementSide.Above
155 case Left -> TrayPlacementSide.Right
156 case Right -> TrayPlacementSide.Left
157 case Automatic -> throw IllegalArgumentException(Some("placement side was not resolved"))
158 }
160private fun vertical(side: TrayPlacementSide): Boolean =
161 side == TrayPlacementSide.Above || side == TrayPlacementSide.Below
163private fun availableSpace(
164 side: TrayPlacementSide,
165 anchor: DesktopRect,
166 work: DesktopRect,
167 gap: Double
168): Double =
169 when (side) {
170 case Above -> smaller(anchor.origin.y - gap, work.origin.y + work.height) - work.origin.y
171 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.x
175 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 }
181private fun aligned(
182 origin: Double,
183 extent: Double,
184 size: Double,
185 alignment: TrayPlacementAlignment
186): Double =
187 when (alignment) {
188 case Start -> origin
189 case Center -> origin + (extent - size) / 2.0
190 case End -> origin + extent - size
191 }
193/** Pure placement policy; it cannot turn a hint or a full monitor into a work area. */
194internal fun planPlacement(
195 anchor: DesktopRect,
196 work: DesktopRect,
197 requestedWidth: Double,
198 requestedHeight: Double,
199 gap: Double,
200 options: TrayWindowPlacement
201): 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.0
214 )
215 throw IllegalArgumentException(Some("invalid tray placement size or gap"))
216 var side =
217 if (options.side == TrayPlacementSide.Automatic)
218 automaticSide(anchor, work)
219 else
220 options.side
221 var space = availableSpace(side, anchor, work, gap)
222 val extent =
223 if (vertical(side))
224 requestedHeight
225 else
226 requestedWidth
227 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 = other
233 }
234 }
235 val maxWidth =
236 if (vertical(side))
237 work.width
238 else
239 smaller(work.width, space)
240 val maxHeight =
241 if (vertical(side))
242 smaller(work.height, space)
243 else
244 work.height
245 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.Unavailable
255 )
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) - height
262 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) - width
264 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 else
271 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.height
286 )
287 throw WindowException(
288 "tray window does not fit without sliding",
289 WindowErrorKind.Unavailable
290 )
291 return DesktopRect(DesktopPosition(x, y), width, height)