EmojiChooser: Ensure always have a selected button
authorDaniel Boles <dboles@src.gnome.org>
Mon, 4 Sep 2017 18:56:47 +0000 (19:56 +0100)
committerDaniel Boles <dboles@src.gnome.org>
Mon, 4 Sep 2017 21:33:24 +0000 (22:33 +0100)
We were only selecting a section’s button if the adjustment y coord was
within its heading, so scrolling slightly into it unchecked all buttons.
This also fixes how we could end up with the first 2 selected, somehow.

https://bugzilla.gnome.org/show_bug.cgi?id=787172

gtk/gtkemojichooser.c

index 7d8c589a04ca48af43105953df99c696712e3dcc..3f74f576afc40b84e144ecfbb7412d65d4d21a69 100644 (file)
@@ -404,38 +404,54 @@ populate_emoji_chooser (GtkEmojiChooser *chooser)
   g_bytes_unref (bytes);
 }
 
-static void
-update_state (EmojiSection *section,
-              double        value)
-{
-  GtkAllocation alloc = { 0, 0, 0, 20 };
-
-  if (section->heading)
-    gtk_widget_get_allocation (section->heading, &alloc);
-
-  if (alloc.y <= value && value < alloc.y + alloc.height)
-    gtk_widget_set_state_flags (section->button, GTK_STATE_FLAG_CHECKED, FALSE);
-  else
-    gtk_widget_unset_state_flags (section->button, GTK_STATE_FLAG_CHECKED);
-}
-
 static void
 adj_value_changed (GtkAdjustment *adj,
                    gpointer       data)
 {
   GtkEmojiChooser *chooser = data;
   double value = gtk_adjustment_get_value (adj);
+  EmojiSection const *sections[] = {
+    &chooser->recent,
+    &chooser->people,
+    &chooser->body,
+    &chooser->nature,
+    &chooser->food,
+    &chooser->travel,
+    &chooser->activities,
+    &chooser->objects,
+    &chooser->symbols,
+    &chooser->flags,
+  };
+  EmojiSection const *select_section = sections[0];
+  gsize i;
+
+  /* Figure out which section the current scroll position is within */
+  for (i = 0; i < G_N_ELEMENTS (sections); ++i)
+    {
+      EmojiSection const *section = sections[i];
+      GtkAllocation alloc;
 
-  update_state (&chooser->recent, value);
-  update_state (&chooser->people, value);
-  update_state (&chooser->body, value);
-  update_state (&chooser->nature, value);
-  update_state (&chooser->food, value);
-  update_state (&chooser->travel, value);
-  update_state (&chooser->activities, value);
-  update_state (&chooser->objects, value);
-  update_state (&chooser->symbols, value);
-  update_state (&chooser->flags, value);
+      if (section->heading)
+        gtk_widget_get_allocation (section->heading, &alloc);
+      else
+        gtk_widget_get_allocation (section->box, &alloc);
+
+      if (value < alloc.y)
+        break;
+
+      select_section = section;
+    }
+
+  /* Un/Check the section buttons accordingly */
+  for (i = 0; i < G_N_ELEMENTS (sections); ++i)
+    {
+      EmojiSection const *section = sections[i];
+
+      if (section == select_section)
+        gtk_widget_set_state_flags (section->button, GTK_STATE_FLAG_CHECKED, FALSE);
+      else
+        gtk_widget_unset_state_flags (section->button, GTK_STATE_FLAG_CHECKED);
+    }
 }
 
 static gboolean