LCOV - code coverage report
Current view: top level - modules/core/overrides - Gtk.js (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 96.9 % 98 95
Test Date: 2025-07-16 04:58:21 Functions: 90.9 % 11 10
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 76.7 % 60 46

             Branch data     Line data    Source code
       1                 :          16 : // application/javascript;version=1.8
       2                 :             : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
       3                 :             : // SPDX-FileCopyrightText: 2013 Giovanni Campagna
       4                 :             : 
       5                 :           9 : const Legacy = imports._legacy;
       6                 :           9 : const {Gio, GjsPrivate, GLib, GObject} = imports.gi;
       7                 :           9 : const {_createBuilderConnectFunc, _createClosure, _registerType} = imports._common;
       8                 :           9 : const Gi = imports._gi;
       9                 :             : 
      10                 :           9 : let Gtk;
      11                 :           9 : let TemplateBuilderScope;
      12                 :             : 
      13                 :           9 : function _init() {
      14                 :           9 :     Gtk = this;
      15                 :             : 
      16                 :           9 :     Gtk.children = GObject.__gtkChildren__;
      17                 :           9 :     Gtk.cssName = GObject.__gtkCssName__;
      18                 :           9 :     Gtk.internalChildren = GObject.__gtkInternalChildren__;
      19                 :           9 :     Gtk.template = GObject.__gtkTemplate__;
      20                 :             : 
      21                 :           9 :     let {GtkWidgetClass} = Legacy.defineGtkLegacyObjects(GObject, Gtk);
      22                 :           9 :     Gtk.Widget.prototype.__metaclass__ = GtkWidgetClass;
      23                 :             : 
      24 [ +  + ][ +  + ]:           9 :     if (Gtk.Container && Gtk.Container.prototype.child_set_property) {
      25                 :           8 :         Gtk.Container.prototype.child_set_property = function (child, property, value) {
      26                 :           1 :             GjsPrivate.gtk_container_child_set_property(this, child, property, value);
      27                 :             :         };
      28                 :             :     }
      29                 :             : 
      30         [ +  + ]:           9 :     if (Gtk.CustomSorter) {
      31                 :           1 :         Gtk.CustomSorter.new = GjsPrivate.gtk_custom_sorter_new;
      32                 :           1 :         Gtk.CustomSorter.prototype.set_sort_func = function (sortFunc) {
      33                 :           2 :             GjsPrivate.gtk_custom_sorter_set_sort_func(this, sortFunc);
      34                 :             :         };
      35                 :             :     }
      36                 :             : 
      37                 :         143 :     Gtk.Widget.prototype._init = function (params) {
      38                 :         134 :         const klass = this.constructor;
      39         [ +  - ]:         134 :         const wrapper = GObject.Object.prototype._init.call(this, params) ?? this;
      40                 :             : 
      41         [ +  + ]:         134 :         if (klass[Gtk.template]) {
      42         [ +  + ]:          45 :             let children = klass[Gtk.children] ?? [];
      43         [ +  + ]:         129 :             for (let child of children) {
      44                 :          84 :                 wrapper[child.replace(/-/g, '_')] =
      45                 :          84 :                     wrapper.get_template_child(klass, child);
      46                 :             :             }
      47                 :             : 
      48         [ +  + ]:          45 :             let internalChildren = klass[Gtk.internalChildren] ?? [];
      49         [ +  + ]:          87 :             for (let child of internalChildren) {
      50                 :          42 :                 wrapper[`_${child.replace(/-/g, '_')}`] =
      51                 :          42 :                     wrapper.get_template_child(klass, child);
      52                 :             :             }
      53                 :             :         }
      54                 :             : 
      55                 :         134 :         return wrapper;
      56                 :         134 :     };
      57                 :             : 
      58                 :           9 :     Gtk.Widget._classInit = function (klass) {
      59                 :          16 :         return GObject.Object._classInit(klass);
      60                 :             :     };
      61                 :             : 
      62                 :          18 :     Object.defineProperty(Gtk.Widget, _registerType, {
      63                 :           9 :         value: _registerWidgetType,
      64                 :           9 :         writable: false,
      65                 :           9 :         configurable: false,
      66                 :           9 :         enumerable: false,
      67                 :             :     });
      68                 :             : 
      69         [ +  + ]:           9 :     if (Gtk.Widget.prototype.get_first_child) {
      70                 :           1 :         Gtk.Widget.prototype[Symbol.iterator] = function* () {
      71         [ #  # ]:           0 :             for (let c = this.get_first_child(); c; c = c.get_next_sibling())
      72                 :           0 :                 yield c;
      73                 :           0 :         };
      74                 :             :     }
      75                 :             : 
      76                 :             :     // Everything after this is GTK4-only, for the Gtk.Builder JS implementation
      77         [ +  + ]:           9 :     if (!Gtk.BuilderScope)
      78                 :           8 :         return;
      79                 :             : 
      80                 :           2 :     TemplateBuilderScope = GObject.registerClass({
      81                 :           1 :         Implements: [Gtk.BuilderScope],
      82         [ -  + ]:           8 :     }, class extends GObject.Object {
      83                 :          44 :         vfunc_create_closure(builder, handlerName, flags, connectObject) {
      84                 :          43 :             const swapped = flags & Gtk.BuilderClosureFlags.SWAPPED;
      85                 :          43 :             const thisArg = builder.get_current_object();
      86                 :          85 :             return Gi.associateClosure(
      87         [ +  + ]:          43 :                 connectObject ?? thisArg,
      88                 :          43 :                 _createClosure(builder, thisArg, handlerName, swapped, connectObject)
      89                 :             :             );
      90                 :          42 :         }
      91                 :             :     });
      92                 :           8 : }
      93                 :             : 
      94                 :          16 : function _registerWidgetType(klass) {
      95                 :          16 :     const template = klass[Gtk.template];
      96                 :          16 :     const cssName = klass[Gtk.cssName];
      97                 :          16 :     const children = klass[Gtk.children];
      98                 :          16 :     const internalChildren = klass[Gtk.internalChildren];
      99                 :             : 
     100         [ +  + ]:          16 :     if (template) {
     101                 :          13 :         klass.prototype._instance_init = function () {
     102                 :          38 :             this.init_template();
     103                 :             :         };
     104                 :             :     }
     105                 :             : 
     106                 :          16 :     GObject.Object[_registerType](klass);
     107                 :             : 
     108         [ +  + ]:          16 :     if (cssName)
     109                 :           4 :         Gtk.Widget.set_css_name.call(klass, cssName);
     110                 :             : 
     111         [ +  + ]:          16 :     if (template) {
     112         [ +  + ]:          13 :         if (typeof template === 'string') {
     113                 :           7 :             try {
     114                 :           7 :                 const uri = GLib.Uri.parse(template, GLib.UriFlags.NONE);
     115                 :           5 :                 const scheme = uri.get_scheme();
     116                 :             : 
     117         [ +  + ]:           5 :                 if (scheme === 'resource') {
     118                 :           2 :                     Gtk.Widget.set_template_from_resource.call(klass, uri.get_path());
     119         [ +  + ]:           3 :                 } else if (scheme === 'file') {
     120                 :           2 :                     const file = Gio.File.new_for_uri(template);
     121 [ -  + ][ -  + ]:           2 :                     const [, contents] = file.load_contents(null);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     122                 :           2 :                     Gtk.Widget.set_template.call(klass, contents);
     123                 :             :                 } else {
     124                 :           1 :                     throw new TypeError(`Invalid template URI: ${template}`);
     125                 :             :                 }
     126                 :           3 :             } catch (err) {
     127         [ +  + ]:           3 :                 if (!(err instanceof GLib.UriError))
     128                 :           1 :                     throw err;
     129                 :             : 
     130                 :           2 :                 const contents = new TextEncoder().encode(template);
     131                 :           2 :                 Gtk.Widget.set_template.call(klass, contents);
     132                 :             :             }
     133                 :             :         } else {
     134                 :           6 :             Gtk.Widget.set_template.call(klass, template);
     135                 :             :         }
     136                 :             : 
     137         [ +  + ]:          12 :         if (TemplateBuilderScope)
     138                 :           7 :             Gtk.Widget.set_template_scope.call(klass, new TemplateBuilderScope());
     139                 :             :         else
     140                 :           5 :             Gtk.Widget.set_connect_func.call(klass, _createBuilderConnectFunc(klass));
     141                 :             :     }
     142                 :             : 
     143         [ +  + ]:          15 :     if (children) {
     144                 :          10 :         children.forEach(child =>
     145                 :          20 :             Gtk.Widget.bind_template_child_full.call(klass, child, false, 0));
     146                 :             :     }
     147                 :             : 
     148         [ +  + ]:          15 :     if (internalChildren) {
     149                 :          10 :         internalChildren.forEach(child =>
     150                 :          10 :             Gtk.Widget.bind_template_child_full.call(klass, child, true, 0));
     151                 :             :     }
     152                 :             : }
        

Generated by: LCOV version 2.0-1