(input: HintPlacementInput)
| 254 | * sie ist, statt dass der Aufrufer raten muss. |
| 255 | */ |
| 256 | export function placeHint(input: HintPlacementInput): HintPlacement { |
| 257 | const gap = input.gap ?? HINT_GAP; |
| 258 | const margin = input.margin ?? HINT_MARGIN; |
| 259 | const { anchor, size, viewport } = input; |
| 260 | const protect = [anchor, ...input.protect]; |
| 261 | /* |
| 262 | * Ausgewichen wird der SEKTION und nicht dem Knopf. Warum, steht an |
| 263 | * {@link avoidFrameOf}; auf der Querachse richtet sich der Kasten weiter am |
| 264 | * Ausloeser aus, damit sichtbar bleibt, wozu er gehoert. |
| 265 | */ |
| 266 | const frame = avoidFrameOf(anchor, input.protect); |
| 267 | |
| 268 | const results: HintPlacement[] = CANDIDATES.map(({ side, align }) => { |
| 269 | let x = 0; |
| 270 | let y = 0; |
| 271 | let fits = true; |
| 272 | |
| 273 | if (side === 'below' || side === 'above') { |
| 274 | y = side === 'below' ? frame.y + frame.height + gap : frame.y - gap - size.height; |
| 275 | fits = side === 'below' |
| 276 | ? y + size.height <= viewport.height - margin |
| 277 | : y >= margin; |
| 278 | x = align === 'start' |
| 279 | ? anchor.x |
| 280 | : align === 'end' |
| 281 | ? anchor.x + anchor.width - size.width |
| 282 | : anchor.x + anchor.width / 2 - size.width / 2; |
| 283 | x = clamp(x, margin, Math.max(margin, viewport.width - margin - size.width)); |
| 284 | y = clamp(y, margin, Math.max(margin, viewport.height - margin - size.height)); |
| 285 | } else { |
| 286 | x = side === 'right' ? frame.x + frame.width + gap : frame.x - gap - size.width; |
| 287 | fits = side === 'right' |
| 288 | ? x + size.width <= viewport.width - margin |
| 289 | : x >= margin; |
| 290 | y = align === 'start' |
| 291 | ? anchor.y |
| 292 | : align === 'end' |
| 293 | ? anchor.y + anchor.height - size.height |
| 294 | : anchor.y + anchor.height / 2 - size.height / 2; |
| 295 | x = clamp(x, margin, Math.max(margin, viewport.width - margin - size.width)); |
| 296 | y = clamp(y, margin, Math.max(margin, viewport.height - margin - size.height)); |
| 297 | } |
| 298 | |
| 299 | const box: HintRect = { x, y, width: size.width, height: size.height }; |
| 300 | const covered = protect.reduce((sum, rect) => sum + overlapArea(box, rect), 0); |
| 301 | return { side, align, x, y, covered, fits }; |
| 302 | }); |
| 303 | |
| 304 | const clean = results.find((entry) => entry.fits && entry.covered === 0); |
| 305 | if (clean !== undefined) { |
| 306 | return clean; |
| 307 | } |
| 308 | const fitting = results.filter((entry) => entry.fits); |
| 309 | const pool = fitting.length > 0 ? fitting : results; |
| 310 | const best = pool.reduce((entry, next) => (next.covered < entry.covered ? next : entry), pool[0]); |
| 311 | const free = slideClear( |
| 312 | { x: best.x, y: best.y, width: size.width, height: size.height }, |
| 313 | viewport, |
no test coverage detected