Skip to content

Add anchor method and fix issue (#100) #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions adafruit_displayio_layout/layouts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SPDX-FileCopyrightText: 2025 Shubham Patel
#
# SPDX-License-Identifier: MIT

from micropython import const

try:
from typing import Tuple
except ImportError:
pass

# Anchor point constants for anchor method.
ANCHOR_TOP_LEFT = const((0.0, 0.0))
ANCHOR_TOP_CENTER = const((0.5, 0.0))
ANCHOR_TOP_RIGHT = const((1.0, 0.0))
ANCHOR_CENTER_LEFT = const((0.0, 0.5))
ANCHOR_CENTER = const((0.5, 0.5))
ANCHOR_CENTER_RIGHT = const((1.0, 0.5))
ANCHOR_BOTTOM_LEFT = const((0.0, 1.0))
ANCHOR_BOTTOM_CENTER = const((0.5, 1.0))
ANCHOR_BOTTOM_RIGHT = const((1.0, 1.0))


def anchor(obj, anchor: Tuple[float, float], width: int, height: int) -> None:
"""Helper to position a display object on screen using a defined anchor point.

Sets `anchor_point` and `anchored_position` for display elements such as `Label`,
`Widget`, `AnchoredTileGrid`, or `AnchoredGroup`.

:param obj: The object to be positioned. Must support `anchor_point` and `anchored_position`.
:param anchor: One of the predefined anchor constants (e.g. ANCHOR_TOP_LEFT, ANCHOR_CENTER)
:param width: Width of the display in pixels
:param height: Height of the display in pixels
"""
if not hasattr(obj, "anchor_point") or not hasattr(obj, "anchored_position"):
raise AttributeError(
"Object must have both `anchor_point` and `anchored_position` attributes."
)

if anchor not in {
ANCHOR_TOP_LEFT,
ANCHOR_TOP_CENTER,
ANCHOR_TOP_RIGHT,
ANCHOR_CENTER_LEFT,
ANCHOR_CENTER,
ANCHOR_CENTER_RIGHT,
ANCHOR_BOTTOM_LEFT,
ANCHOR_BOTTOM_CENTER,
ANCHOR_BOTTOM_RIGHT,
}:
raise ValueError(
"Anchor must be one of: ANCHOR_TOP_LEFT, ANCHOR_TOP_CENTER, ANCHOR_TOP_RIGHT,\n"
"ANCHOR_CENTER_LEFT, ANCHOR_CENTER, ANCHOR_CENTER_RIGHT,\n"
"ANCHOR_BOTTOM_LEFT, ANCHOR_BOTTOM_CENTER, ANCHOR_BOTTOM_RIGHT."
)

obj.anchor_point = anchor
obj.anchored_position = (
int(anchor[0] * width),
int(anchor[1] * height),
)
7 changes: 7 additions & 0 deletions adafruit_displayio_layout/layouts/grid_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ def add_content(
then the cell_anchor_point from the GridLayout will be used.
:return: None"""

grid_x, grid_y = grid_position
max_grid_x, max_grid_y = self.grid_size
if grid_x >= max_grid_x or grid_y >= max_grid_y:
raise ValueError(
f"Grid position {grid_position} is out of bounds for grid size {self.grid_size}"
)

if cell_anchor_point:
_this_cell_anchor_point = cell_anchor_point
else:
Expand Down