[gitg] Add keybindings for stage/unstage/discard



commit 3acfb98cb3311be679a29c27008cf5274b506f60
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Fri Jul 11 14:05:53 2014 +0200

    Add keybindings for stage/unstage/discard

 gitg/Makefile.am                       |    1 +
 gitg/commit/gitg-commit-paned.vala     |    4 +-
 gitg/commit/gitg-commit-sidebar.vala   |   60 +++++++++++++++
 gitg/commit/gitg-commit.vala           |  130 ++++++++++++++++++++++++-------
 gitg/resources/ui/gitg-commit-paned.ui |    2 +-
 5 files changed, 164 insertions(+), 33 deletions(-)
---
diff --git a/gitg/Makefile.am b/gitg/Makefile.am
index 13b71cc..cffa00b 100644
--- a/gitg/Makefile.am
+++ b/gitg/Makefile.am
@@ -66,6 +66,7 @@ gitg_gitg_VALASOURCES =                                               \
        gitg/history/gitg-history-paned.vala                    \
        gitg/commit/gitg-commit.vala                            \
        gitg/commit/gitg-commit-paned.vala                      \
+       gitg/commit/gitg-commit-sidebar.vala                    \
        gitg/commit/gitg-commit-dialog.vala                     \
        libgitg/libgitg-1.0.vapi                                \
        libgitg-ext/libgitg-ext-1.0.vapi
diff --git a/gitg/commit/gitg-commit-paned.vala b/gitg/commit/gitg-commit-paned.vala
index 3ae26bc..fcb760d 100644
--- a/gitg/commit/gitg-commit-paned.vala
+++ b/gitg/commit/gitg-commit-paned.vala
@@ -24,7 +24,7 @@ namespace GitgCommit
 class Paned : Gtk.Paned
 {
        [GtkChild (name = "tree_view_files")]
-       private Gitg.Sidebar d_tree_view_files;
+       private Sidebar d_tree_view_files;
 
        [GtkChild (name = "diff_view")]
        private Gitg.DiffView d_diff_view;
@@ -41,7 +41,7 @@ class Paned : Gtk.Paned
        [GtkChild (name = "button_discard")]
        private Gtk.Button d_button_discard;
 
-       public Gitg.Sidebar sidebar
+       public Sidebar sidebar
        {
                get { return d_tree_view_files; }
        }
diff --git a/gitg/commit/gitg-commit-sidebar.vala b/gitg/commit/gitg-commit-sidebar.vala
new file mode 100644
index 0000000..d07b13e
--- /dev/null
+++ b/gitg/commit/gitg-commit-sidebar.vala
@@ -0,0 +1,60 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2014 - Jesse van den Kieboom
+ *
+ * gitg 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace GitgCommit
+{
+
+class Sidebar : Gitg.Sidebar
+{
+       [Signal(action = true)]
+       public signal void stage_selection();
+
+       [Signal(action = true)]
+       public signal void unstage_selection();
+
+       [Signal(action = true)]
+       public signal void discard_selection();
+
+       construct
+       {
+               unowned Gtk.BindingSet binding_set = Gtk.BindingSet.by_class(get_class());
+
+               Gtk.BindingEntry.add_signal(binding_set,
+                                           Gdk.Key.s,
+                                           Gdk.ModifierType.CONTROL_MASK,
+                                           "stage-selection",
+                                           0);
+
+               Gtk.BindingEntry.add_signal(binding_set,
+                                           Gdk.Key.u,
+                                           Gdk.ModifierType.CONTROL_MASK,
+                                           "unstage-selection",
+                                           0);
+
+               Gtk.BindingEntry.add_signal(binding_set,
+                                           Gdk.Key.d,
+                                           Gdk.ModifierType.CONTROL_MASK,
+                                           "discard-selection",
+                                           0);
+       }
+}
+
+}
+
+// ex: ts=4 noet
diff --git a/gitg/commit/gitg-commit.vala b/gitg/commit/gitg-commit.vala
index ca8ad59..9809fb8 100644
--- a/gitg/commit/gitg-commit.vala
+++ b/gitg/commit/gitg-commit.vala
@@ -33,11 +33,20 @@ namespace GitgCommit
 
                private class SidebarFile : Object, Gitg.SidebarItem
                {
+                       public enum Type
+                       {
+                               STAGED,
+                               UNSTAGED,
+                               UNTRACKED
+                       }
+
                        Gitg.StageStatusFile d_file;
+                       Type d_type;
 
-                       public SidebarFile(Gitg.StageStatusFile f)
+                       public SidebarFile(Gitg.StageStatusFile f, Type type)
                        {
                                d_file = f;
+                               d_type = type;
                        }
 
                        public Gitg.StageStatusFile file
@@ -50,6 +59,11 @@ namespace GitgCommit
                                owned get { return d_file.path; }
                        }
 
+                       public Type stage_type
+                       {
+                               get { return d_type; }
+                       }
+
                        private string? icon_for_status(Ggit.StatusFlags status)
                        {
                                if ((status & (Ggit.StatusFlags.INDEX_NEW |
@@ -323,14 +337,15 @@ namespace GitgCommit
 
                private SidebarFile? append_files(Gitg.SidebarStore      model,
                                                  Gitg.StageStatusFile[] files,
+                                                 SidebarFile.Type       type,
                                                  Gitg.StageStatusFile?  current,
                                                  StageUnstageCallback?  callback)
                {
                        SidebarFile? citem = null;
-       
+
                        foreach (var f in files)
                        {
-                               var item = new SidebarFile(f);
+                               var item = new SidebarFile(f, type);
 
                                if (current != null && f.path == current.path)
                                {
@@ -445,6 +460,7 @@ namespace GitgCommit
                                {
                                        current_staged = append_files(model,
                                                                      staged,
+                                                                     SidebarFile.Type.STAGED,
                                                                      currentfile,
                                                                      on_staged_activated);
                                }
@@ -461,6 +477,7 @@ namespace GitgCommit
                                {
                                        current_unstaged = append_files(model,
                                                                        unstaged,
+                                                                       SidebarFile.Type.UNSTAGED,
                                                                        currentfile,
                                                                        on_unstaged_activated);
                                }
@@ -475,7 +492,11 @@ namespace GitgCommit
                                }
                                else
                                {
-                                       append_files(model, untracked, null, on_unstaged_activated);
+                                       append_files(model,
+                                                    untracked,
+                                                    SidebarFile.Type.UNTRACKED,
+                                                    null,
+                                                    on_unstaged_activated);
                                }
 
                                model.end_header();
@@ -967,6 +988,36 @@ namespace GitgCommit
                        return false;
                }
 
+               private void on_discard_menu_activated(SidebarFile f)
+               {
+                       var primary = _("Discard changes");
+                       var secondary = _("Are you sure you want to permanently discard all changes made to 
the file `%s'?").printf(f.file.path);
+
+                       var q = new GitgExt.UserQuery();
+
+                       q.title = primary;
+                       q.message = secondary;
+                       q.message_type = Gtk.MessageType.QUESTION;
+
+                       q.responses = new GitgExt.UserQueryResponse[] {
+                               new GitgExt.UserQueryResponse(_("Discard"), Gtk.ResponseType.OK),
+                               new GitgExt.UserQueryResponse(_("_Cancel"), Gtk.ResponseType.CANCEL)
+                       };
+
+                       q.default_response = Gtk.ResponseType.OK;
+
+                       q.response.connect((w, r) => {
+                               if (r == Gtk.ResponseType.OK)
+                               {
+                                       return do_discard_file(q, f.file);
+                               }
+
+                               return true;
+                       });
+
+                       application.user_query(q);
+               }
+
                private void do_populate_menu(Gtk.Menu menu)
                {
                        var f = d_main.sidebar.get_selected_item<SidebarFile>();
@@ -976,38 +1027,34 @@ namespace GitgCommit
                                return;
                        }
 
-                       if (!d_current_staged)
+                       if (f.stage_type == SidebarFile.Type.UNSTAGED ||
+                           f.stage_type == SidebarFile.Type.UNTRACKED)
                        {
-                               var discard = new Gtk.MenuItem.with_label(_("Discard changes"));
-                               menu.append(discard);
-
-                               discard.activate.connect(() => {
-                                       var primary = _("Discard changes");
-                                       var secondary = _("Are you sure you want to permanently discard all 
changes made to the file `%s'?").printf(f.file.path);
-
-                                       var q = new GitgExt.UserQuery();
-
-                                       q.title = primary;
-                                       q.message = secondary;
-                                       q.message_type = Gtk.MessageType.QUESTION;
+                               var stage = new Gtk.MenuItem.with_mnemonic(_("_Stage changes"));
+                               menu.append(stage);
 
-                                       q.responses = new GitgExt.UserQueryResponse[] {
-                                               new GitgExt.UserQueryResponse(_("Discard"), 
Gtk.ResponseType.OK),
-                                               new GitgExt.UserQueryResponse(_("_Cancel"), 
Gtk.ResponseType.CANCEL)
-                                       };
+                               stage.activate.connect(() => {
+                                       on_unstaged_activated(f.file, 2);
+                               });
+                       }
 
-                                       q.default_response = Gtk.ResponseType.OK;
+                       if (f.stage_type == SidebarFile.Type.STAGED)
+                       {
+                               var stage = new Gtk.MenuItem.with_mnemonic(_("_Unstage changes"));
+                               menu.append(stage);
 
-                                       q.response.connect((w, r) => {
-                                               if (r == Gtk.ResponseType.OK)
-                                               {
-                                                       return do_discard_file(q, f.file);
-                                               }
+                               stage.activate.connect(() => {
+                                       on_staged_activated(f.file, 2);
+                               });
+                       }
 
-                                               return true;
-                                       });
+                       if (f.stage_type == SidebarFile.Type.UNSTAGED)
+                       {
+                               var discard = new Gtk.MenuItem.with_mnemonic(_("_Discard changes"));
+                               menu.append(discard);
 
-                                       application.user_query(q);
+                               discard.activate.connect(() => {
+                                       on_discard_menu_activated(f);
                                });
                        }
                }
@@ -1027,6 +1074,29 @@ namespace GitgCommit
                                d_main.diff_view.diff = null;
                        });
 
+                       d_main.sidebar.stage_selection.connect(() => {
+                               var sel = d_main.sidebar.get_selected_item<SidebarFile>();
+
+                               if (sel != null && (sel.stage_type == SidebarFile.Type.UNSTAGED ||
+                                                   sel.stage_type == SidebarFile.Type.UNTRACKED))
+                               {
+                                       on_unstaged_activated(sel.file, 2);
+                               }
+                       });
+
+                       d_main.sidebar.unstage_selection.connect(() => {
+                               var sel = d_main.sidebar.get_selected_item<SidebarFile>();
+
+                               if (sel != null && sel.stage_type == SidebarFile.Type.STAGED)
+                               {
+                                       on_staged_activated(sel.file, 2);
+                               }
+                       });
+
+                       d_main.sidebar.discard_selection.connect(() => {
+                               
+                       });
+
                        d_main.button_commit.clicked.connect(() => {
                                on_commit_clicked();
                        });
diff --git a/gitg/resources/ui/gitg-commit-paned.ui b/gitg/resources/ui/gitg-commit-paned.ui
index fb8efef..f8bd7eb 100644
--- a/gitg/resources/ui/gitg-commit-paned.ui
+++ b/gitg/resources/ui/gitg-commit-paned.ui
@@ -25,7 +25,7 @@
           <class name="sidebar"/>
         </style>
         <child>
-          <object class="GitgSidebar" id="tree_view_files">
+          <object class="GitgCommitSidebar" id="tree_view_files">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <property name="headers_visible">False</property>


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