Мое расширение gnome-shell перестает работать после цикла блокировки-разблокировки экрана

Я написал (очень простое) расширение gnome-shell для переключения режима фокусировки (gnome-shell 3.10 в Ubuntu 14.04). Расширение доступно на 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);
}

...и он отлично работает, переключая режим фокусировки при каждом нажатии, меняя значок и делая то, что должен делать.

Но если я блокирую и разблокирую экран, расширение пазлов перестает работать. При нажатии на значок просто отображается то же сообщение (застрявшее в режиме фокусировки, в котором оно было до блокировки). Включение и отключение расширения возобновит правильную работу.

Разве блокировка-разблокировка не должна быть прозрачной для приложений? А если нет, есть какой-то хук, который я могу использовать, чтобы принудительно включить / отключить разблокировку экрана? Зеркало не показывает никаких ошибок, и нигде не видно журналов.


person Rmano    schedule 18.05.2014    source источник


Ответы (2)


В этом случае 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);
}
person GusMilky    schedule 04.01.2018

Я до сих пор не знаю почему, но кажется, что перенос звонка

 button.connect('button-press-event', _switch);

с enable() по init() устраняет проблему.

Тем не менее, если кто-то может объяснить, почему, я отмечу их ответ как правильный!

person Rmano    schedule 18.05.2014