Skip to content

Commit

Permalink
ui: replace use of urwid's deprecated set_focus, get_focus
Browse files Browse the repository at this point in the history
urwid from core24 raises deprecation notices when using set_focus and
get_focus. In core24, one can use the `container.focus = widget` setter
property or `container.focus_position = index` setter property. However,
in core22, the `container.focus` property is read-only.

`container.focus_position` is available in core22 and core24 and does
not produce deprecation warnings ; so let's use it.

Signed-off-by: Olivier Gayot <[email protected]>
  • Loading branch information
ogayot committed Aug 27, 2024
1 parent d9005e0 commit 4fd089a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion subiquity/ui/views/installprogress.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _add_line(self, lb, line):
at_end = len(walker) == 0 or lb.focus_position == len(walker) - 1
walker.append(line)
if at_end:
lb.set_focus(len(walker) - 1)
lb.focus_position = len(walker) - 1
lb.set_focus_valign("bottom")

def event_start(self, context_id, context_parent_id, message):
Expand Down
27 changes: 13 additions & 14 deletions subiquitycore/ui/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def _select_first_selectable(self):
"""Select first selectable child (possibily recursively)."""
for i, (w, o) in enumerate(self.contents):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_first_selectable")
return

def _select_last_selectable(self):
"""Select last selectable child (possibily recursively)."""
for i, (w, o) in reversed(list(enumerate(self.contents))):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_last_selectable")
return

Expand Down Expand Up @@ -189,7 +189,7 @@ def keypress(self, size, key):
next_fp = self.focus_position + 1
for i, (w, o) in enumerate(self._contents[next_fp:], next_fp):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_first_selectable")
return
if not key.endswith(" no wrap"):
Expand All @@ -199,7 +199,7 @@ def keypress(self, size, key):
positions = self._contents[: self.focus_position]
for i, (w, o) in reversed(list(enumerate(positions))):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_last_selectable")
return
if not key.endswith(" no wrap"):
Expand Down Expand Up @@ -265,15 +265,15 @@ def _select_first_selectable(self):
"""Select first selectable child (possibily recursively)."""
for i, (w, o) in enumerate(self.contents):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_first_selectable")
return

def _select_last_selectable(self):
"""Select last selectable child (possibily recursively)."""
for i, (w, o) in reversed(list(enumerate(self.contents))):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_last_selectable")
return

Expand All @@ -289,14 +289,14 @@ def __init__(self, body):
super().__init__(body)

def _set_focus_no_move(self, i):
# We call set_focus twice because otherwise the listbox
# attempts to do the minimal amount of scrolling required to
# We set the focus_position property twice because otherwise the
# listbox attempts to do the minimal amount of scrolling required to
# get the new focus widget into view, which is not what we
# want, as if our first widget is a compound widget it results
# its last widget being focused -- in fact the opposite of
# what we want!
self.set_focus(i)
self.set_focus(i)
self.focus_position = i
self.focus_position = i
# I don't really understand why this is required but it seems it is.
self._invalidate()

Expand Down Expand Up @@ -334,7 +334,7 @@ def keypress(self, size, key):
next_fp = self.focus_position + 1
for i, w in enumerate(self.body[next_fp:], next_fp):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_first_selectable")
return None
if not key.endswith(" no wrap"):
Expand All @@ -344,7 +344,7 @@ def keypress(self, size, key):
positions = self.body[: self.focus_position]
for i, w in reversed(list(enumerate(positions))):
if w.selectable():
self.set_focus(i)
self.focus_position = i
_maybe_call(w, "_select_last_selectable")
return None
if not key.endswith(" no wrap"):
Expand Down Expand Up @@ -465,12 +465,11 @@ def render(self, size, focus=False):

seen_focus = False
height = height_before_focus = 0
focus_widget, focus_pos = lb.body.get_focus()
# Scan through the rows calculating total height and the
# height of the rows before the focus widget.
for widget in lb.body:
rows = widget.rows((maxcol - 1,))
if widget is focus_widget:
if widget is lb.body.focus:
seen_focus = True
elif not seen_focus:
height_before_focus += rows
Expand Down

0 comments on commit 4fd089a

Please sign in to comment.