overlay/position.go

Functions

Functions

func Position

Position calculates the overlay position relative to an anchor rectangle.

 

Parameters:

- placement: preferred placement direction

- anchorGlobal: the anchor widget's bounds in window coordinates

- overlaySize: the measured size of the overlay content

- windowSize: the total window size for viewport clamping

- gap: spacing between the anchor and overlay edges

 

The function applies flip logic (tries the opposite side if the overlay

would go out of bounds) and then clamps to the viewport.

func Position(
	placement Placement,
	anchorGlobal geometry.Rect,
	overlaySize geometry.Size,
	windowSize geometry.Size,
	gap float32,
) geometry.Point {
	var x, y float32

	switch placement {
	case PlacementBelow:
		x = anchorGlobal.Min.X
		y = anchorGlobal.Max.Y + gap
	case PlacementAbove:
		x = anchorGlobal.Min.X
		y = anchorGlobal.Min.Y - overlaySize.Height - gap
	case PlacementRight:
		x = anchorGlobal.Max.X + gap
		y = anchorGlobal.Min.Y
	case PlacementLeft:
		x = anchorGlobal.Min.X - overlaySize.Width - gap
		y = anchorGlobal.Min.Y
	}

	// Flip if the overlay goes out of viewport bounds.
	x, y = flip(placement, x, y, anchorGlobal, overlaySize, windowSize, gap)

	// Clamp to viewport.
	x, y = clampToViewport(x, y, overlaySize, windowSize)

	return geometry.Pt(x, y)
}