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_LIMITitems in the UI at a timen_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_LIMITif 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.
- 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
ntimes.- 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
ntimes.- 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
ntimes. Each time we scroll downSCROLL_SPEEDitems.- 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
ntimes. Each time we scroll upSCROLL_SPEEDitems.- Returns:
tuple of
(select_delta, cursor_delta).
The list of items to show in the UI. The list is determined by
selected_item_indexcursor_positionandshow_items_limittogether. It is a subset ofself.items.- Returns:
a list of tuples, each tuple contains an item and a boolean value indicating whether the item is selected or not.