diff --git a/CHANGELOG.md b/CHANGELOG.md index 36872e6..8976bfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Version 0.12.2 + +- fix: headed menu item presentation logic + ## Version 0.12.1 - fix: headed menu item selection logic diff --git a/pyproject.toml b/pyproject.toml index 28f840e..203d416 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ubo-gui" -version = "0.12.1" +version = "0.12.2" description = "GUI sdk for Ubo Pod" authors = ["Sassan Haradji "] license = "Apache-2.0" diff --git a/ubo_gui/menu/__init__.py b/ubo_gui/menu/__init__.py index 3db9fdb..b5d00ae 100644 --- a/ubo_gui/menu/__init__.py +++ b/ubo_gui/menu/__init__.py @@ -385,7 +385,8 @@ def _menu_items( offset = -(PAGE_SIZE - 1) if isinstance(menu, HeadedMenu) else 0 items: list[Item | None] = list( self.current_menu_items[ - self.page_index * PAGE_SIZE + offset : self.page_index * PAGE_SIZE + max(self.page_index * PAGE_SIZE + offset, 0) : self.page_index + * PAGE_SIZE + PAGE_SIZE + offset ], @@ -438,8 +439,7 @@ def _render_menu(self: MenuWidget, *_: object) -> PageWidget | None: list_widget = HeaderMenuPageWidget( items, name=f'Page {self.get_depth()} 0', - count=PAGE_SIZE + 1 if self.render_surroundings else PAGE_SIZE, - offset=1 if self.render_surroundings else 0, + count=PAGE_SIZE - 2, render_surroundings=self.render_surroundings, padding_bottom=self.padding_bottom, padding_top=self.padding_top, @@ -484,8 +484,7 @@ def handle_sub_heading_change(sub_heading: str) -> None: list_widget = NormalMenuPageWidget( items, name=f'Page {self.get_depth()} 0', - count=PAGE_SIZE + 2 if self.render_surroundings else PAGE_SIZE, - offset=1 if self.render_surroundings else 0, + count=PAGE_SIZE, render_surroundings=self.render_surroundings, padding_bottom=self.padding_bottom, padding_top=self.padding_top, diff --git a/ubo_gui/menu/widgets/header_menu_page_widget.py b/ubo_gui/menu/widgets/header_menu_page_widget.py index 6b179e3..5b74be7 100644 --- a/ubo_gui/menu/widgets/header_menu_page_widget.py +++ b/ubo_gui/menu/widgets/header_menu_page_widget.py @@ -51,10 +51,10 @@ def __init__( def adjust_item_widgets(self: HeaderMenuPageWidget, *args: object) -> None: """Initialize the widget.""" _ = args - for _ in range(len(self.item_widgets), self.count - 2): + for _ in range(len(self.item_widgets), self._count - self._offset): self.item_widgets.append(ItemWidget(size_hint=(1, None))) self.ids.layout.add_widget(self.item_widgets[-1]) - for _ in range(self.count - 2, len(self.item_widgets)): + for _ in range(self._count - self._offset, len(self.item_widgets)): self.ids.layout.remove_widget(self.item_widgets[-1]) del self.item_widgets[-1] self.render() @@ -63,15 +63,13 @@ def render(self: HeaderMenuPageWidget, *_: object) -> None: """Render the widget.""" if not self.item_widgets: return - offset = 1 if self.render_surroundings else 0 - for i in range(self.count - 2): - self.item_widgets[i].item = ( - self.items[i + offset] if i + offset < len(self.items) else None + for i in range(self._offset, self._count): + self.item_widgets[i - self._offset].item = ( + self.items[i] if i < len(self.items) else None ) def get_item(self: HeaderMenuPageWidget, index: int) -> Item | None: """Get the item at the given index.""" - offset = 1 if self.render_surroundings else 0 if index < HEADER_SIZE: warnings.warn( f'index must be greater than or equal to {HEADER_SIZE}', @@ -80,7 +78,7 @@ def get_item(self: HeaderMenuPageWidget, index: int) -> Item | None: ) return None try: - return self.items[index - HEADER_SIZE + offset] + return self.items[index + self.offset - HEADER_SIZE] except IndexError: return None diff --git a/ubo_gui/menu/widgets/normal_menu_page_widget.py b/ubo_gui/menu/widgets/normal_menu_page_widget.py index e715d17..9df5542 100644 --- a/ubo_gui/menu/widgets/normal_menu_page_widget.py +++ b/ubo_gui/menu/widgets/normal_menu_page_widget.py @@ -34,11 +34,11 @@ def __init__( def adjust_item_widgets(self: NormalMenuPageWidget, *args: object) -> None: """Initialize the widget.""" _ = args - for _ in range(len(self.item_widgets), self.count): + for _ in range(len(self.item_widgets), self._count): self.item_widgets.append(ItemWidget(size_hint=(1, None))) self.ids.layout.add_widget(self.item_widgets[-1]) - for _ in range(self.count, len(self.item_widgets)): - self.ids.layout.remove_widgeT(self.item_widgets[-1]) + for _ in range(self._count, len(self.item_widgets)): + self.ids.layout.remove_widget(self.item_widgets[-1]) del self.item_widgets[-1] self.render() @@ -46,7 +46,7 @@ def render(self: NormalMenuPageWidget, *_: object) -> None: """Render the widget.""" if not self.item_widgets: return - for i in range(self.count): + for i in range(self._count): self.item_widgets[i].item = self.items[i] if i < len(self.items) else None diff --git a/ubo_gui/page/__init__.py b/ubo_gui/page/__init__.py index 9cecec3..9e69dbb 100644 --- a/ubo_gui/page/__init__.py +++ b/ubo_gui/page/__init__.py @@ -29,7 +29,6 @@ class PageWidget(Screen): items: Sequence[Item | None] = ListProperty([]) title: str | None = StringProperty(allownone=True, defaultvalue=None) count: int = NumericProperty(defaultvalue=PAGE_MAX_ITEMS) - offset: int = NumericProperty(defaultvalue=0) placeholder: str | None = StringProperty(allownone=True) render_surroundings: bool = BooleanProperty( defaultvalue=False, @@ -44,6 +43,14 @@ def get_is_empty(self: PageWidget) -> bool: is_empty: bool = AliasProperty(getter=get_is_empty, bind=('items',)) + @property + def _count(self: PageWidget) -> int: + return self.count + (2 if self.render_surroundings else 0) + + @property + def _offset(self: PageWidget) -> int: + return 1 if self.render_surroundings else 0 + def go_up(self: Self) -> None: """Implement this method to provide custom logic for up key.""" return @@ -83,14 +90,14 @@ def __init__( """ self.items = items or [] super().__init__(*args, **kwargs) - if items and len(items) > self.count: - msg = f"""`PageWidget` is initialized with more than `MAX_ITEMS`={ + if items and len(items) > self._count: + msg = f"""`PageWidget` is initialized with more than `count`={ self.count} items""" raise ValueError(msg) def get_item(self: PageWidget, index: int) -> Item | None: """Get the page item at the given index.""" - index += self.offset + index += self._offset if not 0 <= index < len(self.items): msg = f"""index must be greater than or equal to 0 and less than { len(self.items)}"""