I need to implement a listbox for a mobile. The only relevant controls are up and down arrow keys. The listbox should display as many rows of items from a list as will fit on the screen (screen_rows), one row should be highighted (sel_row) and the display should wrap if the user hits up arrow when the first item is highlighted or down arrow if the last item is highlighted (that is, the last item should be displayed and highlighted if the user hits up when the first item is highlighted). Up arrow highlights the previous item and down arrow highlights the next item.
I've put something together, but am concerned I've missed something in testing. There must be a standard way to do this, given the prevalence of listboxes out there.
def up_key(self):
if self.sel_row > 0:
self.sel_row -= 1
elif self.top_item > 0: # top_item is the index of the first list item
self.top_item -= 1
elif self.top_item == 0:
if self.n_lines >= self.screen_rows: # n_lines is the number of items in the list
self.top_item = self.n_lines - self.screen_rows
self.sel_row = min(self.screen_rows-1, self.n_lines-1)
else:
self.top_item = 0
self.sel_row = self.n_lines-1
def down_key(self):
if self.sel_row < self.screen_rows-1 and self.sel_row < self.n_lines-1:
self.sel_row += 1
elif self.sel_row == self.screen_rows-1:
bottom_item = self.top_item + self.screen_rows
if bottom_item == self.n_lines:
self.top_item = 0
self.sel_row = 0
if bottom_item < self.n_lines:
self.top_item += 1
elif self.sel_row == self.n_lines-1:
self.top_item = 0
self.sel_row = 0
def set_pos(self, pos): # display item with index pos
if pos < 0:
pos = 0
elif pos >= self.n_lines:
pos = self.n_lines - 1
if pos < self.screen_rows:
self.top_item = 0
self.sel_row = pos
else:
self.sel_row = min(self.screen_rows, self.n_lines)//2 - 1
self.top_item = pos - self.sel_row
if self.top_item >= self.n_lines - self.screen_rows:
self.top_item = self.n_lines - self.screen_rows - 1
self.sel_row = pos - self.top_item - 1
EDIT: after each function I call a redraw screen function, which redraws the screen with top_item at the top and sel-row highlighted.
I've added a pseudo-code tag, in case someone has a version in something that's not python.