一般方法
IconApplet 类只能处理一个图标。我会使用该类的代码作为模板来编写一个能够处理倍数的类MultiIconApplet。
您可以在/usr/share/cinnamon/js/ui/applet.js中找到原始代码并搜索class IconApplet。复制出来修改一下。在构建一个图标容器的地方,您可以拥有一组它们:
var MultiIconApplet = class MultiIconApplet extends Applet {
// provide the number of icons you need as an extra parameter
_init(orientation, panel_height, icon_count, instance_id) {
super._init(orientation, panel_height, instance_id);
this.orientation = orientation; //orientation makes a difference
this._applet_icon_boxes = []; //array of containers
this._applet_icons = []; //array of icons
for (var i = 0; i < icon_count; i++) {
var box = new St.Bin();
this._applet_icon_boxes.push(box);
box.set_fill(true,true);
box.set_alignment(St.Align.MIDDLE,St.Align.MIDDLE);
this.actor.add(box);
}
// this method constructs the actual icons
_ensureIcon(index) {
if (!this._applet_icons[index] ||
!(this._applet_icons[index] instanceof St.Icon))
this._applet_icons[index] = new St.Icon({
reactive: true, track_hover: true,
style_class: 'applet-icon'
});
this._applet_icon_boxes[index].set_child(this._applet_icons[index]);
}
然后,实现每个IconApplet 方法,但说明您要定位的图标的索引。例如:
set_applet_icon_name (index, icon_name) {
this._ensureIcon(index);
this._applet_icon[index].set_icon_name(icon_name);
this._applet_icon[index].set_icon_type(St.IconType.FULLCOLOR);
this._setStyle();
}
对每个方法重复,将this._applet_icon 与this._applet_icons[index] 交换。
有一种方法会覆盖父类方法,所以你需要在没有参数的情况下实现它并循环遍历图标:
on_panel_height_changed_internal() {
this.applet_icons.forEach((icon, index) => {
if (icon) this._setStyle(index);
)};
this.on_panel_height_changed();
}
最后,你需要实现一个对方向变化的反应,这只是父类中的一个抽象方法:
on_orientation_changed(neworientation) {
this.orientation = neworientation;
if (this.orientation == St.Side.TOP || this.orientation == St.Side.BOTTOM)
this.actor.set_vertical(false);
else
this.actor.set_vertical(true);
}
}
工作区切换器的专门解决方案
由于您要实现的是工作区切换器,因此您也可以从/usr/share/cinnamon/applets/workspace-switcher@cinnamon.org 的标准小程序开始并修改其代码。
您需要做的就是在该小程序中查找类SimpleButton,并在其中添加St.Label 来表示工作区,而不是添加St.Icon:
class SimpleButton extends WorkspaceButton {
constructor(index, applet) {
super(index, applet);
this.actor = new St.Button({ name: 'workspaceButton',
style_class: 'workspace-button',
reactive: applet._draggable.inhibit });
if (applet.orientation == St.Side.TOP || applet.orientation == St.Side.BOTTOM) {
this.actor.set_height(applet._panelHeight);
} else {
this.actor.set_width(applet._panelHeight);
this.actor.add_style_class_name('vertical');
}
let icon = new St.Icon({
icon_name: ..., // choose one based on index
icon_size: applet.getPanelIconSize()
style_class: 'applet-icon'
});
this.actor.set_child(icon);
this.update();
}