[gnome-control-center/gbsneto/background: 1/14] background: Turn into a template class
- From: Georges Basile Stavracas Neto <gbsneto src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center/gbsneto/background: 1/14] background: Turn into a template class
- Date: Mon, 27 May 2019 16:04:04 +0000 (UTC)
commit da7e0fc37214ed90f52a6a94f596741f02ae4f06
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date: Tue May 21 13:48:46 2019 -0300
background: Turn into a template class
panels/background/cc-background-panel.c | 326 ++++++++++++++-----------------
panels/background/cc-background-panel.ui | 87 ++-------
2 files changed, 162 insertions(+), 251 deletions(-)
---
diff --git a/panels/background/cc-background-panel.c b/panels/background/cc-background-panel.c
index da4071767..2f034f3b2 100644
--- a/panels/background/cc-background-panel.c
+++ b/panels/background/cc-background-panel.c
@@ -48,7 +48,6 @@ struct _CcBackgroundPanel
{
CcPanel parent_instance;
- GtkBuilder *builder;
GDBusConnection *connection;
GSettings *settings;
@@ -61,77 +60,67 @@ struct _CcBackgroundPanel
GCancellable *copy_cancellable;
+ GtkWidget *bottom_hbox;
+ GtkWidget *desktop_drawing_area;
+ GtkWidget *desktop_slide_image;
+ GtkWidget *lock_drawing_area;
+ GtkWidget *lock_slide_image;
+
GtkWidget *spinner;
GtkWidget *chooser;
};
CC_PANEL_REGISTER (CcBackgroundPanel, cc_background_panel)
-#define WID(y) (GtkWidget *) gtk_builder_get_object (panel->builder, y)
-
-static const char *
-cc_background_panel_get_help_uri (CcPanel *panel)
-{
- return "help:gnome-help/look-background";
-}
-
-static void
-cc_background_panel_dispose (GObject *object)
+static GdkPixbuf*
+get_or_create_cached_pixbuf (CcBackgroundPanel *panel,
+ GtkWidget *widget,
+ CcBackgroundItem *background)
{
- CcBackgroundPanel *panel = CC_BACKGROUND_PANEL (object);
-
- g_clear_object (&panel->builder);
-
- /* destroying the builder object will also destroy the spinner */
- panel->spinner = NULL;
-
- g_clear_object (&panel->settings);
- g_clear_object (&panel->lock_settings);
-
- if (panel->copy_cancellable)
- {
- /* cancel any copy operation */
- g_cancellable_cancel (panel->copy_cancellable);
-
- g_clear_object (&panel->copy_cancellable);
- }
+ GtkAllocation allocation;
+ const gint preview_width = 309;
+ const gint preview_height = 168;
+ gint scale_factor;
+ GdkPixbuf *pixbuf;
- if (panel->chooser)
+ pixbuf = g_object_get_data (G_OBJECT (background), "pixbuf");
+ if (pixbuf == NULL)
{
- gtk_widget_destroy (panel->chooser);
- panel->chooser = NULL;
+ gtk_widget_get_allocation (widget, &allocation);
+ scale_factor = gtk_widget_get_scale_factor (widget);
+ pixbuf = cc_background_item_get_frame_thumbnail (background,
+ panel->thumb_factory,
+ preview_width,
+ preview_height,
+ scale_factor,
+ -2, TRUE);
+ g_object_set_data_full (G_OBJECT (background), "pixbuf", pixbuf, g_object_unref);
}
- g_clear_object (&panel->thumb_factory);
-
- G_OBJECT_CLASS (cc_background_panel_parent_class)->dispose (object);
-}
-
-static void
-cc_background_panel_finalize (GObject *object)
-{
- CcBackgroundPanel *panel = CC_BACKGROUND_PANEL (object);
-
- g_clear_object (&panel->current_background);
- g_clear_object (&panel->current_lock_background);
-
- G_OBJECT_CLASS (cc_background_panel_parent_class)->finalize (object);
+ return pixbuf;
}
static void
-cc_background_panel_class_init (CcBackgroundPanelClass *klass)
+update_display_preview (CcBackgroundPanel *panel,
+ GtkWidget *widget,
+ CcBackgroundItem *background)
{
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
- CcPanelClass *panel_class = CC_PANEL_CLASS (klass);
+ GdkPixbuf *pixbuf;
+ cairo_t *cr;
- panel_class->get_help_uri = cc_background_panel_get_help_uri;
+ pixbuf = get_or_create_cached_pixbuf (panel, widget, background);
- object_class->dispose = cc_background_panel_dispose;
- object_class->finalize = cc_background_panel_finalize;
+ cr = gdk_cairo_create (gtk_widget_get_window (widget));
+ gdk_cairo_set_source_pixbuf (cr,
+ pixbuf,
+ 0, 0);
+ cairo_paint (cr);
+ cairo_destroy (cr);
}
static CcBackgroundItem *
-get_current_background (CcBackgroundPanel *panel, GSettings *settings)
+get_current_background (CcBackgroundPanel *panel,
+ GSettings *settings)
{
if (settings == panel->settings)
return panel->current_background;
@@ -169,17 +158,13 @@ update_preview (CcBackgroundPanel *panel,
if (settings == panel->settings)
{
- gtk_widget_set_visible (WID ("slide_image"), changes_with_time);
- gtk_widget_set_visible (WID ("slide-label"), changes_with_time);
-
- gtk_widget_queue_draw (WID ("background-desktop-drawingarea"));
+ gtk_widget_set_visible (panel->desktop_slide_image, changes_with_time);
+ gtk_widget_queue_draw (panel->desktop_drawing_area);
}
else
{
- gtk_widget_set_visible (WID ("slide_image1"), changes_with_time);
- gtk_widget_set_visible (WID ("slide-label1"), changes_with_time);
-
- gtk_widget_queue_draw (WID ("background-lock-drawingarea"));
+ gtk_widget_set_visible (panel->lock_slide_image, changes_with_time);
+ gtk_widget_queue_draw (panel->lock_drawing_area);
}
}
@@ -193,71 +178,6 @@ get_save_path (CcBackgroundPanel *panel, GSettings *settings)
NULL);
}
-static GdkPixbuf*
-get_or_create_cached_pixbuf (CcBackgroundPanel *panel,
- GtkWidget *widget,
- CcBackgroundItem *background)
-{
- GtkAllocation allocation;
- const gint preview_width = 309;
- const gint preview_height = 168;
- gint scale_factor;
- GdkPixbuf *pixbuf;
-
- pixbuf = g_object_get_data (G_OBJECT (background), "pixbuf");
- if (pixbuf == NULL)
- {
- gtk_widget_get_allocation (widget, &allocation);
- scale_factor = gtk_widget_get_scale_factor (widget);
- pixbuf = cc_background_item_get_frame_thumbnail (background,
- panel->thumb_factory,
- preview_width,
- preview_height,
- scale_factor,
- -2, TRUE);
- g_object_set_data_full (G_OBJECT (background), "pixbuf", pixbuf, g_object_unref);
- }
-
- return pixbuf;
-}
-
-static void
-update_display_preview (CcBackgroundPanel *panel,
- GtkWidget *widget,
- CcBackgroundItem *background)
-{
- GdkPixbuf *pixbuf;
- cairo_t *cr;
-
- pixbuf = get_or_create_cached_pixbuf (panel, widget, background);
-
- cr = gdk_cairo_create (gtk_widget_get_window (widget));
- gdk_cairo_set_source_pixbuf (cr,
- pixbuf,
- 0, 0);
- cairo_paint (cr);
- cairo_destroy (cr);
-}
-
-static gboolean
-on_preview_draw (GtkWidget *widget,
- cairo_t *cr,
- CcBackgroundPanel *panel)
-{
- update_display_preview (panel, widget, panel->current_background);
-
- return TRUE;
-}
-
-static gboolean
-on_lock_preview_draw (GtkWidget *widget,
- cairo_t *cr,
- CcBackgroundPanel *panel)
-{
- update_display_preview (panel, widget, panel->current_lock_background);
- return TRUE;
-}
-
static void
reload_current_bg (CcBackgroundPanel *panel,
GSettings *settings)
@@ -345,8 +265,9 @@ copy_finished_cb (GObject *source_object,
GAsyncResult *result,
gpointer pointer)
{
- g_autoptr(GError) err = NULL;
g_autoptr(CcBackgroundPanel) panel = (CcBackgroundPanel *) pointer;
+ g_autoptr(GError) err = NULL;
+ g_autofree gchar *filename = NULL;
CcBackgroundItem *item;
CcBackgroundItem *current_background;
GSettings *settings;
@@ -376,20 +297,14 @@ copy_finished_cb (GObject *source_object,
if (current_background)
cc_background_item_load (current_background, NULL);
- if (panel->builder)
- {
- g_autofree gchar *filename = NULL;
-
- update_preview (panel, settings, item);
- current_background = get_current_background (panel, settings);
+ update_preview (panel, settings, item);
+ current_background = get_current_background (panel, settings);
- /* Save the source XML if there is one */
- filename = get_save_path (panel, settings);
- if (create_save_dir ())
- cc_background_xml_save (current_background, filename);
- }
+ /* Save the source XML if there is one */
+ filename = get_save_path (panel, settings);
+ if (create_save_dir ())
+ cc_background_xml_save (current_background, filename);
}
-
static void
set_background (CcBackgroundPanel *panel,
GSettings *settings,
@@ -457,8 +372,7 @@ set_background (CcBackgroundPanel *panel,
/* create a spinner while the file downloads */
panel->spinner = gtk_spinner_new ();
gtk_spinner_start (GTK_SPINNER (panel->spinner));
- gtk_box_pack_start (GTK_BOX (WID ("bottom-hbox")), panel->spinner, FALSE,
- FALSE, 6);
+ gtk_box_pack_start (GTK_BOX (panel->bottom_hbox), panel->spinner, FALSE, FALSE, 6);
gtk_widget_show (panel->spinner);
/* reference the panel in case it is removed before the copy is
@@ -541,9 +455,11 @@ static void
launch_chooser (CcBackgroundPanel *panel,
GSettings *settings)
{
+ GtkWidget *toplevel;
GtkWidget *dialog;
- dialog = cc_background_chooser_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (WID
("background-panel"))));
+ 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);
@@ -552,19 +468,98 @@ launch_chooser (CcBackgroundPanel *panel,
}
static void
-on_background_button_clicked (GtkButton *button,
- CcBackgroundPanel *panel)
+on_background_button_clicked_cb (GtkButton *button,
+ CcBackgroundPanel *panel)
{
launch_chooser (panel, panel->settings);
}
static void
-on_lock_button_clicked (GtkButton *button,
- CcBackgroundPanel *panel)
+on_lock_button_clicked_cb (GtkButton *button,
+ CcBackgroundPanel *panel)
{
launch_chooser (panel, panel->lock_settings);
}
+static gboolean
+on_preview_draw_cb (GtkWidget *widget,
+ cairo_t *cr,
+ CcBackgroundPanel *panel)
+{
+ update_display_preview (panel, widget, panel->current_background);
+ return TRUE;
+}
+
+static gboolean
+on_lock_preview_draw_cb (GtkWidget *widget,
+ cairo_t *cr,
+ CcBackgroundPanel *panel)
+{
+ update_display_preview (panel, widget, panel->current_lock_background);
+ return TRUE;
+}
+
+static const char *
+cc_background_panel_get_help_uri (CcPanel *panel)
+{
+ return "help:gnome-help/look-background";
+}
+
+static void
+cc_background_panel_dispose (GObject *object)
+{
+ CcBackgroundPanel *panel = CC_BACKGROUND_PANEL (object);
+
+ /* cancel any copy operation */
+ g_cancellable_cancel (panel->copy_cancellable);
+
+ g_clear_object (&panel->settings);
+ g_clear_object (&panel->lock_settings);
+ g_clear_object (&panel->copy_cancellable);
+ g_clear_object (&panel->thumb_factory);
+
+ g_clear_pointer (&panel->chooser, gtk_widget_destroy);
+
+ G_OBJECT_CLASS (cc_background_panel_parent_class)->dispose (object);
+}
+
+static void
+cc_background_panel_finalize (GObject *object)
+{
+ CcBackgroundPanel *panel = CC_BACKGROUND_PANEL (object);
+
+ g_clear_object (&panel->current_background);
+ g_clear_object (&panel->current_lock_background);
+
+ G_OBJECT_CLASS (cc_background_panel_parent_class)->finalize (object);
+}
+
+static void
+cc_background_panel_class_init (CcBackgroundPanelClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ CcPanelClass *panel_class = CC_PANEL_CLASS (klass);
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+ panel_class->get_help_uri = cc_background_panel_get_help_uri;
+
+ 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, bottom_hbox);
+ gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, desktop_drawing_area);
+ gtk_widget_class_bind_template_child (widget_class, CcBackgroundPanel, desktop_slide_image);
+ 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_lock_preview_draw_cb);
+ gtk_widget_class_bind_template_callback (widget_class, on_preview_draw_cb);
+}
+
static void
on_settings_changed (GSettings *settings,
gchar *key,
@@ -577,23 +572,14 @@ on_settings_changed (GSettings *settings,
static void
cc_background_panel_init (CcBackgroundPanel *panel)
{
- gchar *objects[] = {"background-panel", NULL };
- g_autoptr(GError) err = NULL;
- GtkWidget *widget;
-
- panel->connection = g_application_get_dbus_connection (g_application_get_default ());
g_resources_register (cc_background_get_resource ());
- panel->builder = gtk_builder_new ();
- gtk_builder_add_objects_from_resource (panel->builder,
- "/org/gnome/control-center/background/cc-background-panel.ui",
- objects, &err);
+ gtk_widget_init_template (GTK_WIDGET (panel));
- if (err)
- {
- g_warning ("Could not load ui: %s", err->message);
- return;
- }
+ panel->connection = g_application_get_dbus_connection (g_application_get_default ());
+
+ panel->copy_cancellable = g_cancellable_new ();
+ panel->thumb_factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
panel->settings = g_settings_new (WP_PATH_ID);
g_settings_delay (panel->settings);
@@ -601,22 +587,6 @@ cc_background_panel_init (CcBackgroundPanel *panel)
panel->lock_settings = g_settings_new (WP_LOCK_PATH_ID);
g_settings_delay (panel->lock_settings);
- /* add the top level widget */
- widget = WID ("background-panel");
-
- gtk_container_add (GTK_CONTAINER (panel), widget);
- gtk_widget_show (GTK_WIDGET (panel));
-
- /* setup preview area */
- widget = WID ("background-desktop-drawingarea");
- g_signal_connect (widget, "draw", G_CALLBACK (on_preview_draw), panel);
- widget = WID ("background-lock-drawingarea");
- g_signal_connect (widget, "draw", G_CALLBACK (on_lock_preview_draw), panel);
-
- panel->copy_cancellable = g_cancellable_new ();
-
- panel->thumb_factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
-
/* Load the backgrounds */
reload_current_bg (panel, panel->settings);
update_preview (panel, panel->settings, NULL);
@@ -626,10 +596,4 @@ cc_background_panel_init (CcBackgroundPanel *panel)
/* Background settings */
g_signal_connect (panel->settings, "changed", G_CALLBACK (on_settings_changed), panel);
g_signal_connect (panel->lock_settings, "changed", G_CALLBACK (on_settings_changed), panel);
-
- /* Background buttons */
- widget = WID ("background-set-button");
- g_signal_connect (widget, "clicked", G_CALLBACK (on_background_button_clicked), panel);
- widget = WID ("background-lock-set-button");
- g_signal_connect (widget, "clicked", G_CALLBACK (on_lock_button_clicked), panel);
}
diff --git a/panels/background/cc-background-panel.ui b/panels/background/cc-background-panel.ui
index abf79ea86..6a5273fd6 100644
--- a/panels/background/cc-background-panel.ui
+++ b/panels/background/cc-background-panel.ui
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
- <object class="GtkBox" id="background-panel">
+ <template class="CcBackgroundPanel" parent="CcPanel">
<property name="visible">True</property>
<property name="can_focus">False</property>
- <property name="margin">32</property>
- <property name="spacing">12</property>
- <property name="valign">center</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">24</property>
+ <property name="margin">32</property>
+ <property name="valign">center</property>
<child>
<object class="GtkBox" id="vbox3">
<property name="visible">True</property>
@@ -24,6 +23,7 @@
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
+ <signal name="clicked" handler="on_background_button_clicked_cb" object="CcBackgroundPanel"
swapped="no" />
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
@@ -32,7 +32,7 @@
<property name="spacing">6</property>
<property name="orientation">vertical</property>
<child>
- <object class="GtkDrawingArea" id="background-desktop-drawingarea">
+ <object class="GtkDrawingArea" id="desktop_drawing_area">
<property name="width_request">310</property>
<property name="height_request">170</property>
<property name="visible">True</property>
@@ -40,6 +40,7 @@
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
+ <signal name="draw" handler="on_preview_draw_cb" object="CcBackgroundPanel"
swapped="no" />
</object>
</child>
<child>
@@ -56,7 +57,7 @@
</object>
</child>
<child>
- <object class="GtkBox" id="bottom-hbox">
+ <object class="GtkBox" id="bottom_hbox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">12</property>
@@ -68,7 +69,7 @@
<property name="margin_top">12</property>
<property name="spacing">2</property>
<child>
- <object class="GtkImage" id="slide_image">
+ <object class="GtkImage" id="desktop_slide_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">slideshow-symbolic</property>
@@ -92,8 +93,8 @@
</packing>
</child>
<child>
- <object class="GtkLabel" id="slide-label">
- <property name="visible">True</property>
+ <object class="GtkLabel" id="desktop_slide_label">
+ <property name="visible" bind-source="desktop_slide_image" bind-property="visible"
bind-flags="default|sync-create" />
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes" comments="This refers to a slideshow
background">Changes throughout the day</property>
@@ -137,6 +138,7 @@
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
+ <signal name="clicked" handler="on_lock_button_clicked_cb" object="CcBackgroundPanel"
swapped="no" />
<child>
<object class="GtkBox" id="box3">
<property name="visible">True</property>
@@ -145,7 +147,7 @@
<property name="spacing">6</property>
<property name="orientation">vertical</property>
<child>
- <object class="GtkDrawingArea" id="background-lock-drawingarea">
+ <object class="GtkDrawingArea" id="lock_drawing_area">
<property name="width_request">310</property>
<property name="height_request">170</property>
<property name="visible">True</property>
@@ -153,6 +155,7 @@
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
+ <signal name="draw" handler="on_lock_preview_draw_cb" object="CcBackgroundPanel"
swapped="no" />
</object>
</child>
<child>
@@ -181,7 +184,7 @@
<property name="margin_top">12</property>
<property name="spacing">2</property>
<child>
- <object class="GtkImage" id="slide_image1">
+ <object class="GtkImage" id="lock_slide_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">slideshow-symbolic</property>
@@ -205,8 +208,8 @@
</packing>
</child>
<child>
- <object class="GtkLabel" id="slide-label1">
- <property name="visible">True</property>
+ <object class="GtkLabel">
+ <property name="visible" bind-source="lock_slide_image" bind-property="visible"
bind-flags="default|sync-create" />
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label" translatable="yes" comments="This refers to a slideshow
background">Changes throughout the day</property>
@@ -239,62 +242,6 @@
</packing>
</child>
</object>
- <packing>
- <property name="expand">True</property>
- <property name="fill">True</property>
- <property name="position">0</property>
- </packing>
</child>
- </object>
- <object class="GtkListStore" id="sources-liststore">
- <columns>
- <!-- column-name source-name -->
- <column type="gchararray"/>
- <!-- column-name source-id -->
- <column type="guint"/>
- <!-- column-name source-pointer -->
- <column type="gpointer"/>
- </columns>
- </object>
- <object class="GtkListStore" id="style-liststore">
- <columns>
- <!-- column-name name -->
- <column type="gchararray"/>
- <!-- column-name value -->
- <column type="gint"/>
- </columns>
- <data>
- <row>
- <col id="0" translatable="yes" context="background, style">Tile</col>
- <col id="1">1</col>
- </row>
- <row>
- <col id="0" translatable="yes" context="background, style">Zoom</col>
- <col id="1">5</col>
- </row>
- <row>
- <col id="0" translatable="yes" context="background, style">Center</col>
- <col id="1">2</col>
- </row>
- <row>
- <col id="0" translatable="yes" context="background, style">Scale</col>
- <col id="1">3</col>
- </row>
- <row>
- <col id="0" translatable="yes" context="background, style">Fill</col>
- <col id="1">4</col>
- </row>
- <row>
- <col id="0" translatable="yes" context="background, style">Span</col>
- <col id="1">6</col>
- </row>
- </data>
- </object>
- <object class="GtkSizeGroup" id="sizegroup">
- <property name="mode">vertical</property>
- <widgets>
- <widget name="slide-label"/>
- <widget name="strut"/>
- </widgets>
- </object>
+ </template>
</interface>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]