addons.view.focus: next and prev methods

This commit is contained in:
Aldo Cortesi 2016-10-29 11:50:10 +13:00
parent 32a0a7b860
commit 7ecaeb0214
2 changed files with 42 additions and 3 deletions

View File

@ -181,8 +181,6 @@ class Focus:
def __init__(self, v: View) -> None:
self.view = v
self._focusflow = None
self.focusflow = None
if len(self.view):
self.focusflow = self.view[0]
v.sig_add.connect(self._sig_add)
@ -204,6 +202,22 @@ class Focus:
if self.focusflow:
return self.view.index(self.focusflow)
def next(self):
"""
Sets the focus to the next flow.
"""
if self.focusflow:
idx = min(self.index + 1, len(self.view) - 1)
self.focusflow = self.view[idx]
def prev(self):
"""
Sets the focus to the previous flow.
"""
if self.focusflow:
idx = max(self.index - 1, 0)
self.focusflow = self.view[idx]
def _nearest(self, f, v):
return min(v.bisect(f), len(v) - 1)

View File

@ -235,6 +235,31 @@ def test_focus():
assert f.index is None
def test_focus_nextprev():
v = view.View()
# Nops on an empty view
v.focus.next()
v.focus.prev()
# Nops on a single-flow view
v.add(tft(start=0))
assert v.focus.focusflow == v[0]
v.focus.next()
assert v.focus.focusflow == v[0]
v.focus.prev()
assert v.focus.focusflow == v[0]
v.add(tft(start=1))
v.focus.next()
assert v.focus.focusflow == v[1]
v.focus.next()
assert v.focus.focusflow == v[1]
v.focus.prev()
assert v.focus.focusflow == v[0]
v.focus.prev()
assert v.focus.focusflow == v[0]
def test_settings():
v = view.View()
f = tft()