[gnome-control-center/gbsneto/background: 13/14] background: Add "Open Picture..." header button



commit 36cecde9bca49f5939b7ed90748efde756f0233c
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon May 27 13:00:26 2019 -0300

    background: Add "Open Picture..." header button
    
    This makes the new Background panel feature-complete; we can
    add and remove custom wallpapers now.

 panels/background/cc-background-chooser.c | 121 ++++++++++++++++++++++++++++++
 panels/background/cc-background-chooser.h |   2 +
 panels/background/cc-background-panel.c   |  27 +++++++
 panels/background/cc-background-panel.ui  |   7 ++
 4 files changed, 157 insertions(+)
---
diff --git a/panels/background/cc-background-chooser.c b/panels/background/cc-background-chooser.c
index dc2de77f7..ccf57964c 100644
--- a/panels/background/cc-background-chooser.c
+++ b/panels/background/cc-background-chooser.c
@@ -20,6 +20,8 @@
 
 #define G_LOG_DOMAIN "cc-background-chooser"
 
+#include <glib/gi18n.h>
+
 #include "bg-colors-source.h"
 #include "bg-pictures-source.h"
 #include "bg-recent-source.h"
@@ -212,6 +214,72 @@ on_item_activated_cb (GtkFlowBox          *flowbox,
   gtk_popover_popup (self->selection_popover);
 }
 
+static void
+on_file_chooser_response_cb (GtkDialog           *filechooser,
+                             gint                 response,
+                             CcBackgroundChooser *self)
+{
+  g_autofree gchar *filename = NULL;
+
+  if (response != GTK_RESPONSE_ACCEPT)
+    goto out;
+
+  filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));
+
+  bg_recent_source_add_file (self->recent_source, filename);
+
+out:
+  gtk_widget_destroy (GTK_WIDGET (filechooser));
+}
+
+static void
+on_file_chooser_selection_changed_cb (GtkFileChooser               *chooser,
+                                      GnomeDesktopThumbnailFactory *thumbnail_factory)
+{
+  g_autofree gchar *uri = NULL;
+
+  uri = gtk_file_chooser_get_uri (chooser);
+
+  if (uri)
+    {
+      g_autoptr(GFileInfo) file_info = NULL;
+      g_autoptr(GdkPixbuf) pixbuf = NULL;
+      g_autofree gchar *mime_type = NULL;
+      g_autoptr(GFile) file = NULL;
+      GtkWidget *preview;
+
+      preview = gtk_file_chooser_get_preview_widget (chooser);
+
+      file = g_file_new_for_uri (uri);
+      file_info = g_file_query_info (file,
+                                     "standard::*",
+                                     G_FILE_QUERY_INFO_NONE,
+                                     NULL,
+                                     NULL);
+
+      if (file_info && g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY)
+        mime_type = g_strdup (g_file_info_get_content_type (file_info));
+
+      if (mime_type)
+        {
+          pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumbnail_factory,
+                                                                       uri,
+                                                                       mime_type);
+        }
+
+      gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser),
+                                         GTK_RESPONSE_ACCEPT,
+                                         pixbuf != NULL);
+
+      if (pixbuf)
+        gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
+      else
+        gtk_image_set_from_icon_name (GTK_IMAGE (preview), "dialog-question", GTK_ICON_SIZE_DIALOG);
+    }
+
+  gtk_file_chooser_set_preview_widget_active (chooser, TRUE);
+}
+
 /* GObject overrides */
 
 static void
@@ -266,3 +334,56 @@ cc_background_chooser_init (CcBackgroundChooser *self)
   self->wallpapers_source = bg_wallpapers_source_new (GTK_WIDGET (self));
   setup_flowbox (self);
 }
+
+void
+cc_background_chooser_select_file (CcBackgroundChooser *self)
+{
+  g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL;
+  GtkFileFilter *filter;
+  GtkWidget *filechooser;
+  GtkWindow *toplevel;
+  GtkWidget *preview;
+
+  g_return_if_fail (CC_IS_BACKGROUND_CHOOSER (self));
+
+  toplevel = (GtkWindow*) gtk_widget_get_toplevel (GTK_WIDGET (self));
+  filechooser = gtk_file_chooser_dialog_new (_("Select a picture"),
+                                             toplevel,
+                                             GTK_FILE_CHOOSER_ACTION_OPEN,
+                                             _("_Cancel"), GTK_RESPONSE_CANCEL,
+                                             _("_Open"), GTK_RESPONSE_ACCEPT,
+                                             NULL);
+  gtk_window_set_modal (GTK_WINDOW (filechooser), TRUE);
+
+  preview = gtk_image_new ();
+  gtk_widget_set_size_request (preview, 256, -1);
+  gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (filechooser), preview);
+  gtk_file_chooser_set_use_preview_label (GTK_FILE_CHOOSER (filechooser), FALSE);
+  gtk_widget_show (preview);
+
+  factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
+  g_signal_connect_after (filechooser,
+                          "selection-changed",
+                          G_CALLBACK (on_file_chooser_selection_changed_cb),
+                          factory);
+
+  g_object_set_data_full (G_OBJECT (filechooser),
+                          "factory",
+                          g_object_ref (factory),
+                          g_object_unref);
+
+  filter = gtk_file_filter_new ();
+  gtk_file_filter_add_pixbuf_formats (filter);
+  gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (filechooser), filter);
+
+  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filechooser),
+                                       g_get_user_special_dir (G_USER_DIRECTORY_PICTURES));
+
+  g_signal_connect_object (filechooser,
+                           "response",
+                           G_CALLBACK (on_file_chooser_response_cb),
+                           self,
+                           0);
+
+  gtk_window_present (GTK_WINDOW (filechooser));
+}
diff --git a/panels/background/cc-background-chooser.h b/panels/background/cc-background-chooser.h
index 6d88785e7..ced15191b 100644
--- a/panels/background/cc-background-chooser.h
+++ b/panels/background/cc-background-chooser.h
@@ -34,4 +34,6 @@ typedef enum
 #define CC_TYPE_BACKGROUND_CHOOSER (cc_background_chooser_get_type())
 G_DECLARE_FINAL_TYPE (CcBackgroundChooser, cc_background_chooser, CC, BACKGROUND_CHOOSER, GtkBox)
 
+void cc_background_chooser_select_file (CcBackgroundChooser *self);
+
 G_END_DECLS
diff --git a/panels/background/cc-background-panel.c b/panels/background/cc-background-panel.c
index c4d32a1ec..ab62fdcf6 100644
--- a/panels/background/cc-background-panel.c
+++ b/panels/background/cc-background-panel.c
@@ -61,6 +61,8 @@ struct _CcBackgroundPanel
 
   GCancellable *copy_cancellable;
 
+  CcBackgroundChooser *background_chooser;
+  GtkWidget *add_picture_button;
   GtkWidget *bottom_hbox;
   CcBackgroundPreview *desktop_preview;
   CcBackgroundPreview *lock_screen_preview;
@@ -389,12 +391,33 @@ on_chooser_background_chosen_cb (CcBackgroundChooser        *chooser,
     set_background (self, self->lock_settings, item);
 }
 
+static void
+on_add_picture_button_clicked_cb (GtkWidget         *button,
+                                  CcBackgroundPanel *self)
+{
+  cc_background_chooser_select_file (self->background_chooser);
+}
+
 static const char *
 cc_background_panel_get_help_uri (CcPanel *panel)
 {
   return "help:gnome-help/look-background";
 }
 
+static void
+cc_background_panel_constructed (GObject *object)
+{
+  CcBackgroundPanel *self;
+  CcShell *shell;
+
+  self = CC_BACKGROUND_PANEL (object);
+  shell = cc_panel_get_shell (CC_PANEL (self));
+
+  cc_shell_embed_widget_in_header (shell, self->add_picture_button, GTK_POS_RIGHT);
+
+  G_OBJECT_CLASS (cc_background_panel_parent_class)->constructed (object);
+}
+
 static void
 cc_background_panel_dispose (GObject *object)
 {
@@ -436,16 +459,20 @@ cc_background_panel_class_init (CcBackgroundPanelClass *klass)
 
   panel_class->get_help_uri = cc_background_panel_get_help_uri;
 
+  object_class->constructed = cc_background_panel_constructed;
   object_class->dispose = cc_background_panel_dispose;
   object_class->finalize = cc_background_panel_finalize;
 
   gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/control-center/background/cc-background-panel.ui");
 
+  gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, add_picture_button);
+  gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, background_chooser);
   gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, bottom_hbox);
   gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, desktop_preview);
   gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, lock_screen_preview);
 
   gtk_widget_class_bind_template_callback (widget_class, on_chooser_background_chosen_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_add_picture_button_clicked_cb);
 }
 
 static void
diff --git a/panels/background/cc-background-panel.ui b/panels/background/cc-background-panel.ui
index d3c2aa884..066d3874a 100644
--- a/panels/background/cc-background-panel.ui
+++ b/panels/background/cc-background-panel.ui
@@ -71,4 +71,11 @@
       </object>
     </child>
   </template>
+
+  <!-- Header button -->
+  <object class="GtkButton" id="add_picture_button">
+    <property name="visible">True</property>
+    <property name="label" translatable="yes">Add Picture…</property>
+    <signal name="clicked" handler="on_add_picture_button_clicked_cb" object="CcBackgroundPanel" 
swapped="no" />
+  </object>
 </interface>


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