dropdown#

Dropdown menu implementation.

class zelfred.dropdown.Dropdown(items: List[T_ITEM], show_items_limit: int = 20, scroll_speed: int = 5)[source]#

Simulate an items dropdown menu. User can move or scroll selector up and down to select an item, and then perform action.

For example, the [x] Beautiful is better than ugly., [ ] Explicit is better than implicit. and the [ ] Simple is better than complex. part in the following UI is the dropdown menu.

(Query):
[x] Beautiful is better than ugly.
      subtitle 01
[ ] Explicit is better than implicit.
      subtitle 02
[ ] Simple is better than complex.
      subtitle 03
Parameters:
  • items – All items in this dropdown menu. We only show SHOW_ITEMS_LIMIT items in the UI at a time

  • n_items – total number of items, it is a cache of len(items).

  • selected_item_index – the selected item index, the value can be larger than SHOW_ITEMS_LIMIT if we have many items.

  • cursor_position – the selected item cursor position in the dropdown UI, it is a value from 0 to SHOW_ITEMS_LIMIT - 1.

  • show_items_limit – max number of items to show in the UI.

update(items: List[T_ITEM])[source]#

Update the dropdown menu with new items.

property selected_item: T_ITEM#

Return the selected item object. If there is no item, raise NoItemToSelectError.

press_down(n: int = 1) Tuple[int, int][source]#

Move selector down to pick the next item (if possible) in the dropdown menu.

Example, before dropdown.press_down():

(Query):
[x] Beautiful is better than ugly.
      subtitle 01
[ ] Explicit is better than implicit.
      subtitle 02
[ ] Simple is better than complex.
      subtitle 03

After:

(Query):
[ ] Beautiful is better than ugly.
      subtitle 01
[x] Explicit is better than implicit.
      subtitle 02
[ ] Simple is better than complex.
      subtitle 03
Parameters:

n – move selector down n times.

Returns:

tuple of (select_delta, cursor_delta).

press_up(n: int = 1) Tuple[int, int][source]#

Move selector up to pick the previous item (if possible) in the dropdown menu.

Example, before dropdown.press_up():

(Query):
[ ] Beautiful is better than ugly.
      subtitle 01
[ ] Explicit is better than implicit.
      subtitle 02
[x] Simple is better than complex.
      subtitle 03

After:

(Query):
[ ] Beautiful is better than ugly.
      subtitle 01
[x] Explicit is better than implicit.
      subtitle 02
[ ] Simple is better than complex.
      subtitle 03
Parameters:

n – move selector up n times.

Returns:

tuple of (select_delta, cursor_delta).

scroll_down(n: int = 1) Tuple[int, int][source]#

Scroll the dropdown menu down to show more items, also move the selector.

Parameters:

n – scroll down n times. Each time we scroll down SCROLL_SPEED items.

Returns:

tuple of (select_delta, cursor_delta).

scroll_up(n: int = 1) Tuple[int, int][source]#

Scroll the dropdown menu up to show more items, also move the selector.

Parameters:

n – scroll up n times. Each time we scroll up SCROLL_SPEED items.

Returns:

tuple of (select_delta, cursor_delta).

property menu: List[Tuple[T_ITEM, bool]]#

The list of items to show in the UI. The list is determined by selected_item_index cursor_position and show_items_limit together. It is a subset of self.items.

Returns:

a list of tuples, each tuple contains an item and a boolean value indicating whether the item is selected or not.