HdyCarousel: add method to scroll to a page by index
Currently the only way (that I can find) to scroll to a page in a HdyCarousel
is by calling hdy_carousel_scroll_to()
, which takes a pointer to the child widget. However, all the other HdyCarousel
API operates in terms of page numbers (hdy_carousel_get_position()
, hdy_carousel_get_n_pages()
), so that means you need to do something like this to scroll to a page:
static void
show_relative_page (GsFeaturedCarousel *self,
gint delta)
{
gdouble current_page = hdy_carousel_get_position (self->carousel);
guint n_pages = hdy_carousel_get_n_pages (self->carousel);
gdouble new_page;
g_autoptr(GList) children = gtk_container_get_children (GTK_CONTAINER (self->carousel));
GtkWidget *new_page_widget;
if (n_pages == 0)
return;
new_page = ((guint) current_page + delta + n_pages) % n_pages;
new_page_widget = g_list_nth_data (children, new_page);
g_assert (new_page_widget != NULL);
hdy_carousel_scroll_to (self->carousel, new_page_widget);
}
In particular, calling gtk_container_get_children()
is relatively expensive, since it means allocating a GList
and then freeing it again later.
So it would be nice if there were a function like void hdy_carousel_scroll_to_position (HdyCarousel *, gdouble position)
which operates in terms of page numbers.