[gnome-builder] project-tree: extract project tree into subclass of GbTree



commit 9ee622bc455eab051b12a0bf9afe42c0db70b8c9
Author: Christian Hergert <christian hergert me>
Date:   Thu Apr 9 20:43:28 2015 -0700

    project-tree: extract project tree into subclass of GbTree
    
    This was starting to get out of hand, so lets extract our project tree
    into a self contained widget.
    
    A good followup to this would be to extract settings into their own
    location and simplify the editor workspace accordingly.

 data/gtk/menus.ui                                  |   18 +-
 data/ui/gb-editor-workspace.ui                     |    2 +-
 src/editor/gb-editor-workspace-private.h           |    6 +-
 src/editor/gb-editor-workspace.c                   |   16 +--
 src/gnome-builder.mk                               |   12 +-
 .../gb-project-tree-actions.c                      |  246 ++++++++------------
 .../gb-project-tree-actions.h                      |    5 +-
 .../gb-project-tree-builder.c                      |  103 +--------
 .../gb-project-tree-builder.h                      |    5 +-
 .../gb-project-tree-private.h}                     |   17 +-
 src/project-tree/gb-project-tree.c                 |  113 +++++++++
 .../gb-project-tree.h}                             |   21 ++-
 src/tree/gb-tree-node.h                            |    2 +
 src/tree/gb-tree.c                                 |    6 -
 src/tree/gb-tree.h                                 |    2 +
 15 files changed, 279 insertions(+), 295 deletions(-)
---
diff --git a/data/gtk/menus.ui b/data/gtk/menus.ui
index de5f9bb..caa947a 100644
--- a/data/gtk/menus.ui
+++ b/data/gtk/menus.ui
@@ -109,8 +109,8 @@
     </section>
 -->
   </menu>
-  <menu id="gb-tree-display-options">
-    <section id="display-options">
+  <menu id="gb-project-tree-display-options">
+    <section id="gb-project-tree-display-options-section">
       <submenu>
         <attribute name="label" translatable="yes">Display Options</attribute>
         <section>
@@ -136,8 +136,8 @@
       </submenu>
     </section>
   </menu>
-  <menu id="project-tree-open">
-    <section id="open-section">
+  <menu id="gb-project-tree-open">
+    <section id="gb-project-tree-open-section">
       <item>
         <attribute name="label" translatable="yes">_Open</attribute>
         <attribute name="action">project-tree.open</attribute>
@@ -150,21 +150,21 @@
             <attribute name="action">project-tree.open-with-editor</attribute>
           </item>
         </section>
-        <section id="open-by-mime-section">
+        <section id="gb-project-tree-open-by-mime-section">
         </section>
       </submenu>
     </section>
   </menu>
-  <menu id="project-tree-open-containing">
-    <section id="open-containing-section">
+  <menu id="gb-project-tree-open-containing">
+    <section id="gb-project-tree-open-containing-section">
       <item>
         <attribute name="label" translatable="yes">_Open Containing Folder</attribute>
         <attribute name="action">project-tree.open-containing-folder</attribute>
       </item>
     </section>
   </menu>
-  <menu id="project-tree-build">
-    <section id="build-section">
+  <menu id="gb-project-tree-build">
+    <section id="gb-project-tree-build-section">
       <item>
         <attribute name="label" translatable="yes">_Build</attribute>
         <attribute name="action">workbench.build</attribute>
diff --git a/data/ui/gb-editor-workspace.ui b/data/ui/gb-editor-workspace.ui
index b96d23c..ec3fed1 100644
--- a/data/ui/gb-editor-workspace.ui
+++ b/data/ui/gb-editor-workspace.ui
@@ -51,7 +51,7 @@
                 <property name="vexpand">true</property>
                 <property name="visible">true</property>
                 <child>
-                  <object class="GbTree" id="project_tree">
+                  <object class="GbProjectTree" id="project_tree">
                     <property name="headers-visible">false</property>
                     <property name="visible">true</property>
                   </object>
diff --git a/src/editor/gb-editor-workspace-private.h b/src/editor/gb-editor-workspace-private.h
index 9d650c8..f4950aa 100644
--- a/src/editor/gb-editor-workspace-private.h
+++ b/src/editor/gb-editor-workspace-private.h
@@ -19,7 +19,7 @@
 #ifndef GB_EDITOR_WORKSPACE_PRIVATE_H
 #define GB_EDITOR_WORKSPACE_PRIVATE_H
 
-#include "gb-tree.h"
+#include "gb-project-tree.h"
 #include "gb-view-grid.h"
 #include "gb-workspace.h"
 
@@ -28,14 +28,14 @@ G_BEGIN_DECLS
 struct _GbEditorWorkspace
 {
   GbWorkspace    parent_instance;
+
   GSettings     *editor_settings;
 
   GtkPaned      *project_paned;
   GtkBox        *project_sidebar;
   GtkMenuButton *project_button;
   GtkSpinner    *project_spinner;
-  GbTree        *project_tree;
-  GbTreeBuilder *project_tree_builder;
+  GbProjectTree *project_tree;
   GbViewGrid    *view_grid;
 
   guint          sidebar_position;
diff --git a/src/editor/gb-editor-workspace.c b/src/editor/gb-editor-workspace.c
index 1d7b7df..17816d3 100644
--- a/src/editor/gb-editor-workspace.c
+++ b/src/editor/gb-editor-workspace.c
@@ -26,8 +26,6 @@
 #include "gb-editor-workspace.h"
 #include "gb-editor-workspace-actions.h"
 #include "gb-editor-workspace-private.h"
-#include "gb-project-tree-builder.h"
-#include "gb-project-tree-actions.h"
 #include "gb-string.h"
 #include "gb-tree.h"
 #include "gb-view-grid.h"
@@ -93,7 +91,6 @@ gb_editor_workspace_context_changed (GtkWidget  *workspace,
       IdeBufferManager *bufmgr;
       IdeProject *project;
       GbWorkbench *workbench;
-      GbTreeNode *root;
       g_autoptr(GPtrArray) buffers = NULL;
       gsize i;
 
@@ -127,11 +124,7 @@ gb_editor_workspace_context_changed (GtkWidget  *workspace,
       g_object_bind_property (project, "name", self->project_button, "label",
                               G_BINDING_SYNC_CREATE);
 
-      root = gb_tree_get_root (self->project_tree);
-      gb_tree_node_set_item (root, G_OBJECT (ide_context_get_project (context)));
-
-      gb_project_tree_builder_set_context (GB_PROJECT_TREE_BUILDER (self->project_tree_builder),
-                                           context);
+      gb_project_tree_set_context (self->project_tree, context);
     }
 }
 
@@ -158,7 +151,6 @@ gb_editor_workspace_constructed (GObject *object)
   self->sidebar_position = gtk_paned_get_position (self->project_paned) ?: SIDEBAR_POSITION;
 
   gb_editor_workspace_actions_init (self);
-  gb_project_tree_actions_init (self);
 
   IDE_EXIT;
 }
@@ -197,7 +189,7 @@ gb_editor_workspace_class_init (GbEditorWorkspaceClass *klass)
   GB_WIDGET_CLASS_BIND (klass, GbEditorWorkspace, project_tree);
   GB_WIDGET_CLASS_BIND (klass, GbEditorWorkspace, view_grid);
 
-  g_type_ensure (GB_TYPE_TREE);
+  g_type_ensure (GB_TYPE_PROJECT_TREE);
   g_type_ensure (GB_TYPE_VIEW_GRID);
 }
 
@@ -206,10 +198,6 @@ gb_editor_workspace_init (GbEditorWorkspace *self)
 {
   gtk_widget_init_template (GTK_WIDGET (self));
 
-  self->project_tree_builder = gb_project_tree_builder_new (NULL);
-  gb_tree_add_builder (self->project_tree, GB_TREE_BUILDER (self->project_tree_builder));
-  gb_tree_set_root (self->project_tree, gb_tree_node_new ());
-
   gb_widget_set_context_handler (self, gb_editor_workspace_context_changed);
 }
 
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 71e81e5..e4f994c 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -64,8 +64,6 @@ libgnome_builder_la_SOURCES = \
        src/editor/gb-editor-workspace-private.h \
        src/editor/gb-editor-workspace.c \
        src/editor/gb-editor-workspace.h \
-       src/editor/gb-project-tree-actions.c \
-       src/editor/gb-project-tree-actions.h \
        src/gd/gd-tagged-entry.c \
        src/gd/gd-tagged-entry.h \
        src/gedit/gedit-close-button.c \
@@ -94,6 +92,13 @@ libgnome_builder_la_SOURCES = \
        src/preferences/gb-preferences-page.h \
        src/preferences/gb-preferences-window.c \
        src/preferences/gb-preferences-window.h \
+       src/project-tree/gb-project-tree.c \
+       src/project-tree/gb-project-tree.h \
+       src/project-tree/gb-project-tree-actions.c \
+       src/project-tree/gb-project-tree-actions.h \
+       src/project-tree/gb-project-tree-builder.c \
+       src/project-tree/gb-project-tree-builder.h \
+       src/project-tree/gb-project-tree-private.h \
        src/scrolledwindow/gb-scrolled-window.c \
        src/scrolledwindow/gb-scrolled-window.h \
        src/search/gb-search-box.c \
@@ -106,8 +111,6 @@ libgnome_builder_la_SOURCES = \
        src/search/gb-search-display.h \
        src/support/gb-support.c \
        src/support/gb-support.h \
-       src/tree/gb-project-tree-builder.c \
-       src/tree/gb-project-tree-builder.h \
        src/tree/gb-tree-builder.c \
        src/tree/gb-tree-builder.h \
        src/tree/gb-tree-node.c \
@@ -187,6 +190,7 @@ libgnome_builder_la_CFLAGS = \
        -I$(top_srcdir)/src/keybindings \
        -I$(top_srcdir)/src/nautilus \
        -I$(top_srcdir)/src/preferences \
+       -I$(top_srcdir)/src/project-tree \
        -I$(top_srcdir)/src/resources \
        -I$(top_srcdir)/src/scrolledwindow \
        -I$(top_srcdir)/src/search \
diff --git a/src/editor/gb-project-tree-actions.c b/src/project-tree/gb-project-tree-actions.c
similarity index 59%
rename from src/editor/gb-project-tree-actions.c
rename to src/project-tree/gb-project-tree-actions.c
index 8a0c097..cd94e3f 100644
--- a/src/editor/gb-project-tree-actions.c
+++ b/src/project-tree/gb-project-tree-actions.c
@@ -20,9 +20,10 @@
 #include <gio/gdesktopappinfo.h>
 
 #include "gb-editor-workspace.h"
-#include "gb-editor-workspace-private.h"
 #include "gb-file-manager.h"
-#include "gb-tree.h"
+#include "gb-project-tree.h"
+#include "gb-project-tree-actions.h"
+#include "gb-project-tree-private.h"
 #include "gb-widget.h"
 #include "gb-workbench.h"
 
@@ -56,58 +57,21 @@ project_file_is_directory (GObject *object)
 }
 
 static void
-gb_project_tree_actions_update_actions (GbEditorWorkspace *editor)
-{
-  GActionGroup *group;
-  GbTreeNode *selection;
-  GObject *item = NULL;
-
-  IDE_ENTRY;
-
-  g_assert (GB_IS_EDITOR_WORKSPACE (editor));
-  group = gtk_widget_get_action_group (GTK_WIDGET (editor), "project-tree");
-  g_assert (G_IS_SIMPLE_ACTION_GROUP (group));
-
-  selection = gb_tree_get_selected (editor->project_tree);
-  if (selection != NULL)
-    item = gb_tree_node_get_item (selection);
-
-  action_set (group, "open",
-              "enabled", !project_file_is_directory (item),
-              NULL);
-  action_set (group, "open-with-editor",
-              "enabled", !project_file_is_directory (item),
-              NULL);
-  action_set (group, "open-containing-folder",
-              "enabled", IDE_IS_PROJECT_FILE (item),
-              NULL);
-
-  IDE_EXIT;
-}
-
-static void
-gb_project_tree_actions__notify_selection (GbTree            *tree,
-                                           GParamSpec        *pspec,
-                                           GbEditorWorkspace *editor)
-{
-  g_assert (GB_IS_TREE (tree));
-  g_assert (GB_IS_EDITOR_WORKSPACE (editor));
-
-  gb_project_tree_actions_update_actions (editor);
-}
-
-static void
 gb_project_tree_actions_refresh (GSimpleAction *action,
                                  GVariant      *variant,
                                  gpointer       user_data)
 {
-  GbEditorWorkspace *self = user_data;
+  GbProjectTree *self = user_data;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (self));
+  g_assert (GB_IS_PROJECT_TREE (self));
 
-  gb_tree_rebuild (self->project_tree);
+  gb_tree_rebuild (GB_TREE (self));
 
-  /* TODO: Try to expand back to our current position */
+  /*
+   * TODO:
+   *
+   * Try to expand back to our current position
+   */
 }
 
 static void
@@ -115,11 +79,11 @@ gb_project_tree_actions_collapse_all_nodes (GSimpleAction *action,
                                             GVariant      *variant,
                                             gpointer       user_data)
 {
-  GbEditorWorkspace *self = user_data;
+  GbProjectTree *self = user_data;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (self));
+  g_assert (GB_IS_PROJECT_TREE (self));
 
-  gtk_tree_view_collapse_all (GTK_TREE_VIEW (self->project_tree));
+  gtk_tree_view_collapse_all (GTK_TREE_VIEW (self));
 }
 
 static void
@@ -127,13 +91,17 @@ gb_project_tree_actions_open (GSimpleAction *action,
                               GVariant      *variant,
                               gpointer       user_data)
 {
-  GbEditorWorkspace *self = user_data;
+  GbProjectTree *self = user_data;
+  GbWorkbench *workbench;
   GbTreeNode *selected;
   GObject *item;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (self));
+  g_assert (GB_IS_PROJECT_TREE (self));
+
+  workbench = gb_widget_get_workbench (GTK_WIDGET (self));
+  g_assert (GB_IS_WORKBENCH (workbench));
 
-  if (!(selected = gb_tree_get_selected (self->project_tree)) ||
+  if (!(selected = gb_tree_get_selected (GB_TREE (self))) ||
       !(item = gb_tree_node_get_item (selected)))
     return;
 
@@ -141,7 +109,6 @@ gb_project_tree_actions_open (GSimpleAction *action,
 
   if (IDE_IS_PROJECT_FILE (item))
     {
-      GbWorkbench *workbench;
       GFileInfo *file_info;
       GFile *file;
 
@@ -156,7 +123,6 @@ gb_project_tree_actions_open (GSimpleAction *action,
       if (!file)
         return;
 
-      workbench = gb_widget_get_workbench (GTK_WIDGET (self));
       gb_workbench_open (workbench, file);
     }
 }
@@ -166,11 +132,11 @@ gb_project_tree_actions_open_with (GSimpleAction *action,
                                    GVariant      *variant,
                                    gpointer       user_data)
 {
-  GDesktopAppInfo *app_info = NULL;
-  GbEditorWorkspace *editor = user_data;
+  g_autoptr(GDesktopAppInfo) app_info = NULL;
+  g_autoptr(GdkAppLaunchContext) launch_context = NULL;
+  GbProjectTree *self = user_data;
   GbTreeNode *selected;
   GbWorkbench *workbench;
-  GdkAppLaunchContext *launch_context;
   GdkDisplay *display;
   GFileInfo *file_info;
   GFile *file;
@@ -178,46 +144,25 @@ gb_project_tree_actions_open_with (GSimpleAction *action,
   GObject *item;
   GList *files;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (editor));
+  g_assert (GB_IS_PROJECT_TREE (self));
   g_assert (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING));
 
-  workbench = gb_widget_get_workbench (GTK_WIDGET (editor));
-  if (workbench == NULL)
-    return;
-
-  selected = gb_tree_get_selected (editor->project_tree);
-  if (selected == NULL)
-    return;
-
-  item = gb_tree_node_get_item (selected);
-  if (item == NULL || !IDE_IS_PROJECT_FILE (item))
-    return;
-
-  app_id = g_variant_get_string (variant, NULL);
-  if (app_id == NULL)
-    return;
-
-  app_info = g_desktop_app_info_new (app_id);
-  if (app_info == NULL)
+  if (!(workbench = gb_widget_get_workbench (GTK_WIDGET (self))) ||
+      !(selected = gb_tree_get_selected (GB_TREE (self))) ||
+      !(item = gb_tree_node_get_item (selected)) ||
+      !IDE_IS_PROJECT_FILE (item) ||
+      !(app_id = g_variant_get_string (variant, NULL)) ||
+      !(file_info = ide_project_file_get_file_info (IDE_PROJECT_FILE (item))) ||
+      !(file = ide_project_file_get_file (IDE_PROJECT_FILE (item))) ||
+      !(app_info = g_desktop_app_info_new (app_id)))
     return;
 
-  file_info = ide_project_file_get_file_info (IDE_PROJECT_FILE (item));
-  if (file_info == NULL)
-    return;
-
-  file = ide_project_file_get_file (IDE_PROJECT_FILE (item));
-  if (file == NULL)
-    return;
-
-  display = gtk_widget_get_display (GTK_WIDGET (editor));
+  display = gtk_widget_get_display (GTK_WIDGET (self));
   launch_context = gdk_display_get_app_launch_context (display);
-  files = g_list_append (NULL, file);
 
+  files = g_list_append (NULL, file);
   g_app_info_launch (G_APP_INFO (app_info), files, G_APP_LAUNCH_CONTEXT (launch_context), NULL);
-
   g_list_free (files);
-  g_object_unref (launch_context);
-  g_object_unref (app_info);
 }
 
 static void
@@ -225,38 +170,25 @@ gb_project_tree_actions_open_with_editor (GSimpleAction *action,
                                           GVariant      *variant,
                                           gpointer       user_data)
 {
-  GbEditorWorkspace *self = user_data;
+  GbWorkbench *workbench;
+  GbProjectTree *self = user_data;
+  GFileInfo *file_info;
+  GFile *file;
   GbTreeNode *selected;
   GObject *item;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (self));
+  g_assert (GB_IS_PROJECT_TREE (self));
 
-  if (!(selected = gb_tree_get_selected (self->project_tree)) ||
-      !(item = gb_tree_node_get_item (selected)))
+  if (!(selected = gb_tree_get_selected (GB_TREE (self))) ||
+      !(item = gb_tree_node_get_item (selected)) ||
+      !IDE_IS_PROJECT_FILE (item) ||
+      !(file_info = ide_project_file_get_file_info (IDE_PROJECT_FILE (item))) ||
+      (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY) ||
+      !(file = ide_project_file_get_file (IDE_PROJECT_FILE (item))) ||
+      !(workbench = gb_widget_get_workbench (GTK_WIDGET (self))))
     return;
 
-  item = gb_tree_node_get_item (selected);
-
-  if (IDE_IS_PROJECT_FILE (item))
-    {
-      GbWorkbench *workbench;
-      GFileInfo *file_info;
-      GFile *file;
-
-      file_info = ide_project_file_get_file_info (IDE_PROJECT_FILE (item));
-      if (!file_info)
-        return;
-
-      if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
-        return;
-
-      file = ide_project_file_get_file (IDE_PROJECT_FILE (item));
-      if (!file)
-        return;
-
-      workbench = gb_widget_get_workbench (GTK_WIDGET (self));
-      gb_workbench_open_with_editor (workbench, file);
-    }
+  gb_workbench_open_with_editor (workbench, file);
 }
 
 static void
@@ -267,25 +199,17 @@ gb_project_tree_actions_open_containing_folder (GSimpleAction *action,
   GbEditorWorkspace *self = user_data;
   GbTreeNode *selected;
   GObject *item;
+  GFile *file;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (self));
+  g_assert (GB_IS_PROJECT_TREE (self));
 
-  if (!(selected = gb_tree_get_selected (self->project_tree)) ||
-      !(item = gb_tree_node_get_item (selected)))
+  if (!(selected = gb_tree_get_selected (GB_TREE (self))) ||
+      !(item = gb_tree_node_get_item (selected)) ||
+      !IDE_IS_PROJECT_FILE (item) ||
+      !(file = ide_project_file_get_file (IDE_PROJECT_FILE (item))))
     return;
 
-  item = gb_tree_node_get_item (selected);
-
-  if (IDE_IS_PROJECT_FILE (item))
-    {
-      GFile *file;
-
-      file = ide_project_file_get_file (IDE_PROJECT_FILE (item));
-      if (!file)
-        return;
-
-      gb_file_manager_show (file, NULL);
-    }
+  gb_file_manager_show (file, NULL);
 }
 
 static void
@@ -293,11 +217,14 @@ gb_project_tree_actions_show_icons (GSimpleAction *action,
                                     GVariant      *variant,
                                     gpointer       user_data)
 {
-  GbEditorWorkspace *editor = user_data;
+  GbProjectTree *self = user_data;
+  gboolean show_icons;
 
-  g_assert (GB_IS_EDITOR_WORKSPACE (editor));
+  g_assert (GB_IS_PROJECT_TREE (self));
+  g_assert (g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN));
 
-  gb_tree_set_show_icons (editor->project_tree, g_variant_get_boolean (variant));
+  show_icons = g_variant_get_boolean (variant);
+  gb_tree_set_show_icons (GB_TREE (self), show_icons);
   g_simple_action_set_state (action, variant);
 }
 
@@ -312,27 +239,56 @@ static GActionEntry GbProjectTreeActions[] = {
 };
 
 void
-gb_project_tree_actions_init (GbEditorWorkspace *editor)
+gb_project_tree_actions_init (GbProjectTree *self)
 {
   g_autoptr(GSettings) settings = NULL;
   g_autoptr(GSimpleActionGroup) actions = NULL;
   g_autoptr(GAction) action = NULL;
 
-  settings = g_settings_new ("org.gtk.Settings.FileChooser");
   actions = g_simple_action_group_new ();
 
-  g_action_map_add_action_entries (G_ACTION_MAP (actions), GbProjectTreeActions,
-                                   G_N_ELEMENTS (GbProjectTreeActions), editor);
-
+  settings = g_settings_new ("org.gtk.Settings.FileChooser");
   action = g_settings_create_action (settings, "sort-directories-first");
   g_action_map_add_action (G_ACTION_MAP (actions), action);
 
-  gtk_widget_insert_action_group (GTK_WIDGET (editor), "project-tree", G_ACTION_GROUP (actions));
+  g_action_map_add_action_entries (G_ACTION_MAP (actions),
+                                   GbProjectTreeActions,
+                                   G_N_ELEMENTS (GbProjectTreeActions),
+                                   self);
+  gtk_widget_insert_action_group (GTK_WIDGET (self),
+                                  "project-tree",
+                                  G_ACTION_GROUP (actions));
+
+  gb_project_tree_actions_update (self);
+}
 
-  g_signal_connect (editor->project_tree,
-                    "notify::selection",
-                    G_CALLBACK (gb_project_tree_actions__notify_selection),
-                    editor);
+void
+gb_project_tree_actions_update (GbProjectTree *self)
+{
+  GActionGroup *group;
+  GbTreeNode *selection;
+  GObject *item = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (GB_IS_PROJECT_TREE (self));
+
+  group = gtk_widget_get_action_group (GTK_WIDGET (self), "project-tree");
+  g_assert (G_IS_SIMPLE_ACTION_GROUP (group));
 
-  gb_project_tree_actions_update_actions (editor);
+  selection = gb_tree_get_selected (GB_TREE (self));
+  if (selection != NULL)
+    item = gb_tree_node_get_item (selection);
+
+  action_set (group, "open",
+              "enabled", !project_file_is_directory (item),
+              NULL);
+  action_set (group, "open-with-editor",
+              "enabled", !project_file_is_directory (item),
+              NULL);
+  action_set (group, "open-containing-folder",
+              "enabled", IDE_IS_PROJECT_FILE (item),
+              NULL);
+
+  IDE_EXIT;
 }
diff --git a/src/editor/gb-project-tree-actions.h b/src/project-tree/gb-project-tree-actions.h
similarity index 85%
copy from src/editor/gb-project-tree-actions.h
copy to src/project-tree/gb-project-tree-actions.h
index 6f570e7..2837c84 100644
--- a/src/editor/gb-project-tree-actions.h
+++ b/src/project-tree/gb-project-tree-actions.h
@@ -19,11 +19,12 @@
 #ifndef GB_PROJECT_TREE_ACTIONS_H
 #define GB_PROJECT_TREE_ACTIONS_H
 
-#include "gb-editor-workspace.h"
+#include "gb-project-tree.h"
 
 G_BEGIN_DECLS
 
-void gb_project_tree_actions_init (GbEditorWorkspace *editor);
+void gb_project_tree_actions_init   (GbProjectTree *self);
+void gb_project_tree_actions_update (GbProjectTree *self);
 
 G_END_DECLS
 
diff --git a/src/tree/gb-project-tree-builder.c b/src/project-tree/gb-project-tree-builder.c
similarity index 78%
rename from src/tree/gb-project-tree-builder.c
rename to src/project-tree/gb-project-tree-builder.c
index 659c3d2..bfd0cc7 100644
--- a/src/tree/gb-project-tree-builder.c
+++ b/src/project-tree/gb-project-tree-builder.c
@@ -27,54 +27,15 @@
 struct _GbProjectTreeBuilder
 {
   GbTreeBuilder  parent_instance;
-  IdeContext    *context;
   GSettings     *file_chooser_settings;
 };
 
 G_DEFINE_TYPE (GbProjectTreeBuilder, gb_project_tree_builder, GB_TYPE_TREE_BUILDER)
 
-enum {
-  PROP_0,
-  PROP_CONTEXT,
-  LAST_PROP
-};
-
-static GParamSpec *gParamSpecs [LAST_PROP];
-
 GbTreeBuilder *
-gb_project_tree_builder_new (IdeContext *context)
+gb_project_tree_builder_new (void)
 {
-  g_return_val_if_fail (!context || IDE_IS_CONTEXT (context), NULL);
-
-  return g_object_new (GB_TYPE_PROJECT_TREE_BUILDER,
-                       "context", context,
-                       NULL);
-}
-
-IdeContext *
-gb_project_tree_builder_get_context (GbProjectTreeBuilder *self)
-{
-  g_return_val_if_fail (GB_IS_PROJECT_TREE_BUILDER (self), NULL);
-
-  return self->context;
-}
-
-void
-gb_project_tree_builder_set_context (GbProjectTreeBuilder *self,
-                                     IdeContext           *context)
-{
-  g_return_if_fail (GB_IS_PROJECT_TREE_BUILDER (self));
-  g_return_if_fail (!context || IDE_IS_CONTEXT (context));
-
-  if (g_set_object (&self->context, context))
-    {
-      GtkWidget *tree;
-
-      g_object_notify (G_OBJECT (self), "context");
-
-      if ((tree = gb_tree_builder_get_tree (GB_TREE_BUILDER (self))))
-        gb_tree_rebuild (GB_TREE (tree));
-    }
+  return g_object_new (GB_TYPE_PROJECT_TREE_BUILDER, NULL);
 }
 
 static const gchar *
@@ -324,22 +285,24 @@ gb_project_tree_builder_node_popup (GbTreeBuilder *builder,
 
   if (IDE_IS_PROJECT_ITEM (item) || IDE_IS_PROJECT (item))
     {
-      submenu = gtk_application_get_menu_by_id (app, "project-tree-build");
+      submenu = gtk_application_get_menu_by_id (app, "gb-project-tree-build");
       g_menu_prepend_section (menu, NULL, G_MENU_MODEL (submenu));
     }
 
   if (IDE_IS_PROJECT_FILE (item))
     {
-      submenu = gtk_application_get_menu_by_id (app, "project-tree-open-containing");
+      submenu = gtk_application_get_menu_by_id (app, "gb-project-tree-open-containing");
       g_menu_prepend_section (menu, NULL, G_MENU_MODEL (submenu));
 
-      submenu = gtk_application_get_menu_by_id (app, "project-tree-open");
+      submenu = gtk_application_get_menu_by_id (app, "gb-project-tree-open");
       g_menu_prepend_section (menu, NULL, G_MENU_MODEL (submenu));
 
-      submenu = gtk_application_get_menu_by_id (app, "open-by-mime-section");
+      submenu = gtk_application_get_menu_by_id (app, "gb-project-tree-open-by-mime-section");
       populate_mime_handlers (submenu, IDE_PROJECT_FILE (item));
     }
 
+  submenu = gtk_application_get_menu_by_id (app, "gb-project-tree-display-options");
+  g_menu_append_section (menu, NULL, G_MENU_MODEL (submenu));
 }
 
 static gboolean
@@ -404,72 +367,22 @@ gb_project_tree_builder_finalize (GObject *object)
 {
   GbProjectTreeBuilder *self = (GbProjectTreeBuilder *)object;
 
-  g_clear_object (&self->context);
   g_clear_object (&self->file_chooser_settings);
 
   G_OBJECT_CLASS (gb_project_tree_builder_parent_class)->finalize (object);
 }
 
 static void
-gb_project_tree_builder_get_property (GObject    *object,
-                                      guint       prop_id,
-                                      GValue     *value,
-                                      GParamSpec *pspec)
-{
-  GbProjectTreeBuilder *self = GB_PROJECT_TREE_BUILDER (object);
-
-  switch (prop_id)
-    {
-    case PROP_CONTEXT:
-      g_value_set_object (value, gb_project_tree_builder_get_context (self));
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    }
-}
-
-static void
-gb_project_tree_builder_set_property (GObject      *object,
-                                      guint         prop_id,
-                                      const GValue *value,
-                                      GParamSpec   *pspec)
-{
-  GbProjectTreeBuilder *self = GB_PROJECT_TREE_BUILDER (object);
-
-  switch (prop_id)
-    {
-    case PROP_CONTEXT:
-      gb_project_tree_builder_set_context (self, g_value_get_object (value));
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    }
-}
-
-static void
 gb_project_tree_builder_class_init (GbProjectTreeBuilderClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GbTreeBuilderClass *tree_builder_class = GB_TREE_BUILDER_CLASS (klass);
 
   object_class->finalize = gb_project_tree_builder_finalize;
-  object_class->get_property = gb_project_tree_builder_get_property;
-  object_class->set_property = gb_project_tree_builder_set_property;
 
   tree_builder_class->build_node = gb_project_tree_builder_build_node;
   tree_builder_class->node_activated = gb_project_tree_builder_node_activated;
   tree_builder_class->node_popup = gb_project_tree_builder_node_popup;
-
-  gParamSpecs [PROP_CONTEXT] =
-    g_param_spec_object ("context",
-                         _("Context"),
-                         _("The ide context for the project tree."),
-                         IDE_TYPE_CONTEXT,
-                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-  g_object_class_install_property (object_class, PROP_CONTEXT,
-                                   gParamSpecs [PROP_CONTEXT]);
 }
 
 static void
diff --git a/src/tree/gb-project-tree-builder.h b/src/project-tree/gb-project-tree-builder.h
similarity index 77%
rename from src/tree/gb-project-tree-builder.h
rename to src/project-tree/gb-project-tree-builder.h
index 66ac9d9..2924185 100644
--- a/src/tree/gb-project-tree-builder.h
+++ b/src/project-tree/gb-project-tree-builder.h
@@ -30,10 +30,7 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (GbProjectTreeBuilder, gb_project_tree_builder,
                       GB, PROJECT_TREE_BUILDER, GbTreeBuilder)
 
-GbTreeBuilder  *gb_project_tree_builder_new         (IdeContext           *context);
-IdeContext     *gb_project_tree_builder_get_context (GbProjectTreeBuilder *self);
-void            gb_project_tree_builder_set_context (GbProjectTreeBuilder *self,
-                                                     IdeContext           *context);
+GbTreeBuilder  *gb_project_tree_builder_new (void);
 
 G_END_DECLS
 
diff --git a/src/editor/gb-project-tree-actions.h b/src/project-tree/gb-project-tree-private.h
similarity index 75%
copy from src/editor/gb-project-tree-actions.h
copy to src/project-tree/gb-project-tree-private.h
index 6f570e7..0ee4cc7 100644
--- a/src/editor/gb-project-tree-actions.h
+++ b/src/project-tree/gb-project-tree-private.h
@@ -1,4 +1,4 @@
-/* gb-project-tree-actions.h
+/* gb-project-tree-private.h
  *
  * Copyright (C) 2015 Christian Hergert <christian hergert me>
  *
@@ -16,15 +16,20 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef GB_PROJECT_TREE_ACTIONS_H
-#define GB_PROJECT_TREE_ACTIONS_H
+#ifndef GB_PROJECT_TREE_PRIVATE_H
+#define GB_PROJECT_TREE_PRIVATE_H
 
-#include "gb-editor-workspace.h"
+#include "gb-tree.h"
 
 G_BEGIN_DECLS
 
-void gb_project_tree_actions_init (GbEditorWorkspace *editor);
+struct _GbProjectTree
+{
+  GbTree     parent_instance;
+
+  GSettings *settings;
+};
 
 G_END_DECLS
 
-#endif /* GB_PROJECT_TREE_ACTIONS_H */
+#endif /* GB_PROJECT_TREE_PRIVATE_H */
diff --git a/src/project-tree/gb-project-tree.c b/src/project-tree/gb-project-tree.c
new file mode 100644
index 0000000..e72402a
--- /dev/null
+++ b/src/project-tree/gb-project-tree.c
@@ -0,0 +1,113 @@
+/* gb-project-tree.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "gb-project-tree.h"
+#include "gb-project-tree-actions.h"
+#include "gb-project-tree-builder.h"
+#include "gb-project-tree-private.h"
+
+G_DEFINE_TYPE (GbProjectTree, gb_project_tree, GB_TYPE_TREE)
+
+GtkWidget *
+gb_project_tree_new (void)
+{
+  return g_object_new (GB_TYPE_PROJECT_TREE, NULL);
+}
+
+IdeContext *
+gb_project_tree_get_context (GbProjectTree *self)
+{
+  GbTreeNode *root;
+  GObject *item;
+
+  g_return_val_if_fail (GB_IS_PROJECT_TREE (self), NULL);
+
+  if ((root = gb_tree_get_root (GB_TREE (self))) &&
+      (item = gb_tree_node_get_item (root)) &&
+      IDE_IS_OBJECT (item))
+    return ide_object_get_context (IDE_OBJECT (item));
+
+  return NULL;
+}
+
+void
+gb_project_tree_set_context (GbProjectTree *self,
+                             IdeContext    *context)
+{
+  GbTreeNode *root;
+  IdeProject *project = NULL;
+
+  g_return_if_fail (GB_IS_PROJECT_TREE (self));
+  g_return_if_fail (!context || IDE_IS_CONTEXT (context));
+
+  if (context != NULL)
+    project = ide_context_get_project (context);
+
+  root = gb_tree_get_root (GB_TREE (self));
+  gb_tree_node_set_item (root, G_OBJECT (project));
+
+  gb_tree_rebuild (GB_TREE (self));
+}
+
+static void
+gb_project_tree_notify_selection (GbProjectTree *self)
+{
+  g_assert (GB_IS_PROJECT_TREE (self));
+
+  gb_project_tree_actions_update (self);
+}
+
+static void
+gb_project_tree_finalize (GObject *object)
+{
+  GbProjectTree *self = (GbProjectTree *)object;
+
+  g_clear_object (&self->settings);
+
+  G_OBJECT_CLASS (gb_project_tree_parent_class)->finalize (object);
+}
+
+static void
+gb_project_tree_class_init (GbProjectTreeClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gb_project_tree_finalize;
+}
+
+static void
+gb_project_tree_init (GbProjectTree *self)
+{
+  GbTreeBuilder *builder;
+
+  gb_tree_set_root (GB_TREE (self), gb_tree_node_new ());
+
+  //self->settings = g_settings_new ("org.gnome.builder.project-tree");
+
+  builder = gb_project_tree_builder_new ();
+  gb_tree_add_builder (GB_TREE (self), builder);
+
+  g_signal_connect (self,
+                    "notify::selection",
+                    G_CALLBACK (gb_project_tree_notify_selection),
+                    NULL);
+
+  gb_project_tree_actions_init (self);
+}
diff --git a/src/editor/gb-project-tree-actions.h b/src/project-tree/gb-project-tree.h
similarity index 58%
rename from src/editor/gb-project-tree-actions.h
rename to src/project-tree/gb-project-tree.h
index 6f570e7..872d7f8 100644
--- a/src/editor/gb-project-tree-actions.h
+++ b/src/project-tree/gb-project-tree.h
@@ -1,4 +1,4 @@
-/* gb-project-tree-actions.h
+/* gb-project-tree.h
  *
  * Copyright (C) 2015 Christian Hergert <christian hergert me>
  *
@@ -16,15 +16,24 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef GB_PROJECT_TREE_ACTIONS_H
-#define GB_PROJECT_TREE_ACTIONS_H
+#ifndef GB_PROJECT_TREE_H
+#define GB_PROJECT_TREE_H
 
-#include "gb-editor-workspace.h"
+#include <ide.h>
+
+#include "gb-tree.h"
 
 G_BEGIN_DECLS
 
-void gb_project_tree_actions_init (GbEditorWorkspace *editor);
+#define GB_TYPE_PROJECT_TREE (gb_project_tree_get_type())
+
+G_DECLARE_FINAL_TYPE (GbProjectTree, gb_project_tree, GB, PROJECT_TREE, GbTree)
+
+GtkWidget  *gb_project_tree_new         (void);
+void        gb_project_tree_set_context (GbProjectTree *self,
+                                         IdeContext    *context);
+IdeContext *gb_project_tree_get_context (GbProjectTree *self);
 
 G_END_DECLS
 
-#endif /* GB_PROJECT_TREE_ACTIONS_H */
+#endif /* GB_PROJECT_TREE_H */
diff --git a/src/tree/gb-tree-node.h b/src/tree/gb-tree-node.h
index 0d0217c..b93e40b 100644
--- a/src/tree/gb-tree-node.h
+++ b/src/tree/gb-tree-node.h
@@ -39,6 +39,8 @@ typedef struct _GbTreeNode        GbTreeNode;
 typedef struct _GbTreeNodeClass   GbTreeNodeClass;
 typedef struct _GbTreeNodePrivate GbTreeNodePrivate;
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (GbTreeNode, g_object_unref)
+
 struct _GbTreeNode
 {
        GInitiallyUnowned parent;
diff --git a/src/tree/gb-tree.c b/src/tree/gb-tree.c
index d840962..f2de741 100644
--- a/src/tree/gb-tree.c
+++ b/src/tree/gb-tree.c
@@ -182,19 +182,13 @@ static GMenu *
 gb_tree_create_menu (GbTree     *self,
                      GbTreeNode *node)
 {
-  GtkApplication *app;
   GMenu *menu;
-  GMenu *submenu;
   guint i;
 
   g_return_val_if_fail (GB_IS_TREE (self), NULL);
   g_return_val_if_fail (GB_IS_TREE_NODE (node), NULL);
 
   menu = g_menu_new ();
-  app = GTK_APPLICATION (g_application_get_default ());
-
-  submenu = gtk_application_get_menu_by_id (app, "gb-tree-display-options");
-  g_menu_append_section (menu, NULL, G_MENU_MODEL (submenu));
 
   for (i = 0; i < self->priv->builders->len; i++)
     {
diff --git a/src/tree/gb-tree.h b/src/tree/gb-tree.h
index 61b5936..d30ee54 100644
--- a/src/tree/gb-tree.h
+++ b/src/tree/gb-tree.h
@@ -38,6 +38,8 @@ typedef struct _GbTree        GbTree;
 typedef struct _GbTreeClass   GbTreeClass;
 typedef struct _GbTreePrivate GbTreePrivate;
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (GbTree, g_object_unref)
+
 struct _GbTree
 {
        GtkTreeView parent;


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