Refactor navigate_terminals from Terminator to Window

This commit is contained in:
Chris Jones 2010-01-29 13:12:33 +00:00
parent 789092bb07
commit 4b58b0c738
4 changed files with 71 additions and 68 deletions

View File

@ -117,7 +117,8 @@ class Notebook(Container, gtk.Notebook):
'group-tab': top_window.group_tab, 'group-tab': top_window.group_tab,
'ungroup-tab': top_window.ungroup_tab, 'ungroup-tab': top_window.ungroup_tab,
'move-tab': top_window.move_tab, 'move-tab': top_window.move_tab,
'tab-new': top_window.tab_new} 'tab-new': top_window.tab_new,
'navigate': top_window.navigate_terminal}
if maker.isinstance(widget, 'Terminal'): if maker.isinstance(widget, 'Terminal'):
for signal in signals: for signal in signals:

View File

@ -92,7 +92,8 @@ class Paned(Container):
'ungroup-tab': top_window.ungroup_tab, 'ungroup-tab': top_window.ungroup_tab,
'move-tab': top_window.move_tab, 'move-tab': top_window.move_tab,
'maximise': [top_window.zoom, False], 'maximise': [top_window.zoom, False],
'tab-new': top_window.tab_new} 'tab-new': top_window.tab_new,
'navigate': top_window.navigate_terminal}
for signal in signals: for signal in signals:
args = [] args = []

View File

@ -71,7 +71,6 @@ class Terminator(Borg):
(id(terminal), type(terminal))) (id(terminal), type(terminal)))
self.terminals.append(terminal) self.terminals.append(terminal)
terminal.connect('ungroup-all', self.ungroup_all) terminal.connect('ungroup-all', self.ungroup_all)
terminal.connect('navigate', self.navigate_terminal)
def deregister_terminal(self, terminal): def deregister_terminal(self, terminal):
"""De-register a terminal widget""" """De-register a terminal widget"""
@ -101,70 +100,6 @@ class Terminator(Borg):
for terminal in self.terminals: for terminal in self.terminals:
terminal.reconfigure() terminal.reconfigure()
def navigate_terminal(self, terminal, direction):
"""Nagivate around the terminals"""
current = self.terminals.index(terminal)
length = len(self.terminals)
next = None
if length <= 1:
return
if direction == 'next':
next = current + 1
if next >= length:
next = 0
elif direction == 'prev':
next = current - 1
if next < 0:
next = length - 1
elif direction in ['left', 'right', 'up', 'down']:
window = get_top_window(terminal)
layout = window.get_visible_terminals()
allocation = terminal.get_allocation()
possibles = []
# Get the co-ordinate of the appropriate edge for this direction
edge = util.get_edge(allocation, direction)
# Find all visible terminals which are, in their entirity, in the
# direction we want to move
for term in layout:
rect = layout[term]
if util.get_nav_possible(edge, rect, direction):
possibles.append(term)
if len(possibles) == 0:
return
# Find out how far away each of the possible terminals is, then
# find the smallest distance. The winning terminals are all of
# those who are that distance away.
offsets = {}
for term in possibles:
rect = layout[term]
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]]
next = self.terminals.index(winners[0])
if len(winners) > 1:
# Break an n-way tie with the cursor position
cursor_x, cursor_y = terminal.get_cursor_position()
cursor_x = cursor_x + allocation.x
cursor_y = cursor_y + allocation.y
for term in winners:
rect = layout[term]
if util.get_nav_tiebreak(direction, cursor_x, cursor_y, rect):
next = self.terminals.index(term)
break;
else:
err('Unknown navigation direction: %s' % direction)
if next is not None:
self.terminals[next].grab_focus()
def create_group(self, name): def create_group(self, name):
"""Create a new group""" """Create a new group"""
if name not in self.groups: if name not in self.groups:

View File

@ -10,6 +10,7 @@ import gtk
import glib import glib
from util import dbg, err from util import dbg, err
import util
from translation import _ from translation import _
from version import APP_NAME from version import APP_NAME
from container import Container from container import Container
@ -261,7 +262,8 @@ class Window(Container, gtk.Window):
'group-tab': self.group_tab, 'group-tab': self.group_tab,
'ungroup-tab': self.ungroup_tab, 'ungroup-tab': self.ungroup_tab,
'move-tab': self.move_tab, 'move-tab': self.move_tab,
'tab-new': self.tab_new} 'tab-new': self.tab_new,
'navigate': self.navigate_terminal}
for signal in signals: for signal in signals:
self.connect_child(widget, signal, signals[signal]) self.connect_child(widget, signal, signals[signal])
@ -498,6 +500,70 @@ class Window(Container, gtk.Window):
notebook.reorder_child(child, page) notebook.reorder_child(child, page)
def navigate_terminal(self, terminal, direction):
"""Navigate around terminals"""
_containers, terminals = util.enumerate_descendants(self)
current = terminals.index(terminal)
length = len(terminals)
next = None
if length <= 1:
return
if direction == 'next':
next = current + 1
if next >= length:
next = 0
elif direction == 'prev':
next = current - 1
if next < 0:
next = length - 1
elif direction in ['left', 'right', 'up', 'down']:
layout = self.get_visible_terminals()
allocation = terminal.get_allocation()
possibles = []
# Get the co-ordinate of the appropriate edge for this direction
edge = util.get_edge(allocation, direction)
# Find all visible terminals which are, in their entirity, in the
# direction we want to move
for term in layout:
rect = layout[term]
if util.get_nav_possible(edge, rect, direction):
possibles.append(term)
if len(possibles) == 0:
return
# Find out how far away each of the possible terminals is, then
# find the smallest distance. The winning terminals are all of
# those who are that distance away.
offsets = {}
for term in possibles:
rect = layout[term]
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]]
next = terminals.index(winners[0])
if len(winners) > 1:
# Break an n-way tie using the cursor position
cursor_x, cursor_y = terminal.get_cursor_position()
cursor_x = cursor_x + allocation.x
cursor_y = cursor_y + allocation.y
for term in winners:
rect = layout[term]
if util.get_nav_tiebreak(direction, cursor_x, cursor_y,
rect):
next = terminals.index(term)
break;
else:
err('Unknown navigation direction: %s' % direction)
if next is not None:
terminals[next].grab_focus()
class WindowTitle(object): class WindowTitle(object):
"""Class to handle the setting of the window title""" """Class to handle the setting of the window title"""