Commit fc49a646 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Enable scrolling inside a todo list.

This is implemented using the ListBox widget.
parent e3e59df1
...@@ -25,13 +25,14 @@ class TodoListWidget(urwid.LineBox): ...@@ -25,13 +25,14 @@ class TodoListWidget(urwid.LineBox):
title_widget = urwid.Filler(urwid.Text(p_title, align='center')) title_widget = urwid.Filler(urwid.Text(p_title, align='center'))
self.todo_pile = urwid.Pile([]) self.todolist = urwid.SimpleFocusListWalker([])
self.listbox = urwid.ListBox(self.todolist)
self.update() self.update()
pile = urwid.Pile([ pile = urwid.Pile([
(1, title_widget), (1, title_widget),
(1, urwid.Filler(urwid.Divider(u'\u2500'))), (1, urwid.Filler(urwid.Divider(u'\u2500'))),
('weight', 1, urwid.Filler(self.todo_pile, valign='top')), ('weight', 1, self.listbox),
]) ])
pile.focus_position = 2 pile.focus_position = 2
...@@ -45,50 +46,42 @@ class TodoListWidget(urwid.LineBox): ...@@ -45,50 +46,42 @@ class TodoListWidget(urwid.LineBox):
Updates the todo list according to the todos in the view associated Updates the todo list according to the todos in the view associated
with this list. with this list.
""" """
try: old_focus_position = self.todolist.focus
old_focus_position = self.todo_pile.focus_position
except IndexError:
old_focus_position = 0
items = [] del self.todolist[:]
for todo in self.view.todos: for todo in self.view.todos:
todowidget = TodoWidget(todo, self.view.todolist.number(todo)) todowidget = TodoWidget(todo, self.view.todolist.number(todo))
items.append((todowidget, ('pack', None))) self.todolist.append(todowidget)
items.append((urwid.Divider(u'-'), ('weight', 1))) self.todolist.append(urwid.Divider(u'-'))
self.todo_pile.contents = items
self.todo_pile.focus_position = min(old_focus_position, len(items) - 1)
def _focus_down(self):
size = len(self.todo_pile.contents)
if self.todo_pile.focus_position < size - 2:
self.todo_pile.focus_position += 2
def _focus_up(self): if old_focus_position:
if self.todo_pile.focus_position > 1: self.todolist.set_focus(old_focus_position)
self.todo_pile.focus_position -= 2
def keypress(self, p_size, p_key): def keypress(self, p_size, p_key):
dispatch = { if p_key == 'x':
'j': self._focus_down, self._complete_selected_item()
'down': self._focus_down, elif p_key == 'j':
'k': self._focus_up, self.listbox.keypress(p_size, 'down')
'up': self._focus_up, elif p_key == 'k':
'x': self._complete_selected_item, self.listbox.keypress(p_size, 'up')
} else:
if self.listbox.keypress(p_size, p_key):
try: return super(TodoListWidget, self).keypress(p_size, p_key)
dispatch[p_key]()
except KeyError:
return super(TodoListWidget, self).keypress(p_size, p_key)
def selectable(self): def selectable(self):
return True return True
def _complete_selected_item(self): def _complete_selected_item(self):
todo = self.todo_pile.focus.todo """
self.view.todolist.number(todo) Marks the highlighted todo item as complete.
"""
urwid.emit_signal(self, 'execute_command', "do {}".format( try:
text_type(self.view.todolist.number(todo)))) todo = self.listbox.focus.todo
self.view.todolist.number(todo)
urwid.emit_signal(self, 'execute_command', "do {}".format(
text_type(self.view.todolist.number(todo))))
except AttributeError:
# No todo item selected
pass
...@@ -50,3 +50,13 @@ class TodoWidget(urwid.WidgetWrap): ...@@ -50,3 +50,13 @@ class TodoWidget(urwid.WidgetWrap):
super(TodoWidget, self).__init__(self.widget) super(TodoWidget, self).__init__(self.widget)
def keypress(self, p_size, p_key):
"""
Override keypress to prevent the wrapped Columns widget to
receive any key.
"""
return p_key
def selectable(self):
# make sure that ListBox will highlight this widget
return True
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment