[英]My gnome-shell extension stops working after a lock-unlock screen cycle
我已经编写了一个(非常简单的)gnome-shell扩展程序来切换焦点模式(在Ubuntu 14.04上为gnome-shell 3.10)。 该扩展在github上可用; 我正在等待提交它,因为它有一个非常令人讨厌的错误---我无法理解,因此要修复。
该扩展基于标准示例扩展,该代码非常简单,尽管需要图标文件才能起作用,但我可以将其完全粘贴到此处。
const FFM_VARIANT='sloppy';
const St = imports.gi.St;
const Main = imports.ui.main;
const Tweener = imports.ui.tweener;
const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const ExtensionUtils = imports.misc.extensionUtils;
const Meta = ExtensionUtils.getCurrentExtension();
const Util = imports.misc.util;
const Gio = imports.gi.Gio;
let text, button, icon_f, icon_c, wm_prefs;
var focus;
const FFM=0;
const CTF=1;
function _set_FFM() {
focus = FFM;
button.set_child(icon_f);
wm_prefs.set_string('focus-mode', FFM_VARIANT);
}
function _set_CTF() {
focus = CTF;
button.set_child(icon_c);
wm_prefs.set_string('focus-mode', 'click');
}
function _hideMsg() {
if (text) {
Main.uiGroup.remove_actor(text);
text = null;
}
}
function _showMsg(what) {
if (!text) {
text = new St.Label({ style_class: 'msg-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: 3,
transition: 'easeOutQuad',
onComplete: _hideMsg });
}
function _switch() {
_hideMsg();
if (focus == FFM) {
_showMsg("Setting Click-to-focus");
_set_CTF();
} else {
_showMsg("Setting Focus-follow-mouse");
_set_FFM();
}
}
function init() {
button = new St.Bin({ style_class: 'panel-button',
reactive: true,
can_focus: true,
x_fill: true,
y_fill: false,
track_hover: true });
Gtk.IconTheme.get_default().append_search_path(Meta.dir.get_child('icons').get_path());
icon_f = new St.Icon({ icon_name: 'fmode',
style_class: 'system-status-icon' });
icon_c = new St.Icon({ icon_name: 'cmode',
style_class: 'system-status-icon' });
wm_prefs=new Gio.Settings({schema: 'org.gnome.desktop.wm.preferences'});
}
function enable() {
// start with the current mode --- sync icon and internal state.
what=wm_prefs.get_string('focus-mode');
if (what == 'click') {
_set_CTF();
} else { // sloppy or mouse
_set_FFM();
}
button.connect('button-press-event', _switch);
Main.panel._rightBox.insert_child_at_index(button, 0);
}
function disable() {
Main.panel._rightBox.remove_child(button);
}
...并且效果很好,每次单击都可以切换焦点模式,更改图标并执行应做的事情。
但是,如果我锁定和解锁屏幕,则扩展名之谜将停止工作。 单击图标仅显示相同的消息(停留在锁定之前的聚焦模式)。 启用和禁用扩展程序将恢复正常工作。
难道不是锁-解锁对应用程序透明吗? 如果没有,我可以使用一些挂钩来强制启用/禁用屏幕解锁吗? 窥镜不会显示任何错误,也看不到任何日志。
在这种情况下,Gnome-Shell会在您锁定屏幕时调用disable()
函数,而在解锁屏幕时调用enable()
函数。 init()
函数在您的会话中执行一次。 因此,每次在enable()
内调用函数时,您的按钮都会在为屏幕解锁时为该函数添加新的连接。
该链接可能会帮助http://blog.mecheye.net/2011/11/modern-gnome-shell-extension-part-1/
您可以尝试使用类似下面的代码的方法来将事件与enable()
和disable()
函数一起使用:
var switch_event;
function enable() {
switch_event = button.connect('button-press-event', _switch);
}
function disable() {
button.disconnect(switch_event);
}
我仍然不知道为什么,但是似乎打了个电话
button.connect('button-press-event', _switch);
从enable()
到init()
可解决此问题。
不过,如果有人可以解释原因,我会标记为正确答案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.