+ def refresh(self):
+ """
+ Called when the current view must be updated. Never call
+ directly. Subclasses should extend this method, not update.
+ """
+ self.refresh_menu()
+
+ def refresh_menu(self):
+ """
+ Refresh the menu and its bindings. Calls self.menu_items() to
+ get the new menu.
+ """
+ # Two helper functions
+ def shortcut_prefix(key_name):
+ short = key_shortname(key_name)
+ return '[%s]' % short if short else ' '
+
+ def do_entry((text, callback, key_name)):
+ key = get_key(key_name)
+ if key:
+ self.view.bind(key, callback)
+ self.menu_keys.append(key)
+ title = "%s %s" % (shortcut_prefix(key_name), text)
+ return(title, callback)
+
+ # Clear the bindings we previously added (we can't just clear
+ # all bindings, since other classes might have added other
+ # bindings)
+ for key in self.menu_keys:
+ self.view.bind(key, no_action)
+ self.menu_keys = []
+
+ # Set the menu, and let do_entry add binds at the same time.
+ appuifw.app.menu = [do_entry(item) for item in self.menu_items()]
+
+ def menu_items(self):
+ """
+ Should return a list of menu items. Each menu item is a tuple:
+ (text, callback, shortcut key name).
+ """
+ return [(u'Exit', self.exit, None)]
+
+class ListView(View):
+ def __init__(self):
+ super(ListView, self).__init__()
+ self.current_index = None
+ self.items_cache = []
+ self.set_view(appuifw.Listbox([], self.entry_selected))
+ self.view.bind(EKeyUpArrow,lambda: self.arrow_key_pressed(-1))
+ self.view.bind(EKeyDownArrow,lambda: self.arrow_key_pressed(1))
+
+ def arrow_key_pressed(self, dir):
+ """
+ This function is called when an arrow key is pressed. Since we
+ don't get any "current list index has changed" events, we'll
+ have to create these ourselves this way.
+
+ Since the current index is only updated after the key event,
+ we'll have to adjust the index with the direction of the
+ keypress (-1 for up, +1 for down).
+ """
+ self.current_index = self.wrap_index(self.selected_index() + dir)
+ self.index_changed()
+ self.current_index = None