summaryrefslogtreecommitdiff
path: root/modules/core/overrides/Gtk.js
blob: 90deefb721d7f380b30c3b0e40fead50cf8d79e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// application/javascript;version=1.8
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2013 Giovanni Campagna

const Legacy = imports._legacy;
const {Gio, GjsPrivate, GObject} = imports.gi;
const {_registerType, definePrivateProperties} = imports._common;

let Gtk;
let BuilderScope;

function defineChildren(instance, constructor, target = instance) {
    let children = constructor[Gtk.children] || [];
    for (let child of children) {
        target[child.replace(/-/g, '_')] =
            instance.get_template_child(constructor, child);
    }

    let internalChildren = constructor[Gtk.internalChildren] || [];
    for (let child of internalChildren) {
        target[`_${child.replace(/-/g, '_')}`] =
            instance.get_template_child(constructor, child);
    }
}

function _init() {
    Gtk = this;

    Gtk.children = GObject.__gtkChildren__;
    Gtk.cssName = GObject.__gtkCssName__;
    Gtk.internalChildren = GObject.__gtkInternalChildren__;
    Gtk.template = GObject.__gtkTemplate__;

    let {GtkWidgetClass} = Legacy.defineGtkLegacyObjects(GObject, Gtk);
    Gtk.Widget.prototype.__metaclass__ = GtkWidgetClass;

    if (Gtk.Container && Gtk.Container.prototype.child_set_property) {
        Gtk.Container.prototype.child_set_property = function (child, property, value) {
            GjsPrivate.gtk_container_child_set_property(this, child, property, value);
        };
    }

    Gtk.Widget.prototype._init = function (params) {
        let wrapper = this;

        if (wrapper.constructor[Gtk.template]) {
            if (!BuilderScope) {
                Gtk.Widget.set_connect_func.call(wrapper.constructor,
                    (builder, obj, signalName, handlerName, connectObj, flags) => {
                        const swapped = flags & GObject.ConnectFlags.SWAPPED;
                        const closure = _createClosure(
                            builder, wrapper, handlerName, swapped, connectObj);

                        if (flags & GObject.ConnectFlags.AFTER)
                            obj.connect_after(signalName, closure);
                        else
                            obj.connect(signalName, closure);
                    });
            }
        }

        wrapper = GObject.Object.prototype._init.call(wrapper, params) ?? wrapper;

        if (wrapper.constructor[Gtk.template])
            defineChildren(this, wrapper.constructor);

        return wrapper;
    };

    Gtk.Widget._classInit = function (klass) {
        return GObject.Object._classInit(klass);
    };

    definePrivateProperties(Gtk.Widget, {
        [_registerType]() {
            let klass = this;

            let template = klass[Gtk.template];
            let cssName = klass[Gtk.cssName];
            let children = klass[Gtk.children];
            let internalChildren = klass[Gtk.internalChildren];

            if (template) {
                klass.prototype._instance_init = function () {
                    this.init_template();
                };
            }

            GObject.Object[_registerType].call(klass);

            if (cssName)
                Gtk.Widget.set_css_name.call(klass, cssName);

            if (template) {
                if (typeof template === 'string') {
                    if (template.startsWith('resource:///')) {
                        Gtk.Widget.set_template_from_resource.call(klass,
                            template.slice(11));
                    } else if (template.startsWith('file:///')) {
                        let file = Gio.File.new_for_uri(template);
                        let [, contents] = file.load_contents(null);
                        Gtk.Widget.set_template.call(klass, contents);
                    }
                } else {
                    Gtk.Widget.set_template.call(klass, template);
                }

                if (BuilderScope)
                    Gtk.Widget.set_template_scope.call(klass, new BuilderScope());
            }

            if (children) {
                children.forEach(child =>
                    Gtk.Widget.bind_template_child_full.call(klass, child, false, 0));
            }

            if (internalChildren) {
                internalChildren.forEach(child =>
                    Gtk.Widget.bind_template_child_full.call(klass, child, true, 0));
            }
        },
    });

    if (Gtk.Widget.prototype.get_first_child) {
        Gtk.Widget.prototype[Symbol.iterator] = function* () {
            for (let c = this.get_first_child(); c; c = c.get_next_sibling())
                yield c;
        };
    }

    if (Gtk.BuilderScope) {
        BuilderScope = GObject.registerClass({
            Implements: [Gtk.BuilderScope],
        }, class extends GObject.Object {
            vfunc_create_closure(builder, handlerName, flags, connectObject) {
                const swapped = flags & Gtk.BuilderClosureFlags.SWAPPED;
                return _createClosure(
                    builder, builder.get_current_object(),
                    handlerName, swapped, connectObject);
            }
        });
    }
}

function _createClosure(builder, thisArg, handlerName, swapped, connectObject) {
    connectObject = connectObject || thisArg;

    if (swapped) {
        throw new Error('Unsupported template signal flag "swapped"');
    } else if (typeof thisArg[handlerName] === 'undefined') {
        throw new Error(`A handler called ${handlerName} was not ` +
            `defined on ${thisArg}`);
    }

    return thisArg[handlerName].bind(connectObject);
}