[gtranslator/gtk4: 40/52] Use GtkDropDown for lang button




commit 1c8c2867c3354236d90394384f34db263a41fb6c
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Fri Jul 22 16:06:47 2022 +0200

    Use GtkDropDown for lang button
    
    With this change we can remove completely the custom widget
    GtrFilterSelection because now it's replaced with GtkDropDown that give
    us almost the same functionality.

 src/gtr-filter-selection-new.c  | 276 -----------------------------------
 src/gtr-filter-selection-old.ui |  75 ----------
 src/gtr-filter-selection.c      | 311 ----------------------------------------
 src/gtr-filter-selection.h      |  69 ---------
 src/gtr-filter-selection.ui     |  49 -------
 src/gtr-lang-button.c           |  89 ++++++++++--
 src/gtr-lang-button.h           |   5 +-
 src/gtr-languages-fetcher.c     |   4 -
 src/gtranslator.gresource.xml   |   1 -
 src/meson.build                 |   1 -
 10 files changed, 83 insertions(+), 797 deletions(-)
---
diff --git a/src/gtr-lang-button.c b/src/gtr-lang-button.c
index d2fefa37..b088fad3 100644
--- a/src/gtr-lang-button.c
+++ b/src/gtr-lang-button.c
@@ -24,10 +24,17 @@
 
 #include "gtr-language.h"
 #include "gtr-lang-button.h"
+#include "gtr-drop-down-option.h"
+
+typedef struct
+{
+  GListStore *model;
+  GtkWidget *widget;
+} GtrLangButtonPrivate;
 
 struct _GtrLangButton
 {
-  GtrFilterSelection parent_instance;
+  AdwBin parent_instance;
 };
 
 enum
@@ -36,7 +43,8 @@ enum
   LAST_SIGNAL
 };
 
-G_DEFINE_TYPE (GtrLangButton, gtr_lang_button, GTR_TYPE_FILTER_SELECTION)
+G_DEFINE_TYPE_WITH_PRIVATE (GtrLangButton, gtr_lang_button, ADW_TYPE_BIN)
+static guint signals[LAST_SIGNAL] = { 0 };
 
 static gpointer
 copy_lang (gconstpointer src,
@@ -48,25 +56,76 @@ copy_lang (gconstpointer src,
   return g_strdup ((char *)langname);
 }
 
+static void
+gtr_lang_button_changed (GtkWidget  *widget,
+                         GParamSpec *spec,
+                         GtrLangButton *self)
+{
+  g_signal_emit (self, signals[CHANGED], 0, NULL);
+}
+
+static void
+gtr_lang_button_dispose (GObject *object)
+{
+  GtrLangButtonPrivate *priv = gtr_lang_button_get_instance_private (GTR_LANG_BUTTON (object));
+
+  g_clear_pointer (&priv->model, g_object_unref);
+  g_clear_pointer (&priv->widget, gtk_widget_unparent);
+
+  G_OBJECT_CLASS (gtr_lang_button_parent_class)->dispose (object);
+}
+
 static void
 gtr_lang_button_class_init (GtrLangButtonClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  object_class->dispose = gtr_lang_button_dispose;
+
+  signals[CHANGED] =
+    g_signal_newv ("changed",
+                   G_OBJECT_CLASS_TYPE (object_class),
+                   G_SIGNAL_RUN_LAST,
+                   NULL, NULL, NULL, NULL,
+                   G_TYPE_NONE, 0, NULL);
 }
 
 static void
 gtr_lang_button_init (GtrLangButton *self)
 {
   const GSList *languages;
+  const GSList *o;
   GSList *langs;
+  GtrLangButtonPrivate *priv = gtr_lang_button_get_instance_private (self);
+  g_autoptr (GtkExpression) expression = NULL;
+
   languages = gtr_language_get_languages ();
   langs = g_slist_copy_deep ((GSList *)languages, copy_lang, NULL);
   langs = g_slist_sort (langs, (GCompareFunc)g_utf8_collate);
-  gtr_filter_selection_set_options (GTR_FILTER_SELECTION (self), langs);
-  gtr_filter_selection_set_text (GTR_FILTER_SELECTION (self), _("Choose Language"));
+
+  expression = gtk_property_expression_new (GTR_TYPE_DROP_DOWN_OPTION, NULL, "name");
+  priv->model = g_list_store_new (GTR_TYPE_DROP_DOWN_OPTION);
+  priv->widget = GTK_WIDGET (
+    gtk_drop_down_new (G_LIST_MODEL (priv->model), expression)
+  );
+  gtk_drop_down_set_enable_search (GTK_DROP_DOWN (priv->widget), TRUE);
+
+  for (o = langs; o != NULL; o = g_slist_next (o))
+    {
+      const char *lang = (char *)o->data;
+      GtrDropDownOption *option = gtr_drop_down_option_new (lang, NULL);
+      g_list_store_append (priv->model, option);
+      g_object_unref (option);
+    }
+
+  adw_bin_set_child (ADW_BIN (self), priv->widget);
 
   if (langs)
     g_slist_free_full (langs, g_free);
-  g_debug("ran init of lang button\n");
+
+  g_signal_connect (priv->widget,
+                    "notify::selected",
+                    G_CALLBACK (gtr_lang_button_changed),
+                    self);
 }
 
 GtrLangButton*
@@ -78,16 +137,30 @@ gtr_lang_button_new () {
 const gchar *
 gtr_lang_button_get_lang (GtrLangButton *self)
 {
-  const GtrFilterOption *opt = gtr_filter_selection_get_option (GTR_FILTER_SELECTION (self));
+  GtrLangButtonPrivate *priv = gtr_lang_button_get_instance_private (GTR_LANG_BUTTON (self));
+  GtrDropDownOption *opt = GTR_DROP_DOWN_OPTION (
+    gtk_drop_down_get_selected_item (GTK_DROP_DOWN (priv->widget))
+  );
   if (!opt)
     return NULL;
-  return opt->name;
+
+  return gtr_drop_down_option_get_name (opt);
 }
 
 void
 gtr_lang_button_set_lang (GtrLangButton *self,
                           const gchar   *name)
 {
-  gtr_filter_selection_set_option (GTR_FILTER_SELECTION (self), name);
+  GtrLangButtonPrivate *priv = gtr_lang_button_get_instance_private (GTR_LANG_BUTTON (self));
+  unsigned int lang_pos = 0;
+  GtrDropDownOption *option = gtr_drop_down_option_new (name, NULL);
+
+  g_list_store_find_with_equal_func (
+    priv->model,
+    option,
+    (GEqualFunc)gtr_drop_down_option_equal,
+    &lang_pos
+  );
+  gtk_drop_down_set_selected (GTK_DROP_DOWN (priv->widget), lang_pos);
 }
 
diff --git a/src/gtr-lang-button.h b/src/gtr-lang-button.h
index 9d79e0d4..ad1ca9bf 100644
--- a/src/gtr-lang-button.h
+++ b/src/gtr-lang-button.h
@@ -19,14 +19,13 @@
 #pragma once
 
 #include <gtk/gtk.h>
-// TODO: use GtkDropDown instead of filter selection
-#include "gtr-filter-selection.h"
+#include <adwaita.h>
 
 G_BEGIN_DECLS
 
 #define GTR_TYPE_LANG_BUTTON (gtr_lang_button_get_type())
 
-G_DECLARE_FINAL_TYPE (GtrLangButton, gtr_lang_button, GTR, LANG_BUTTON, GtrFilterSelection)
+G_DECLARE_FINAL_TYPE (GtrLangButton, gtr_lang_button, GTR, LANG_BUTTON, AdwBin)
 
 GtrLangButton*  gtr_lang_button_new       ();
 const gchar *   gtr_lang_button_get_lang  (GtrLangButton *self);
diff --git a/src/gtr-languages-fetcher.c b/src/gtr-languages-fetcher.c
index e85daf14..6ed9cd56 100644
--- a/src/gtr-languages-fetcher.c
+++ b/src/gtr-languages-fetcher.c
@@ -386,10 +386,6 @@ gtr_languages_fetcher_init (GtrLanguagesFetcher *fetcher)
   /* add items to comboboxes */
   append_from_languages (fetcher);
 
-  g_signal_connect (G_OBJECT (priv->language),
-                    "clicked",
-                    G_CALLBACK (on_language_activate),
-                    fetcher);
   g_signal_connect (GTK_ENTRY (gtk_combo_box_get_child(GTK_COMBO_BOX (priv->language_code))),
                     "activate",
                     G_CALLBACK (on_language_code_activate),
diff --git a/src/gtranslator.gresource.xml b/src/gtranslator.gresource.xml
index 6c32d50e..1df479f7 100644
--- a/src/gtranslator.gresource.xml
+++ b/src/gtranslator.gresource.xml
@@ -14,7 +14,6 @@
     <file preprocess="xml-stripblanks">gtr-window.ui</file>
     <file preprocess="xml-stripblanks">gtr-projects.ui</file>
     <file preprocess="xml-stripblanks">gtr-dl-teams.ui</file>
-    <file preprocess="xml-stripblanks">gtr-filter-selection.ui</file>
     <file preprocess="xml-stripblanks">help-overlay.ui</file>
     <file preprocess="xml-stripblanks">gtr-search-bar.ui</file>
     <file preprocess="xml-stripblanks">gtr-upload-dialog.ui</file>
diff --git a/src/meson.build b/src/meson.build
index 85f4e2a7..9b73441c 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -68,7 +68,6 @@ sources = files(
   'gtr-projects.c',
   'gtr-dl-teams.c',
   'gtr-drop-down-option.c',
-  'gtr-filter-selection.c',
   'gtr-lang-button.c',
   'gtr-progress.c',
   'gtr-window.c',


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