[gnome-builder] workbench: add GbWorkbench:active-view property



commit 7bb09fc85d6d1dc89f9b15f4a263d86ad87b2ee2
Author: Christian Hergert <christian hergert me>
Date:   Tue Jun 16 22:25:25 2015 -0700

    workbench: add GbWorkbench:active-view property
    
    Track focus changes to the workbench so we have a convenient way to get
    the active view.

 src/workbench/gb-workbench-private.h |    3 +
 src/workbench/gb-workbench.c         |   74 ++++++++++++++++++++++++++++++++++
 src/workbench/gb-workbench.h         |    1 +
 3 files changed, 78 insertions(+), 0 deletions(-)
---
diff --git a/src/workbench/gb-workbench-private.h b/src/workbench/gb-workbench-private.h
index 1ffd7e9..8ec7d8e 100644
--- a/src/workbench/gb-workbench-private.h
+++ b/src/workbench/gb-workbench-private.h
@@ -43,6 +43,9 @@ struct _GbWorkbench
   gchar                  *current_folder_uri;
   PeasExtensionSet       *extensions;
 
+  /* Weak Reference */
+  GtkWidget              *active_view;
+
   /* Template references */
   GeditMenuStackSwitcher *gear_menu_button;
   GbProjectTree          *project_tree;
diff --git a/src/workbench/gb-workbench.c b/src/workbench/gb-workbench.c
index 29912b6..9253e3b 100644
--- a/src/workbench/gb-workbench.c
+++ b/src/workbench/gb-workbench.c
@@ -39,6 +39,7 @@ G_DEFINE_TYPE (GbWorkbench, gb_workbench, GTK_TYPE_APPLICATION_WINDOW)
 
 enum {
   PROP_0,
+  PROP_ACTIVE_VIEW,
   PROP_BUILDING,
   PROP_CONTEXT,
   LAST_PROP
@@ -512,6 +513,56 @@ gb_workbench_constructed (GObject *object)
 }
 
 static void
+gb_workbench_active_view_unref (gpointer  data,
+                                GObject  *where_object_was)
+{
+  GbWorkbench *self = data;
+
+  g_assert (GB_IS_WORKBENCH (self));
+
+  self->active_view = NULL;
+  g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_ACTIVE_VIEW]);
+}
+
+static void
+gb_workbench_set_focus (GtkWindow *window,
+                        GtkWidget *widget)
+{
+  GbWorkbench *self = (GbWorkbench *)window;
+  GtkWidget *active_view = NULL;
+
+  g_assert (GTK_IS_WINDOW (window));
+  g_assert (!widget || GTK_IS_WIDGET (widget));
+
+  if (widget != NULL)
+    active_view = gtk_widget_get_ancestor (widget, GB_TYPE_VIEW);
+
+  if ((active_view == NULL) || (active_view == self->active_view))
+    goto chainup;
+
+  if (self->active_view != NULL)
+    {
+      g_object_weak_unref (G_OBJECT (self->active_view),
+                           gb_workbench_active_view_unref,
+                           self);
+      self->active_view = NULL;
+    }
+
+  if (active_view != NULL)
+    {
+      self->active_view = active_view;
+      g_object_weak_ref (G_OBJECT (self->active_view),
+                         gb_workbench_active_view_unref,
+                         self);
+    }
+
+  g_object_notify_by_pspec (G_OBJECT (window), gParamSpecs [PROP_ACTIVE_VIEW]);
+
+chainup:
+  GTK_WINDOW_CLASS (gb_workbench_parent_class)->set_focus (window, widget);
+}
+
+static void
 gb_workbench_dispose (GObject *object)
 {
   GbWorkbench *self = (GbWorkbench *)object;
@@ -521,6 +572,7 @@ gb_workbench_dispose (GObject *object)
   self->disposing++;
 
   g_clear_object (&self->unload_cancellable);
+  ide_clear_weak_pointer (&self->active_view);
 
   G_OBJECT_CLASS (gb_workbench_parent_class)->dispose (object);
 
@@ -555,6 +607,10 @@ gb_workbench_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_ACTIVE_VIEW:
+      g_value_set_object (value, self->active_view);
+      break;
+
     case PROP_BUILDING:
       g_value_set_boolean (value, self->building);
       break;
@@ -592,6 +648,7 @@ gb_workbench_class_init (GbWorkbenchClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+  GtkWindowClass *window_class = GTK_WINDOW_CLASS (klass);
 
   object_class->constructed = gb_workbench_constructed;
   object_class->dispose = gb_workbench_dispose;
@@ -605,6 +662,15 @@ gb_workbench_class_init (GbWorkbenchClass *klass)
   widget_class->grab_focus = gb_workbench_grab_focus;
   widget_class->realize = gb_workbench_realize;
 
+  window_class->set_focus = gb_workbench_set_focus;
+
+  gParamSpecs [PROP_ACTIVE_VIEW] =
+    g_param_spec_object ("active-view",
+                         _("Active View"),
+                         _("Active View"),
+                         GB_TYPE_VIEW,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
   gParamSpecs [PROP_BUILDING] =
     g_param_spec_boolean ("building",
                           _("Building"),
@@ -1077,3 +1143,11 @@ gb_workbench_get_slider (GbWorkbench *self)
 
   return GTK_WIDGET (self->slider);
 }
+
+GtkWidget *
+gb_workbench_get_active_view (GbWorkbench *self)
+{
+  g_return_val_if_fail (GB_IS_WORKBENCH (self), NULL);
+
+  return self->active_view;
+}
diff --git a/src/workbench/gb-workbench.h b/src/workbench/gb-workbench.h
index 4297fb4..3e57c9f 100644
--- a/src/workbench/gb-workbench.h
+++ b/src/workbench/gb-workbench.h
@@ -53,6 +53,7 @@ GtkWidget        *gb_workbench_get_view_grid        (GbWorkbench         *self);
 GtkWidget        *gb_workbench_get_slider           (GbWorkbench         *self);
 void              gb_workbench_reveal_file          (GbWorkbench         *self,
                                                      GFile               *file);
+GtkWidget        *gb_workbench_get_active_view      (GbWorkbench         *self);
 
 G_END_DECLS
 


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