From 2f032f224ae3487edb95355939286b4402be3705 Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Tue, 21 May 2019 20:17:14 -0300 Subject: background: Introduce CcBackgroundChooser It will become essentially what CcBackgroundChooserDialog, but without a dialog and organized differently. Right now, it is as minimal as it could be and only shows wallpapers. As we grow it, it may cover pictures and even colors. --- panels/background/background.gresource.xml | 1 + panels/background/cc-background-chooser.c | 182 ++++++++++++++++ panels/background/cc-background-chooser.h | 37 ++++ panels/background/cc-background-chooser.ui | 76 +++++++ panels/background/cc-background-panel.c | 55 ++--- panels/background/cc-background-panel.ui | 319 ++++++++++++----------------- panels/background/meson.build | 6 +- 7 files changed, 450 insertions(+), 226 deletions(-) create mode 100644 panels/background/cc-background-chooser.c create mode 100644 panels/background/cc-background-chooser.h create mode 100644 panels/background/cc-background-chooser.ui diff --git a/panels/background/background.gresource.xml b/panels/background/background.gresource.xml index fb562fd79..7fa4dbac6 100644 --- a/panels/background/background.gresource.xml +++ b/panels/background/background.gresource.xml @@ -1,6 +1,7 @@ + cc-background-chooser.ui cc-background-panel.ui diff --git a/panels/background/cc-background-chooser.c b/panels/background/cc-background-chooser.c new file mode 100644 index 000000000..2b01bb024 --- /dev/null +++ b/panels/background/cc-background-chooser.c @@ -0,0 +1,182 @@ +/* cc-background-chooser.c + * + * Copyright 2019 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define G_LOG_DOMAIN "cc-background-chooser" + +#include "bg-colors-source.h" +#include "bg-pictures-source.h" +#include "bg-wallpapers-source.h" +#include "cc-background-chooser.h" + +struct _CcBackgroundChooser +{ + GtkBox parent; + + GtkIconView *icon_view; + GtkPopover *selection_popover; + + BgWallpapersSource *wallpapers_source; +}; + +G_DEFINE_TYPE (CcBackgroundChooser, cc_background_chooser, GTK_TYPE_BOX) + +enum +{ + BACKGROUND_CHOSEN, + N_SIGNALS, +}; + +static guint signals [N_SIGNALS]; + +static void +emit_background_chosen (CcBackgroundChooser *self, + CcBackgroundSelectionFlags flags) +{ + g_autolist (GtkTreePath) list = NULL; + CcBackgroundItem *item; + GtkTreeModel *model; + GtkTreeIter iter; + + model = gtk_icon_view_get_model (self->icon_view); + list = gtk_icon_view_get_selected_items (self->icon_view); + g_assert (g_list_length (list) == 1); + + if (gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) list->data) == FALSE) + return; + + gtk_tree_model_get (model, &iter, 1, &item, -1); + + g_signal_emit (self, signals[BACKGROUND_CHOSEN], 0, item, flags); +} + +static void +setup_icon_view (CcBackgroundChooser *self) +{ + GtkCellRenderer *renderer; + GtkListStore *model; + + model = bg_source_get_liststore (BG_SOURCE (self->wallpapers_source)); + + gtk_icon_view_set_model (self->icon_view, GTK_TREE_MODEL (model)); + + renderer = gtk_cell_renderer_pixbuf_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self->icon_view), + renderer, + FALSE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self->icon_view), + renderer, + "surface", 0, + NULL); + +} + +static void +on_selection_desktop_lock_clicked_cb (GtkButton *button, + CcBackgroundChooser *self) +{ + emit_background_chosen (self, CC_BACKGROUND_SELECTION_DESKTOP | CC_BACKGROUND_SELECTION_LOCK_SCREEN); + gtk_popover_popdown (self->selection_popover); +} + +static void +on_selection_desktop_clicked_cb (GtkButton *button, + CcBackgroundChooser *self) +{ + emit_background_chosen (self, CC_BACKGROUND_SELECTION_DESKTOP); + gtk_popover_popdown (self->selection_popover); +} + +static void +on_selection_lock_clicked_cb (GtkButton *button, + CcBackgroundChooser *self) +{ + emit_background_chosen (self, CC_BACKGROUND_SELECTION_LOCK_SCREEN); + gtk_popover_popdown (self->selection_popover); +} + +static void +on_selection_changed_cb (GtkIconView *icon_view, + CcBackgroundChooser *self) +{ +} + +static void +on_item_activated_cb (GtkIconView *icon_view, + GtkTreePath *path, + CcBackgroundChooser *self) +{ + GdkRectangle rect; + + g_message ("Item activated"); + + gtk_icon_view_get_cell_rect (icon_view, path, NULL, &rect); + gtk_popover_set_pointing_to (self->selection_popover, &rect); + gtk_popover_popup (self->selection_popover); +} + +/* GObject overrides */ + +static void +cc_background_chooser_finalize (GObject *object) +{ + CcBackgroundChooser *self = (CcBackgroundChooser *)object; + + g_clear_object (&self->wallpapers_source); + + G_OBJECT_CLASS (cc_background_chooser_parent_class)->finalize (object); +} + +static void +cc_background_chooser_class_init (CcBackgroundChooserClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->finalize = cc_background_chooser_finalize; + + signals[BACKGROUND_CHOSEN] = g_signal_new ("background-chosen", + CC_TYPE_BACKGROUND_CHOOSER, + G_SIGNAL_RUN_FIRST, + 0, NULL, NULL, NULL, + G_TYPE_NONE, + 2, + CC_TYPE_BACKGROUND_ITEM, + G_TYPE_INT); + + gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/background/cc-background-chooser.ui"); + + gtk_widget_class_bind_template_child (widget_class, CcBackgroundChooser, icon_view); + gtk_widget_class_bind_template_child (widget_class, CcBackgroundChooser, selection_popover); + + gtk_widget_class_bind_template_callback (widget_class, on_item_activated_cb); + gtk_widget_class_bind_template_callback (widget_class, on_selection_changed_cb); + gtk_widget_class_bind_template_callback (widget_class, on_selection_desktop_lock_clicked_cb); + gtk_widget_class_bind_template_callback (widget_class, on_selection_desktop_clicked_cb); + gtk_widget_class_bind_template_callback (widget_class, on_selection_lock_clicked_cb); +} + +static void +cc_background_chooser_init (CcBackgroundChooser *self) +{ + gtk_widget_init_template (GTK_WIDGET (self)); + + self->wallpapers_source = bg_wallpapers_source_new (GTK_WIDGET (self)); + setup_icon_view (self); +} diff --git a/panels/background/cc-background-chooser.h b/panels/background/cc-background-chooser.h new file mode 100644 index 000000000..6d88785e7 --- /dev/null +++ b/panels/background/cc-background-chooser.h @@ -0,0 +1,37 @@ +/* cc-background-chooser.h + * + * Copyright 2019 Georges Basile Stavracas Neto + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +typedef enum +{ + CC_BACKGROUND_SELECTION_NONE = 0, + CC_BACKGROUND_SELECTION_DESKTOP = 1 << 0, + CC_BACKGROUND_SELECTION_LOCK_SCREEN = 1 << 1, +} CcBackgroundSelectionFlags; + +#define CC_TYPE_BACKGROUND_CHOOSER (cc_background_chooser_get_type()) +G_DECLARE_FINAL_TYPE (CcBackgroundChooser, cc_background_chooser, CC, BACKGROUND_CHOOSER, GtkBox) + +G_END_DECLS diff --git a/panels/background/cc-background-chooser.ui b/panels/background/cc-background-chooser.ui new file mode 100644 index 000000000..7bab33f26 --- /dev/null +++ b/panels/background/cc-background-chooser.ui @@ -0,0 +1,76 @@ + + + + + + + icon_view + bottom + + + True + False + vertical + 6 + 12 + + + True + Set Background and Lock Screen + + + + + + + True + Set Background + + + + + + True + Set Lock Screen + + + + + + + diff --git a/panels/background/cc-background-panel.c b/panels/background/cc-background-panel.c index 2f034f3b2..098a86ae7 100644 --- a/panels/background/cc-background-panel.c +++ b/panels/background/cc-background-panel.c @@ -29,6 +29,7 @@ #include "cc-background-panel.h" +#include "cc-background-chooser.h" #include "cc-background-chooser-dialog.h" #include "cc-background-item.h" #include "cc-background-resources.h" @@ -305,6 +306,7 @@ copy_finished_cb (GObject *source_object, if (create_save_dir ()) cc_background_xml_save (current_background, filename); } + static void set_background (CcBackgroundPanel *panel, GSettings *settings, @@ -434,51 +436,19 @@ set_background (CcBackgroundPanel *panel, } } -static void -on_chooser_dialog_response (GtkDialog *dialog, - int response_id, - CcBackgroundPanel *panel) -{ - if (response_id == GTK_RESPONSE_OK) - { - g_autoptr(CcBackgroundItem) item = NULL; - - item = cc_background_chooser_dialog_get_item (CC_BACKGROUND_CHOOSER_DIALOG (dialog)); - if (item != NULL) - set_background (panel, g_object_get_data (G_OBJECT (dialog), "settings"), item); - } - - gtk_widget_destroy (GTK_WIDGET (dialog)); -} static void -launch_chooser (CcBackgroundPanel *panel, - GSettings *settings) +on_chooser_background_chosen_cb (CcBackgroundChooser *chooser, + CcBackgroundItem *item, + CcBackgroundSelectionFlags flags, + CcBackgroundPanel *self) { - GtkWidget *toplevel; - GtkWidget *dialog; - - toplevel = gtk_widget_get_toplevel (GTK_WIDGET (panel)); - dialog = cc_background_chooser_dialog_new (GTK_WINDOW (toplevel)); - g_object_set_data (G_OBJECT (dialog), "settings", settings); - gtk_widget_show (dialog); - g_signal_connect (dialog, "response", G_CALLBACK (on_chooser_dialog_response), panel); - panel->chooser = dialog; - g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &panel->chooser); -} -static void -on_background_button_clicked_cb (GtkButton *button, - CcBackgroundPanel *panel) -{ - launch_chooser (panel, panel->settings); -} + if (flags & CC_BACKGROUND_SELECTION_DESKTOP) + set_background (self, self->settings, item); -static void -on_lock_button_clicked_cb (GtkButton *button, - CcBackgroundPanel *panel) -{ - launch_chooser (panel, panel->lock_settings); + if (flags & CC_BACKGROUND_SELECTION_LOCK_SCREEN) + set_background (self, self->lock_settings, item); } static gboolean @@ -541,6 +511,8 @@ cc_background_panel_class_init (CcBackgroundPanelClass *klass) CcPanelClass *panel_class = CC_PANEL_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + g_type_ensure (CC_TYPE_BACKGROUND_CHOOSER); + panel_class->get_help_uri = cc_background_panel_get_help_uri; object_class->dispose = cc_background_panel_dispose; @@ -554,8 +526,7 @@ cc_background_panel_class_init (CcBackgroundPanelClass *klass) gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, lock_drawing_area); gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, lock_slide_image); - gtk_widget_class_bind_template_callback (widget_class, on_background_button_clicked_cb); - gtk_widget_class_bind_template_callback (widget_class, on_lock_button_clicked_cb); + gtk_widget_class_bind_template_callback (widget_class, on_chooser_background_chosen_cb); gtk_widget_class_bind_template_callback (widget_class, on_lock_preview_draw_cb); gtk_widget_class_bind_template_callback (widget_class, on_preview_draw_cb); } diff --git a/panels/background/cc-background-panel.ui b/panels/background/cc-background-panel.ui index 6a5273fd6..c7823f54a 100644 --- a/panels/background/cc-background-panel.ui +++ b/panels/background/cc-background-panel.ui @@ -5,241 +5,194 @@ True False - + True False - 24 - 32 - center + vertical + - + True False - end - center - vertical + 24 + 32 + center - + True - True - True - + False + end + center + vertical - + True - False - 6 - 6 - vertical - - - 310 - 170 - True - False - center - True - True - - - + True + True + - + True False - _Background - True - background-set-button + 6 + 6 + vertical + + + 310 + 170 + True + False + center + + + + + + True + False + _Background + True + background-set-button + + - - - - - True - False - 12 - + True False - center - 12 - 2 - - - True - False - slideshow-symbolic - - - False - True - 0 - - + 12 - + True False - + center + 12 + 2 + + + True + False + slideshow-symbolic + + + + + True + False + + + + + + + False + 0 + Changes throughout the day + + - - False - True - 1 - - - - - - False - 0 - Changes throughout the day - - - False - True - 1 - - - True - True - 0 - - - False - True - 2 - - - - False - True - 0 - - - - - True - False - vertical - start - center - + True - True - True - + False + vertical + start + center - + True - False - 6 - 6 - vertical - - - 310 - 170 - True - False - center - True - True - - - + True + True + - + True False - _Lock Screen - True - background-lock-set-button + 6 + 6 + vertical + + + 310 + 170 + True + False + center + + + + + + True + False + _Lock Screen + True + background-lock-set-button + + - - - - - True - False - 12 - + True False - center - 12 - 2 - - - True - False - slideshow-symbolic - - - False - True - 0 - - + 12 - + True False - + center + 12 + 2 + + + True + False + slideshow-symbolic + + + + + True + False + + + + + + + False + 0 + Changes throughout the day + + - - False - True - 1 - - - - - - False - 0 - Changes throughout the day - - - False - True - 1 - - - True - True - 0 - - - False - True - 2 - - - False - True - 1 - + + + + + True + False + True + + diff --git a/panels/background/meson.build b/panels/background/meson.build index 27202baf6..3dbe86107 100644 --- a/panels/background/meson.build +++ b/panels/background/meson.build @@ -59,7 +59,10 @@ common_sources += gnome.mkenums( vtail: ' { 0, NULL, NULL }\n };\n etype = g_@type@_register_static ("@EnumName@", values);\n }\n return etype;\n}\n' ) -resource_data = files('cc-background-panel.ui') +resource_data = files( + 'cc-background-chooser.ui', + 'cc-background-panel.ui', +) common_sources += gnome.compile_resources( 'cc-@0@-resources'.format(cappletname), @@ -74,6 +77,7 @@ sources = common_sources + files( 'bg-pictures-source.c', 'bg-source.c', 'bg-wallpapers-source.c', + 'cc-background-chooser.c', 'cc-background-chooser-dialog.c', 'cc-background-grilo-miner.c', 'cc-background-item.c', -- cgit v1.2.1