[gitg/wip/albfan/diff-images-as-text] Allow to diff images as text if its mime type supports it




commit e1984e38bf003047e3a039a3bf4f1d75995fc2d3
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Wed Oct 16 14:34:23 2019 +0200

    Allow to diff images as text if its mime type supports it

 libgitg/gitg-diff-view-file.vala            | 107 ++++++++++++++--------------
 libgitg/gitg-diff-view.vala                 |  43 ++++++-----
 libgitg/resources/ui/gitg-diff-view-file.ui |  83 ++++++++++++---------
 3 files changed, 124 insertions(+), 109 deletions(-)
---
diff --git a/libgitg/gitg-diff-view-file.vala b/libgitg/gitg-diff-view-file.vala
index 112085c3..1765e4ab 100644
--- a/libgitg/gitg-diff-view-file.vala
+++ b/libgitg/gitg-diff-view-file.vala
@@ -32,41 +32,21 @@ class Gitg.DiffViewFile : Gtk.Grid
        [GtkChild( name = "revealer_content" )]
        private unowned Gtk.Revealer d_revealer_content;
 
-       [GtkChild( name = "scrolled_window" )]
-       private unowned Gtk.ScrolledWindow? scrolled_window;
+       [GtkChild( name = "stack_switcher" )]
+       private unowned Gtk.StackSwitcher? d_stack_switcher;
 
-       private bool d_expanded;
+       [GtkChild( name = "stack_file_renderer" )]
+       private unowned Gtk.Stack? d_stack_file_renderer;
 
-       private Binding? d_vexpand_binding;
+       private bool d_expanded;
 
-       private DiffViewFileRenderer? _renderer;
+       public DiffViewFileRendererText? renderer_text {get; private set;}
 
-       public DiffViewFileRenderer? renderer
+       public void add_renderer(Gtk.Widget widget, string name, string title)
        {
-               owned get
-               {
-                       return _renderer;
-               }
-
-               construct set
-               {
-                       if (_renderer != value)
-                       {
-                               if (_renderer != null)
-                                       scrolled_window.remove(_renderer);
-
-                               _renderer = value;
-                               if (d_vexpand_binding != null)
-                               {
-                                       d_vexpand_binding.unbind();
-                                       d_vexpand_binding = null;
-                               }
-
-                               scrolled_window.add (_renderer);
-
-                               d_vexpand_binding = this.bind_property("vexpand", _renderer, "vexpand", 
BindingFlags.SYNC_CREATE);
-                       }
-               }
+               d_stack_file_renderer.add_titled(widget, name, title);
+               bool visible = d_stack_file_renderer.get_children().length() > 1;
+               d_stack_switcher.set_visible(visible);
        }
 
        public bool new_is_workdir { get; construct set; }
@@ -99,49 +79,53 @@ class Gitg.DiffViewFile : Gtk.Grid
                }
        }
 
-       public Ggit.DiffDelta? delta { get; construct set; }
-       public Repository? repository { get; construct set; }
+       public DiffViewFileInfo? info {get; construct set;}
 
-       public DiffViewFile(Repository? repository, Ggit.DiffDelta delta)
+       public DiffViewFile(DiffViewFileInfo? info)
        {
-               Object(repository: repository, delta: delta);
+               Object(info: info);
+               bind_property("vexpand", d_stack_file_renderer, "vexpand", BindingFlags.SYNC_CREATE);
        }
 
-       public DiffViewFile.text(DiffViewFileInfo info, bool handle_selection)
+       public void add_text_renderer(bool handle_selection)
        {
-               this(info.repository, info.delta);
-
-               this.renderer = new DiffViewFileRendererText(info, handle_selection);
-               this.renderer.show();
-
-               this.renderer.bind_property("added", d_diff_stat_file, "added");
-               this.renderer.bind_property("removed", d_diff_stat_file, "removed");
+               renderer_text = new DiffViewFileRendererText(info, handle_selection);
+               renderer_text.show();
+               var scrolled_window = new Gtk.ScrolledWindow (null, null);
+               scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
+               scrolled_window.add(renderer_text);
+               scrolled_window.show();
+
+               renderer_text.bind_property("added", d_diff_stat_file, "added");
+               renderer_text.bind_property("removed", d_diff_stat_file, "removed");
+               add_renderer(scrolled_window, "text", _("Text"));
        }
 
-       public DiffViewFile.binary(Repository? repository, Ggit.DiffDelta delta)
+       public void add_binary_renderer()
        {
-               this(repository, delta);
-
-               this.renderer = new DiffViewFileRendererBinary();
-               this.renderer.show();
+               var renderer = new DiffViewFileRendererBinary();
+               renderer.show();
+               add_renderer(renderer, "binary", _("Binary"));
 
-               d_diff_stat_file.hide();
+               //TODO: Only for text page
+               //d_diff_stat_file.hide();
        }
 
-       public DiffViewFile.image(Repository? repository, Ggit.DiffDelta delta)
+       public void add_image_renderer()
        {
-               this(repository, delta);
-
-               this.renderer = new DiffViewFileRendererImage(repository, delta);
-               this.renderer.show();
+               var renderer = new DiffViewFileRendererImage(info.repository, info.delta);
+               renderer.show();
+               add_renderer(renderer, "image", _("Image"));
 
-               d_diff_stat_file.hide();
+               //TODO: Only for text page
+               //d_diff_stat_file.hide();
        }
 
        protected override void constructed()
        {
                base.constructed();
 
+               var delta = info.delta;
                var oldfile = delta.get_old_file();
                var newfile = delta.get_new_file();
 
@@ -163,6 +147,7 @@ class Gitg.DiffViewFile : Gtk.Grid
 
                d_expander.bind_property("expanded", this, "expanded", BindingFlags.BIDIRECTIONAL);
 
+               var repository = info.repository;
                if (repository != null && !repository.is_bare)
                {
                        d_expander.popup_menu.connect(expander_popup_menu);
@@ -174,6 +159,7 @@ class Gitg.DiffViewFile : Gtk.Grid
        {
                var menu = new Gtk.Menu();
 
+               var delta  = info.delta;
                var oldpath = delta.get_old_file().get_path();
                var newpath = delta.get_new_file().get_path();
 
@@ -182,6 +168,7 @@ class Gitg.DiffViewFile : Gtk.Grid
 
                File? location = null;
 
+               var repository = info.repository;
                if (newpath != null && newpath != "")
                {
                        location = repository.get_workdir().get_child(newpath);
@@ -262,7 +249,17 @@ class Gitg.DiffViewFile : Gtk.Grid
 
        public void add_hunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
        {
-               this.renderer.add_hunk(hunk, lines);
+               if (renderer_text != null) {
+                       renderer_text.add_hunk(hunk, lines);
+               }
+               foreach (Gtk.Widget page in d_stack_file_renderer.get_children())
+               {
+                       if (page is DiffViewFileRenderer)
+                       {
+                               var renderer = (DiffViewFileRenderer)page;
+                               renderer.add_hunk(hunk, lines);
+                       }
+               }
        }
 }
 
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index 2ec9c031..9553755a 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -642,7 +642,7 @@ public class Gitg.DiffView : Gtk.Grid
 
                foreach (var file in d_grid_files.get_children())
                {
-                       var selectable = ((Gitg.DiffViewFile) file).renderer as DiffSelectable;
+                       var selectable = ((Gitg.DiffViewFile) file).renderer_text as DiffSelectable;
 
                        if (selectable != null && selectable.has_selection)
                        {
@@ -756,7 +756,6 @@ public class Gitg.DiffView : Gtk.Grid
                        if (current_file != null)
                        {
                                current_file.show();
-                               current_file.renderer.notify["has-selection"].connect(on_selection_changed);  
  
 
                                files.add(current_file);
 
@@ -823,18 +822,28 @@ public class Gitg.DiffView : Gtk.Grid
                                                mime_type_for_image = 
ContentType.get_mime_type(info.new_file_content_type);
                                        }
 
-                                       if (mime_type_for_image != null && 
s_image_mime_types.contains(mime_type_for_image))
+                                       bool can_diff_as_text = ContentType.is_mime_type(mime_type_for_image, 
"text/plain");
+
+                                       current_file = new Gitg.DiffViewFile(info);
+
+                                       if (can_diff_as_text)
                                        {
-                                               current_file = new Gitg.DiffViewFile.image(repository, delta);
+                                               current_file.add_text_renderer(handle_selection);
+                                               var renderer_text = current_file.renderer_text;
+                                               bind_property("highlight", renderer_text, "highlight", 
BindingFlags.SYNC_CREATE);
+                                               bind_property("wrap-lines", renderer_text, "wrap-lines", 
BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
+                                               bind_property("tab-width", renderer_text, "tab-width", 
BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
+                                               renderer_text.maxlines = maxlines;
+                                               
renderer_text.notify["has-selection"].connect(on_selection_changed);
                                        }
-                                       else if (current_is_binary)
+                                       if (mime_type_for_image != null
+                                               && s_image_mime_types.contains(mime_type_for_image))
                                        {
-                                               current_file = new Gitg.DiffViewFile.binary(repository, 
delta);
+                                               current_file.add_image_renderer();
                                        }
-                                       else
+                                       if (current_is_binary)
                                        {
-                                               current_file = new Gitg.DiffViewFile.text(info, 
handle_selection);
-                                               this.bind_property("highlight", current_file.renderer, 
"highlight", BindingFlags.SYNC_CREATE);
+                                               current_file.add_binary_renderer();
                                        }
 
                                        return 0;
@@ -898,7 +907,7 @@ public class Gitg.DiffView : Gtk.Grid
 
                        if (preserve_expanded && f.expanded)
                        {
-                               var path = primary_path(f.delta);
+                               var path = primary_path(f.info.delta);
 
                                if (path != null)
                                {
@@ -915,20 +924,10 @@ public class Gitg.DiffView : Gtk.Grid
                for (var i = 0; i < files.size; i++)
                {
                        var file = files[i];
-                       var path = primary_path(file.delta);
+                       var path = primary_path(file.info.delta);
 
                        file.expanded = d_commit_details.expanded || (path != null && 
was_expanded.contains(path));
 
-                       var renderer_text = file.renderer as DiffViewFileRendererText;
-
-                       if (renderer_text != null)
-                       {
-                               renderer_text.maxlines = maxlines;
-
-                               this.bind_property("wrap-lines", renderer_text, "wrap-lines", 
BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
-                               this.bind_property("tab-width", renderer_text, "tab-width", 
BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
-                       }
-
                        if (i == files.size - 1)
                        {
                                file.vexpand = true;
@@ -960,7 +959,7 @@ public class Gitg.DiffView : Gtk.Grid
 
                foreach (var file in d_grid_files.get_children())
                {
-                       var sel = ((Gitg.DiffViewFile) file).renderer as DiffSelectable;
+                       var sel = ((Gitg.DiffViewFile) file).renderer_text as DiffSelectable;
 
                        if (sel != null && sel.has_selection && sel.selection.patches.length != 0)
                        {
diff --git a/libgitg/resources/ui/gitg-diff-view-file.ui b/libgitg/resources/ui/gitg-diff-view-file.ui
index 3a7aa76b..22e67905 100644
--- a/libgitg/resources/ui/gitg-diff-view-file.ui
+++ b/libgitg/resources/ui/gitg-diff-view-file.ui
@@ -11,44 +11,64 @@
       <class name="gitg-file-header"/>
     </style>
     <child>
-      <object class="GtkExpander" id="expander">
+      <object class="GtkBox" id="header_grid">
         <property name="visible">True</property>
-        <property name="can_focus">True</property>
-        <property name="hexpand">True</property>
-        <property name="margin_start">3</property>
-        <child type="label">
-          <object class="GtkGrid" id="grid_file_header">
+        <property name="can_focus">False</property>
+        <property name="homogeneous">False</property>
+        <property name="orientation">horizontal</property>
+        <child>
+          <object class="GtkExpander" id="expander">
             <property name="visible">True</property>
-            <property name="can_focus">False</property>
+            <property name="can_focus">True</property>
             <property name="hexpand">True</property>
-            <property name="row_spacing">6</property>
-            <property name="column_spacing">12</property>
-            <property name="orientation">horizontal</property>
-            <child>
-              <object class="GitgDiffStat" id="diff_stat_file">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="vexpand">False</property>
-                <property name="valign">baseline</property>
-                <property name="margin_top">6</property>
-                <property name="margin_bottom">6</property>
-                <style>
-                  <class name="no-frame"/>
-                </style>
-              </object>
-            </child>
-            <child>
-              <object class="GtkLabel" id="label_file_header">
+            <property name="margin_start">3</property>
+            <child type="label">
+              <object class="GtkGrid" id="grid_file_header">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="hexpand">True</property>
-                <property name="label">the/file/header</property>
-                <property name="halign">start</property>
-                <property name="margin_top">6</property>
-                <property name="margin_bottom">6</property>
+                <property name="row_spacing">6</property>
+                <property name="column_spacing">12</property>
+                <property name="orientation">horizontal</property>
+                <child>
+                  <object class="GitgDiffStat" id="diff_stat_file">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="vexpand">False</property>
+                    <property name="valign">baseline</property>
+                    <property name="margin_top">6</property>
+                    <property name="margin_bottom">6</property>
+                    <style>
+                      <class name="no-frame"/>
+                    </style>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="label_file_header">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="hexpand">True</property>
+                    <property name="label">the/file/header</property>
+                    <property name="halign">start</property>
+                    <property name="margin_top">6</property>
+                    <property name="margin_bottom">6</property>
+                  </object>
+                </child>
               </object>
             </child>
           </object>
+          <packing>
+            <property name="pack-type">start</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkStackSwitcher" id="stack_switcher">
+            <property name="visible">True</property>
+            <property name="stack">stack_file_renderer</property>
+          </object>
+          <packing>
+            <property name="pack-type">end</property>
+          </packing>
         </child>
       </object>
     </child>
@@ -58,11 +78,10 @@
         <property name="can_focus">False</property>
         <property name="transition_type">slide-down</property>
         <child>
-          <object class="GtkScrolledWindow" id="scrolled_window">
+          <object class="GtkStack" id="stack_file_renderer">
             <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <property name="hexpand">True</property>
             <property name="vexpand">True</property>
+            <property name="hexpand">True</property>
           </object>
         </child>
       </object>


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