diff --git a/python/PiFinder/ui/obs_list.py b/python/PiFinder/ui/obs_list.py index da406a303..ae4bc905c 100644 --- a/python/PiFinder/ui/obs_list.py +++ b/python/PiFinder/ui/obs_list.py @@ -29,21 +29,6 @@ class UIObsList(UITextMenu): __title__ = "Obs Lists" - # The list is drawn as a 7-row window centered on the selection. Each row has - # a fixed color, vertical offset and font; the middle row is the selected - # item -- brightest and largest, and it scrolls when its text overflows. - _SELECTED_ROW = 3 - _ROW_STYLES = ( - # (color, y_offset, font_name) - (96, 0, "base"), - (128, 13, "base"), - (192, 25, "bold"), - (255, 40, "large"), - (192, 60, "bold"), - (128, 76, "base"), - (96, 89, "base"), - ) - def __init__(self, *args, **kwargs): incoming = kwargs.get("item_definition", {}) subdir = incoming.get("subdir", "") @@ -89,44 +74,25 @@ def _get_scrollspeed(self): TextLayouterScroll.MEDIUM, ) - def update(self, force=False): - self.clear_screen() - self.draw.rectangle((-1, 60, 129, 80), outline=self.colors.get(128), width=1) - - line_horiz_pos = 13 - window_start = self._current_item_index - self._SELECTED_ROW - - for line_number, style in enumerate(self._ROW_STYLES): - i = window_start + line_number - if not (0 <= i < self.get_nr_of_menu_items()): - continue - - line_color, line_pos, font_name = style - line_font = getattr(self.fonts, font_name) - line_pos += 20 - item_text = str(self._menu_items[i]) - - if line_number == self._SELECTED_ROW: - # Scroll the selected item if it's too long - if self._scroll_item != item_text: - self._scroll_item = item_text - self._scroll_text = TextLayouterScroll( - text=_(item_text), - draw=self.draw, - color=self.colors.get(line_color), - font=line_font, - scrollspeed=self._get_scrollspeed(), - ) - self._scroll_text.draw((line_horiz_pos, line_pos)) - else: - self.draw.text( - (line_horiz_pos, line_pos), - _(item_text), - font=line_font.font, - fill=self.colors.get(line_color), - ) - - return self.screen_update() + def _draw_item_text(self, pos, text, row, is_focus): + """Scroll the selected item when it is too long for the line. + + UITextMenu draws everything else: the rows, the selection box and the + other items. + """ + if not is_focus: + super()._draw_item_text(pos, text, row, is_focus) + return + if self._scroll_item != text: + self._scroll_item = text + self._scroll_text = TextLayouterScroll( + text=text, + draw=self.draw, + color=self.colors.get(row.brightness), + font=row.font, + scrollspeed=self._get_scrollspeed(), + ) + self._scroll_text.draw(pos) def key_right(self): if not self._menu_items: diff --git a/python/PiFinder/ui/text_menu.py b/python/PiFinder/ui/text_menu.py index feba282ba..7c464e450 100644 --- a/python/PiFinder/ui/text_menu.py +++ b/python/PiFinder/ui/text_menu.py @@ -110,11 +110,11 @@ def update(self, force=False): except Exception: suffix = "" - self.draw.text( + self._draw_item_text( (layout.text_x, row.y), _(item_text) + suffix, # I18N: translate item for display, add suffix - font=row.font.font, - fill=self.colors.get(row.brightness), + row, + is_focus=slot == half, ) if ( item_def is not None @@ -129,6 +129,20 @@ def update(self, force=False): return self.screen_update() + def _draw_item_text(self, pos, text: str, row, is_focus: bool) -> None: + """Draw one item's text on its carousel row. + + Subclasses override this to change how an item is drawn (for example, + to scroll the selected item when it is too long) and keep the shared + carousel layout. + """ + self.draw.text( + pos, + text, + font=row.font.font, + fill=self.colors.get(row.brightness), + ) + def menu_scroll(self, direction: int): self._current_item_index += direction if self._current_item_index < 0: diff --git a/python/tests/test_obs_list_layout.py b/python/tests/test_obs_list_layout.py new file mode 100644 index 000000000..2a02cc2a9 --- /dev/null +++ b/python/tests/test_obs_list_layout.py @@ -0,0 +1,112 @@ +""" +Layout tests for the observing-list picker (UIObsList). + +The picker has no drawing loop of its own: UITextMenu.update draws it with the +resolution-flexible ``carousel_layout``, and the picker only overrides +``_draw_item_text`` so the selected row can scroll. On the 176x176 rev4 panel +that is 9 rows with the selection box on the centre line, not the 7 fixed rows +of the 128x128 panel. +""" + +import pytest +from PIL import Image, ImageDraw + +# Installs the _() gettext builtin the UI modules rely on; must precede ui imports. +import PiFinder.i18n # noqa: F401 + +from PiFinder.displays import get_display +from PiFinder.ui.layout import carousel_layout +from PiFinder.ui.obs_list import UIObsList + +pytestmark = pytest.mark.unit + + +class RecordingDraw: + """Wraps a real ImageDraw and records rectangle / text calls.""" + + def __init__(self, size): + self._draw = ImageDraw.Draw(Image.new("RGB", size)) + self.rectangles = [] + self.texts = [] + + def rectangle(self, xy, *args, **kwargs): + self.rectangles.append(tuple(xy)) + return self._draw.rectangle(xy, *args, **kwargs) + + def text(self, xy, text, *args, **kwargs): + self.texts.append((tuple(xy), text)) + return self._draw.text(xy, text, *args, **kwargs) + + # TextLayouterScroll (the selected row) draws through multiline_text. + def multiline_text(self, xy, text, *args, **kwargs): + self.texts.append((tuple(xy), text)) + return self._draw.multiline_text(xy, text, *args, **kwargs) + + +def _picker(display_name, n_items, index): + """A UIObsList with only the attributes update() touches, built without + the heavy UIModule.__init__ (no shared state, catalogs or obslist dir).""" + display = get_display(display_name) + ui = UIObsList.__new__(UIObsList) + ui.display_class = display + ui.colors = display.colors + ui.fonts = display.fonts + ui.draw = RecordingDraw(display.resolution) + ui.clear_screen = lambda: None + ui.screen_update = lambda *a, **k: None + ui.config_object = type("Cfg", (), {"get_option": lambda self, k, d=None: d})() + names = [f"List {i:02d}" for i in range(n_items)] + ui.item_definition = { + "select": "single", + "items": [{"name": n, "value": f"/obslists/{n}.txt"} for n in names], + } + ui._menu_items = names + ui._selected_values = [] + ui._current_item_index = index + ui._scroll_text = None + ui._scroll_item = None + return ui, carousel_layout(display) + + +@pytest.mark.parametrize( + "display_name, visible", [("headless", 7), ("headless_176", 9)] +) +def test_picker_fills_every_carousel_row(display_name, visible): + ui, layout = _picker(display_name, n_items=20, index=10) + ui.update() + + assert len(layout.rows) == visible + # One text per carousel row, at the layout's positions, top -> bottom. + assert [xy for xy, _ in ui.draw.texts] == [ + (layout.text_x, row.y) for row in layout.rows + ] + half = layout.center_index + assert [t for _, t in ui.draw.texts] == [ + f"List {i:02d}" for i in range(10 - half, 10 + half + 1) + ] + + +@pytest.mark.parametrize("display_name", ["headless", "headless_176"]) +def test_selection_box_brackets_the_selected_row(display_name): + ui, layout = _picker(display_name, n_items=20, index=10) + ui.update() + + assert ui.draw.rectangles == [layout.selection_box] + _x0, y0, x1, y1 = layout.selection_box + assert x1 >= ui.display_class.resX - 1 # spans the full width + focus = layout.rows[layout.center_index] + assert y0 < focus.y < y1 + # The selected item is the one drawn on the focus row, by the scroller. + assert ((layout.text_x, focus.y), "List 10") in ui.draw.texts + assert ui._scroll_item == "List 10" + + +@pytest.mark.parametrize("display_name", ["headless", "headless_176"]) +def test_short_list_draws_only_existing_items(display_name): + ui, layout = _picker(display_name, n_items=3, index=0) + ui.update() + + half = layout.center_index + assert [xy for xy, _ in ui.draw.texts] == [ + (layout.text_x, row.y) for row in layout.rows[half : half + 3] + ]