[epiphany] ephy-location-entry: Get rid of the cell data func to set the text



commit 592e5dff5b6aadeccf437c3ddbfb2e4b0134353e
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Fri Aug 7 11:15:57 2015 +0200

    ephy-location-entry: Get rid of the cell data func to set the text
    
    It seems that changing the model in the cell data func callback confuses
    GtkTreeView that keeps validating the rows all the time while the popup
    is hidden. The fact that GtkEntry recomputes the size of the popup on
    every size_allocate even when the popup is hidden doesn't help either.
    So, not changing the model inside the cell data func callback prevents
    the rows from being validated indefinitely, but still the cell data func
    is called too often in my opinion. And we are always setting the same
    text for every row, so I think we should set the title in the model and
    get rid of the cell data func.
    This patch also ensures that the URL is not shown twice in the
    completion menu when there's no title.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753321

 lib/widgets/ephy-location-entry.c |   76 +------------------------------------
 src/ephy-completion-model.c       |   63 ++++++++++++++++++++++++++++--
 2 files changed, 60 insertions(+), 79 deletions(-)
---
diff --git a/lib/widgets/ephy-location-entry.c b/lib/widgets/ephy-location-entry.c
index b32f959..629be35 100644
--- a/lib/widgets/ephy-location-entry.c
+++ b/lib/widgets/ephy-location-entry.c
@@ -56,7 +56,6 @@ struct _EphyLocationEntryPrivate
 {
        GdkPixbuf *favicon;
        GtkTreeModel *model;
-       GtkEntryCompletion *completion;
 
        GSList *search_terms;
 
@@ -1047,72 +1046,6 @@ cursor_on_match_cb  (GtkEntryCompletion *completion,
        return TRUE;
 }
 
-static char *
-rgba_to_hex (GdkRGBA *color)
-{
-       char *hex;
-
-       hex = g_strdup_printf ("#%04X%04X%04X",
-                              (guint)(color->red * (gdouble)65535),
-                              (guint)(color->green * (gdouble)65535),
-                              (guint)(color->blue * (gdouble)65535));
-       return hex;
-}
-
-static void
-textcell_data_func (GtkCellLayout *cell_layout,
-                   GtkCellRenderer *cell,
-                   GtkTreeModel *tree_model,
-                   GtkTreeIter *iter,
-                   gpointer data)
-{
-       GtkWidget *entry;
-       EphyLocationEntryPrivate *priv;
-       char *ctext;
-       char *title;
-       char *url;
-       GtkStyleContext *style;
-       GValue text = { 0, };
-
-       entry = GTK_WIDGET (data);
-       priv = EPHY_LOCATION_ENTRY (data)->priv;
-       gtk_tree_model_get (tree_model, iter,
-                       priv->text_col, &title,
-                       priv->url_col, &url,
-                       -1);
-
-       if (url)
-       {
-               GdkRGBA color;
-               char *color_text;
-               char *unescaped_url;
-
-               sanitize_location (&url);
-               unescaped_url = g_uri_unescape_string (url, NULL);
-
-               style = gtk_widget_get_style_context (entry);
-               gtk_style_context_get_color (style, GTK_STATE_FLAG_INSENSITIVE,
-                                            &color);
-
-               color_text = rgba_to_hex (&color);
-               ctext = g_markup_printf_escaped ("%s\n<span font-size=\"small\" color=\"%s\">%s</span>", 
title, color_text, unescaped_url);
-               g_free (color_text);
-               g_free (title);
-               g_free (unescaped_url);
-       }
-       else
-       {
-               ctext = title;
-       }
-
-       g_value_init (&text, G_TYPE_STRING);
-       g_value_take_string (&text, ctext);
-       g_object_set_property (G_OBJECT (cell), "markup", &text);
-       g_value_unset (&text);
-
-       g_free (url);
-}
-
 static void
 extracell_data_func (GtkCellLayout *cell_layout,
                        GtkCellRenderer *cell,
@@ -1268,7 +1201,7 @@ ephy_location_entry_set_completion (EphyLocationEntry *entry,
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion),
                                    cell, TRUE);
        gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (completion),
-                                      cell, "text", text_col);
+                                      cell, "markup", text_col);
 
        /* Pixel-perfect aligment with the text in the location entry.
         * See above.
@@ -1288,11 +1221,6 @@ ephy_location_entry_set_completion (EphyLocationEntry *entry,
        gtk_cell_renderer_set_fixed_size (cell, 1, -1);
        gtk_cell_renderer_text_set_fixed_height_from_font (GTK_CELL_RENDERER_TEXT (cell), 2);
 
-       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
-                                           cell, textcell_data_func,
-                                           entry,
-                                           NULL);
-
        cell = gtk_cell_renderer_pixbuf_new ();
        g_object_set (cell, "follow-state", TRUE, NULL);
        gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (completion),
@@ -1312,8 +1240,6 @@ ephy_location_entry_set_completion (EphyLocationEntry *entry,
                          G_CALLBACK (cursor_on_match_cb), entry);
 
        gtk_entry_set_completion (GTK_ENTRY (entry), completion);
-
-       priv->completion = completion;
        g_object_unref (completion);
 }
 
diff --git a/src/ephy-completion-model.c b/src/ephy-completion-model.c
index 654c907..60120da 100644
--- a/src/ephy-completion-model.c
+++ b/src/ephy-completion-model.c
@@ -238,25 +238,47 @@ icon_loaded_cb (GObject *source, GAsyncResult *result, gpointer user_data)
   g_slice_free (IconLoadData, data);
 }
 
+static gchar *
+get_row_text (const gchar *url, const gchar *title, const gchar *subtitle_color)
+{
+  gchar *unescaped_url;
+  gchar *text;
+
+  if (!url)
+    return g_markup_escape_text (title, -1);
+
+  unescaped_url = g_uri_unescape_string (url, NULL);
+  if (g_strcmp0 (url, title) == 0)
+    text = g_markup_escape_text (unescaped_url, -1);
+  else
+    text = g_markup_printf_escaped ("%s\n<span font-size=\"small\" color=\"%s\">%s</span>", title, 
subtitle_color, unescaped_url);
+  g_free (unescaped_url);
+
+  return text;
+}
+
 static void
-set_row_in_model (EphyCompletionModel *model, int position, PotentialRow *row)
+set_row_in_model (EphyCompletionModel *model, int position, PotentialRow *row, const gchar *subtitle_color)
 {
   GtkTreeIter iter;
   GtkTreePath *path;
   IconLoadData *data;
   WebKitFaviconDatabase* database;
+  gchar *text;
   EphyEmbedShell *shell = ephy_embed_shell_get_default ();
 
   database = webkit_web_context_get_favicon_database (ephy_embed_shell_get_web_context (shell));
 
+  text = get_row_text (row->location, row->title, subtitle_color);
   gtk_list_store_insert_with_values (GTK_LIST_STORE (model), &iter, position,
-                                     EPHY_COMPLETION_TEXT_COL, row->title ? row->title : "",
+                                     EPHY_COMPLETION_TEXT_COL, text ? text : "",
                                      EPHY_COMPLETION_URL_COL, row->location,
                                      EPHY_COMPLETION_ACTION_COL, row->location,
                                      EPHY_COMPLETION_KEYWORDS_COL, row->keywords ? row->keywords : "",
                                      EPHY_COMPLETION_EXTRA_COL, row->is_bookmark,
                                      EPHY_COMPLETION_RELEVANCE_COL, row->relevance,
                                      -1);
+  g_free (text);
 
   data = g_slice_new (IconLoadData);
   data->model = GTK_LIST_STORE (g_object_ref(model));
@@ -268,21 +290,54 @@ set_row_in_model (EphyCompletionModel *model, int position, PotentialRow *row)
                                        NULL, icon_loaded_cb, data);
 }
 
+static gchar *
+get_text_column_subtitle_color (void)
+{
+  GtkWidgetPath *path;
+  GtkStyleContext *style_context;
+  GdkRGBA rgba;
+
+  path = gtk_widget_path_new ();
+  gtk_widget_path_prepend_type (path, GTK_TYPE_ENTRY);
+  gtk_widget_path_iter_add_class (path, 0, GTK_STYLE_CLASS_ENTRY);
+
+  style_context = gtk_style_context_new ();
+  gtk_style_context_set_path (style_context, path);
+  gtk_widget_path_free (path);
+
+  gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_ENTRY);
+  gtk_style_context_get_color (style_context, GTK_STATE_FLAG_INSENSITIVE, &rgba);
+  g_object_unref (style_context);
+
+  return g_strdup_printf ("#%04X%04X%04X",
+                          (guint)(rgba.red * (gdouble)65535),
+                          (guint)(rgba.green * (gdouble)65535),
+                          (guint)(rgba.blue * (gdouble)65535));
+}
+
 static void
 replace_rows_in_model (EphyCompletionModel *model, GSList *new_rows)
 {
   /* This is by far the simplest way of doing, and yet it gives
    * basically the same result than the other methods... */
   int i;
+  gchar *subtitle_color;
 
   gtk_list_store_clear (GTK_LIST_STORE (model));
 
+  if (!new_rows)
+    return;
+
+  subtitle_color = get_text_column_subtitle_color ();
+
   for (i = 0; new_rows != NULL; i++) {
     PotentialRow *row = (PotentialRow*)new_rows->data;
-    
-    set_row_in_model (model, i, row);
+
+    set_row_in_model (model, i, row, subtitle_color);
     new_rows = new_rows->next;
   }
+
+  g_free (subtitle_color);
 }
 
 static gboolean


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