[gnome-weather/wip/gjs-experiments: 3/6] Util: improve GtkBuilder loader



commit 7650e8074a0b8cbf19a4fa87ec26c44e125e15d9
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Mar 9 12:56:34 2013 +0100

    Util: improve GtkBuilder loader
    
    Accept an optional scope object that is exposed to GtkBuilder as 'scope'.
    This allows to use <external-object> to have UI definitions that set
    properties of JS derived classes.
    Additionally, support connecting signals, using property names on the
    scope object.
    
    (XXX: the external-object thing is in the GTK docs, but I saw no code for it...)

 src/util.js |   30 +++++++++++++++++++++++++++++-
 1 files changed, 29 insertions(+), 1 deletions(-)
---
diff --git a/src/util.js b/src/util.js
index f4dcf26..c1fc2ab 100644
--- a/src/util.js
+++ b/src/util.js
@@ -24,9 +24,37 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-function loadUI(resource) {
+function loadUI(resource, scope) {
+    // Normalize scope to be an object or the global object
+    scope = scope || window;
+
     let ui = new Gtk.Builder();
+    if (scope instanceof GObject.Object)
+        ui.expose_object('scope', scope);
+
     ui.add_from_resource(resource);
+    ui.connect_signals_full(function(builder, object, signal_name, handler_name, connect_object, flags) {
+        let realHandler = scope[handler_name];
+        let handler;
+        if (connect_object) {
+            if (flags & GObject.ConnectFlags.SWAPPED) {
+                handler = function () {
+                    let args = [connect_object].concat(Array.prototype.slice.call(arguments, 
1)).concat(arguments[0]);
+                    return realHandler.apply(scope, args);
+                }
+            } else {
+                handler = Lang.bind(scope, realHandler, connect_object);
+            }
+        } else {
+            handler = Lang.bind(scope, realHandler);
+        }
+
+        if (flags & GObject.ConnectFlags.AFTER)
+            object.connect_after(signal_name, handler);
+        else
+            object.connect(signal_name, handler);
+    });
+
     return ui;
 }
 


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