diff --git a/terminatorlib/terminator.py b/terminatorlib/terminator.py index ca7469d9..790ae03d 100755 --- a/terminatorlib/terminator.py +++ b/terminatorlib/terminator.py @@ -124,7 +124,6 @@ class Terminator(Borg): if next < 0: next = length - 1 elif direction in ['left', 'right', 'up', 'down']: - print direction window = get_top_window(terminal) layout = window.get_visible_terminals() @@ -147,8 +146,7 @@ class Terminator(Borg): offsets = {} for term in possibles: rect = layout[term] - offset = edge - (rect.x + rect.width) - offsets[term] = offset + offsets[term] = util.get_nav_offset(edge, rect, direction) keys = offsets.values() keys.sort() winners = [k for k, v in offsets.iteritems() if v == keys[0]] @@ -160,7 +158,7 @@ class Terminator(Borg): cursor_y = cursor_y + allocation.y for term in winners: rect = layout[term] - if cursor_y >= rect.y and cursor_y <= (rect.y + rect.height): + if util.get_nav_tiebreak(direction, cursor_x, cursor_y, rect): next = self.terminals.index(term) break; else: diff --git a/terminatorlib/util.py b/terminatorlib/util.py index aa5997d0..bca77884 100755 --- a/terminatorlib/util.py +++ b/terminatorlib/util.py @@ -202,3 +202,27 @@ def get_nav_possible(edge, allocation, direction): else: raise ValueError('Unknown direction: %s' % direction) +def get_nav_offset(edge, allocation, direction): + """Work out how far edge is from a particular point on the allocation + rectangle, in the given direction""" + if direction == 'left': + return(edge - (allocation.x + allocation.width)) + elif direction == 'right': + return(edge + allocation.x) + elif direction == 'up': + return(edge - (allocation.y - allocation.height)) + elif direction == 'down': + return(edge + allocation.y) + else: + raise ValueError('Unknown direction: %s' % direction) + +def get_nav_tiebreak(direction, cursor_x, cursor_y, rect): + """We have multiple candidate terminals. Pick the closest by cursor + position""" + if direction in ['left', 'right']: + return(cursor_y >= rect.y and cursor_y <= (rect.y + rect.height)) + elif direction in ['up', 'down']: + return(cursor_x >= rect.x and cursor_x <= (rect.x + rect.width)) + else: + raise ValueError('Unknown direction: %s' % direction) +