[gnome-builder] tree: add basic plumbing to create new files



commit 880eab134df62991cc46ca0298302738ce1a24a8
Author: Christian Hergert <christian hergert me>
Date:   Fri Apr 10 17:16:52 2015 -0700

    tree: add basic plumbing to create new files
    
    This isn't ready yet for general consumption, but it is almost there.
    We still need to add the file to the project and update the file tree.
    But to do so, we need to do a bit more plumbing.

 data/gtk/menus.ui                          |   19 ++
 data/ui/gb-new-file-popover.ui             |   57 ++++
 src/gnome-builder.mk                       |    2 +
 src/project-tree/gb-new-file-popover.c     |  388 ++++++++++++++++++++++++++++
 src/project-tree/gb-new-file-popover.h     |   39 +++
 src/project-tree/gb-project-tree-actions.c |  146 +++++++++++
 src/project-tree/gb-project-tree-builder.c |    3 +
 src/resources/gnome-builder.gresource.xml  |    1 +
 src/tree/gb-tree-node.c                    |   18 ++
 src/tree/gb-tree-node.h                    |   34 ++--
 10 files changed, 691 insertions(+), 16 deletions(-)
---
diff --git a/data/gtk/menus.ui b/data/gtk/menus.ui
index caa947a..163821c 100644
--- a/data/gtk/menus.ui
+++ b/data/gtk/menus.ui
@@ -136,6 +136,25 @@
       </submenu>
     </section>
   </menu>
+  <menu id="gb-project-tree-new">
+    <section id="gb-project-tree-new-section">
+      <submenu>
+        <attribute name="label" translatable="yes">_New</attribute>
+        <section>
+          <item>
+            <attribute name="label" translatable="yes">_Empty File</attribute>
+            <attribute name="action">project-tree.new-file</attribute>
+          </item>
+        </section>
+      </submenu>
+    </section>
+    <section id="gb-project-tree-new-folder-section">
+      <item>
+        <attribute name="label" translatable="yes">_New Folder</attribute>
+        <attribute name="action">project-tree.new-directory</attribute>
+      </item>
+    </section>
+  </menu>
   <menu id="gb-project-tree-open">
     <section id="gb-project-tree-open-section">
       <item>
diff --git a/data/ui/gb-new-file-popover.ui b/data/ui/gb-new-file-popover.ui
new file mode 100644
index 0000000..6e69057
--- /dev/null
+++ b/data/ui/gb-new-file-popover.ui
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.16 -->
+  <template class="GbNewFilePopover" parent="GtkPopover">
+    <child>
+      <object class="GtkBox">
+        <property name="border-width">12</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">6</property>
+        <property name="visible">true</property>
+        <child>
+          <object class="GtkLabel" id="title">
+            <property name="label" translatable="yes">File Name</property>
+            <property name="xalign">0.0</property>
+            <property name="visible">true</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="orientation">horizontal</property>
+            <property name="spacing">9</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkEntry" id="entry">
+                <property name="width-chars">20</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton" id="button">
+                <property name="sensitive">false</property>
+                <property name="label" translatable="yes">_Create</property>
+                <property name="use-underline">true</property>
+                <property name="visible">true</property>
+                <style>
+                  <class name="suggested-action"/>
+                </style>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel" id="message">
+            <property name="xalign">0.0</property>
+            <property name="visible">true</property>
+            <style>
+              <class name="dim-label"/>
+            </style>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index ae22244..22fdefa 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -94,6 +94,8 @@ libgnome_builder_la_SOURCES = \
        src/preferences/gb-preferences-switch.h \
        src/preferences/gb-preferences-window.c \
        src/preferences/gb-preferences-window.h \
+       src/project-tree/gb-new-file-popover.c \
+       src/project-tree/gb-new-file-popover.h \
        src/project-tree/gb-project-tree.c \
        src/project-tree/gb-project-tree.h \
        src/project-tree/gb-project-tree-actions.c \
diff --git a/src/project-tree/gb-new-file-popover.c b/src/project-tree/gb-new-file-popover.c
new file mode 100644
index 0000000..850f8fc
--- /dev/null
+++ b/src/project-tree/gb-new-file-popover.c
@@ -0,0 +1,388 @@
+/* gb-new-file-popover.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-new-file-popover.h"
+#include "gb-string.h"
+#include "gb-widget.h"
+
+struct _GbNewFilePopover
+{
+  GtkPopover    parent_instance;
+
+  GFileType     file_type;
+  GFile        *directory;
+  GCancellable *cancellable;
+
+  GtkButton    *button;
+  GtkEntry     *entry;
+  GtkLabel     *message;
+  GtkLabel     *title;
+};
+
+G_DEFINE_TYPE (GbNewFilePopover, gb_new_file_popover, GTK_TYPE_POPOVER)
+
+enum {
+  PROP_0,
+  PROP_DIRECTORY,
+  PROP_FILE_TYPE,
+  LAST_PROP
+};
+
+enum {
+  CREATE_FILE,
+  LAST_SIGNAL
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+static guint       gSignals [LAST_SIGNAL];
+
+static void
+gb_new_file_popover__button_clicked (GbNewFilePopover *self,
+                                     GtkButton        *button)
+{
+  g_autoptr(GFile) file = NULL;
+  const gchar *path;
+
+  g_assert (GB_IS_NEW_FILE_POPOVER (self));
+  g_assert (GTK_IS_BUTTON (button));
+
+  if (self->directory == NULL)
+    return;
+
+  path = gtk_entry_get_text (self->entry);
+  if (gb_str_empty0 (path))
+    return;
+
+  file = g_file_get_child (self->directory, path);
+
+  g_signal_emit (self, gSignals [CREATE_FILE], 0, file, self->file_type);
+}
+
+static void
+gb_new_file_popover__entry_activate (GbNewFilePopover *self,
+                                     GtkEntry         *entry)
+{
+  g_assert (GB_IS_NEW_FILE_POPOVER (self));
+  g_assert (GTK_IS_ENTRY (entry));
+
+  if (gtk_widget_get_sensitive (GTK_WIDGET (self->button)))
+    gtk_widget_activate (GTK_WIDGET (self->button));
+}
+
+static void
+gb_new_file_popover__query_info_cb (GObject      *object,
+                                    GAsyncResult *result,
+                                    gpointer      user_data)
+{
+  GFile *file = (GFile *)object;
+  g_autoptr(GFileInfo) file_info = NULL;
+  g_autoptr(GbNewFilePopover) self = user_data;
+  g_autoptr(GError) error = NULL;
+  GFileType file_type;
+
+  file_info = g_file_query_info_finish (file, result, &error);
+
+  if (file_info == NULL &&
+      g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+    return;
+
+  if ((file_info == NULL) &&
+      g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+    {
+      gtk_label_set_label (self->message, NULL);
+      gtk_widget_set_sensitive (GTK_WIDGET (self->button), TRUE);
+      return;
+    }
+
+  if (file_info == NULL)
+    {
+      gtk_label_set_label (self->message, error->message);
+      return;
+    }
+
+  file_type = g_file_info_get_file_type (file_info);
+
+  if (file_type == G_FILE_TYPE_DIRECTORY)
+    gtk_label_set_label (self->message,
+                         _("A folder with that name already exists."));
+  else
+    gtk_label_set_label (self->message,
+                         _("A file with that name already exists."));
+
+  gtk_widget_set_sensitive (GTK_WIDGET (self->button), FALSE);
+}
+
+static void
+gb_new_file_popover_check_exists (GbNewFilePopover *self,
+                                  GFile            *directory,
+                                  const gchar      *path)
+{
+  g_autoptr(GFile) child = NULL;
+
+  g_assert (GB_IS_NEW_FILE_POPOVER (self));
+  g_assert (!directory || G_IS_FILE (directory));
+
+  if (self->cancellable != NULL)
+    {
+      if (!g_cancellable_is_cancelled (self->cancellable))
+        g_cancellable_cancel (self->cancellable);
+      g_clear_object (&self->cancellable);
+    }
+
+  gtk_label_set_label (self->message, NULL);
+  gtk_widget_set_sensitive (GTK_WIDGET (self->button), FALSE);
+
+  if (directory == NULL)
+    return;
+
+  if (gb_str_empty0 (path))
+    return;
+
+  child = g_file_get_child (directory, path);
+
+  self->cancellable = g_cancellable_new ();
+
+  g_file_query_info_async (child,
+                           G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                           G_FILE_QUERY_INFO_NONE,
+                           G_PRIORITY_DEFAULT,
+                           self->cancellable,
+                           gb_new_file_popover__query_info_cb,
+                           g_object_ref (self));
+
+}
+
+static void
+gb_new_file_popover__entry_changed (GbNewFilePopover *self,
+                                    GtkEntry         *entry)
+{
+  const gchar *text;
+
+  g_assert (GB_IS_NEW_FILE_POPOVER (self));
+  g_assert (GTK_IS_ENTRY (entry));
+
+  text = gtk_entry_get_text (entry);
+
+  gtk_widget_set_sensitive (GTK_WIDGET (self->button), !gb_str_empty0 (text));
+
+  gb_new_file_popover_check_exists (self, self->directory, text);
+}
+
+static void
+gb_new_file_popover_finalize (GObject *object)
+{
+  GbNewFilePopover *self = (GbNewFilePopover *)object;
+
+  if (self->cancellable && !g_cancellable_is_cancelled (self->cancellable))
+    g_cancellable_cancel (self->cancellable);
+
+  g_clear_object (&self->cancellable);
+  g_clear_object (&self->directory);
+
+  G_OBJECT_CLASS (gb_new_file_popover_parent_class)->finalize (object);
+}
+
+static void
+gb_new_file_popover_get_property (GObject    *object,
+                                  guint       prop_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
+{
+  GbNewFilePopover *self = GB_NEW_FILE_POPOVER(object);
+
+  switch (prop_id)
+    {
+    case PROP_DIRECTORY:
+      g_value_set_object (value, gb_new_file_popover_get_directory (self));
+      break;
+
+    case PROP_FILE_TYPE:
+      g_value_set_enum (value, gb_new_file_popover_get_file_type (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+/**
+ * gb_new_file_popover_set_property:
+ * @object: (in): A #GObject.
+ * @prop_id: (in): The property identifier.
+ * @value: (in): The given property.
+ * @pspec: (in): A #ParamSpec.
+ *
+ * Set a given #GObject property.
+ */
+static void
+gb_new_file_popover_set_property (GObject      *object,
+                                  guint         prop_id,
+                                  const GValue *value,
+                                  GParamSpec   *pspec)
+{
+  GbNewFilePopover *self = GB_NEW_FILE_POPOVER(object);
+
+  switch (prop_id)
+    {
+    case PROP_DIRECTORY:
+      gb_new_file_popover_set_directory (self, g_value_get_object (value));
+      break;
+
+    case PROP_FILE_TYPE:
+      gb_new_file_popover_set_file_type (self, g_value_get_enum (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+static void
+gb_new_file_popover_class_init (GbNewFilePopoverClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gb_new_file_popover_finalize;
+  object_class->get_property = gb_new_file_popover_get_property;
+  object_class->set_property = gb_new_file_popover_set_property;
+
+  gParamSpecs [PROP_DIRECTORY] =
+    g_param_spec_object ("directory",
+                         _("Directory"),
+                         _("Directory"),
+                         G_TYPE_FILE,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_DIRECTORY,
+                                   gParamSpecs [PROP_DIRECTORY]);
+
+  gParamSpecs [PROP_FILE_TYPE] =
+    g_param_spec_enum ("file-type",
+                       _("File Type"),
+                       _("The file type to create."),
+                       G_TYPE_FILE_TYPE,
+                       G_FILE_TYPE_REGULAR,
+                       (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_FILE_TYPE,
+                                   gParamSpecs [PROP_FILE_TYPE]);
+
+  gSignals [CREATE_FILE] =
+    g_signal_new ("create-file",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0,
+                  NULL, NULL,
+                  g_cclosure_marshal_generic,
+                  G_TYPE_NONE,
+                  2,
+                  G_TYPE_FILE,
+                  G_TYPE_FILE_TYPE);
+
+  GB_WIDGET_CLASS_TEMPLATE (klass, "gb-new-file-popover.ui");
+
+  GB_WIDGET_CLASS_BIND (klass, GbNewFilePopover, button);
+  GB_WIDGET_CLASS_BIND (klass, GbNewFilePopover, entry);
+  GB_WIDGET_CLASS_BIND (klass, GbNewFilePopover, message);
+  GB_WIDGET_CLASS_BIND (klass, GbNewFilePopover, title);
+}
+
+static void
+gb_new_file_popover_init (GbNewFilePopover *self)
+{
+  self->file_type = G_FILE_TYPE_REGULAR;
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  g_signal_connect_object (self->entry,
+                           "activate",
+                           G_CALLBACK (gb_new_file_popover__entry_activate),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->entry,
+                           "changed",
+                           G_CALLBACK (gb_new_file_popover__entry_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->button,
+                           "clicked",
+                           G_CALLBACK (gb_new_file_popover__button_clicked),
+                           self,
+                           G_CONNECT_SWAPPED);
+}
+
+GFileType
+gb_new_file_popover_get_file_type (GbNewFilePopover *self)
+{
+  g_return_val_if_fail (GB_IS_NEW_FILE_POPOVER (self), 0);
+
+  return self->file_type;
+}
+
+void
+gb_new_file_popover_set_file_type (GbNewFilePopover *self,
+                                   GFileType         file_type)
+{
+  g_return_if_fail (GB_IS_NEW_FILE_POPOVER (self));
+  g_return_if_fail ((file_type == G_FILE_TYPE_REGULAR) ||
+                    (file_type == G_FILE_TYPE_DIRECTORY));
+
+  if (file_type != self->file_type)
+    {
+      self->file_type = file_type;
+
+      if (file_type == G_FILE_TYPE_REGULAR)
+        gtk_label_set_label (self->title, _("File Name"));
+      else
+        gtk_label_set_label (self->title, _("Folder Name"));
+
+      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_FILE_TYPE]);
+    }
+}
+
+void
+gb_new_file_popover_set_directory (GbNewFilePopover *self,
+                                   GFile            *directory)
+{
+  g_return_if_fail (GB_IS_NEW_FILE_POPOVER (self));
+  g_return_if_fail (G_IS_FILE (directory));
+
+  if (g_set_object (&self->directory, directory))
+    {
+      const gchar *path;
+
+      path = gtk_entry_get_text (self->entry);
+      gb_new_file_popover_check_exists (self, directory, path);
+      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_DIRECTORY]);
+    }
+}
+
+/**
+ * gb_new_file_popover_get_directory:
+ *
+ * Returns: (transfer none) (nullable): A #GFile or %NULL.
+ */
+GFile *
+gb_new_file_popover_get_directory (GbNewFilePopover *self)
+{
+  g_return_val_if_fail (GB_IS_NEW_FILE_POPOVER (self), NULL);
+
+  return self->directory;
+}
diff --git a/src/project-tree/gb-new-file-popover.h b/src/project-tree/gb-new-file-popover.h
new file mode 100644
index 0000000..106cdea
--- /dev/null
+++ b/src/project-tree/gb-new-file-popover.h
@@ -0,0 +1,39 @@
+/* gb-new-file-popover.h
+ *
+ * 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/>.
+ */
+
+#ifndef GB_NEW_FILE_POPOVER_H
+#define GB_NEW_FILE_POPOVER_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_NEW_FILE_POPOVER (gb_new_file_popover_get_type())
+
+G_DECLARE_FINAL_TYPE (GbNewFilePopover, gb_new_file_popover, GB, NEW_FILE_POPOVER, GtkPopover)
+
+GFileType  gb_new_file_popover_get_file_type (GbNewFilePopover *self);
+void       gb_new_file_popover_set_file_type (GbNewFilePopover *self,
+                                              GFileType         file_type);
+void       gb_new_file_popover_set_directory (GbNewFilePopover *self,
+                                              GFile            *directory);
+GFile     *gb_new_file_popover_get_directory (GbNewFilePopover *self);
+
+G_END_DECLS
+
+#endif /* GB_NEW_FILE_POPOVER_H */
diff --git a/src/project-tree/gb-project-tree-actions.c b/src/project-tree/gb-project-tree-actions.c
index cd94e3f..873a9f9 100644
--- a/src/project-tree/gb-project-tree-actions.c
+++ b/src/project-tree/gb-project-tree-actions.c
@@ -21,6 +21,7 @@
 
 #include "gb-editor-workspace.h"
 #include "gb-file-manager.h"
+#include "gb-new-file-popover.h"
 #include "gb-project-tree.h"
 #include "gb-project-tree-actions.h"
 #include "gb-project-tree-private.h"
@@ -228,8 +229,150 @@ gb_project_tree_actions_show_icons (GSimpleAction *action,
   g_simple_action_set_state (action, variant);
 }
 
+static void
+gb_project_tree_actions__make_directory_cb (GObject      *object,
+                                            GAsyncResult *result,
+                                            gpointer      user_data)
+{
+  GFile *file = (GFile *)object;
+  g_autoptr(GbProjectTree) self = user_data;
+  g_autoptr(GError) error = NULL;
+
+  if (!g_file_make_directory_finish (file, result, &error))
+    {
+      /* todo: show error messsage */
+    }
+
+  /* todo: add file to project/vcs/etc */
+  /* todo: reload portion of tree */
+}
+
+static void
+gb_project_tree_actions__create_cb (GObject      *object,
+                                    GAsyncResult *result,
+                                    gpointer      user_data)
+{
+  GFile *file = (GFile *)object;
+  g_autoptr(GbProjectTree) self = user_data;
+  g_autoptr(GError) error = NULL;
+  GbWorkbench *workbench;
+
+  if (!g_file_create_finish (file, result, &error))
+    {
+      /* todo: show error messsage */
+    }
+
+  workbench = gb_widget_get_workbench (GTK_WIDGET (self));
+  gb_workbench_open (workbench, file);
+
+  /* todo: add file to project/vcs/etc */
+  /* todo: reload portion of tree */
+}
+
+static void
+gb_project_tree_actions__popover_create_file_cb (GbProjectTree    *self,
+                                                 GFile            *file,
+                                                 GFileType         file_type,
+                                                 GbNewFilePopover *popover)
+{
+  g_assert (GB_IS_PROJECT_TREE (self));
+  g_assert (G_IS_FILE (file));
+  g_assert ((file_type == G_FILE_TYPE_DIRECTORY) ||
+            (file_type == G_FILE_TYPE_REGULAR));
+  g_assert (GB_IS_NEW_FILE_POPOVER (popover));
+
+  if (file_type == G_FILE_TYPE_DIRECTORY)
+    {
+      g_file_make_directory_async (file,
+                                   G_PRIORITY_DEFAULT,
+                                   NULL, /* cancellable */
+                                   gb_project_tree_actions__make_directory_cb,
+                                   g_object_ref (self));
+    }
+  else if (file_type == G_FILE_TYPE_REGULAR)
+    {
+      g_file_create_async (file,
+                           G_FILE_CREATE_NONE,
+                           G_PRIORITY_DEFAULT,
+                           NULL, /* cancellable */
+                           gb_project_tree_actions__create_cb,
+                           g_object_ref (self));
+    }
+  else
+    {
+      g_assert_not_reached ();
+    }
+
+  gtk_widget_destroy (GTK_WIDGET (popover));
+}
+
+static void
+gb_project_tree_actions_new (GbProjectTree *self,
+                             GFileType      file_type)
+{
+  GbTreeNode *selected;
+  GObject *item;
+  GtkPopover *popover;
+  GdkRectangle rect;
+  IdeProjectFile *project_file;
+  GFile *file;
+
+  g_assert (GB_IS_PROJECT_TREE (self));
+  g_assert ((file_type == G_FILE_TYPE_DIRECTORY) ||
+            (file_type == G_FILE_TYPE_REGULAR));
+
+  if (!(selected = gb_tree_get_selected (GB_TREE (self))) ||
+      !(item = gb_tree_node_get_item (selected)) ||
+      !IDE_IS_PROJECT_FILE (item) ||
+      !project_file_is_directory (item) ||
+      !(project_file = IDE_PROJECT_FILE (item)) ||
+      !(file = ide_project_file_get_file (project_file)))
+    return;
+
+  gb_tree_node_get_area (selected, &rect);
+
+  popover = g_object_new (GB_TYPE_NEW_FILE_POPOVER, NULL);
+  gtk_popover_set_relative_to (popover, GTK_WIDGET (self));
+  gtk_popover_set_pointing_to (popover, &rect);
+  gtk_popover_set_position (popover, GTK_POS_BOTTOM);
+  gb_new_file_popover_set_file_type (GB_NEW_FILE_POPOVER (popover), file_type);
+  gb_new_file_popover_set_directory (GB_NEW_FILE_POPOVER (popover), file);
+  g_signal_connect_object (popover,
+                           "create-file",
+                           G_CALLBACK (gb_project_tree_actions__popover_create_file_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+  gtk_widget_show (GTK_WIDGET (popover));
+}
+
+static void
+gb_project_tree_actions_new_directory (GSimpleAction *action,
+                                       GVariant      *variant,
+                                       gpointer       user_data)
+{
+  GbProjectTree *self = user_data;
+
+  g_assert (GB_IS_PROJECT_TREE (self));
+
+  gb_project_tree_actions_new (self, G_FILE_TYPE_DIRECTORY);
+}
+
+static void
+gb_project_tree_actions_new_file (GSimpleAction *action,
+                                  GVariant      *variant,
+                                  gpointer       user_data)
+{
+  GbProjectTree *self = user_data;
+
+  g_assert (GB_IS_PROJECT_TREE (self));
+
+  gb_project_tree_actions_new (self, G_FILE_TYPE_REGULAR);
+}
+
 static GActionEntry GbProjectTreeActions[] = {
   { "collapse-all-nodes",     gb_project_tree_actions_collapse_all_nodes },
+  { "new-directory",          gb_project_tree_actions_new_directory },
+  { "new-file",               gb_project_tree_actions_new_file },
   { "open",                   gb_project_tree_actions_open },
   { "open-containing-folder", gb_project_tree_actions_open_containing_folder },
   { "open-with",              gb_project_tree_actions_open_with, "s" },
@@ -280,6 +423,9 @@ gb_project_tree_actions_update (GbProjectTree *self)
   if (selection != NULL)
     item = gb_tree_node_get_item (selection);
 
+  action_set (group, "new-file",
+              "enabled", project_file_is_directory (item),
+              NULL);
   action_set (group, "open",
               "enabled", !project_file_is_directory (item),
               NULL);
diff --git a/src/project-tree/gb-project-tree-builder.c b/src/project-tree/gb-project-tree-builder.c
index bfd0cc7..876d35d 100644
--- a/src/project-tree/gb-project-tree-builder.c
+++ b/src/project-tree/gb-project-tree-builder.c
@@ -299,6 +299,9 @@ gb_project_tree_builder_node_popup (GbTreeBuilder *builder,
 
       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-new");
+      g_menu_prepend_section (menu, NULL, G_MENU_MODEL (submenu));
     }
 
   submenu = gtk_application_get_menu_by_id (app, "gb-project-tree-display-options");
diff --git a/src/resources/gnome-builder.gresource.xml b/src/resources/gnome-builder.gresource.xml
index 8a30e3f..9369d00 100644
--- a/src/resources/gnome-builder.gresource.xml
+++ b/src/resources/gnome-builder.gresource.xml
@@ -41,6 +41,7 @@
     <file alias="ui/gb-editor-view.ui">../../data/ui/gb-editor-view.ui</file>
     <file alias="ui/gb-editor-workspace.ui">../../data/ui/gb-editor-workspace.ui</file>
     <file alias="ui/gb-html-view.ui">../../data/ui/gb-html-view.ui</file>
+    <file alias="ui/gb-new-file-popover.ui">../../data/ui/gb-new-file-popover.ui</file>
     <file alias="ui/gb-new-project-dialog.ui">../../data/ui/gb-new-project-dialog.ui</file>
     <file alias="ui/gb-preferences-page-editor.ui">../../data/ui/gb-preferences-page-editor.ui</file>
     <file 
alias="ui/gb-preferences-page-experimental.ui">../../data/ui/gb-preferences-page-experimental.ui</file>
diff --git a/src/tree/gb-tree-node.c b/src/tree/gb-tree-node.c
index 3aada51..0bd464d 100644
--- a/src/tree/gb-tree-node.c
+++ b/src/tree/gb-tree-node.c
@@ -387,6 +387,24 @@ gb_tree_node_select (GbTreeNode  *node)
   gtk_tree_path_free (path);
 }
 
+void
+gb_tree_node_get_area (GbTreeNode   *node,
+                       GdkRectangle *area)
+{
+  GbTree *tree;
+  GtkTreeViewColumn *column;
+  GtkTreePath *path;
+
+  g_return_if_fail (GB_IS_TREE_NODE (node));
+  g_return_if_fail (area != NULL);
+
+  tree = gb_tree_node_get_tree (node);
+  path = gb_tree_node_get_path (node);
+  column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree), 0);
+  gtk_tree_view_get_cell_area (GTK_TREE_VIEW (tree), path, column, area);
+  gtk_tree_path_free (path);
+}
+
 /**
  * gb_tree_node_finalize:
  * @object: (in): A #GbTreeNode.
diff --git a/src/tree/gb-tree-node.h b/src/tree/gb-tree-node.h
index b93e40b..9d30994 100644
--- a/src/tree/gb-tree-node.h
+++ b/src/tree/gb-tree-node.h
@@ -55,24 +55,26 @@ struct _GbTreeNodeClass
 };
 
 GbTreeNode    *gb_tree_node_new           (void);
-void           gb_tree_node_append        (GbTreeNode  *node,
-                                           GbTreeNode  *child);
-const gchar   *gb_tree_node_get_icon_name (GbTreeNode  *node);
-GObject       *gb_tree_node_get_item      (GbTreeNode  *node);
-GbTreeNode    *gb_tree_node_get_parent    (GbTreeNode  *node);
-GtkTreePath   *gb_tree_node_get_path      (GbTreeNode  *node);
+void           gb_tree_node_append        (GbTreeNode   *node,
+                                           GbTreeNode   *child);
+const gchar   *gb_tree_node_get_icon_name (GbTreeNode   *node);
+GObject       *gb_tree_node_get_item      (GbTreeNode   *node);
+GbTreeNode    *gb_tree_node_get_parent    (GbTreeNode   *node);
+GtkTreePath   *gb_tree_node_get_path      (GbTreeNode   *node);
 GType          gb_tree_node_get_type      (void);
 void           gb_tree_node_prepend       (GbTreeNode  *node,
-                                           GbTreeNode  *child);
-void           gb_tree_node_remove        (GbTreeNode  *node,
-                                           GbTreeNode  *child);
-void           gb_tree_node_set_icon_name (GbTreeNode  *node,
-                                           const gchar *icon_name);
-void           gb_tree_node_set_item      (GbTreeNode  *node,
-                                           GObject     *item);
-void           gb_tree_node_expand        (GbTreeNode  *node,
-                                           gboolean     expand_ancestors);
-void           gb_tree_node_select        (GbTreeNode  *node);
+                                           GbTreeNode   *child);
+void           gb_tree_node_remove        (GbTreeNode   *node,
+                                           GbTreeNode   *child);
+void           gb_tree_node_set_icon_name (GbTreeNode   *node,
+                                           const gchar  *icon_name);
+void           gb_tree_node_set_item      (GbTreeNode   *node,
+                                           GObject      *item);
+void           gb_tree_node_expand        (GbTreeNode   *node,
+                                           gboolean      expand_ancestors);
+void           gb_tree_node_select        (GbTreeNode   *node);
+void           gb_tree_node_get_area      (GbTreeNode   *node,
+                                           GdkRectangle *area);
 
 G_END_DECLS
 


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