[libdazzle] suggestion: add DzlSuggestionEntry:suggestion property



commit 730caf372868b59a045b13d5351e47c5aa7c0de3
Author: Christian Hergert <chergert redhat com>
Date:   Thu Aug 9 11:18:34 2018 -0700

    suggestion: add DzlSuggestionEntry:suggestion property
    
    This allows for notify::suggestion signal callbacks when the selected entry
    changes.

 src/suggestions/dzl-suggestion-entry.c | 39 ++++++++++++++++++++++++++++++++++
 tests/test-suggestion.c                | 20 +++++++++++++++++
 2 files changed, 59 insertions(+)
---
diff --git a/src/suggestions/dzl-suggestion-entry.c b/src/suggestions/dzl-suggestion-entry.c
index 297c8ec..9474bef 100644
--- a/src/suggestions/dzl-suggestion-entry.c
+++ b/src/suggestions/dzl-suggestion-entry.c
@@ -50,6 +50,7 @@ enum {
   PROP_0,
   PROP_MODEL,
   PROP_TYPED_TEXT,
+  PROP_SUGGESTION,
   N_PROPS
 };
 
@@ -314,6 +315,17 @@ dzl_suggestion_entry_activate_suggestion (DzlSuggestionEntry *self)
   DZL_EXIT;
 }
 
+static void
+dzl_suggestion_entry_notify_selected_cb (DzlSuggestionEntry   *self,
+                                         GParamSpec           *pspec,
+                                         DzlSuggestionPopover *popover)
+{
+  g_assert (DZL_IS_SUGGESTION_ENTRY (self));
+  g_assert (DZL_IS_SUGGESTION_POPOVER (popover));
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SUGGESTION]);
+}
+
 static void
 dzl_suggestion_entry_constructed (GObject *object)
 {
@@ -363,6 +375,10 @@ dzl_suggestion_entry_get_property (GObject    *object,
       g_value_set_object (value, dzl_suggestion_entry_get_model (self));
       break;
 
+    case PROP_SUGGESTION:
+      g_value_set_object (value, dzl_suggestion_entry_get_suggestion (self));
+      break;
+
     case PROP_TYPED_TEXT:
       g_value_set_string (value, dzl_suggestion_entry_get_typed_text (self));
       break;
@@ -386,6 +402,10 @@ dzl_suggestion_entry_set_property (GObject      *object,
       dzl_suggestion_entry_set_model (self, g_value_get_object (value));
       break;
 
+    case PROP_SUGGESTION:
+      dzl_suggestion_entry_set_suggestion (self, g_value_get_object (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -425,6 +445,20 @@ dzl_suggestion_entry_class_init (DzlSuggestionEntryClass *klass)
                          NULL,
                          (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * DzlSuggestionEntry:suggestion:
+   *
+   * The "suggestion" property is the currently selected suggestion, if any.
+   *
+   * Since: 3.30
+   */
+  properties [PROP_SUGGESTION] =
+    g_param_spec_object ("suggestion",
+                         "Suggestion",
+                         "The currently selected suggestion",
+                         DZL_TYPE_SUGGESTION,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, N_PROPS, properties);
 
   signals [HIDE_SUGGESTIONS] =
@@ -505,6 +539,11 @@ dzl_suggestion_entry_init (DzlSuggestionEntry *self)
                                 "relative-to", self,
                                 "type", GTK_WINDOW_POPUP,
                                 NULL);
+  g_signal_connect_object (priv->popover,
+                           "notify::selected",
+                           G_CALLBACK (dzl_suggestion_entry_notify_selected_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
   g_signal_connect_object (priv->popover,
                            "suggestion-activated",
                            G_CALLBACK (dzl_suggestion_entry_suggestion_activated),
diff --git a/tests/test-suggestion.c b/tests/test-suggestion.c
index 70d995a..0233a1c 100644
--- a/tests/test-suggestion.c
+++ b/tests/test-suggestion.c
@@ -235,6 +235,25 @@ load_css (void)
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 }
 
+static void
+notify_suggestion_cb (DzlSuggestionEntry *entry,
+                      GParamSpec         *pspec,
+                      gpointer            unused)
+{
+  DzlSuggestion *suggestion;
+
+  g_assert (DZL_IS_SUGGESTION_ENTRY (entry));
+
+  if ((suggestion = dzl_suggestion_entry_get_suggestion (entry)))
+    {
+      g_print  ("Suggestion changed to %s\n", dzl_suggestion_get_id (suggestion));
+    }
+  else
+    {
+      g_print ("Suggestion cleared\n");
+    }
+}
+
 int
 main (gint   argc,
       gchar *argv[])
@@ -291,6 +310,7 @@ main (gint   argc,
                         "visible", TRUE,
                         "width-chars", 30,
                         NULL);
+  g_signal_connect (entry, "notify::suggestion", G_CALLBACK (notify_suggestion_cb), NULL);
   dzl_suggestion_entry_set_position_func (DZL_SUGGESTION_ENTRY (entry),
                                           dzl_suggestion_entry_window_position_func,
                                           NULL, NULL);


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