X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fmobilegtd.git;a=blobdiff_plain;f=src%2Fgui%2Fgui.py;h=a981b61111d5c44bd7daadbe7360cb9154294729;hp=94ade0689c9a09b1a4a212d9f38a61660a579c4b;hb=80501ced4dd4ccb5567283f16b49327140474fdf;hpb=e3125688649dc16f60c42a7b9479a2663a88bace diff --git a/src/gui/gui.py b/src/gui/gui.py index 94ade06..a981b61 100644 --- a/src/gui/gui.py +++ b/src/gui/gui.py @@ -217,7 +217,7 @@ class ListView(View): we'll have to adjust the index with the direction of the keypress (-1 for up, +1 for down). """ - self.current_index = (self.selected_index() + dir) % len(self.items()) + self.current_index = self.wrap_index(self.selected_index() + dir) self.index_changed() self.current_index = None @@ -258,6 +258,24 @@ class ListView(View): else: return self.view.current() + def clip_index(self, index): + """ + Make sure the given index fits within the bounds of this + list. If it doesn't, clip it off at the ends of the list (e.g, + -1 becomes 0). + """ + max_index = len(self.items_cache) - 1 + return max (0, min(max_index, index)) + + def wrap_index(self, index): + """ + Make sure the given index fits within the bounds of this + list. If it doesn't, wrap it around (e.g., -1 becomes 5 in a + 6-element list). + """ + count = len(self.items_cache) + return index % count + class WidgetBasedListView(ListView): def __init__(self): self.binding_map = {}