[gjs] Gtk: automatically bind declared template children



commit 40ff788fc16fd1f61ddf958289ecfa8c1ffcc011
Author: Giovanni Campagna <scampa giovanni gmail com>
Date:   Thu Oct 30 14:09:13 2014 -0700

    Gtk: automatically bind declared template children
    
    Wrap Gtk.Widget.prototype._init() so that custom subclasses of
    Gtk.Widget that are using the automatic template system will
    also get automatic children properties added.
    
    Note that like the rest of the widget template system, this
    only supports a flat JS inheritance, ie, you cannot inherit
    from a JS class that has a GtkWidget template - only the
    most derived JS class can define a template.
    
    Includes tests by Bastien Nocera.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=737661

 installed-tests/js/testGtk.js |   20 +++++++++++++++++++-
 modules/overrides/Gtk.js      |   16 ++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletions(-)
---
diff --git a/installed-tests/js/testGtk.js b/installed-tests/js/testGtk.js
index 5f66486..bc8ab19 100755
--- a/installed-tests/js/testGtk.js
+++ b/installed-tests/js/testGtk.js
@@ -23,6 +23,18 @@ const template = ' \
         <property name="visible">True</property> \
       </object> \
     </child> \
+    <child> \
+      <object class="GtkLabel" id="label-child2"> \
+        <property name="label">Complex as well!</property> \
+        <property name="visible">True</property> \
+      </object> \
+    </child> \
+    <child> \
+      <object class="GtkLabel" id="internal-label-child"> \
+        <property name="label">Complex and internal!</property> \
+        <property name="visible">True</property> \
+      </object> \
+    </child> \
   </template> \
 </interface>';
 
@@ -30,13 +42,17 @@ const MyComplexGtkSubclass = new Lang.Class({
     Name: 'MyComplexGtkSubclass',
     Extends: Gtk.Grid,
     Template: ByteArray.fromString(template),
-    Children: ['label-child'],
+    Children: ['label-child', 'label-child2'],
+    InternalChildren: ['internal-label-child'],
 
     _init: function(params) {
         this.parent(params);
 
         this._internalLabel = this.get_template_child(MyComplexGtkSubclass, 'label-child');
         JSUnit.assertNotEquals(this._internalLabel, null);
+
+        JSUnit.assertNotEquals(this.label_child2, null);
+        JSUnit.assertNotEquals(this._internal_label_child, null);
     }
 });
 
@@ -48,6 +64,8 @@ function testGtk() {
     win.add(content);
 
     JSUnit.assertEquals("label is set to 'Complex!'", 'Complex!', content._internalLabel.get_label());
+    JSUnit.assertEquals("label is set to 'Complex as well!'", 'Complex as well!', 
content.label_child2.get_label());
+    JSUnit.assertEquals("label is set to 'Complex and internal!'", 'Complex and internal!', 
content._internal_label_child.get_label());
 }
 
 JSUnit.gjstestRun(this, JSUnit.setUp, JSUnit.tearDown);
diff --git a/modules/overrides/Gtk.js b/modules/overrides/Gtk.js
index 5d5228b..9b7d891 100644
--- a/modules/overrides/Gtk.js
+++ b/modules/overrides/Gtk.js
@@ -57,6 +57,8 @@ const GtkWidgetClass = new Lang.Class({
         }
 
         this.Template = template;
+        this.Children = children;
+        this.InternalChildren = internalChildren;
 
         if (children) {
             for (let i = 0; i < children.length; i++)
@@ -94,4 +96,18 @@ function _init() {
             GjsPrivate.gtk_container_child_set_property(this, child, property, value);
         };
     }
+
+    Gtk.Widget.prototype._init = function(params) {
+        GObject.Object.prototype._init.call(this, params);
+
+        if (this.constructor.Template) {
+            let children = this.constructor.Children || [];
+            for (let child of children)
+                this[child.replace('-', '_', 'g')] = this.get_template_child(this.constructor, child);
+
+            let internalChildren = this.constructor.InternalChildren || [];
+            for (let child of internalChildren)
+                this['_' + child.replace('-', '_', 'g')] = this.get_template_child(this.constructor, child);
+        }
+    };
 }


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