sidebar: Use the Gtk.ListView.scroll_to() function for scrolling

Rather than activating an action through a GLib.Variant, we can use the
newly added scroll_to() function to do most of the work for us.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-30 11:11:43 -04:00
parent c195e68216
commit 7358183fef
2 changed files with 3 additions and 13 deletions

View File

@ -2,7 +2,6 @@
"""A sidebar Header attached to a hidden ListView for selecting playlists.""" """A sidebar Header attached to a hidden ListView for selecting playlists."""
import typing import typing
from gi.repository import GObject from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gtk from gi.repository import Gtk
from .. import db from .. import db
from .. import factory from .. import factory
@ -86,9 +85,7 @@ class Section(header.Header):
def select_playlist(self, playlist: db.playlist.Playlist) -> None: def select_playlist(self, playlist: db.playlist.Playlist) -> None:
"""Select the requested playlist.""" """Select the requested playlist."""
if (index := self.playlist_index(playlist)) is not None: if (index := self.playlist_index(playlist)) is not None:
self._selection.select_item(index, True) self._listview.scroll_to(index, Gtk.ListScrollFlags.SELECT)
self._listview.activate_action("list.scroll-to-item",
GLib.Variant.new_uint32(index))
@GObject.Signal(arg_types=(db.playlist.Playlist,)) @GObject.Signal(arg_types=(db.playlist.Playlist,))
def playlist_activated(self, playlist: db.playlist.Playlist): def playlist_activated(self, playlist: db.playlist.Playlist):

View File

@ -4,7 +4,6 @@ import emmental.db
import emmental.sidebar.section import emmental.sidebar.section
import tests.util import tests.util
import unittest.mock import unittest.mock
from gi.repository import GLib
from gi.repository import Gtk from gi.repository import Gtk
@ -105,18 +104,12 @@ class TestSection(tests.util.TestCase):
def test_select_playlist(self): def test_select_playlist(self):
"""Test selecting a specific playlist.""" """Test selecting a specific playlist."""
self.section.do_get_subtitle = unittest.mock.Mock(return_value="") self.section.do_get_subtitle = unittest.mock.Mock(return_value="")
playlist_selected = unittest.mock.Mock()
self.section.connect("playlist-selected", playlist_selected)
playlist = self.table.create("Test Playlist") playlist = self.table.create("Test Playlist")
playlist_selected.assert_not_called()
with unittest.mock.patch.object(self.section._listview, with unittest.mock.patch.object(self.section._listview,
"activate_action") as mock_action: "scroll_to") as mock_scroll_to:
self.section.select_playlist(playlist) self.section.select_playlist(playlist)
playlist_selected.assert_called_with(self.section, playlist) mock_scroll_to.assert_called_with(0, Gtk.ListScrollFlags.SELECT)
mock_action.assert_called_with("list.scroll-to-item",
GLib.Variant.new_uint32(0))
def test_playlist_selected(self): def test_playlist_selected(self):
"""Test selecting a playlist in the list.""" """Test selecting a playlist in the list."""