1

これは、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);
}

ボタンを押すたびにtoggle0 から 1 に、またはその逆に切り替えることができるという (おそらくナイーブな) 考えでした。

代わりに、ボタンをクリックするたびに、同じ「トグルを 1 に設定しています」というメッセージが表示されます。

誰が何が起こっているのか説明できますか? ありがとう。

4

1 に答える 1

2

に何か問題があると思います_switchif2 番目のステートメントの前に、else が必要です。それがなければ、2 番目の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

フィドルの例

于 2014-05-15T04:28:10.763 に答える