[gnome-builder/wip/gtk4-port] search: port IdeSearchResult away from DzlSuggestion



commit afcd5275386f4458985376d2496ccd1acbb60795
Author: Christian Hergert <chergert redhat com>
Date:   Fri Sep 24 11:14:22 2021 -0700

    search: port IdeSearchResult away from DzlSuggestion
    
    We'll be doing something new for the GTK 4 port instead of the search
    box from the top-right.

 src/libide/search/ide-search-result.c | 169 +++++++++++++++++++++++-----------
 src/libide/search/ide-search-result.h |  60 ++++++------
 2 files changed, 148 insertions(+), 81 deletions(-)
---
diff --git a/src/libide/search/ide-search-result.c b/src/libide/search/ide-search-result.c
index 3d6385b58..7edc2d374 100644
--- a/src/libide/search/ide-search-result.c
+++ b/src/libide/search/ide-search-result.c
@@ -26,43 +26,33 @@
 
 typedef struct
 {
-  GIcon  *icon;
-  gfloat  score;
-  guint   priority;
+  char         *title;
+  GdkPaintable *paintable;
+  float         score;
+  guint         priority;
 } IdeSearchResultPrivate;
 
 enum {
   PROP_0,
-  PROP_SCORE,
+  PROP_PAINTABLE,
   PROP_PRIORITY,
+  PROP_SCORE,
+  PROP_TITLE,
   N_PROPS
 };
 
-G_DEFINE_TYPE_WITH_PRIVATE (IdeSearchResult, ide_search_result, DZL_TYPE_SUGGESTION)
+G_DEFINE_TYPE_WITH_PRIVATE (IdeSearchResult, ide_search_result, G_TYPE_OBJECT)
 
 static GParamSpec *properties [N_PROPS];
 
-static GIcon *
-ide_search_result_real_get_icon (DzlSuggestion *suggestion)
-{
-  IdeSearchResult *self = (IdeSearchResult *)suggestion;
-  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
-
-  g_assert (IDE_IS_SEARCH_RESULT (self));
-
-  if (priv->icon != NULL)
-    return g_object_ref (priv->icon);
-
-  return DZL_SUGGESTION_CLASS (ide_search_result_parent_class)->get_icon (suggestion);
-}
-
 static void
 ide_search_result_finalize (GObject *object)
 {
   IdeSearchResult *self = (IdeSearchResult *)object;
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
 
-  g_clear_object (&priv->icon);
+  g_clear_object (&priv->paintable);
+  g_clear_pointer (&priv->title, g_free);
 
   G_OBJECT_CLASS (ide_search_result_parent_class)->finalize (object);
 }
@@ -77,14 +67,22 @@ ide_search_result_get_property (GObject    *object,
 
   switch (prop_id)
     {
-    case PROP_SCORE:
-      g_value_set_float (value, ide_search_result_get_score (self));
+    case PROP_PAINTABLE:
+      g_value_set_object (value, ide_search_result_get_paintable (self));
       break;
 
     case PROP_PRIORITY:
       g_value_set_int (value, ide_search_result_get_priority (self));
       break;
 
+    case PROP_SCORE:
+      g_value_set_float (value, ide_search_result_get_score (self));
+      break;
+
+    case PROP_TITLE:
+      g_value_set_string (value, ide_search_result_get_title (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -100,14 +98,22 @@ ide_search_result_set_property (GObject      *object,
 
   switch (prop_id)
     {
-    case PROP_SCORE:
-      ide_search_result_set_score (self, g_value_get_float (value));
+    case PROP_PAINTABLE:
+      ide_search_result_set_paintable (self, g_value_get_object (value));
       break;
 
     case PROP_PRIORITY:
       ide_search_result_set_priority (self, g_value_get_int (value));
       break;
 
+    case PROP_SCORE:
+      ide_search_result_set_score (self, g_value_get_float (value));
+      break;
+
+    case PROP_TITLE:
+      ide_search_result_set_title (self, g_value_get_string (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -117,13 +123,26 @@ static void
 ide_search_result_class_init (IdeSearchResultClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  DzlSuggestionClass *suggestion_class = DZL_SUGGESTION_CLASS (klass);
 
   object_class->finalize = ide_search_result_finalize;
   object_class->get_property = ide_search_result_get_property;
   object_class->set_property = ide_search_result_set_property;
 
-  suggestion_class->get_icon = ide_search_result_real_get_icon;
+  properties [PROP_PAINTABLE] =
+    g_param_spec_object ("paintable",
+                      "Paintable",
+                      "The paintable for the row icon",
+                      GDK_TYPE_PAINTABLE,
+                      (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_PRIORITY] =
+    g_param_spec_int ("priority",
+                      "Priority",
+                      "The priority of search result group",
+                      G_MININT,
+                      G_MAXINT,
+                      0,
+                      (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   properties [PROP_SCORE] =
     g_param_spec_float ("score",
@@ -134,14 +153,12 @@ ide_search_result_class_init (IdeSearchResultClass *klass)
                         0.0f,
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-  properties [PROP_PRIORITY] =
-    g_param_spec_int ("priority",
-                      "Priority",
-                      "The priority of search result group",
-                      G_MININT,
-                      G_MAXINT,
-                      0,
-                      (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  properties [PROP_TITLE] =
+    g_param_spec_string ("title",
+                         "Title",
+                         "The title of the search result",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_properties (object_class, N_PROPS, properties);
 }
@@ -157,7 +174,7 @@ ide_search_result_new (void)
   return g_object_new (IDE_TYPE_SEARCH_RESULT, NULL);
 }
 
-gint
+int
 ide_search_result_compare (gconstpointer a,
                            gconstpointer b)
 {
@@ -165,22 +182,21 @@ ide_search_result_compare (gconstpointer a,
   IdeSearchResult *rb = (IdeSearchResult *)b;
   IdeSearchResultPrivate *priva = ide_search_result_get_instance_private (ra);
   IdeSearchResultPrivate *privb = ide_search_result_get_instance_private (rb);
-  gint ret;
 
-  ret = priva->priority - privb->priority;
+  if (priva->priority < privb->priority)
+    return -1;
+  else if (priva->priority > privb->priority)
+    return 1;
 
-  if (ret == 0)
-    {
-      if (priva->score < privb->score)
-        priva->score = -1;
-      else if (priva->score > privb->score)
-        priva->score = 1;
-    }
+  if (priva->score < privb->score)
+    return -1;
+  else if (priva->score > privb->score)
+    return 1;
 
-  return ret;
+  return 0;
 }
 
-gfloat
+float
 ide_search_result_get_score (IdeSearchResult *self)
 {
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
@@ -192,7 +208,7 @@ ide_search_result_get_score (IdeSearchResult *self)
 
 void
 ide_search_result_set_score (IdeSearchResult *self,
-                             gfloat           score)
+                             float            score)
 {
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
 
@@ -205,7 +221,7 @@ ide_search_result_set_score (IdeSearchResult *self,
     }
 }
 
-gint
+int
 ide_search_result_get_priority (IdeSearchResult *self)
 {
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
@@ -217,7 +233,7 @@ ide_search_result_get_priority (IdeSearchResult *self)
 
 void
 ide_search_result_set_priority (IdeSearchResult *self,
-                                gint             priority)
+                                int              priority)
 {
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
 
@@ -237,8 +253,6 @@ ide_search_result_set_priority (IdeSearchResult *self,
  *
  * Requests that @self activate. @last_focus is provided so that the search
  * result may activate #GAction or other context-specific actions.
- *
- * Since: 3.32
  */
 void
 ide_search_result_activate (IdeSearchResult *self,
@@ -251,14 +265,59 @@ ide_search_result_activate (IdeSearchResult *self,
     IDE_SEARCH_RESULT_GET_CLASS (self)->activate (self, last_focus);
 }
 
+/**
+ * ide_search_result_get_paintable:
+ * @self: a #IdeSearchResult
+ *
+ * Gets the paintable for the row, if any.
+ *
+ * Returns: (transfer none) (nullable): a #GdkPaintable or %NULL
+ */
+GdkPaintable *
+ide_search_result_get_paintable (IdeSearchResult *self)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT (self), NULL);
+
+  return priv->paintable;
+}
+
+void
+ide_search_result_set_paintable (IdeSearchResult *self,
+                                 GdkPaintable    *paintable)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SEARCH_RESULT (self));
+  g_return_if_fail (!paintable || GDK_IS_PAINTABLE (paintable));
+
+  if (g_set_object (&priv->paintable, paintable))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_PAINTABLE]);
+}
+
+const char *
+ide_search_result_get_title (IdeSearchResult *self)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT (self), NULL);
+
+  return priv->title;
+}
+
 void
-ide_search_result_set_icon (IdeSearchResult *self,
-                            GIcon           *icon)
+ide_search_result_set_title (IdeSearchResult *self,
+                             const char      *title)
 {
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
 
   g_return_if_fail (IDE_IS_SEARCH_RESULT (self));
 
-  /* don't notify, since it's not really needed */
-  g_set_object (&priv->icon, icon);
+  if (g_strcmp0 (priv->title, title) != 0)
+    {
+      g_free (priv->title);
+      priv->title = g_strdup (title);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
+    }
 }
diff --git a/src/libide/search/ide-search-result.h b/src/libide/search/ide-search-result.h
index a5a0fd960..90cf41618 100644
--- a/src/libide/search/ide-search-result.h
+++ b/src/libide/search/ide-search-result.h
@@ -1,6 +1,6 @@
 /* ide-search-result.h
  *
- * Copyright 2017-2019 Christian Hergert <chergert redhat com>
+ * Copyright 2017-2021 Christian Hergert <chergert redhat com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,19 +24,20 @@
 # error "Only <libide-search.h> can be included directly."
 #endif
 
+#include <gtk/gtk.h>
+
 #include <libide-core.h>
-#include <dazzle.h>
 
 G_BEGIN_DECLS
 
 #define IDE_TYPE_SEARCH_RESULT (ide_search_result_get_type())
 
-IDE_AVAILABLE_IN_3_32
-G_DECLARE_DERIVABLE_TYPE (IdeSearchResult, ide_search_result, IDE, SEARCH_RESULT, DzlSuggestion)
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_DERIVABLE_TYPE (IdeSearchResult, ide_search_result, IDE, SEARCH_RESULT, GObject)
 
 struct _IdeSearchResultClass
 {
-  DzlSuggestionClass parent_class;
+  GObjectClass parent_class;
 
   void (*activate) (IdeSearchResult *self,
                     GtkWidget       *last_focus);
@@ -45,26 +46,33 @@ struct _IdeSearchResultClass
   gpointer _reserved[8];
 };
 
-IDE_AVAILABLE_IN_3_32
-IdeSearchResult *ide_search_result_new          (void);
-IDE_AVAILABLE_IN_3_32
-void             ide_search_result_activate     (IdeSearchResult *self,
-                                                 GtkWidget       *last_focus);
-IDE_AVAILABLE_IN_3_32
-gint             ide_search_result_compare      (gconstpointer    a,
-                                                 gconstpointer    b);
-IDE_AVAILABLE_IN_3_32
-gint             ide_search_result_get_priority (IdeSearchResult *self);
-IDE_AVAILABLE_IN_3_32
-void             ide_search_result_set_priority (IdeSearchResult *self,
-                                                 gint             priority);
-IDE_AVAILABLE_IN_3_32
-gfloat           ide_search_result_get_score    (IdeSearchResult *self);
-IDE_AVAILABLE_IN_3_32
-void             ide_search_result_set_score    (IdeSearchResult *self,
-                                                 gfloat           score);
-IDE_AVAILABLE_IN_3_32
-void             ide_search_result_set_icon     (IdeSearchResult *self,
-                                                 GIcon           *icon);
+IDE_AVAILABLE_IN_ALL
+IdeSearchResult *ide_search_result_new           (void);
+IDE_AVAILABLE_IN_ALL
+void             ide_search_result_activate      (IdeSearchResult *self,
+                                                  GtkWidget       *last_focus);
+IDE_AVAILABLE_IN_ALL
+int              ide_search_result_compare       (gconstpointer    a,
+                                                  gconstpointer    b);
+IDE_AVAILABLE_IN_ALL
+int              ide_search_result_get_priority  (IdeSearchResult *self);
+IDE_AVAILABLE_IN_ALL
+void             ide_search_result_set_priority  (IdeSearchResult *self,
+                                                  int             priority);
+IDE_AVAILABLE_IN_ALL
+float            ide_search_result_get_score     (IdeSearchResult *self);
+IDE_AVAILABLE_IN_ALL
+void             ide_search_result_set_score     (IdeSearchResult *self,
+                                                  gfloat           score);
+IDE_AVAILABLE_IN_ALL
+GdkPaintable    *ide_search_result_get_paintable (IdeSearchResult *self);
+IDE_AVAILABLE_IN_ALL
+void             ide_search_result_set_paintable (IdeSearchResult *self,
+                                                  GdkPaintable    *paintable);
+IDE_AVAILABLE_IN_ALL
+const char      *ide_search_result_get_title     (IdeSearchResult *self);
+IDE_AVAILABLE_IN_ALL
+void             ide_search_result_set_title     (IdeSearchResult *self,
+                                                  const char      *title);
 
 G_END_DECLS


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