[epiphany/wip/exalm/container: 4/6] general-page: Stop using GtkFileChooserButton




commit fb7e8fbf189b05a63dc6cb29298c817633e6027a
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Feb 16 20:01:36 2022 +0500

    general-page: Stop using GtkFileChooserButton
    
    Unfortunately, we have to reimplement fetching the display name. GTK does
    a quite elborate thing here, we don't need to be that fancy, but would be
    nice to have at least Downloads and Home be named as such and not as your
    username. Additionally, since we handle Desktop, let's special case that as
    well.
    
    Make this all a helper function, as we'll need to do it in the download
    dialog as well.
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1072>

 lib/ephy-file-helpers.c                 | 34 ++++++++++++++
 lib/ephy-file-helpers.h                 |  1 +
 src/preferences/prefs-general-page.c    | 83 +++++++++++++++++++++------------
 src/resources/gtk/prefs-general-page.ui | 36 ++++++++++++++
 4 files changed, 124 insertions(+), 30 deletions(-)
---
diff --git a/lib/ephy-file-helpers.c b/lib/ephy-file-helpers.c
index d1ad88e26..469e35200 100644
--- a/lib/ephy-file-helpers.c
+++ b/lib/ephy-file-helpers.c
@@ -175,6 +175,40 @@ ephy_file_desktop_dir (void)
   return g_build_filename (g_get_home_dir (), _("Desktop"), NULL);
 }
 
+char *
+ephy_file_get_display_name (GFile *file)
+{
+  g_autofree char *path = NULL;
+  g_autoptr (GFileInfo) info = NULL;
+  g_autoptr (GError) error = NULL;
+
+  path = g_file_get_path (file);
+
+  if (!g_strcmp0 (path, g_get_home_dir ()))
+    return g_strdup (_("Home"));
+
+  if (!g_strcmp0 (path, ephy_file_desktop_dir ()))
+    return g_strdup (_("Desktop"));
+
+  if (!g_strcmp0 (path, ephy_file_download_dir ()))
+    return g_strdup (_("Downloads"));
+
+  info =
+    g_file_query_info (file,
+                       G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
+                       G_FILE_QUERY_INFO_NONE,
+                       NULL,
+                       &error);
+
+  if (error) {
+    g_warning ("Failed to query display name for %s: %s", path, error->message);
+
+    return g_file_get_basename (file);
+  }
+
+  return g_strdup (g_file_info_get_display_name (info));
+}
+
 /**
  * ephy_file_tmp_filename:
  * @base: the base name of the temp file to create, containing "XXXXXX"
diff --git a/lib/ephy-file-helpers.h b/lib/ephy-file-helpers.h
index c09d145c8..e7a881e42 100644
--- a/lib/ephy-file-helpers.h
+++ b/lib/ephy-file-helpers.h
@@ -60,6 +60,7 @@ char       *       ephy_default_config_dir                  (void);
 void               ephy_file_helpers_shutdown               (void);
 char       *       ephy_file_get_downloads_dir              (void);
 char       *       ephy_file_desktop_dir                    (void);
+char       *       ephy_file_get_display_name               (GFile                 *file);
 const char *       ephy_file_tmp_dir                        (void);
 char       *       ephy_file_tmp_filename                   (const char            *base,
                                                              const char            *extension);
diff --git a/src/preferences/prefs-general-page.c b/src/preferences/prefs-general-page.c
index 0613796a1..cfce24fff 100644
--- a/src/preferences/prefs-general-page.c
+++ b/src/preferences/prefs-general-page.c
@@ -36,8 +36,6 @@
 #include "gnome-languages.h"
 #include <glib/gi18n.h>
 
-#define DOWNLOAD_BUTTON_WIDTH   8
-
 enum {
   COL_LANG_NAME,
   COL_LANG_CODE
@@ -69,6 +67,7 @@ struct _PrefsGeneralPage {
   GtkWidget *download_box;
   GtkWidget *ask_on_download_switch;
   GtkWidget *download_folder_row;
+  GtkWidget *download_folder_label;
 
   /* Search Engines */
   GtkWidget *search_engine_group;
@@ -691,43 +690,57 @@ add_system_language_entry (PrefsGeneralPage *general_page)
 }
 
 static void
-download_path_changed_cb (GtkFileChooser *button)
+download_folder_file_chooser_cb (GtkNativeDialog  *chooser,
+                                 GtkResponseType   response,
+                                 PrefsGeneralPage *general_page)
 {
-  char *dir;
+  if (response == GTK_RESPONSE_ACCEPT) {
+    g_autofree char *dir = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
 
-  dir = gtk_file_chooser_get_filename (button);
-  if (dir == NULL)
-    return;
+    if (dir)
+      g_settings_set_string (EPHY_SETTINGS_STATE,
+                             EPHY_PREFS_STATE_DOWNLOAD_DIR, dir);
+  }
 
-  g_settings_set_string (EPHY_SETTINGS_STATE,
-                         EPHY_PREFS_STATE_DOWNLOAD_DIR, dir);
-  g_free (dir);
+  gtk_native_dialog_destroy (chooser);
 }
 
 static void
-create_download_path_button (PrefsGeneralPage *general_page)
+download_folder_row_activated_cb (PrefsGeneralPage *general_page)
 {
-  GtkWidget *button;
-  char *dir;
+  GtkWidget *parent;
+  g_autofree char *dir = NULL;
+  GtkFileChooserNative *chooser;
+
+  parent = gtk_widget_get_toplevel (GTK_WIDGET (general_page));
+  chooser = gtk_file_chooser_native_new (_("Select a Directory"),
+                                         GTK_WINDOW (parent),
+                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
+                                         _("_Select"),
+                                         _("_Cancel"));
+  gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (chooser), TRUE);
 
   dir = ephy_file_get_downloads_dir ();
+  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), dir);
+
+  g_signal_connect (chooser, "response",
+                    G_CALLBACK (download_folder_file_chooser_cb),
+                    general_page);
+
+  gtk_native_dialog_show (GTK_NATIVE_DIALOG (chooser));
+}
+
+static gboolean
+download_folder_get_mapping (GValue   *value,
+                             GVariant *variant,
+                             gpointer  user_data)
+{
+  g_autofree char *path = ephy_file_get_downloads_dir ();
+  g_autoptr (GFile) dir = g_file_new_for_path (path);
 
-  button = gtk_file_chooser_button_new (_("Select a directory"),
-                                        GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
-
-  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button), dir);
-  gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (button),
-                                           DOWNLOAD_BUTTON_WIDTH);
-  g_signal_connect (button, "selection-changed",
-                    G_CALLBACK (download_path_changed_cb), general_page);
-  gtk_container_add (GTK_CONTAINER (general_page->download_folder_row), button);
-  gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
-  gtk_widget_show (button);
-
-  g_settings_bind_writable (EPHY_SETTINGS_STATE,
-                            EPHY_PREFS_STATE_DOWNLOAD_DIR,
-                            button, "sensitive", FALSE);
-  g_free (dir);
+  g_value_take_string (value, ephy_file_get_display_name (dir));
+
+  return TRUE;
 }
 
 static gboolean
@@ -1068,6 +1081,7 @@ prefs_general_page_class_init (PrefsGeneralPageClass *klass)
   gtk_widget_class_bind_template_child (widget_class, PrefsGeneralPage, download_box);
   gtk_widget_class_bind_template_child (widget_class, PrefsGeneralPage, ask_on_download_switch);
   gtk_widget_class_bind_template_child (widget_class, PrefsGeneralPage, download_folder_row);
+  gtk_widget_class_bind_template_child (widget_class, PrefsGeneralPage, download_folder_label);
 
   /* Search Engines */
   gtk_widget_class_bind_template_child (widget_class, PrefsGeneralPage, search_engine_group);
@@ -1092,6 +1106,7 @@ prefs_general_page_class_init (PrefsGeneralPageClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, on_webapp_icon_button_clicked);
   gtk_widget_class_bind_template_callback (widget_class, on_webapp_entry_changed);
   gtk_widget_class_bind_template_callback (widget_class, on_manage_webapp_additional_urls_button_clicked);
+  gtk_widget_class_bind_template_callback (widget_class, download_folder_row_activated_cb);
 }
 
 static void
@@ -1216,7 +1231,15 @@ setup_general_page (PrefsGeneralPage *general_page)
   if (ephy_is_running_inside_sandbox ())
     gtk_widget_hide (general_page->download_box);
   else
-    create_download_path_button (general_page);
+    g_settings_bind_with_mapping (EPHY_SETTINGS_STATE,
+                                  EPHY_PREFS_STATE_DOWNLOAD_DIR,
+                                  general_page->download_folder_label,
+                                  "label",
+                                  G_SETTINGS_BIND_GET,
+                                  download_folder_get_mapping,
+                                  NULL,
+                                  general_page,
+                                  NULL);
 
   g_settings_bind (web_settings,
                    EPHY_PREFS_WEB_ASK_ON_DOWNLOAD,
diff --git a/src/resources/gtk/prefs-general-page.ui b/src/resources/gtk/prefs-general-page.ui
index 98d534899..2c9a19ade 100644
--- a/src/resources/gtk/prefs-general-page.ui
+++ b/src/resources/gtk/prefs-general-page.ui
@@ -205,6 +205,42 @@
             <property name="title" translatable="yes">_Download Folder</property>
             <property name="use_underline">True</property>
             <property name="visible">True</property>
+            <property name="activatable">True</property>
+            <signal name="activated" handler="download_folder_row_activated_cb" swapped="yes"/>
+            <child>
+              <object class="GtkBox">
+                <property name="visible">True</property>
+                <property name="spacing">6</property>
+                <child>
+                  <object class="GtkImage">
+                    <property name="visible">True</property>
+                    <property name="icon-name">folder-symbolic</property>
+                    <style>
+                      <class name="dim-label"/>
+                    </style>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="download_folder_label">
+                    <property name="visible">True</property>
+                    <property name="ellipsize">end</property>
+                    <property name="xalign">1</property>
+                    <style>
+                      <class name="dim-label"/>
+                    </style>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">go-next-symbolic</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+            </child>
           </object>
         </child>
       </object>


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