From c15804d124d464d1bc822fbdd0fd3c6aaedad935 Mon Sep 17 00:00:00 2001 From: Alice Mikhaylenko Date: Tue, 21 May 2024 19:33:52 +0400 Subject: [PATCH] spinner: Only set :checked state when mapped Avoid doing the animation while it's invisible. See https://gitlab.gnome.org/GNOME/gtk/-/issues/1025 --- gtk/gtkspinner.c | 54 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/gtk/gtkspinner.c b/gtk/gtkspinner.c index 1668251594..f2985b8abb 100644 --- a/gtk/gtkspinner.c +++ b/gtk/gtkspinner.c @@ -64,6 +64,8 @@ typedef struct _GtkSpinnerClass GtkSpinnerClass; struct _GtkSpinner { GtkWidget parent; + + guint spinning : 1; }; struct _GtkSpinnerClass @@ -81,6 +83,17 @@ G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET) #define DEFAULT_SIZE 16 +static void +update_state_flags (GtkSpinner *spinner) +{ + if (spinner->spinning && gtk_widget_get_mapped (GTK_WIDGET (spinner))) + gtk_widget_set_state_flags (GTK_WIDGET (spinner), + GTK_STATE_FLAG_CHECKED, FALSE); + else + gtk_widget_unset_state_flags (GTK_WIDGET (spinner), + GTK_STATE_FLAG_CHECKED); +} + static void gtk_spinner_measure (GtkWidget *widget, GtkOrientation orientation, @@ -108,6 +121,26 @@ gtk_spinner_snapshot (GtkWidget *widget, gtk_widget_get_height (widget)); } +static void +gtk_spinner_map (GtkWidget *widget) +{ + GtkSpinner *spinner = GTK_SPINNER (widget); + + GTK_WIDGET_CLASS (gtk_spinner_parent_class)->map (widget); + + update_state_flags (spinner); +} + +static void +gtk_spinner_unmap (GtkWidget *widget) +{ + GtkSpinner *spinner = GTK_SPINNER (widget); + + GTK_WIDGET_CLASS (gtk_spinner_parent_class)->unmap (widget); + + update_state_flags (spinner); +} + static void gtk_spinner_css_changed (GtkWidget *widget, GtkCssStyleChange *change) @@ -139,7 +172,7 @@ gtk_spinner_get_spinning (GtkSpinner *spinner) { g_return_val_if_fail (GTK_IS_SPINNER (spinner), FALSE); - return (gtk_widget_get_state_flags ((GtkWidget *)spinner) & GTK_STATE_FLAG_CHECKED) > 0; + return spinner->spinning; } /** @@ -157,17 +190,14 @@ gtk_spinner_set_spinning (GtkSpinner *spinner, spinning = !!spinning; - if (spinning != gtk_spinner_get_spinning (spinner)) - { - g_object_notify (G_OBJECT (spinner), "spinning"); + if (spinning == spinner->spinning) + return; - if (spinning) - gtk_widget_set_state_flags (GTK_WIDGET (spinner), - GTK_STATE_FLAG_CHECKED, FALSE); - else - gtk_widget_unset_state_flags (GTK_WIDGET (spinner), - GTK_STATE_FLAG_CHECKED); - } + spinner->spinning = spinning; + + update_state_flags (spinner); + + g_object_notify (G_OBJECT (spinner), "spinning"); } static void @@ -215,6 +245,8 @@ gtk_spinner_class_init (GtkSpinnerClass *klass) widget_class = GTK_WIDGET_CLASS(klass); widget_class->snapshot = gtk_spinner_snapshot; widget_class->measure = gtk_spinner_measure; + widget_class->map = gtk_spinner_map; + widget_class->unmap = gtk_spinner_unmap; widget_class->css_changed = gtk_spinner_css_changed; /**