[gnome-builder] libide/tweaks: improve selection state across navigation



commit f27271eb9e381184148112aff45308725f9035c1
Author: Christian Hergert <chergert redhat com>
Date:   Thu Aug 4 14:16:45 2022 -0700

    libide/tweaks: improve selection state across navigation
    
    This ensures that we drop/restore selection when swappin between the
    folded and/or selection-mode states.

 src/libide/tweaks/ide-tweaks-panel-list.c | 47 +++++++++++++++++++++++++++++++
 src/libide/tweaks/ide-tweaks-window.c     | 22 +++++++++------
 2 files changed, 61 insertions(+), 8 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-panel-list.c b/src/libide/tweaks/ide-tweaks-panel-list.c
index a20a6ca1c..9f6b9aaa4 100644
--- a/src/libide/tweaks/ide-tweaks-panel-list.c
+++ b/src/libide/tweaks/ide-tweaks-panel-list.c
@@ -34,6 +34,7 @@ struct _IdeTweaksPanelList
   AdwBin          parent_instance;
 
   IdeTweaksItem  *item;
+  IdeTweaksItem  *selected;
 
   GtkListBox     *list_box;
   GtkSearchEntry *search_entry;
@@ -156,6 +157,8 @@ ide_tweaks_panel_list_row_activated_cb (IdeTweaksPanelList    *self,
   g_assert (page != NULL);
   g_assert (IDE_IS_TWEAKS_PAGE (page));
 
+  g_set_object (&self->selected, page);
+
   g_signal_emit (self, signals [PAGE_ACTIVATED], 0, page);
 }
 
@@ -165,6 +168,7 @@ ide_tweaks_panel_list_dispose (GObject *object)
   IdeTweaksPanelList *self = (IdeTweaksPanelList *)object;
 
   g_clear_object (&self->item);
+  g_clear_object (&self->selected);
 
   G_OBJECT_CLASS (ide_tweaks_panel_list_parent_class)->dispose (object);
 }
@@ -334,6 +338,43 @@ ide_tweaks_panel_list_set_search_mode (IdeTweaksPanelList *self,
     }
 }
 
+static void
+ide_tweaks_panel_list_select (IdeTweaksPanelList *self,
+                              IdeTweaksItem      *item)
+{
+  g_assert (IDE_IS_TWEAKS_PANEL_LIST (self));
+  g_assert (!item || IDE_IS_TWEAKS_ITEM (item));
+
+  if (item == NULL)
+    return;
+
+  g_signal_handlers_block_by_func (self->list_box,
+                                   G_CALLBACK (ide_tweaks_panel_list_row_activated_cb),
+                                   self);
+
+  for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (self->list_box));
+       child != NULL;
+       child = gtk_widget_get_next_sibling (child))
+    {
+      IdeTweaksItem *row_item;
+
+      if (!IDE_IS_TWEAKS_PANEL_LIST_ROW (child))
+        continue;
+
+      row_item = ide_tweaks_panel_list_row_get_item (IDE_TWEAKS_PANEL_LIST_ROW (child));
+
+      if (row_item == item)
+        {
+          gtk_list_box_select_row (self->list_box, GTK_LIST_BOX_ROW (child));
+          break;
+        }
+    }
+
+  g_signal_handlers_unblock_by_func (self->list_box,
+                                     G_CALLBACK (ide_tweaks_panel_list_row_activated_cb),
+                                     self);
+}
+
 GtkSelectionMode
 ide_tweaks_panel_list_get_selection_mode (IdeTweaksPanelList *self)
 {
@@ -351,6 +392,12 @@ ide_tweaks_panel_list_set_selection_mode (IdeTweaksPanelList *self,
   if (selection_mode != ide_tweaks_panel_list_get_selection_mode (self))
     {
       gtk_list_box_set_selection_mode (self->list_box, selection_mode);
+
+      if (selection_mode == GTK_SELECTION_NONE)
+        gtk_list_box_unselect_all (self->list_box);
+      else
+        ide_tweaks_panel_list_select (self, self->selected);
+
       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTION_MODE]);
     }
 }
diff --git a/src/libide/tweaks/ide-tweaks-window.c b/src/libide/tweaks/ide-tweaks-window.c
index c6879451b..648843f7a 100644
--- a/src/libide/tweaks/ide-tweaks-window.c
+++ b/src/libide/tweaks/ide-tweaks-window.c
@@ -137,9 +137,6 @@ ide_tweaks_window_page_activated_cb (IdeTweaksWindow    *self,
   g_assert (IDE_IS_TWEAKS_PAGE (page));
   g_assert (IDE_IS_TWEAKS_PANEL_LIST (list));
 
-  if (page == ide_tweaks_window_get_current_page (self))
-    return;
-
   name = ide_tweaks_item_get_id (IDE_TWEAKS_ITEM (page));
   has_subpages = ide_tweaks_page_get_has_subpage (page);
 
@@ -160,6 +157,7 @@ ide_tweaks_window_page_activated_cb (IdeTweaksWindow    *self,
         }
 
       gtk_stack_set_visible_child (self->panel_stack, panel);
+
       adw_leaflet_navigate (self->leaflet, ADW_NAVIGATION_DIRECTION_FORWARD);
     }
   else
@@ -180,7 +178,10 @@ ide_tweaks_window_page_activated_cb (IdeTweaksWindow    *self,
       gtk_stack_set_visible_child (self->panel_list_stack, sublist);
       ide_tweaks_panel_list_set_search_mode (IDE_TWEAKS_PANEL_LIST (sublist), TRUE);
 
-      if (!self->folded)
+      if (self->folded)
+        ide_tweaks_panel_list_set_selection_mode (IDE_TWEAKS_PANEL_LIST (sublist),
+                                                  GTK_SELECTION_NONE);
+      else
         ide_tweaks_panel_list_select_first (IDE_TWEAKS_PANEL_LIST (sublist));
     }
 
@@ -222,7 +223,9 @@ ide_tweaks_window_rebuild (IdeTweaksWindow *self)
                        list,
                        ide_tweaks_item_get_id (IDE_TWEAKS_ITEM (self->tweaks)));
 
-  if (!self->folded)
+  if (self->folded)
+    ide_tweaks_panel_list_set_selection_mode (IDE_TWEAKS_PANEL_LIST (list), GTK_SELECTION_NONE);
+  else
     ide_tweaks_panel_list_select_first (IDE_TWEAKS_PANEL_LIST (list));
 
   ide_tweaks_window_update_actions (self);
@@ -343,16 +346,19 @@ ide_tweaks_window_set_folded (IdeTweaksWindow *self,
   if (self->folded != folded)
     {
       GtkSelectionMode selection_mode;
-      GtkWidget *child;
 
       self->folded = folded;
 
       selection_mode = folded ? GTK_SELECTION_NONE : GTK_SELECTION_SINGLE;
 
-      if ((child = gtk_stack_get_visible_child (self->panel_stack)))
+      for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (self->panel_stack));
+           child != NULL;
+           child = gtk_widget_get_next_sibling (child))
         ide_tweaks_panel_set_folded (IDE_TWEAKS_PANEL (child), folded);
 
-      if ((child = gtk_stack_get_visible_child (self->panel_list_stack)))
+      for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (self->panel_list_stack));
+           child != NULL;
+           child = gtk_widget_get_next_sibling (child))
         ide_tweaks_panel_list_set_selection_mode (IDE_TWEAKS_PANEL_LIST (child), selection_mode);
 
       ide_tweaks_window_update_actions (self);


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