[gnome-builder] GbGitSearchResult: improve styling a bit



commit 06ae1cd9c4f9ebd96971ef7316821fdb379eed80
Author: Christian Hergert <christian hergert me>
Date:   Wed Dec 17 16:26:16 2014 -0800

    GbGitSearchResult: improve styling a bit
    
    Also, try not to be completely braindead when building the widgets with
    regards to string copying. It's not great, but it's a step forward.

 src/git/gb-git-search-provider.c         |   80 ++++++++++++++++++++++++++++++
 src/git/gb-git-search-result.c           |   72 +++++++++++++++++++++------
 src/git/gb-git-search-result.h           |    3 +-
 src/resources/ui/gb-git-search-result.ui |    7 ++-
 4 files changed, 141 insertions(+), 21 deletions(-)
---
diff --git a/src/git/gb-git-search-provider.c b/src/git/gb-git-search-provider.c
index 1ce84e0..7e675ec 100644
--- a/src/git/gb-git-search-provider.c
+++ b/src/git/gb-git-search-provider.c
@@ -187,6 +187,30 @@ remove_spaces (const gchar *text)
   return g_string_free (str, FALSE);
 }
 
+static gchar **
+split_path (const gchar  *path,
+            gchar       **shortname)
+{
+  gchar **parts;
+  gsize len;
+
+  g_return_val_if_fail (path, NULL);
+  g_return_val_if_fail (shortname, NULL);
+
+  *shortname = NULL;
+
+  parts = g_strsplit (path, "/", 0);
+  len = g_strv_length (parts);
+
+  if (len)
+    {
+      *shortname = parts [len-1];
+      parts [len-1] = 0;
+    }
+
+  return parts;
+}
+
 static void
 gb_git_search_provider_populate (GbSearchProvider *provider,
                                  GbSearchContext  *context,
@@ -201,30 +225,85 @@ gb_git_search_provider_populate (GbSearchProvider *provider,
 
   if (self->priv->file_index)
     {
+      GString *str = g_string_new (NULL);
       const gchar *search_text;
       gchar *delimited;
       GArray *matches;
       guint i;
+      guint truncate_len;
 
       search_text = gb_search_context_get_search_text (context);
       delimited = remove_spaces (search_text);
       matches = fuzzy_match (self->priv->file_index, delimited,
                              GB_GIT_SEARCH_PROVIDER_MAX_MATCHES);
 
+      if (self->priv->repository)
+        {
+          GgitRef *ref;
+          GFile *repo_dir = NULL;
+
+          repo_dir = ggit_repository_get_location (self->priv->repository);
+          if (repo_dir)
+            {
+              gchar *repo_name;
+
+              repo_name = g_file_get_basename (repo_dir);
+
+              if (g_strcmp0 (repo_name, ".git") == 0)
+                {
+                  GFile *tmp;
+
+                  tmp = repo_dir;
+                  repo_dir = g_file_get_parent (repo_dir);
+                  g_clear_object (&tmp);
+
+                  repo_name = g_file_get_basename (repo_dir);
+                }
+
+              g_string_append (str, repo_name);
+
+              g_clear_object (&repo_dir);
+              g_free (repo_name);
+            }
+
+          ref = ggit_repository_get_head (self->priv->repository, NULL);
+          if (ref)
+            {
+              g_string_append_printf (str, "[%s]",
+                                      ggit_ref_get_shorthand (ref));
+              g_clear_object (&ref);
+            }
+        }
+
+      truncate_len = str->len;
+
       for (i = 0; i < matches->len; i++)
         {
           FuzzyMatch *match;
           GtkWidget *widget;
+          gchar *shortname = NULL;
+          gchar **parts;
+          guint j;
 
           match = &g_array_index (matches, FuzzyMatch, i);
 
+          parts = split_path (match->key, &shortname);
+          for (j = 0; parts [j]; j++)
+            g_string_append_printf (str, " / %s", parts [j]);
+
           /* TODO: Make a git file search result */
           widget = g_object_new (GB_TYPE_GIT_SEARCH_RESULT,
                                  "visible", TRUE,
                                  "score", match->score,
+                                 "repository-name", str->str,
                                  "path", match->key,
+                                 "display-name", shortname,
                                  NULL);
           list = g_list_prepend (list, widget);
+
+          g_free (shortname);
+          g_strfreev (parts);
+          g_string_truncate (str, truncate_len);
         }
 
       list = g_list_reverse (list);
@@ -232,6 +311,7 @@ gb_git_search_provider_populate (GbSearchProvider *provider,
 
       g_array_unref (matches);
       g_free (delimited);
+      g_string_free (str, TRUE);
     }
 }
 
diff --git a/src/git/gb-git-search-result.c b/src/git/gb-git-search-result.c
index c6614aa..bfa3b82 100644
--- a/src/git/gb-git-search-result.c
+++ b/src/git/gb-git-search-result.c
@@ -17,6 +17,7 @@
  */
 
 #include <glib/gi18n.h>
+#include <libgit2-glib/ggit.h>
 
 #include "gb-document-manager.h"
 #include "gb-editor-document.h"
@@ -30,6 +31,7 @@ struct _GbGitSearchResultPrivate
   gchar *path;
 
   GtkLabel *label;
+  GtkLabel *repository;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GbGitSearchResult, gb_git_search_result,
@@ -37,16 +39,36 @@ G_DEFINE_TYPE_WITH_PRIVATE (GbGitSearchResult, gb_git_search_result,
 
 enum {
   PROP_0,
+  PROP_DISPLAY_NAME,
   PROP_PATH,
+  PROP_REPOSITORY_NAME,
   LAST_PROP
 };
 
 static GParamSpec *gParamSpecs [LAST_PROP];
 
-GtkWidget *
-gb_git_search_result_new (const gchar *path)
+static void
+gb_git_search_result_set_repository_name (GbGitSearchResult *result,
+                                          const gchar       *repository_name)
+{
+  g_return_if_fail (GB_IS_GIT_SEARCH_RESULT (result));
+
+  if (!repository_name)
+    repository_name = "";
+
+  gtk_label_set_label (result->priv->repository, repository_name);
+}
+
+static void
+gb_git_search_result_set_display_name (GbGitSearchResult *result,
+                                       const gchar       *display_name)
 {
-  return g_object_new (GB_TYPE_GIT_SEARCH_RESULT, "path", path, NULL);
+  g_return_if_fail (GB_IS_GIT_SEARCH_RESULT (result));
+
+  if (!display_name)
+    display_name = "";
+
+  gtk_label_set_label (result->priv->label, display_name);
 }
 
 const gchar *
@@ -67,7 +89,6 @@ gb_git_search_result_set_path (GbGitSearchResult *result,
     {
       g_free (result->priv->path);
       result->priv->path = g_strdup (path);
-      g_object_notify_by_pspec (G_OBJECT (result), gParamSpecs [PROP_PATH]);
     }
 }
 
@@ -92,17 +113,6 @@ gb_git_search_result_activate (GbSearchResult *result)
 }
 
 static void
-gb_git_search_result_constructed (GObject *object)
-{
-  GbGitSearchResult *self = (GbGitSearchResult *)object;
-
-  G_OBJECT_CLASS (gb_git_search_result_parent_class)->constructed (object);
-
-  g_object_bind_property (self, "path", self->priv->label, "label",
-                          G_BINDING_SYNC_CREATE);
-}
-
-static void
 gb_git_search_result_finalize (GObject *object)
 {
   GbGitSearchResultPrivate *priv = GB_GIT_SEARCH_RESULT (object)->priv;
@@ -141,10 +151,19 @@ gb_git_search_result_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_DISPLAY_NAME:
+      gb_git_search_result_set_display_name (self, g_value_get_string (value));
+      break;
+
     case PROP_PATH:
       gb_git_search_result_set_path (self, g_value_get_string (value));
       break;
 
+    case PROP_REPOSITORY_NAME:
+      gb_git_search_result_set_repository_name (self,
+                                                g_value_get_string (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -156,13 +175,21 @@ gb_git_search_result_class_init (GbGitSearchResultClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GbSearchResultClass *result_class = GB_SEARCH_RESULT_CLASS (klass);
 
-  object_class->constructed = gb_git_search_result_constructed;
   object_class->finalize = gb_git_search_result_finalize;
   object_class->get_property = gb_git_search_result_get_property;
   object_class->set_property = gb_git_search_result_set_property;
 
   result_class->activate = gb_git_search_result_activate;
 
+  gParamSpecs [PROP_DISPLAY_NAME] =
+    g_param_spec_string ("display-name",
+                         _("Display Name"),
+                         _("The display name for the file."),
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
+                                   gParamSpecs [PROP_DISPLAY_NAME]);
+
   gParamSpecs [PROP_PATH] =
     g_param_spec_string ("path",
                          _("Path"),
@@ -172,13 +199,26 @@ gb_git_search_result_class_init (GbGitSearchResultClass *klass)
   g_object_class_install_property (object_class, PROP_PATH,
                                    gParamSpecs [PROP_PATH]);
 
+  gParamSpecs [PROP_REPOSITORY_NAME] =
+    g_param_spec_string ("repository-name",
+                         _("Repository Name"),
+                         _("The name of the repository, taken from basename."),
+                         NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_REPOSITORY_NAME,
+                                   gParamSpecs [PROP_REPOSITORY_NAME]);
+
   GB_WIDGET_CLASS_TEMPLATE (klass, "gb-git-search-result.ui");
   GB_WIDGET_CLASS_BIND (klass, GbGitSearchResult, label);
+  GB_WIDGET_CLASS_BIND (klass, GbGitSearchResult, repository);
 }
 
 static void
 gb_git_search_result_init (GbGitSearchResult *self)
 {
   self->priv = gb_git_search_result_get_instance_private (self);
+
   gtk_widget_init_template (GTK_WIDGET (self));
 }
diff --git a/src/git/gb-git-search-result.h b/src/git/gb-git-search-result.h
index 52f2fba..02e875e 100644
--- a/src/git/gb-git-search-result.h
+++ b/src/git/gb-git-search-result.h
@@ -48,8 +48,7 @@ struct _GbGitSearchResultClass
   GbSearchResultClass parent;
 };
 
-GType      gb_git_search_result_get_type (void);
-GtkWidget *gb_git_search_result_new      (const gchar *path);
+GType gb_git_search_result_get_type (void);
 
 G_END_DECLS
 
diff --git a/src/resources/ui/gb-git-search-result.ui b/src/resources/ui/gb-git-search-result.ui
index 0dde9ac..ed3c7d2 100644
--- a/src/resources/ui/gb-git-search-result.ui
+++ b/src/resources/ui/gb-git-search-result.ui
@@ -7,12 +7,13 @@
         <property name="visible">true</property>
         <property name="expand">true</property>
         <property name="orientation">horizontal</property>
-        <property name="spacing">6</property>
+        <property name="spacing">12</property>
+        <property name="border_width">6</property>
         <child>
           <object class="GtkImage" id="image">
             <property name="pixel_size">32</property>
             <property name="visible">true</property>
-            <property name="icon_name">folder</property>
+            <property name="icon_name">folder-symbolic</property>
             <property name="can_focus">false</property>
           </object>
         </child>
@@ -30,7 +31,7 @@
               </object>
             </child>
             <child>
-              <object class="GtkLabel" id="dirname_label">
+              <object class="GtkLabel" id="repository">
                 <property name="visible">true</property>
                 <property name="xalign">0.0</property>
                 <property name="valign">baseline</property>


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