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

4

9

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.

Yellows answered 5/12, 2013 at 8:21 Comment(0)
T
14

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 });
Toomer answered 14/1, 2014 at 0:57 Comment(1)
Thanks, that is exactly what I was talking about.Yellows
P
6

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'});
Prehistoric answered 21/12, 2013 at 16:37 Comment(3)
Yes, that's what I'm doing right now. However, there is a proper solution or workaround for the problem. I have seen an example where the developer included his own 'icons' directory to the search path. I will let you know where I've seen it whenever I have time for a google. Thanks.Yellows
I'm confused why you think this is not a "proper solution". Is there something this is missing?Supermarket
@EricFossum If we were to compare this to HTML, it would be like using the img tag, then not defining a src but setting the background image style for that img tag. That's what makes this not a proper solution.Rashida
M
0

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();
Mayday answered 27/12, 2013 at 15:55 Comment(0)
M
0

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");

    // ...
}
Mahaffey answered 27/7, 2016 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.