2014-05-18 33 views
2

我已經編寫了一個(非常簡單)的gnome-shell擴展來切換焦點模式(Ubuntu 14.04上的gnome-shell 3.10)。該分機可在github;我等着提交它,因爲它有一個非常煩人的錯誤 - 我無法理解,因此需要修復。我的gnome-shell擴展在鎖定解鎖屏幕循環後停止工作

該擴展是基於標準的示例擴展,代碼非常簡單,我可以完全粘貼它,儘管它需要圖標文件的工作。

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); 
} 

...它完美地工作,每次點擊切換焦點模式,更改圖標和做它應該做的事情。

但是,如果我鎖定和解鎖屏幕,擴展令人困惑停止工作。點擊圖標就會顯示相同的消息(卡在鎖定之前的焦點模式)。啓用和禁用擴展將恢復正確的工作。

不應該對應用程序透明嗎?如果沒有,我可以使用一些鉤子強制屏幕解鎖啓用/禁用?展望玻璃沒有顯示任何錯誤,也沒有任何地方可以看到日誌。

回答

1

在這種情況下,Gnome-Shell在鎖定屏幕和enable()函數時調用disable()函數。 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); 
} 
0

我仍然不知道爲什麼,但似乎從enable()init()修復問題移動電話

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

不過,如果有人能解釋爲什麼,我會標記他們的答案是正確的!