这对于gnome-shell扩展如何工作一定是一些基本的误解(就我而言)。我努力寻找一些文件,但是,唉,它似乎有点稀疏。
我想编写一个简单的扩展来将焦点模式从FFM切换到点击到焦点点击面板中的图标,因为我通常使用FFM,但某些程序被破坏了。所以我从基本gnome-shell-extension-tool --create-extension
开始,并按以下方式修改它:
const St = imports.gi.St;
const Main = imports.ui.main;
const Tweener = imports.ui.tweener;
let text, button, icon;
var toggle;
function _hideHello() {
Main.uiGroup.remove_actor(text);
text = null;
}
function _showHello(what) {
if (!text) {
text = new St.Label({ style_class: 'helloworld-label', text: what });
Main.uiGroup.add_actor(text);
}
text.opacity = 255;
let monitor = Main.layoutManager.primaryMonitor;
text.set_position(Math.floor(monitor.width / 2 - text.width / 2),
Math.floor(monitor.height / 2 - text.height / 2));
Tweener.addTween(text,
{ opacity: 0,
time: 2,
transition: 'easeOutQuad',
onComplete: _hideHello });
}
function _switch() {
if (toggle == 0) {
toggle = 1;
_showHello("Setting toggle to " + toggle);
}
if (toggle == 1) {
toggle = 0;
_showHello("Setting toggle to " + toggle);
}
}
function init() {
button = new St.Bin({ style_class: 'panel-button',
reactive: true,
can_focus: true,
x_fill: true,
y_fill: false,
track_hover: true });
icon = new St.Icon({ icon_name: 'system-run-symbolic',
style_class: 'system-status-icon' });
button.set_child(icon);
toggle = 0;
button.connect('button-press-event', _switch);
}
function enable() {
Main.panel._rightBox.insert_child_at_index(button, 0);
}
function disable() {
Main.panel._rightBox.remove_child(button);
}
(可能天真)的想法是每次按下按钮我都可以将toggle
从0切换到1,反之亦然。
相反,每次点击按钮时都会显示相同的“设置切换为1”消息。
任何人都可以解释发生了什么吗?谢谢。
答案 0 :(得分:2)
我认为_switch
出了问题。在第二个if
语句之前应该有一个else。没有它,第二个if
语句将始终运行,toggle
将始终为0。
当前代码:
if (toggle == 0) {
toggle = 1;
_showHello("Setting toggle to " + toggle);
}
if (toggle == 1) { //at this stage, toggle will always be 1
toggle = 0;
_showHello("Setting toggle to " + toggle);
}
建议代码:
if (toggle == 0) {
toggle = 1;
_showHello("Setting toggle to " + toggle);
} else if (toggle == 1) {
toggle = 0;
_showHello("Setting toggle to " + toggle);
}
作为替代方案,您也可以考虑使用这些来切换值,而不是使用if statements
toggle=!toggle; //value becomes true/false instead of 1/0 if that matters
toggle= toggle ? 0 : 1; //ternary operator