gui/treeview: Add a function for scrolling the treeview

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-24 08:25:32 -04:00
parent 9e37062920
commit 730395aeff
5 changed files with 50 additions and 17 deletions

View File

@ -167,4 +167,23 @@ void gui_treeview_set_playlist(struct playlist *playlist)
__gui_treeview_clear_sorting();
__gui_treeview_set_sort_indicators();
gui_treeview_scroll();
}
void gui_treeview_scroll()
{
struct playlist *playlist = gui_model_get_playlist();
GtkTreePath *path;
if (!playlist || (int)playlist->pl_queue.q_cur.it_pos < 0)
return;
path = gui_filter_path_from_index(playlist->pl_queue.q_cur.it_pos);
if (!path)
return;
gtk_tree_view_set_cursor(gui_treeview(), path, NULL, false);
gtk_tree_view_scroll_to_cell(gui_treeview(), path, NULL, true, 0.5, 0.5);
gtk_tree_path_free(path);
}

View File

@ -300,24 +300,11 @@ void gui_view_init()
void gui_view_set_playlist(struct playlist *playlist)
{
gui_filter_set_playlist(playlist);
gui_view_scroll();
gui_treeview_set_playlist(playlist);
}
void gui_view_scroll()
{
struct playlist *playlist = gui_model_get_playlist();
struct queue *queue = playlist ? &playlist->pl_queue : NULL;
GtkTreePath *path;
if (!queue || (int)queue->q_cur.it_pos < 0 || view_no_scroll)
return;
path = gui_filter_path_from_index(queue->q_cur.it_pos);
if (!path)
return;
gtk_tree_view_set_cursor(view_treeview, path, NULL, false);
gtk_tree_view_scroll_to_cell(view_treeview, path, NULL, TRUE, 0.5, 0.5);
gtk_tree_path_free(path);
if (!view_no_scroll)
gui_treeview_scroll();
}

View File

@ -14,6 +14,9 @@ void gui_treeview_deinit();
/* Called to set the current playlist. */
void gui_treeview_set_playlist(struct playlist *);
/* Called to scroll the treeview to the current track. */
void gui_treeview_scroll();
/* Called to access the treeview widget. */
static inline GtkTreeView *gui_treeview()
{

View File

@ -852,7 +852,7 @@ audio-volume-medium</property>
<property name="primary_icon_sensitive">False</property>
<property name="placeholder_text" translatable="yes">Type here to filter</property>
<signal name="search-changed" handler="__gui_filter_search_changed" swapped="no"/>
<signal name="search-changed" handler="gui_view_scroll" after="yes" swapped="no"/>
<signal name="search-changed" handler="gui_treeview_scroll" after="yes" swapped="no"/>
<accelerator key="slash" signal="grab-focus"/>
</object>
<packing>

View File

@ -12,10 +12,33 @@ struct core_init_data init_data = {};
void test_treeview_init()
{
GtkTreePath *path;
g_assert_nonnull(gui_treeview());
g_assert_true(GTK_IS_TREE_VIEW(gui_treeview()));
g_assert(gtk_tree_view_get_model(gui_treeview()) ==
GTK_TREE_MODEL(gui_filter_get()));
/* No scrolling without a playlist. */
gui_treeview_scroll();
gtk_tree_view_get_cursor(gui_treeview(), &path, NULL);
g_assert_null(path);
gui_treeview_set_playlist(playlist_get(PL_SYSTEM, "Collection"));
g_assert(gui_model_get_playlist() ==
playlist_get(PL_SYSTEM, "Collection"));
/* No scrolling when playlist index is -1. */
gui_treeview_scroll();
gtk_tree_view_get_cursor(gui_treeview(), &path, NULL);
g_assert_null(path);
/* Okay, NOW we can scroll! */
playlist_get(PL_SYSTEM, "Collection")->pl_queue.q_cur.it_pos = 4;
gui_treeview_scroll();
gtk_tree_view_get_cursor(gui_treeview(), &path, NULL);
g_assert_nonnull(path);
g_assert_cmpuint(gui_filter_path_get_track(path)->tr_track, ==, 5);
}
void test_treeview_sort()
@ -119,6 +142,7 @@ int main(int argc, char **argv)
gui_model_init();
gui_filter_init();
gui_treeview_init();
playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony");
while (idle_run_task()) {}
g_test_init(&argc, &argv, NULL);