carousel: Port MctCarousel to GTK4

Make it an AdwBin subclass. Purge all GtkContainer usage, and expose
GTK4-inspired API to add items to the carousel, and also to control
the internal GtkRevealer.
This commit is contained in:
Georges Basile Stavracas Neto 2022-07-08 16:34:35 -03:00
parent 0fca70768a
commit b71b83f348
5 changed files with 44 additions and 37 deletions

View File

@ -122,7 +122,8 @@ Dependencies
* gio-unix-2.0 ≥ 2.60
* glib-2.0 ≥ 2.60
* gobject-2.0 ≥ 2.60
* gtk+-3.0
* gtk4
* libadwaita
* polkit-gobject-1
Licensing

View File

@ -64,7 +64,9 @@ mct_carousel_item_init (MctCarouselItem *self)
}
struct _MctCarousel {
GtkRevealer parent;
AdwBin parent;
GtkRevealer *revealer;
GList *children;
gint visible_page;
@ -81,7 +83,7 @@ struct _MctCarousel {
GtkStyleProvider *provider;
};
G_DEFINE_TYPE (MctCarousel, mct_carousel, GTK_TYPE_REVEALER)
G_DEFINE_TYPE (MctCarousel, mct_carousel, ADW_TYPE_BIN)
enum {
ITEM_ACTIVATED,
@ -160,7 +162,7 @@ mct_carousel_move_arrow (MctCarousel *self)
}
self->provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (self->provider), css, -1, NULL);
gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (self->provider), css, -1);
gtk_style_context_add_provider (context, self->provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_free (css);
@ -307,24 +309,17 @@ mct_carousel_goto_next_page (GtkWidget *button,
mct_carousel_select_item_at_index (self, self->visible_page * ITEMS_PER_PAGE);
}
static void
mct_carousel_add (GtkContainer *container,
GtkWidget *widget)
void
mct_carousel_add (MctCarousel *self,
MctCarouselItem *item)
{
MctCarousel *self = MCT_CAROUSEL (container);
gboolean last_box_is_full;
if (!MCT_IS_CAROUSEL_ITEM (widget))
{
GTK_CONTAINER_CLASS (mct_carousel_parent_class)->add (container, widget);
return;
}
g_return_if_fail (MCT_IS_CAROUSEL (self));
g_return_if_fail (MCT_IS_CAROUSEL_ITEM (item));
gtk_style_context_add_class (gtk_widget_get_style_context (widget), "menu");
gtk_button_set_relief (GTK_BUTTON (widget), GTK_RELIEF_NONE);
self->children = g_list_append (self->children, widget);
MCT_CAROUSEL_ITEM (widget)->page = get_last_page_number (self);
self->children = g_list_append (self->children, item);
item->page = get_last_page_number (self);
g_signal_connect (item, "clicked", G_CALLBACK (on_item_toggled), self);
last_box_is_full = ((g_list_length (self->children) - 1) % ITEMS_PER_PAGE == 0);
@ -332,30 +327,24 @@ mct_carousel_add (GtkContainer *container,
{
g_autofree gchar *page = NULL;
page = g_strdup_printf ("%d", MCT_CAROUSEL_ITEM (widget)->page);
page = g_strdup_printf ("%d", item->page);
self->last_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_valign (self->last_box, GTK_ALIGN_CENTER);
gtk_stack_add_named (self->stack, self->last_box, page);
}
gtk_box_append (GTK_BOX (self->last_box), widget);
gtk_box_append (GTK_BOX (self->last_box), GTK_WIDGET (item));
update_buttons_visibility (self);
}
static void
destroy_widget_cb (GtkWidget *widget,
gpointer user_data)
{
gtk_widget_destroy (widget);
}
void
mct_carousel_purge_items (MctCarousel *self)
{
gtk_container_forall (GTK_CONTAINER (self->stack),
destroy_widget_cb,
NULL);
GtkWidget *child;
while ((child = gtk_widget_get_first_child (GTK_WIDGET (self->stack))) != NULL)
gtk_stack_remove (self->stack, child);
g_list_free (self->children);
self->children = NULL;
@ -389,7 +378,6 @@ mct_carousel_class_init (MctCarouselClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
gtk_widget_class_set_template_from_resource (wclass,
"/org/freedesktop/MalcontentControl/ui/carousel.ui");
@ -398,14 +386,13 @@ mct_carousel_class_init (MctCarouselClass *klass)
gtk_widget_class_bind_template_child (wclass, MctCarousel, go_back_button);
gtk_widget_class_bind_template_child (wclass, MctCarousel, go_next_button);
gtk_widget_class_bind_template_child (wclass, MctCarousel, arrow);
gtk_widget_class_bind_template_child (wclass, MctCarousel, revealer);
gtk_widget_class_bind_template_callback (wclass, mct_carousel_goto_previous_page);
gtk_widget_class_bind_template_callback (wclass, mct_carousel_goto_next_page);
object_class->dispose = mct_carousel_dispose;
container_class->add = mct_carousel_add;
signals[ITEM_ACTIVATED] =
g_signal_new ("item-activated",
MCT_TYPE_CAROUSEL,
@ -463,3 +450,12 @@ mct_carousel_get_item_count (MctCarousel *self)
{
return g_list_length (self->children);
}
void
mct_carousel_set_revealed (MctCarousel *self,
gboolean revealed)
{
g_return_if_fail (MCT_IS_CAROUSEL (self));
gtk_revealer_set_reveal_child (self->revealer, revealed);
}

View File

@ -32,7 +32,7 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (MctCarouselItem, mct_carousel_item, MCT, CAROUSEL_ITEM, GtkButton)
#define MCT_TYPE_CAROUSEL (mct_carousel_get_type ())
G_DECLARE_FINAL_TYPE (MctCarousel, mct_carousel, MCT, CAROUSEL, GtkRevealer)
G_DECLARE_FINAL_TYPE (MctCarousel, mct_carousel, MCT, CAROUSEL, AdwBin)
GtkWidget *mct_carousel_item_new (void);
@ -52,4 +52,10 @@ void mct_carousel_select_item (MctCarousel *self,
guint mct_carousel_get_item_count (MctCarousel *self);
void mct_carousel_add (MctCarousel *self,
MctCarouselItem *item);
void mct_carousel_set_revealed (MctCarousel *self,
gboolean revealed);
G_END_DECLS

View File

@ -3,7 +3,9 @@
<!-- Copyright © 2020 Endless, Inc. -->
<interface>
<requires lib="gtk+" version="3.12"/>
<template class="MctCarousel" parent="GtkRevealer">
<template class="MctCarousel" parent="AdwBin">
<child>
<object class="GtkRevealer" id="revealer">
<property name="transition_duration">400</property>
<property name="reveal-child">True</property>
<child>
@ -101,5 +103,7 @@
</child>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -397,7 +397,7 @@ reload_users (MctUserSelector *self,
g_object_set (settings, "gtk-enable-animations", animations, NULL);
gtk_revealer_set_reveal_child (GTK_REVEALER (self->carousel), TRUE);
mct_carousel_set_revealed (self->carousel, TRUE);
}
static GtkWidget *
@ -458,7 +458,7 @@ user_added_cb (ActUserManager *user_manager,
mct_carousel_item_set_child (MCT_CAROUSEL_ITEM (item), widget);
g_object_set_data (G_OBJECT (item), "uid", GINT_TO_POINTER (act_user_get_uid (user)));
gtk_container_add (GTK_CONTAINER (self->carousel), item);
mct_carousel_add (self->carousel, MCT_CAROUSEL_ITEM (item));
}
static void