Skip to content

Commit

Permalink
fix: headed menu item presentation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Jul 19, 2024
1 parent c9ba356 commit 5569f44
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "Apache-2.0"
Expand Down
9 changes: 4 additions & 5 deletions ubo_gui/menu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions ubo_gui/menu/widgets/header_menu_page_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}',
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions ubo_gui/menu/widgets/normal_menu_page_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ 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()

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


Expand Down
15 changes: 11 additions & 4 deletions ubo_gui/page/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)}"""
Expand Down

0 comments on commit 5569f44

Please sign in to comment.