[gtk+] Inspector: Preserve selection when rescanning



commit 142efd186ded65597d3c7b32b0b6471c8d68f384
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Aug 2 15:20:17 2015 -0400

    Inspector: Preserve selection when rescanning
    
    We are semiregularly repopulating the object tree. Whenever
    we do, we loose the selection, needlessly. Do the extra work
    to preserve it.
    
    http://bugzilla.gnome.org/show_bug.cgi?id=753130

 gtk/inspector/inspect-button.c |    9 +++--
 gtk/inspector/object-tree.c    |   64 ++++++++++++++++++++++++++++------------
 gtk/inspector/object-tree.h    |    2 +-
 3 files changed, 51 insertions(+), 24 deletions(-)
---
diff --git a/gtk/inspector/inspect-button.c b/gtk/inspector/inspect-button.c
index d9a821b..b2cdfb5 100644
--- a/gtk/inspector/inspect-button.c
+++ b/gtk/inspector/inspect-button.c
@@ -214,13 +214,14 @@ select_widget (GtkInspectorWindow *iw,
                GtkWidget          *widget)
 {
   GtkInspectorObjectTree *wt = GTK_INSPECTOR_OBJECT_TREE (iw->object_tree);
-  GtkTreeIter iter;
 
   iw->selected_widget = widget;
 
-  if (!gtk_inspector_object_tree_find_object (wt, G_OBJECT (widget), &iter))
-    gtk_inspector_object_tree_scan (wt, gtk_widget_get_toplevel (widget));
-  gtk_inspector_object_tree_select_object (wt, G_OBJECT (widget));
+  if (!gtk_inspector_object_tree_select_object (wt, G_OBJECT (widget)))
+    {
+      gtk_inspector_object_tree_scan (wt, gtk_widget_get_toplevel (widget));
+      gtk_inspector_object_tree_select_object (wt, G_OBJECT (widget));
+    }
 }
 
 static void
diff --git a/gtk/inspector/object-tree.c b/gtk/inspector/object-tree.c
index 99d42ce..e35fd5c 100644
--- a/gtk/inspector/object-tree.c
+++ b/gtk/inspector/object-tree.c
@@ -828,6 +828,45 @@ gtk_inspector_object_tree_append_object (GtkInspectorObjectTree *wt,
     }
 }
 
+gboolean
+select_object_internal (GtkInspectorObjectTree *wt,
+                        GObject                *object,
+                        gboolean                activate)
+{
+  GtkTreeIter iter;
+
+  if (gtk_inspector_object_tree_find_object (wt, object, &iter))
+    {
+      GtkTreePath *path;
+      GtkTreeSelection *selection;
+
+      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (wt->priv->tree));
+      path = gtk_tree_model_get_path (GTK_TREE_MODEL (wt->priv->model), &iter);
+      gtk_tree_view_expand_to_path (GTK_TREE_VIEW (wt->priv->tree), path);
+      if (!activate)
+        g_signal_handlers_block_by_func (selection, on_selection_changed, wt);
+      gtk_tree_selection_select_iter (selection, &iter);
+      if (!activate)
+        g_signal_handlers_block_by_func (selection, on_selection_changed, wt);
+
+      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (wt->priv->tree), path, NULL, TRUE, 0.5, 0);
+      if (activate)
+        gtk_tree_view_row_activated (GTK_TREE_VIEW (wt->priv->tree), path, NULL);
+      gtk_tree_path_free (path);
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+gboolean
+gtk_inspector_object_tree_select_object (GtkInspectorObjectTree *wt,
+                                         GObject                *object)
+{
+  return select_object_internal (wt, object, TRUE);
+}
+
 void
 gtk_inspector_object_tree_scan (GtkInspectorObjectTree *wt,
                                 GtkWidget              *window)
@@ -835,6 +874,9 @@ gtk_inspector_object_tree_scan (GtkInspectorObjectTree *wt,
   GtkWidget *inspector_win;
   GList *toplevels, *l;
   GdkScreen *screen;
+  GObject *selected;
+
+  selected = gtk_inspector_object_tree_get_selected (wt);
 
   gtk_tree_store_clear (wt->priv->model);
   g_hash_table_remove_all (wt->priv->iters);
@@ -861,6 +903,9 @@ gtk_inspector_object_tree_scan (GtkInspectorObjectTree *wt,
   g_list_free (toplevels);
 
   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (wt->priv->tree));
+
+  if (selected)
+    select_object_internal (wt, selected, FALSE);
 }
 
 gboolean
@@ -885,24 +930,5 @@ gtk_inspector_object_tree_find_object (GtkInspectorObjectTree *wt,
   return FALSE;
 }
 
-void
-gtk_inspector_object_tree_select_object (GtkInspectorObjectTree *wt,
-                                         GObject                *object)
-{
-  GtkTreeIter iter;
-
-  if (gtk_inspector_object_tree_find_object (wt, object, &iter))
-    {
-      GtkTreePath *path;
-
-      path = gtk_tree_model_get_path (GTK_TREE_MODEL (wt->priv->model), &iter);
-      gtk_tree_view_expand_to_path (GTK_TREE_VIEW (wt->priv->tree), path);
-      gtk_tree_selection_select_iter (gtk_tree_view_get_selection (GTK_TREE_VIEW (wt->priv->tree)), &iter);
-      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (wt->priv->tree), path, NULL, TRUE, 0.5, 0);
-      gtk_tree_view_row_activated (GTK_TREE_VIEW (wt->priv->tree), path, NULL);
-      gtk_tree_path_free (path);
-    }
-}
-
 
 // vim: set et sw=2 ts=2:
diff --git a/gtk/inspector/object-tree.h b/gtk/inspector/object-tree.h
index 0210137..e89cb82 100644
--- a/gtk/inspector/object-tree.h
+++ b/gtk/inspector/object-tree.h
@@ -60,7 +60,7 @@ GType      gtk_inspector_object_tree_get_type            (void);
 
 void       gtk_inspector_object_tree_scan                (GtkInspectorObjectTree *wt,
                                                           GtkWidget              *window);
-void       gtk_inspector_object_tree_select_object       (GtkInspectorObjectTree *wt,
+gboolean   gtk_inspector_object_tree_select_object       (GtkInspectorObjectTree *wt,
                                                           GObject                *object);
 void       gtk_inspector_object_tree_append_object       (GtkInspectorObjectTree *wt,
                                                           GObject                *object,



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