~gnome-shell-extensions/gnome-shell-extensions/appindicator-support-head

1 by Jonas Kuemmerlin
initial commit
1
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3
const Lang = imports.lang;
4
const St = imports.gi.St;
5
const GdkPixbuf = imports.gi.GdkPixbuf;
6
const Clutter = imports.gi.Clutter;
7
const Cogl = imports.gi.Cogl;
8
const Gio = imports.gi.Gio;
9
const GLib = imports.gi.GLib;
6 by Jonas Kuemmerlin
ported dbusMenu.js to GDbus
10
const byteArray = imports.byteArray;
1 by Jonas Kuemmerlin
initial commit
11
12
/*
13
 * UtilMixin:
14
 * Mixes in the given properties in _mixin into the object
15
 *
16
 */
17
const Mixin = new Lang.Class({
29 by Jonas Kuemmerlin
replacing all tabs with 4 spaces
18
    Name: 'UtilMixin',
19
    
20
    _init: function() {
21
        this._lateMixin = {};
22
    },
23
    
24
    _mixin: {},
25
    
26
    _conserve: [],
27
    
28
    attach: function(o) {
29
        if (!this._mixin) return;
30
        if (this._conserve && this._conserve.forEach) {
31
            o._conserved = {};
32
            this._conserve.forEach(function(e) {
33
                    if (e in o) {
34
                        o._conserved[e] = o[e];
35
                    } else if (o.prototype && e in o.prototype) {
36
                        o._conserved[e] = o.prototype[e];
37
                    } else {
38
                        log("WARNING: attempted to conserve property '"+e+"' but not found.");
39
                    }
40
            });
41
        }
42
        for (var i in this._mixin) {
43
            o[i] = this._mixin[i];
44
        }
45
        for (var i in this._lateMixin) {
46
            o[i] = this._lateMixin[i]
47
        }
48
        if (this._mixinInit) {
32 by Jonas Kuemmerlin
added proper support for the LayoutUpdated event.
49
            this._mixinInit.apply(o, Array.prototype.slice.call(arguments, 1));
29 by Jonas Kuemmerlin
replacing all tabs with 4 spaces
50
        }
51
    }
1 by Jonas Kuemmerlin
initial commit
52
});
53
32 by Jonas Kuemmerlin
added proper support for the LayoutUpdated event.
54
/*
55
 * AsyncTaskQueue:
40 by Jonas Kuemmerlin
Inline documentation updates everywhere
56
 * Schedules asynchrouns tasks which may not overlap during execution
32 by Jonas Kuemmerlin
added proper support for the LayoutUpdated event.
57
 *
58
 * The scheduled functions are required to take a callback as their last arguments, and all other arguments
59
 * need to be bound using Function.prototype.bind
60
 */
61
const AsyncTaskQueue = new Lang.Class({
62
    Name: 'AsyncTaskQueue',
63
    
64
    _init: function() {
65
        this._taskList = [];
66
    },
67
    
68
    // shedule the async task for execution or execute right away if there's no current task
69
    add: function(task, callback, context) {
70
        this._taskList.push({task: task, callback: callback, context: context});
71
        if (this._taskList.length == 1) this._executeNext();
72
    },
73
    
74
    _executeNext: function() {
75
        this._taskList[0].task.call(null, (function() {
76
            if (this._taskList[0].callback) this._taskList[0].callback.apply(this._taskList[0].context, arguments);
77
            this._taskList.shift();
78
            if (this._taskList.length) this._executeNext();
79
        }).bind(this));
80
    }
81
});
82
1 by Jonas Kuemmerlin
initial commit
83
const createActorFromPixmap = function(pixmap, icon_size) {
29 by Jonas Kuemmerlin
replacing all tabs with 4 spaces
84
    if (!(pixmap && pixmap.length)) return null;
85
    // pixmap is actually an array of icons, so that hosts can pick the
1 by Jonas Kuemmerlin
initial commit
86
    // best size (here considered as the area covered by the icon)
87
    // XXX: should we use sum of width and height instead? or consider
88
    // only one dimension?
89
    let best = 0;
90
    let bestHeight = pixmap[0][1];
91
    let goal = icon_size;
92
    for (let i = 1; i < pixmap.length; i++) {
93
        let height = pixmap[i][1];
94
        if (Math.abs(goal - height) < Math.abs(goal - bestHeight)) {
95
            best = i;
96
            bestHeight = height;
97
        }
98
    }
99
    let [width, height, imageData] = pixmap[best];
100
    // each image is ARGB32
101
    // XXX: we're not getting a rowstride! let's hope images are compressed enough
102
    let rowstride = width * 4;
103
    return St.TextureCache.get_default().load_from_raw(imageData, imageData.length,
104
                                                       true, width, height, rowstride,
105
                                                       icon_size);
106
};
107
28 by Jonas Kuemmerlin
refined handling of png icon data; should fix numerous crashes and work more efficiently on recent gjs versions.
108
//data: GBytes
1 by Jonas Kuemmerlin
initial commit
109
const createActorFromMemoryImage = function(data) {
29 by Jonas Kuemmerlin
replacing all tabs with 4 spaces
110
    var stream = Gio.MemoryInputStream.new_from_bytes(data);
111
    var pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, null);
37 by Jonas Kuemmerlin
menu icons: now using St.Icon as suggested by gcampax.
112
    return new St.Icon({ gicon: pixbuf, icon_size: pixbuf.get_width() });
1 by Jonas Kuemmerlin
initial commit
113
}
114
28 by Jonas Kuemmerlin
refined handling of png icon data; should fix numerous crashes and work more efficiently on recent gjs versions.
115
//HACK: GLib.Variant.prototype.get_data_as_bytes only exists in recent gjs versions
116
const variantToGBytes = function(variant) {
29 by Jonas Kuemmerlin
replacing all tabs with 4 spaces
117
    if (typeof(GLib.Variant.prototype.get_data_as_bytes) != "undefined") {
118
        return variant.get_data_as_bytes();
119
    } else {
120
        //FIXME: this is very very inefficient. we're sorry.
121
        var data = variant.deep_unpack(); //will create an array of doubles...
122
        var data_length = data.length;
123
        var array = new imports.byteArray.ByteArray(data_length);
124
        for (var i = 0; i < data_length; i++) {
125
            array[i] = data[i];
126
        }
127
        return GLib.ByteArray.free_to_bytes(array); //this can't be correct but it suprisingly works like a charm.
128
    }
1 by Jonas Kuemmerlin
initial commit
129
}