Open History to see the last commit
1import type { VerticalHinge } from "./layout";
2
3/** Keep menus on their anchor's side of a creased hinge. */
4export function deriveMenuHorizontalPlacement(input: {
5 readonly anchorX: number;
6 readonly anchorWidth: number;
7 readonly overlayWidth: number;
8 readonly desiredWidth: number;
9 readonly margin: number;
10 /** In the overlay's coordinates. */
11 readonly hinge: VerticalHinge | null;
12}) {
13 const center = input.anchorX + input.anchorWidth / 2;
14 let start = 0;
15 let end = input.overlayWidth;
16 if (input.hinge) {
17 if (center < input.hinge.x) end = input.hinge.x;
18 else start = input.hinge.x + input.hinge.width;
19 }
20 const width = Math.min(input.desiredWidth, Math.max(0, end - start - 2 * input.margin));
21 const preferredLeft =
22 center <= (start + end) / 2 ? input.anchorX : input.anchorX + input.anchorWidth - width;
23 const left = Math.min(Math.max(preferredLeft, start + input.margin), end - width - input.margin);
24 return { left, width };
25}