如何在St.Icon的Gnome Shell扩展中设置PNG文件

问题描述 投票:9回答:4

我的Gnome Shell扩展程序有一个文件夹'icons',里面有一个名为'MyIcon.png'的图标文件。我想把它作为St.Icon对象的参数。

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

谢谢你的帮助。

塞尔丘克。

gnome gnome-shell gnome-shell-extensions
4个回答
10
投票

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

2
投票

昨天偶然发现了同样的问题。

你必须做两件事:

第1步:将此代码添加到“stylesheet.css”

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

'background-size','height'和'width'就是预先缩放图像,可以省略它们以保持原始尺寸。

第2步:在.js文件中写下:

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

1
投票

以下是如何执行此操作的示例:

_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
投票

对于任何想要将其扩展名的图标/(或图像/等)子目录添加到搜索路径以便使用CSS样式的人来说,这对我有用:

function init(metadata) {
    // ...

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

    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.