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.

有帮助吗?

解决方案

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

其他提示

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'});

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

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

    // ...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top