Display file header in top line of diff

To remind the user of the path of the file shown at the top of the
diff, always display the filename there, even after the file header
has scrolled past the top of the screen.

Change-Id: Icf3419647c2ccec45f878fbdc882e3d624eadebb
This commit is contained in:
James E. Blair 2015-04-07 12:19:21 -07:00
parent cef92ab99d
commit ddd2088e57
3 changed files with 65 additions and 1 deletions

View File

@ -110,6 +110,9 @@ class BaseFileHeader(urwid.Button):
def selectable(self): def selectable(self):
return True return True
class BaseFileReminder(urwid.WidgetWrap):
pass
class DiffContextButton(urwid.WidgetWrap): class DiffContextButton(urwid.WidgetWrap):
def selectable(self): def selectable(self):
return True return True
@ -232,7 +235,8 @@ class BaseDiffView(urwid.WidgetWrap):
comment_filenames.add(comment.file) comment_filenames.add(comment.file)
repo = self.app.getRepo(self.project_name) repo = self.app.getRepo(self.project_name)
self._w.contents.append((self.app.header, ('pack', 1))) self._w.contents.append((self.app.header, ('pack', 1)))
self._w.contents.append((urwid.Divider(), ('pack', 1))) self.file_reminder = self.makeFileReminder()
self._w.contents.append((self.file_reminder, ('pack', 1)))
lines = [] # The initial set of lines to display lines = [] # The initial set of lines to display
self.file_diffs = [{}, {}] # Mapping of fn -> DiffFile object (old, new) self.file_diffs = [{}, {}] # Mapping of fn -> DiffFile object (old, new)
# this is a list of files: # this is a list of files:
@ -343,6 +347,9 @@ class BaseDiffView(urwid.WidgetWrap):
def makeFileHeader(self, diff, comment_lists): def makeFileHeader(self, diff, comment_lists):
raise NotImplementedError raise NotImplementedError
def makeFileReminder(self):
raise NotImplementedError
def interested(self, event): def interested(self, event):
if not ((isinstance(event, sync.ChangeAddedEvent) and if not ((isinstance(event, sync.ChangeAddedEvent) and
self.change_key in event.related_change_keys) self.change_key in event.related_change_keys)
@ -358,10 +365,37 @@ class BaseDiffView(urwid.WidgetWrap):
#TODO #TODO
pass pass
def getContextAtTop(self, size):
middle, top, bottom = self.listbox.calculate_visible(size, True)
if top and top[1]:
(widget, pos, rows) = top[1][-1]
elif middle:
pos = middle[2]
# Make sure the first header shows up as soon as it scrolls up
if pos > 1:
pos -= 1
context = None
while True:
item = self.listbox.body[pos]
if hasattr(item, 'context'):
break
pos -= 1
if pos > 0:
context = item.context
return context
def keypress(self, size, key): def keypress(self, size, key):
old_focus = self.listbox.focus old_focus = self.listbox.focus
r = super(BaseDiffView, self).keypress(size, key) r = super(BaseDiffView, self).keypress(size, key)
new_focus = self.listbox.focus new_focus = self.listbox.focus
context = self.getContextAtTop(size)
if context:
self.file_reminder.set(context.old_fn,
context.new_fn)
else:
self.file_reminder.set('', '')
commands = self.app.config.keymap.getCommands(r) commands = self.app.config.keymap.getCommands(r)
if (isinstance(old_focus, BaseDiffCommentEdit) and if (isinstance(old_focus, BaseDiffCommentEdit) and
(old_focus != new_focus or (keymap.PREV_SCREEN in commands))): (old_focus != new_focus or (keymap.PREV_SCREEN in commands))):

View File

@ -105,6 +105,16 @@ class SideFileHeader(BaseFileHeader):
'filename': 'focused-filename'} 'filename': 'focused-filename'}
self._w = urwid.AttrMap(col, None, focus_map=map) self._w = urwid.AttrMap(col, None, focus_map=map)
class SideFileReminder(BaseFileReminder):
def __init__(self):
self.old_text = urwid.Text(('filename', ''))
self.new_text = urwid.Text(('filename', ''))
col = urwid.Columns([self.old_text, self.new_text])
super(SideFileReminder, self).__init__(col)
def set(self, old, new):
self.old_text.set_text(('filename', old))
self.new_text.set_text(('filename', new))
class SideDiffView(BaseDiffView): class SideDiffView(BaseDiffView):
def makeLines(self, diff, lines_to_add, comment_lists): def makeLines(self, diff, lines_to_add, comment_lists):
@ -148,6 +158,9 @@ class SideDiffView(BaseDiffView):
old_comment, new_comment)) old_comment, new_comment))
return lines return lines
def makeFileReminder(self):
return SideFileReminder()
def makeFileHeader(self, diff, comment_lists): def makeFileHeader(self, diff, comment_lists):
context = LineContext( context = LineContext(
self.old_revision_key, self.new_revision_key, self.old_revision_key, self.new_revision_key,

View File

@ -105,6 +105,20 @@ class UnifiedFileHeader(BaseFileHeader):
'filename': 'focused-filename'} 'filename': 'focused-filename'}
self._w = urwid.AttrMap(col, None, focus_map=map) self._w = urwid.AttrMap(col, None, focus_map=map)
class UnifiedFileReminder(BaseFileReminder):
def __init__(self):
self.old_text = urwid.Text(('filename', ''))
self.new_text = urwid.Text(('filename', ''))
self.col = urwid.Columns([('pack', self.old_text),
('pack', self.new_text),
urwid.Text(u'')], dividechars=2)
super(UnifiedFileReminder, self).__init__(self.col)
def set(self, old, new):
self.old_text.set_text(('filename', old))
self.new_text.set_text(('filename', new))
self.col._invalidate()
class UnifiedDiffView(BaseDiffView): class UnifiedDiffView(BaseDiffView):
def makeLines(self, diff, lines_to_add, comment_lists): def makeLines(self, diff, lines_to_add, comment_lists):
lines = [] lines = []
@ -153,6 +167,9 @@ class UnifiedDiffView(BaseDiffView):
new_comment)) new_comment))
return lines return lines
def makeFileReminder(self):
return UnifiedFileReminder()
def makeFileHeader(self, diff, comment_lists): def makeFileHeader(self, diff, comment_lists):
context = LineContext( context = LineContext(
self.old_revision_key, self.new_revision_key, self.old_revision_key, self.new_revision_key,