[gtk/mcatanzaro/#1492: 2/2] filechoosernative: sort current_filter first in filters list



commit fa86859c25132c6160075546ef230f75ef9faeda
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Thu Nov 29 16:14:07 2018 -0600

    filechoosernative: sort current_filter first in filters list
    
    gtk_file_chooser_set_filter() doesn't work for GtkFileChooserNative. The
    previous commit fixed this for fallback dialogs, but the portal has no
    D-Bus interface to designate a default selected filter. Instead, the
    first filter in the list is used as the initial filter. So let's reorder
    the filters to make sure the one that is supposed to be selected gets
    sent first.
    
    This has the downside that the client application's filters will no
    longer be sorted in the proper order. That seems much better than
    selecting the wrong filter by default, though. To fix this without
    reordering the filters would require adding new D-Bus API to
    xdg-desktop-portal.
    
    Fixes #1492

 gtk/gtkfilechoosernativeportal.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtkfilechoosernativeportal.c b/gtk/gtkfilechoosernativeportal.c
index f22d734f5a..b052deed1b 100644
--- a/gtk/gtkfilechoosernativeportal.c
+++ b/gtk/gtkfilechoosernativeportal.c
@@ -231,13 +231,25 @@ open_file_msg_cb (GObject *source_object,
 }
 
 static GVariant *
-get_filters (GtkFileChooser *self)
+get_filters (GtkFileChooserNative *self)
 {
   GSList *list, *l;
   GVariantBuilder builder;
 
   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sa(us))"));
-  list = gtk_file_chooser_list_filters (self);
+  list = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (self));
+
+  /* The current filter should be listed first. */
+  if (self->current_filter)
+    {
+      l = g_slist_find (list, self->current_filter);
+      if (l)
+        {
+          list = g_slist_remove (list, self->current_filter);
+          list = g_slist_prepend (list, self->current_filter);
+        }
+    }
+
   for (l = list; l; l = l->next)
     {
       GtkFileFilter *filter = l->data;
@@ -331,7 +343,7 @@ show_portal_file_chooser (GtkFileChooserNative *self,
                            g_variant_new_string (self->cancel_label));
   g_variant_builder_add (&opt_builder, "{sv}", "modal",
                          g_variant_new_boolean (data->modal));
-  g_variant_builder_add (&opt_builder, "{sv}", "filters", get_filters (GTK_FILE_CHOOSER (self)));
+  g_variant_builder_add (&opt_builder, "{sv}", "filters", get_filters (self));
   if (GTK_FILE_CHOOSER_NATIVE (self)->current_name)
     g_variant_builder_add (&opt_builder, "{sv}", "current_name",
                            g_variant_new_string (GTK_FILE_CHOOSER_NATIVE (self)->current_name));


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