[gnome-builder] terminal: add new-terminal-in-dir action



commit e8696caf45f0ef0232f1bee676e1a5ff5debc7b5
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jan 15 04:22:52 2018 -0800

    terminal: add new-terminal-in-dir action
    
    This allows opening a terminal in the same directory as a
    given file. Right click on the file and select
    "New terminal in directory".
    
    Fixes #57

 src/plugins/terminal/gb-terminal-view-private.h    |  2 ++
 src/plugins/terminal/gb-terminal-view.c            | 19 ++++++++++++++-
 src/plugins/terminal/gb-terminal-workbench-addin.c | 27 ++++++++++++++++++++++
 src/plugins/terminal/gtk/menus.ui                  |  9 ++++++++
 4 files changed, 56 insertions(+), 1 deletion(-)
---
diff --git a/src/plugins/terminal/gb-terminal-view-private.h b/src/plugins/terminal/gb-terminal-view-private.h
index c1eba5864..c8263807f 100644
--- a/src/plugins/terminal/gb-terminal-view-private.h
+++ b/src/plugins/terminal/gb-terminal-view-private.h
@@ -47,6 +47,8 @@ struct _GbTerminalView
 
   gchar               *selection_buffer;
 
+  gchar               *cwd;
+
   VtePty              *pty;
 
   gint64               last_respawn;
diff --git a/src/plugins/terminal/gb-terminal-view.c b/src/plugins/terminal/gb-terminal-view.c
index 06b390afd..8667c187a 100644
--- a/src/plugins/terminal/gb-terminal-view.c
+++ b/src/plugins/terminal/gb-terminal-view.c
@@ -37,6 +37,7 @@ G_DEFINE_TYPE (GbTerminalView, gb_terminal_view, IDE_TYPE_LAYOUT_VIEW)
 
 enum {
   PROP_0,
+  PROP_CWD,
   PROP_MANAGE_SPAWN,
   PROP_PTY,
   PROP_RUNTIME,
@@ -253,7 +254,6 @@ gb_terminal_respawn (GbTerminalView *self,
   ide_subprocess_launcher_set_flags (launcher, 0);
   ide_subprocess_launcher_set_run_on_host (launcher, self->run_on_host);
   ide_subprocess_launcher_set_clear_env (launcher, FALSE);
-  ide_subprocess_launcher_set_cwd (launcher, workpath);
   ide_subprocess_launcher_push_argv (launcher, shell);
   ide_subprocess_launcher_take_stdin_fd (launcher, tty_fd);
   ide_subprocess_launcher_take_stdout_fd (launcher, stdout_fd);
@@ -262,6 +262,11 @@ gb_terminal_respawn (GbTerminalView *self,
   ide_subprocess_launcher_setenv (launcher, "INSIDE_GNOME_BUILDER", PACKAGE_VERSION, TRUE);
   ide_subprocess_launcher_setenv (launcher, "SHELL", shell, TRUE);
 
+  if (self->cwd != NULL)
+    ide_subprocess_launcher_set_cwd (launcher, self->cwd);
+  else
+    ide_subprocess_launcher_set_cwd (launcher, workpath);
+
   if (pipeline != NULL)
     {
       ide_subprocess_launcher_setenv (launcher, "BUILDDIR", ide_build_pipeline_get_builddir (pipeline), 
TRUE);
@@ -498,6 +503,7 @@ gb_terminal_view_finalize (GObject *object)
   GbTerminalView *self = GB_TERMINAL_VIEW (object);
 
   g_clear_object (&self->save_as_file_top);
+  g_clear_pointer (&self->cwd, g_free);
   g_clear_pointer (&self->selection_buffer, g_free);
   g_clear_object (&self->pty);
   g_clear_object (&self->runtime);
@@ -546,6 +552,10 @@ gb_terminal_view_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_CWD:
+      self->cwd = g_value_dup_string (value);
+      break;
+
     case PROP_MANAGE_SPAWN:
       self->manage_spawn = g_value_get_boolean (value);
       break;
@@ -590,6 +600,13 @@ gb_terminal_view_class_init (GbTerminalViewClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GbTerminalView, top_scrollbar);
   gtk_widget_class_bind_template_child (widget_class, GbTerminalView, terminal_overlay_top);
 
+  properties [PROP_CWD] =
+    g_param_spec_string ("cwd",
+                         "CWD",
+                         "The directory to spawn the terminal in",
+                         NULL,
+                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
   properties [PROP_MANAGE_SPAWN] =
     g_param_spec_boolean ("manage-spawn",
                           "Manage Spawn",
diff --git a/src/plugins/terminal/gb-terminal-workbench-addin.c 
b/src/plugins/terminal/gb-terminal-workbench-addin.c
index b927a3c23..c19c35292 100644
--- a/src/plugins/terminal/gb-terminal-workbench-addin.c
+++ b/src/plugins/terminal/gb-terminal-workbench-addin.c
@@ -72,6 +72,7 @@ new_terminal_activate (GSimpleAction *action,
                        gpointer       user_data)
 {
   GbTerminalWorkbenchAddin *self = user_data;
+  g_autofree gchar *cwd = NULL;
   GbTerminalView *view;
   IdePerspective *perspective;
   IdeRuntime *runtime = NULL;
@@ -91,7 +92,31 @@ new_terminal_activate (GSimpleAction *action,
   perspective = ide_workbench_get_perspective_by_name (self->workbench, "editor");
   ide_workbench_set_visible_perspective (self->workbench, perspective);
 
+  if (g_strcmp0 (name, "new-terminal-in-dir") == 0)
+    {
+      IdeLayoutView *editor;
+
+      editor = ide_editor_perspective_get_active_view (IDE_EDITOR_PERSPECTIVE (perspective));
+
+      if (IDE_IS_EDITOR_VIEW (editor))
+        {
+          IdeBuffer *buffer;
+
+          buffer = ide_editor_view_get_buffer (IDE_EDITOR_VIEW (editor));
+
+          if (buffer != NULL)
+            {
+              IdeFile *file = ide_buffer_get_file (buffer);
+              GFile *gfile = ide_file_get_file (file);
+              g_autoptr(GFile) parent = g_file_get_parent (gfile);
+
+              cwd = g_file_get_path (parent);
+            }
+        }
+    }
+
   view = g_object_new (GB_TYPE_TERMINAL_VIEW,
+                       "cwd", cwd,
                        "run-on-host", run_on_host,
                        "runtime", runtime,
                        "visible", TRUE,
@@ -246,6 +271,7 @@ gb_terminal_workbench_addin_load (IdeWorkbenchAddin *addin,
   static const GActionEntry actions[] = {
     { "new-terminal", new_terminal_activate },
     { "new-terminal-in-runtime", new_terminal_activate },
+    { "new-terminal-in-dir", new_terminal_activate },
     { "debug-terminal", new_terminal_activate },
   };
 
@@ -310,6 +336,7 @@ gb_terminal_workbench_addin_unload (IdeWorkbenchAddin *addin,
 
   g_action_map_remove_action (G_ACTION_MAP (self->workbench), "new-terminal");
   g_action_map_remove_action (G_ACTION_MAP (self->workbench), "new-terminal-in-runtime");
+  g_action_map_remove_action (G_ACTION_MAP (self->workbench), "new-terminal-in-dir");
 
   context = ide_workbench_get_context (workbench);
 
diff --git a/src/plugins/terminal/gtk/menus.ui b/src/plugins/terminal/gtk/menus.ui
index d4837abc3..d4aa602a6 100644
--- a/src/plugins/terminal/gtk/menus.ui
+++ b/src/plugins/terminal/gtk/menus.ui
@@ -47,4 +47,13 @@
       </item>
     </section>
   </menu>
+  <menu id="ide-source-view-popup-menu">
+    <section id="ide-source-view-popup-menu-terminal-section">
+      <attribute name="after">ide-source-view-popup-menu-zoom-section</attribute>
+      <item>
+        <attribute name="label" translatable="yes">New terminal in directory</attribute>
+        <attribute name="action">win.new-terminal-in-dir</attribute>
+      </item>
+    </section>
+  </menu>
 </interface>


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