Skip to content

Commit

Permalink
Support relative scroll for ListBox (#858)
Browse files Browse the repository at this point in the history
* Support relative scroll for ListBox

* By default use relative scroll
  if amount_of_widgets >= maxrow * 3
* Simplify typing for `calculate_visible`

* Expose `calculate_visible` types on the listbox module level

* Support scroll for `TreeListBox`

* narrower `ScrollSupportingBody` API and rework `rows_max`

---------

Co-authored-by: Aleksei Stepanov <alekseis@nvidia.com>
  • Loading branch information
penguinolog and Aleksei Stepanov committed Mar 13, 2024
1 parent 18d4236 commit ca18cf5
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 91 deletions.
2 changes: 1 addition & 1 deletion examples/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def __init__(self) -> None:
self.listbox.offset_rows = 1
self.footer = urwid.AttrMap(urwid.Text(self.footer_text), "foot")
self.view = urwid.Frame(
urwid.AttrMap(self.listbox, "body"),
urwid.AttrMap(urwid.ScrollBar(self.listbox), "body"),
header=urwid.AttrMap(self.header, "head"),
footer=self.footer,
)
Expand Down
27 changes: 24 additions & 3 deletions tests/test_scrollable.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,28 @@ def test_negative(self):
with self.assertRaises(TypeError):
urwid.ScrollBar(urwid.SolidFill(" "))

def test_no_scrollbar(self):
"""If widget fit without scroll - no scrollbar needed"""
widget = urwid.ScrollBar(
urwid.Scrollable(urwid.BigText("1", urwid.HalfBlockHeavy6x5Font())),
trough_char=urwid.ScrollBar.Symbols.LITE_SHADE,
thumb_char=urwid.ScrollBar.Symbols.DARK_SHADE,
)
reduced_size = (8, 5)
self.assertEqual(
(
" ▐█▌ ",
" ▀█▌ ",
" █▌ ",
" █▌ ",
" ███▌ ",
),
widget.render(reduced_size).decoded_text,
)


class TestScrollBarListBox(unittest.TestCase):
def test_non_selectable(self):
def test_relative_non_selectable(self):
widget = urwid.ScrollBar(
urwid.ListBox(urwid.SimpleListWalker(urwid.Text(line) for line in LGPL_HEADER.splitlines()))
)
Expand Down Expand Up @@ -245,13 +264,15 @@ def test_non_selectable(self):

widget.keypress(reduced_size, "end")

# Here we have ListBox issue: "end" really scroll not to the "dead end"
# in case of the bottom focus position is multiline
self.assertEqual(
(
"GNU Lesser General Public ",
"License along with this library; if ",
"not, write to the Free Software ",
"Foundation, Inc., 51 Franklin Street, ",
"Fifth Floor, Boston, MA 02110-1301 ",
"USA █",
"Fifth Floor, Boston, MA 02110-1301 █",
),
widget.render(reduced_size).decoded_text,
)
Expand Down

0 comments on commit ca18cf5

Please sign in to comment.