addons.view.focus: Better handling of view refresh

When we refresh and our current focus goes out of scope, we set the focus to
the element nearest the old focus.
This commit is contained in:
Aldo Cortesi 2016-10-29 11:08:35 +13:00
parent 90e7142b5c
commit 14df969434
2 changed files with 6 additions and 4 deletions

View File

@ -199,18 +199,20 @@ class Focus:
if self.focusflow:
return self.view.index(self.focusflow)
def _nearest(self, f, v):
return min(v.bisect(f), len(v)-1)
def _sig_remove(self, view, flow):
if len(view) == 0:
self.focusflow = None
elif flow is self.focusflow:
idx = min(view.bisect(self.focusflow), len(view)-1)
self.focusflow = view[idx]
self.focusflow = view[self._nearest(self.focusflow, view)]
def _sig_refresh(self, view):
if len(view) == 0:
self.focusflow = None
elif self.focusflow not in view:
self.focusflow = view[0]
self.focusflow = view[self._nearest(self.focusflow, view)]
def _sig_add(self, view, flow):
# We only have to act if we don't have a focus element

View File

@ -228,7 +228,7 @@ def test_focus():
filt = flowfilter.parse("~m get")
v.set_filter(filt)
assert f.index == 0
assert f.index == 2
filt = flowfilter.parse("~m oink")
v.set_filter(filt)