[gtk+] EmojiChooser: Ensure always have a selected button



commit b97f1900b77850e2cda0f2b336f1c87c81f0f786
Author: Daniel Boles <dboles src gnome org>
Date:   Mon Sep 4 19:56:47 2017 +0100

    EmojiChooser: Ensure always have a selected button
    
    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 |   66 ++++++++++++++++++++++++++++++------------------
 1 files changed, 41 insertions(+), 25 deletions(-)
---
diff --git a/gtk/gtkemojichooser.c b/gtk/gtkemojichooser.c
index 8da1723..d3d96b0 100644
--- a/gtk/gtkemojichooser.c
+++ b/gtk/gtkemojichooser.c
@@ -367,37 +367,53 @@ populate_emoji_chooser (GtkEmojiChooser *chooser)
 }
 
 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;
+
+      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];
 
-  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 == 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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]