How to set a PNG file in a Gnome Shell extension for St.Icon

3.6k views Asked by At

My Gnome Shell extension has a folder 'icons' and there is an icon file called 'MyIcon.png' inside. I want to take it as a parameter to an St.Icon object.

let icon = new St.Icon({ /* I need what to code here*/ });

Thanks for any help.

Selcuk.

4

There are 4 answers

1
Edouard Chevalier On BEST ANSWER

Here is a solution for GnomeShell v3.8.4:

const St = imports.gi.St;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Gio = imports.gi.Gio;
let gicon = Gio.icon_new_for_string(Me.path + "/icons/my_icon.png");
icon = new St.Icon({ gicon });
3
nils277 On

Stumbled upon the very same problem yesterday.

You have to do two things:

Step 1: add this code to you "stylesheet.css"

.your-icon-name {
    background-image: url("icons/MyIcon.png");
    background-size: 20px;
    height: 20px;
    width: 20px;
}

'background-size', 'height' and 'width' are just there to prescale the image, they can be left out to keep the original size.

Step 2: Write this in your .js file:

let icon = new St.Icon({style_class: 'your-icon-name'});
0
fpmurphy On

Here is an example of how to do it:

_getIconImage: function() {
     let icon_file = this._path + "/icons/icon.png";
     let file = Gio.file_new_for_path(icon_file);
     let icon_uri = file.get_uri();

     return St.TextureCache.get_default().load_uri_async(icon_uri, 64, 64);
},

myIcon = _getIconImage();
0
Richard Fellinger On

For anyone wanting to add their extension's icon/ (or images/, etc.) sub directory to the search path in order to use CSS styles here is what is working for me:

function init(metadata) {
    // ...

    let theme = imports.gi.Gtk.IconTheme.get_default();
    theme.append_search_path(metadata.path + "/icons");

    // ...
}