[gnome-photos] application: Use a GResource for the application menu



commit 755d1fb47d2c4a30523691afa02e10929692d306
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Nov 14 09:36:10 2012 +0100

    application: Use a GResource for the application menu
    
    Editing an XML file is way easier than writing code manually for the
    menu.
    
    Original patch from Cosimo Cecchi for gnome-documents.

 configure.ac             |    3 +++
 src/Makefile.am          |   16 ++++++++++++++++
 src/photos-app-menu.ui   |   20 ++++++++++++++++++++
 src/photos-application.c |   27 ++++++++++++++++++---------
 src/photos.gresource.xml |    6 ++++++
 5 files changed, 63 insertions(+), 9 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 523510a..4891df5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -103,6 +103,9 @@ LIBGD_INIT([
   static
 ])
 
+GLIB_COMPILE_RESOURCES=`$PKG_CONFIG --variable glib_compile_resources gio-2.0`
+AC_SUBST(GLIB_COMPILE_RESOURCES)
+
 AC_CONFIG_FILES([
 Makefile
 data/Makefile
diff --git a/src/Makefile.am b/src/Makefile.am
index 71d95f9..01550b1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,6 +7,8 @@ gnome_photos_built_sources = \
 	photos-enums.h \
 	photos-marshalers.c \
 	photos-marshalers.h \
+	photos-resources.c \
+	photos-resources.h \
 	photos-tracker-resources.c \
 	photos-tracker-resources.h \
 	$(NULL)
@@ -126,6 +128,8 @@ EXTRA_DIST = \
 	photos-enums.c.template \
 	photos-enums.h.template \
 	photos-generate-about \
+	photos.gresource.xml \
+	photos-app-menu.ui \
 	photos-marshalers.list \
 	photos-tracker-resources.xml \
 	$(null)
@@ -264,6 +268,18 @@ photos-marshalers.c: photos-marshalers.list photos-marshalers.h Makefile
 		&& rm -f xgen-tmc \
 	)
 
+photos-resources.c: photos.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --generate-dependencies $(srcdir)/photos.gresource.xml)
+	$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ \
+		--sourcedir=$(srcdir) \
+		--generate-source \
+		$<
+
+photos-resources.h: photos.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --generate-dependencies $(srcdir)/photos.gresource.xml)
+	$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ \
+		--sourcedir=$(srcdir) \
+		--generate-header \
+		$<
+
 photos-tracker-resources.h photos-tracker-resources.c: photos-tracker-resources.xml
 	gdbus-codegen \
 		--c-namespace Tracker \
diff --git a/src/photos-app-menu.ui b/src/photos-app-menu.ui
new file mode 100644
index 0000000..fcf36e1
--- /dev/null
+++ b/src/photos-app-menu.ui
@@ -0,0 +1,20 @@
+<interface>
+  <menu id="app-menu">
+    <section>
+      <item>
+        <attribute name="action">app.fullscreen</attribute>
+        <attribute name="label" translatable="yes">Fullscreen</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="action">app.about</attribute>
+        <attribute name="label" translatable="yes">About Documents</attribute>
+      </item>
+      <item>
+        <attribute name="action">app.quit</attribute>
+        <attribute name="label" translatable="yes">Quit</attribute>
+      </item>
+    </section>
+  </menu>
+</interface>
diff --git a/src/photos-application.c b/src/photos-application.c
index b718d9a..ad43551 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -34,10 +34,12 @@
 #include "photos-application.h"
 #include "photos-main-window.h"
 #include "photos-mode-controller.h"
+#include "photos-resources.h"
 
 
 struct _PhotosApplicationPrivate
 {
+  GResource *resource;
   GSimpleAction *fs_action;
   GtkWidget *main_window;
   PhotosModeController *mode_cntrlr;
@@ -94,9 +96,9 @@ photos_application_startup (GApplication *application)
 {
   PhotosApplication *self = PHOTOS_APPLICATION (application);
   PhotosApplicationPrivate *priv = self->priv;
-  GMenu *doc_actions;
   GMenu *menu;
   GSimpleAction *action;
+  GtkBuilder *builder;
   GtkSettings *settings;
 
   G_APPLICATION_CLASS (photos_application_parent_class)
@@ -108,6 +110,9 @@ photos_application_startup (GApplication *application)
       return;
     }
 
+  priv->resource = photos_get_resource ();
+  g_resources_register (priv->resource);
+
   settings = gtk_settings_get_default ();
   g_object_set (settings, "gtk-application-prefer-dark-theme", TRUE, NULL);
 
@@ -133,16 +138,13 @@ photos_application_startup (GApplication *application)
   g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (action));
   g_object_unref (action);
 
-  menu = g_menu_new ();
-
-  doc_actions = g_menu_new ();
-  g_menu_append (doc_actions, _("Fullscreen"), "app.fullscreen");
-  g_menu_append_section (menu, NULL, G_MENU_MODEL (doc_actions));
-
-  g_menu_append (menu, _("About Photos"), "app.about");
-  g_menu_append (menu, _("Quit"), "app.quit");
+  builder = gtk_builder_new ();
+  gtk_builder_add_from_resource (builder, "/org/gnome/photos/app-menu.ui", NULL);
 
+  menu = G_MENU (gtk_builder_get_object (builder, "app-menu"));
   gtk_application_set_app_menu (GTK_APPLICATION (self), G_MENU_MODEL (menu));
+  g_object_unref (builder);
+
   gtk_application_add_accelerator (GTK_APPLICATION (self), "<Primary>q", "app.quit", NULL);
   gtk_application_add_accelerator (GTK_APPLICATION (self), "F11", "app.fullscreen", NULL);
 
@@ -174,6 +176,13 @@ photos_application_dispose (GObject *object)
   PhotosApplication *self = PHOTOS_APPLICATION (object);
   PhotosApplicationPrivate *priv = self->priv;
 
+  if (priv->resource != NULL)
+    {
+      g_resources_unregister (priv->resource);
+      g_resource_unref (priv->resource);
+      priv->resource = NULL;
+    }
+
   if (priv->fs_action != NULL)
     {
       g_object_unref (priv->fs_action);
diff --git a/src/photos.gresource.xml b/src/photos.gresource.xml
new file mode 100644
index 0000000..3722e36
--- /dev/null
+++ b/src/photos.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/photos">
+    <file alias="app-menu.ui" preprocess="xml-stripblanks">photos-app-menu.ui</file>
+  </gresource>
+</gresources>



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