[gitg] Handle selection on the commit view



commit 2b61f0ae7aca01460978425f9dd33251304357d1
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Dec 15 16:07:10 2015 +0100

    Handle selection on the commit view

 gitg/resources/ui/gitg-commit-paned.ui |    1 +
 libgitg/gitg-diff-view-file.vala       |   12 ++++-
 libgitg/gitg-diff-view-hunk.vala       |   64 +++++++++++++++++++++++++++++++-
 libgitg/gitg-diff-view.vala            |    9 ++++-
 4 files changed, 80 insertions(+), 6 deletions(-)
---
diff --git a/gitg/resources/ui/gitg-commit-paned.ui b/gitg/resources/ui/gitg-commit-paned.ui
index e374edc..9534d46 100644
--- a/gitg/resources/ui/gitg-commit-paned.ui
+++ b/gitg/resources/ui/gitg-commit-paned.ui
@@ -45,6 +45,7 @@
             <property name="hexpand">True</property>
             <property name="vexpand">True</property>
             <property name="can_focus">True</property>
+            <property name="handle_selection">True</property>
           </object>
         </child>
         <child>
diff --git a/libgitg/gitg-diff-view-file.vala b/libgitg/gitg-diff-view-file.vala
index f1a0b4c..430a2b2 100644
--- a/libgitg/gitg-diff-view-file.vala
+++ b/libgitg/gitg-diff-view-file.vala
@@ -86,9 +86,15 @@ class Gitg.DiffViewFile : Gtk.Grid
                construct set;
        }
 
-       public DiffViewFile(Ggit.DiffDelta delta)
+       public bool handle_selection
        {
-               Object(delta: delta);
+               get;
+               construct set;
+       }
+
+       public DiffViewFile(Ggit.DiffDelta delta, bool handle_selection)
+       {
+               Object(delta: delta, handle_selection: handle_selection);
        }
 
        protected override void constructed()
@@ -119,7 +125,7 @@ class Gitg.DiffViewFile : Gtk.Grid
 
        public void add_hunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
        {
-               var widget = new Gitg.DiffViewHunk(hunk, lines);
+               var widget = new Gitg.DiffViewHunk(hunk, lines, handle_selection);
                widget.show();
 
                d_diff_stat_file.added += widget.added;
diff --git a/libgitg/gitg-diff-view-hunk.vala b/libgitg/gitg-diff-view-hunk.vala
index 3994a18..0e4c4e7 100644
--- a/libgitg/gitg-diff-view-hunk.vala
+++ b/libgitg/gitg-diff-view-hunk.vala
@@ -25,6 +25,8 @@ class Gitg.DiffViewHunk : Gtk.Grid
        [GtkChild( name = "sourceview_hunk" )]
        private Gtk.SourceView d_sourceview_hunk;
 
+       private string d_selection_category = "selection";
+
        public Ggit.DiffHunk hunk
        {
                get;
@@ -37,9 +39,15 @@ class Gitg.DiffViewHunk : Gtk.Grid
                construct set;
        }
 
-       public DiffViewHunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
+       public bool handle_selection
        {
-               Object(hunk: hunk, lines: lines);
+               get;
+               construct set;
+       }
+
+       public DiffViewHunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines, bool handle_selection)
+       {
+               Object(hunk: hunk, lines: lines, handle_selection: handle_selection);
        }
 
        private uint d_added;
@@ -116,6 +124,16 @@ class Gitg.DiffViewHunk : Gtk.Grid
                d_new_lines.notify["size"].connect(update_top_window_size);
                d_sym_lines.notify["size"].connect(update_top_window_size);
 
+               if (handle_selection)
+               {
+                       var selection_attributes = new Gtk.SourceMarkAttributes();
+
+                       selection_attributes.background = Gdk.RGBA() { red = 168.0 / 255.0, green = 207.0 / 
255.0, blue = 214.0 / 255.0, alpha = 1.0 };
+                       d_sourceview_hunk.set_mark_attributes(d_selection_category, selection_attributes, 0);
+
+                       d_sourceview_hunk.button_release_event.connect(button_release_event_on_view);
+               }
+
                update_hunk_label();
                update_lines();
 
@@ -126,6 +144,48 @@ class Gitg.DiffViewHunk : Gtk.Grid
                update_top_window_size();
        }
 
+       private bool button_release_event_on_view(Gdk.EventButton event)
+       {
+               var text_view = d_sourceview_hunk as Gtk.TextView;
+               var win = text_view.get_window(Gtk.TextWindowType.TEXT);
+               int x, y, width, height;
+
+               width = win.get_width();
+               height = win.get_height();
+
+               var pointer = Gdk.Display.get_default().get_device_manager().get_client_pointer();
+               win.get_device_position(pointer, out x, out y, null);
+
+               if (x < 0 || y < 0 || x > width || y > height)
+               {
+                       return false;
+               }
+
+               int win_x, win_y;
+               text_view.window_to_buffer_coords(Gtk.TextWindowType.TEXT, x, y, out win_x, out win_y);
+
+               Gtk.TextIter iter;
+               text_view.get_iter_at_location(out iter, win_x, win_y);
+
+               iter.set_line_offset(0);
+               var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
+               var marks = buffer.get_source_marks_at_iter(iter, d_selection_category);
+               if (marks != null)
+               {
+                       Gtk.TextIter end;
+
+                       end = iter;
+                       end.forward_to_line_end();
+                       buffer.remove_source_marks(iter, end, d_selection_category);
+               }
+               else
+               {
+                       buffer.create_source_mark(null, d_selection_category, iter);
+               }
+
+               return false;
+       }
+
        private void update_top_window_size()
        {
                int minheight, natheight;
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index 407c167..f21efec 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -183,6 +183,13 @@ public class Gitg.DiffView : Gtk.Grid
                default = 3;
        }
 
+       public bool handle_selection
+       {
+               get;
+               construct set;
+               default = false;
+       }
+
        private ulong d_expanded_notify;
        private ulong d_parent_commit_notify;
 
@@ -324,7 +331,7 @@ public class Gitg.DiffView : Gtk.Grid
 
                                        add_file();
 
-                                       current_file = new Gitg.DiffViewFile(delta);
+                                       current_file = new Gitg.DiffViewFile(delta, handle_selection);
                                        return 0;
                                },
 


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