After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 739739 - Unable to use template from resource
Unable to use template from resource
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
: 739706 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2014-11-06 14:31 UTC by Jonas Danielsson
Modified: 2014-11-17 11:53 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Gtk: Scrub resource:// prefix for template path (1.37 KB, patch)
2014-11-06 14:36 UTC, Jonas Danielsson
needs-work Details | Review
Gtk: Add test case for template from resource (29.68 KB, patch)
2014-11-06 14:36 UTC, Jonas Danielsson
none Details | Review
Gtk: Add test case for template from resource (3.70 KB, patch)
2014-11-06 14:40 UTC, Jonas Danielsson
needs-work Details | Review
Gtk: Add test case for template from resource (5.52 KB, patch)
2014-11-06 17:34 UTC, Jonas Danielsson
reviewed Details | Review
Gtk: Scrub resource:// prefix for template path (1.21 KB, patch)
2014-11-06 17:34 UTC, Jonas Danielsson
reviewed Details | Review
Gtk: Add test case for template from resource (7.38 KB, patch)
2014-11-09 19:40 UTC, Jonas Danielsson
none Details | Review
Gtk: Add test case for template from resource (5.78 KB, patch)
2014-11-10 06:46 UTC, Jonas Danielsson
none Details | Review
Gtk: Add test case for template from resource (5.89 KB, patch)
2014-11-10 09:27 UTC, Jonas Danielsson
reviewed Details | Review
Gtk: Scrub resource:// prefix for template path (1.09 KB, patch)
2014-11-13 10:40 UTC, Jonas Danielsson
committed Details | Review
Gtk: Add test case for template from resource (5.55 KB, patch)
2014-11-13 10:40 UTC, Jonas Danielsson
committed Details | Review

Description Jonas Danielsson 2014-11-06 14:31:16 UTC
Hi,

So I wanted to try the new template bindings in Gjs. So I tried it out in Polari.

I kept getting messages like this:
"Unable to load resource for composite template for type 'Gjs_UserListRow': The resource at '/org/gnome/Polari/resources/user-list-row.ui' does not exist"

And the trouble seems to be two-fold.

One is that Gjs sends in the complete string e.g: "resource:///org/gnome/Polari/user-list-row.ui" when Gtk.Widget.bind_template_child_full expects an absolute path like: "/org/gnome/Polari/user-list-row.ui".

That did not help in the Polari case tho. The second issue is that the resource needs to be registered before any import that defines the a template class is used.

So in Polari that meant moving the register call like so:

diff --git a/src/application.js b/src/application.js
index 06528c4..23e0aa7 100644
--- a/src/application.js
+++ b/src/application.js
@@ -9,10 +9,16 @@ const Gtk = imports.gi.Gtk;
 const Polari = imports.gi.Polari;
 const Tp = imports.gi.TelepathyGLib;
 
+// We need to register our resources before we import anything that uses
+// templates. Otherwise the resources will not be found in GtkWidgetClass init.
+const Config = imports.config;
+let resource = Gio.Resource.load(Config.RESOURCE_DIR + '/polari.gresource');
+resource._register();
+
 const AccountsMonitor = imports.accountsMonitor;
 const AppNotifications = imports.appNotifications;
 const ChatroomManager = imports.chatroomManager;
-const Config = imports.config;
+
 const Connections = imports.connections;
 const Format = imports.format;
 const Gettext = imports.gettext;
@@ -45,9 +51,6 @@ const Application = new Lang.Class({
     },
 
     vfunc_startup: function() {
-        let resource = Gio.Resource.load(Config.RESOURCE_DIR + '/polari.gresource');
-        resource._register();
-
         this.parent();
         String.prototype.format = Format.format;


This might be hard to realize for application developers?

I will post a patch to fixup the resource:// path bug and to add a test.
The test is pretty much guaranteed done wrong. I would like some assistance on how to add the resource properly to the test.

Thanks!
Comment 1 Jonas Danielsson 2014-11-06 14:36:49 UTC
Created attachment 290108 [details] [review]
Gtk: Scrub resource:// prefix for template path

Gtk.Widget.bind_template_child_full expects an absolute path to the
resource. We need to scrub the resource:// part of the template string.
Comment 2 Jonas Danielsson 2014-11-06 14:36:58 UTC
Created attachment 290109 [details] [review]
Gtk: Add test case for template from resource
Comment 3 Jonas Danielsson 2014-11-06 14:40:45 UTC
Created attachment 290110 [details] [review]
Gtk: Add test case for template from resource
Comment 4 Bastien Nocera 2014-11-06 14:42:37 UTC
Review of attachment 290108 [details] [review]:

::: modules/overrides/Gtk.js
@@ -52,3 @@
-            if (typeof template == 'string' &&
-                template.startsWith('resource:///'))
-                Gtk.Widget.set_template_from_resource.call(this, template);

Or just use:
Gtk.Widget.set_template_from_resource.call(this, template.slice(11));
Comment 5 Bastien Nocera 2014-11-06 14:43:36 UTC
Review of attachment 290110 [details] [review]:

complex.ui is missing from the patch it seems.
Comment 6 Bastien Nocera 2014-11-06 14:51:10 UTC
Review of attachment 290110 [details] [review]:

Oops, that's the correct status.
Comment 7 Jonas Danielsson 2014-11-06 17:34:27 UTC
Created attachment 290113 [details] [review]
Gtk: Add test case for template from resource
Comment 8 Jonas Danielsson 2014-11-06 17:34:31 UTC
Created attachment 290114 [details] [review]
Gtk: Scrub resource:// prefix for template path

Gtk.Widget.bind_template_child_full expects an absolute path to the
resource. We need to scrub the resource:// part of the template string.
Comment 9 Jonas Danielsson 2014-11-07 12:43:09 UTC
Review of attachment 290113 [details] [review]:

::: installed-tests/js/testGtk.js
@@ +10,3 @@
 
+try {
+    let resource = Gio.Resource.load('installed-tests/js/jsunit.gresource');

Anyone got an idea how to get this filename available for both local and installed tests?
Generating a config.js? How to set the search_path correct in that case?
Comment 10 Giovanni Campagna 2014-11-08 02:05:50 UTC
Review of attachment 290113 [details] [review]:

::: installed-tests/js/testGtk.js
@@ +10,3 @@
 
+try {
+    let resource = Gio.Resource.load('installed-tests/js/jsunit.gresource');

Don't do this. Instead, generate C/H files and compile/link them togheter with gjs-unit.cpp in the jsunit runner.

@@ +95,3 @@
+
+    assertEquals(new MyComplexGtkSubclass());
+    assertEquals(new MyComplexGtkSubclassFromResource());

Please don't use the assertEquals() name, I thought this was a typo (missing JUnit.) before realizing you defined your own assertEquals()
Comment 11 Giovanni Campagna 2014-11-08 02:05:54 UTC
*** Bug 739706 has been marked as a duplicate of this bug. ***
Comment 12 Giovanni Campagna 2014-11-08 02:07:49 UTC
Btw, re the resource registration issue, it is true that resources need to be registered before the JS module is loaded, but it is also true that if one uses package.js or a C launcher binary this works fine already.
Only apps using gnome-documents-style launching need special code - and those should be ported anyway because they don't pack the JS files into GResources.
Comment 13 Jonas Danielsson 2014-11-09 19:39:54 UTC
Review of attachment 290113 [details] [review]:

::: installed-tests/js/testGtk.js
@@ +10,3 @@
 
+try {
+    let resource = Gio.Resource.load('installed-tests/js/jsunit.gresource');

Well, I did this and could not get it to work. And it does not work in the gnome-maps case where bundle the resource like that.

Did I mess up some where? Will upload how I did it. I also included a "standard" builder xml-file in the bundle. And loading that did work. But if I uncomment the template from resource code it says it cannot find the complex.ui resource.
Comment 14 Jonas Danielsson 2014-11-09 19:40:09 UTC
Created attachment 290285 [details] [review]
Gtk: Add test case for template from resource
Comment 15 Mattias Bengtsson 2014-11-09 21:38:06 UTC
(In reply to comment #13)
> Review of attachment 290113 [details] [review]:
> 
> ::: installed-tests/js/testGtk.js
> @@ +10,3 @@
> 
> +try {
> +    let resource = Gio.Resource.load('installed-tests/js/jsunit.gresource');
> 
> Well, I did this and could not get it to work. And it does not work in the
> gnome-maps case where bundle the resource like that.

I tested it a day or two ago and it actually works without doing a manual register in Maps. The thing that made stuff fail for Maps was the "resource://"-prefix.
Comment 16 Jonas Danielsson 2014-11-10 06:46:36 UTC
Created attachment 290306 [details] [review]
Gtk: Add test case for template from resource
Comment 17 Jonas Danielsson 2014-11-10 09:21:57 UTC
I ran this on a different system and it passed, so might be that I wasn't fully up-to-date, or that something wasn't clean in my build.
Comment 18 Jonas Danielsson 2014-11-10 09:27:18 UTC
Created attachment 290322 [details] [review]
Gtk: Add test case for template from resource
Comment 19 Bastien Nocera 2014-11-13 09:15:32 UTC
Review of attachment 290322 [details] [review]:

Rest looks good.

::: Makefile-insttest.am
@@ +31,3 @@
+jsunit_SOURCES =	\
+	installed-tests/gjs-unit.cpp \
+	jsunit-resources.c

js-unit-resources.h is missing from the sources list

@@ +34,3 @@
+
+jsunit_resources_files = $(shell glib-compile-resources --sourcedir=$(srcdir)/installed-tests/js --generate-dependencies $(srcdir)/test/jsunit.gresource.xml)
+jsunit-resources.h: $(srcdir)/installed-tests/js/jsunit.gresources.xml $(jsunit_resource_files)

You'll need to add the generated files to the CLEANFILES.

::: installed-tests/js/testGtk.js
@@ +79,3 @@
     win.add(content);
 
+    JSUnit.assertEquals("label is set to 'Complex!'",

No need to change the indentation of those lines...
Comment 20 Bastien Nocera 2014-11-13 09:18:16 UTC
Review of attachment 290114 [details] [review]:

::: modules/overrides/Gtk.js
@@ -51,3 @@
         if (template) {
-            if (typeof template == 'string' &&
-                template.startsWith('resource:///'))

No need to change the indentation here either.
Comment 21 Jonas Danielsson 2014-11-13 10:40:47 UTC
Created attachment 290622 [details] [review]
Gtk: Scrub resource:// prefix for template path

Gtk.Widget.bind_template_child_full expects an absolute path to the
resource. We need to scrub the resource:// part of the template string.
Comment 22 Jonas Danielsson 2014-11-13 10:40:52 UTC
Created attachment 290623 [details] [review]
Gtk: Add test case for template from resource
Comment 23 Bastien Nocera 2014-11-13 10:54:11 UTC
Review of attachment 290622 [details] [review]:

Looks good.
Comment 24 Bastien Nocera 2014-11-13 10:54:55 UTC
Review of attachment 290623 [details] [review]:

Looks good as well.
Comment 25 Mattias Bengtsson 2014-11-17 03:34:04 UTC
Is anything blocking this or can we apply these patches. Giovanni what do you think?
Comment 26 Giovanni Campagna 2014-11-17 05:32:34 UTC
I think that Bastien reviewed the patches already, so they're good to go. I don't know why he didn't mark them acn.
Comment 27 Bastien Nocera 2014-11-17 11:53:42 UTC
(In reply to comment #26)
> I think that Bastien reviewed the patches already, so they're good to go. I
> don't know why he didn't mark them acn.

I didn't think I had the authority, and wanted to be sure somebody with more knowledge of the codebase looked at it. I guess that given that I had worked on shaping the patches for inclusion, I could have acn'ed them.