[gtk/suggestion-entry: 26/27] suggestionentry: Add minimum-length



commit 9ca9405cae89a184a2bcf166f342cfcba5fee935
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 26 19:54:37 2020 -0400

    suggestionentry: Add minimum-length
    
    A direct copy of GtkEntryCompletion:minimmum-key-length.

 docs/reference/gtk/gtk4-sections.txt |  2 ++
 gtk/gtksuggestionentry.c             | 67 ++++++++++++++++++++++++++++++++++--
 gtk/gtksuggestionentry.h             |  8 +++++
 3 files changed, 75 insertions(+), 2 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 7763c7d6ad..a8385cbad1 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -7619,4 +7619,6 @@ gtk_suggestion_entry_set_insert_prefix
 gtk_suggestion_entry_get_insert_prefix
 gtk_suggestion_entry_set_show_button
 gtk_suggestion_entry_get_show_button
+gtk_suggestion_entry_set_minimum_length
+gtk_suggestion_entry_get_minimum_length
 </SECTION>
diff --git a/gtk/gtksuggestionentry.c b/gtk/gtksuggestionentry.c
index 351b1f5583..55b0622dbb 100644
--- a/gtk/gtksuggestionentry.c
+++ b/gtk/gtksuggestionentry.c
@@ -115,6 +115,8 @@ struct _GtkSuggestionEntry
 
   gulong changed_id;
 
+  guint minimum_length;
+
   guint use_filter       : 1;
   guint insert_selection : 1;
   guint insert_prefix    : 1;
@@ -139,6 +141,7 @@ enum
   PROP_USE_FILTER,
   PROP_INSERT_PREFIX,
   PROP_INSERT_SELECTION,
+  PROP_MINIMUM_LENGTH,
   PROP_SHOW_BUTTON,
 
   N_PROPERTIES,
@@ -234,6 +237,10 @@ gtk_suggestion_entry_get_property (GObject    *object,
       g_value_set_boolean (value, gtk_suggestion_entry_get_insert_prefix (self));
       break;
 
+    case PROP_MINIMUM_LENGTH:
+      g_value_set_uint (value, gtk_suggestion_entry_get_minimum_length (self));
+      break;
+
     case PROP_SHOW_BUTTON:
       g_value_set_boolean (value, gtk_suggestion_entry_get_show_button (self));
       break;
@@ -289,6 +296,10 @@ gtk_suggestion_entry_set_property (GObject      *object,
       gtk_suggestion_entry_set_insert_prefix (self, g_value_get_boolean (value));
       break;
 
+    case PROP_MINIMUM_LENGTH:
+      gtk_suggestion_entry_set_minimum_length (self, g_value_get_uint (value));
+      break;
+
     case PROP_SHOW_BUTTON:
       gtk_suggestion_entry_set_show_button (self, g_value_get_boolean (value));
       break;
@@ -455,6 +466,13 @@ gtk_suggestion_entry_class_init (GtkSuggestionEntryClass *klass)
                             FALSE,
                             GTK_PARAM_READWRITE);
 
+  properties[PROP_MINIMUM_LENGTH] =
+      g_param_spec_uint ("minimum-length",
+                         P_("Minimum Length"),
+                         P_("Minimum length for matches when filtering"),
+                         0, G_MAXUINT, 1,
+                         GTK_PARAM_READWRITE);
+
   g_object_class_install_properties (object_class, N_PROPERTIES, properties);
   gtk_editable_install_properties (object_class, N_PROPERTIES);
 
@@ -542,17 +560,19 @@ text_changed_idle (gpointer data)
 {
   GtkSuggestionEntry *self = data;
   const char *text;
+  glong len;
   guint matches;
   GtkFilter *filter;
 
   text = gtk_editable_get_text (GTK_EDITABLE (self->entry));
+  len = g_utf8_strlen (text, -1);
   filter = gtk_filter_list_model_get_filter (self->filter_model);
   if (filter)
     gtk_string_filter_set_search (GTK_STRING_FILTER (filter), text);
 
   matches = g_list_model_get_n_items (G_LIST_MODEL (self->selection));
 
-  if (!text || !*text)
+  if (len < self->minimum_length)
     gtk_suggestion_entry_set_popup_visible (self, FALSE);
   else
     gtk_suggestion_entry_set_popup_visible (self, self->use_filter && matches > 0);
@@ -679,7 +699,6 @@ gtk_suggestion_entry_key_pressed (GtkEventControllerKey *controller,
           g_signal_handler_block (self->entry, self->changed_id);
 
           text = self->prefix ? self->prefix : "";
-g_print ("set text: %s\n", text);
           gtk_editable_set_text (GTK_EDITABLE (self->entry), text);
           filter = gtk_filter_list_model_get_filter (self->filter_model);
           if (filter)
@@ -843,7 +862,11 @@ gtk_suggestion_entry_init (GtkSuggestionEntry *self)
   GtkWidget *sw;
   GtkEventController *controller;
 
+  self->minimum_length = 1;
   self->use_filter = TRUE;
+  self->insert_selection = FALSE;
+  self->insert_prefix = FALSE;
+  self->show_button = FALSE;
 
   self->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   gtk_widget_add_css_class (self->box, "linked");
@@ -1480,6 +1503,8 @@ gtk_suggestion_entry_set_show_button (GtkSuggestionEntry *self,
   if (self->show_button == show_button)
     return;
 
+  self->show_button = show_button;
+
   if (self->button)
     gtk_widget_set_visible (self->button, show_button);
 
@@ -1501,3 +1526,41 @@ gtk_suggestion_entry_get_show_button (GtkSuggestionEntry *self)
 
   return self->show_button;
 }
+
+/**
+ * gtk_suggestion_entry_set_minimum_length:
+ * @self: a #GtkSuggestionEntry
+ * @minimum_length: the minimum length of matches when filtering
+ *
+ * Sets the minimum number of characters the user has to enter
+ * before the GtkSuggestionEntry presents the suggestion popup.
+ */
+void
+gtk_suggestion_entry_set_minimum_length (GtkSuggestionEntry *self,
+                                         guint               minimum_length)
+{
+  g_return_if_fail (GTK_IS_SUGGESTION_ENTRY (self));
+
+  if (self->minimum_length == minimum_length)
+    return;
+
+  self->minimum_length = minimum_length;
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MINIMUM_LENGTH]);
+}
+
+/**
+ * gtk_suggestion_entry_get_minimum_length:
+ * @self: a #GtkSuggestionEntry
+ *
+ * Gets the value set by gtk_suggestion_entry_set_minimum_length().
+ *
+ * Returns: the minimum length of matches when filtering
+ */
+guint
+gtk_suggestion_entry_get_minimum_length (GtkSuggestionEntry *self)
+{
+  g_return_val_if_fail (GTK_IS_SUGGESTION_ENTRY (self), 1);
+
+  return self->minimum_length;
+}
diff --git a/gtk/gtksuggestionentry.h b/gtk/gtksuggestionentry.h
index 25ad7074da..4e7d534bc2 100644
--- a/gtk/gtksuggestionentry.h
+++ b/gtk/gtksuggestionentry.h
@@ -90,6 +90,14 @@ void            gtk_suggestion_entry_set_show_button      (GtkSuggestionEntry  *
                                                            gboolean             show_button);
 GDK_AVAILABLE_IN_ALL
 gboolean        gtk_suggestion_entry_get_show_button      (GtkSuggestionEntry  *self);
+
+GDK_AVAILABLE_IN_ALL
+void            gtk_suggestion_entry_set_minimum_length   (GtkSuggestionEntry  *self,
+                                                           guint                minimum_length);
+GDK_AVAILABLE_IN_ALL
+guint           gtk_suggestion_entry_get_minimum_length   (GtkSuggestionEntry  *self);
+
+
 G_END_DECLS
 
 #endif /* __GTK_SUGGESTION_ENTRY_H__ */


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