[英]Gnome-shell extension puzzle: setting variables not working?
在我看来,这必须是对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 toggle
到1,反之亦然。
取而代之的是,每次我单击该按钮时,都会显示相同的“设置切换为1”消息。
谁能解释发生了什么事? 谢谢。
我认为_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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.