2013-03-15 20 views
0

當我嘗試從肉桂小程序打開GtkWindow時,整個桌面會凍結。
~/.cinnamon/glass.log文件中沒有錯誤。直到Gtk.main()則沒有顯示窗口執行無法從肉桂小程序打開GtkWindow

const Gtk = imports.gi.Gtk; 

function MyApplet(orientation) 
{ 
    this._init(orientation); 
} 

MyApplet.prototype = 
{ 
    __proto__: Applet.IconApplet.prototype, 

    _init: function(orientation) 
    { 
     Applet.IconApplet.prototype._init.call(this, orientation); 

     try { 
      this.set_applet_icon_name("dialog-question"); 
      this.set_applet_tooltip("test"); 
     } 
     catch (e) { 
      global.logError(e); 
     }; 
    }, 

    on_applet_clicked: function(event) 
    {    
     Gtk.init(null, 0); 

     let mwindow = new Gtk.Window ({type : Gtk.WindowType.TOPLEVEL}); 

     mwindow.title = "Hello World!"; 
     mwindow.connect ("destroy", function(){Gtk.main_quit()}); 

     mwindow.show(); 

     Gtk.main(); 
    } 
}; 

function main(metadata, orientation) 
{ 
    let myApplet = new MyApplet(orientation); 
    return myApplet; 
} 

的代碼和桌面獲得凍結。
任何人都知道如何使它正常工作?

+0

我不知道溝通,但你確定調用'Gtk.init'是真的需要嗎?無論如何,我認爲你已經交換了參數,它應該是'Gtk.init(0,null)'。 – rodrigo 2013-03-15 23:25:45

+0

根據文檔,看起來你是對的,但使用'Gtk.init(0,null)'使''參數'argv'的期望類型爲utf8,但在'Gtk'時獲得了類型'number'(nil)'。 init(null,0)'在簡單的Gjs腳本(肉桂小程序之外)中運行良好。另外,我似乎可以從肉桂applet中刪除'Gtk.init',而不做任何更改。 – Nicolas 2013-03-16 08:11:32

回答

0

Javascript不能做多線程,這就是爲什麼叫Gtk.main();打破肉桂。
肉桂小程序已經運行一個主循環,Gtk.main();的調用嘗試創建另一個。
所以無法直接在Javascript中從肉桂applet中打開GtkWindow。
解決方案可能是通過Python腳本打開GtkWindow,並使用DBus在Cinnamon小程序和Python/GTK窗口之間進行通信。

Opened issue in Cinnamon GitHub

0

這是你如何做到這一點:

const Gtk = imports.gi.Gtk; 
const Util = imports.misc.util; 

function MyApplet(orientation) 
{ 
    this._init(orientation); 
} 

MyApplet.prototype = 
{ 
    __proto__: Applet.IconApplet.prototype, 

    _init: function(orientation) 
    { 
     Applet.IconApplet.prototype._init.call(this, orientation); 

     try { 
      this.set_applet_icon_name("dialog-question"); 
      this.set_applet_tooltip("test"); 
     } 
     catch (e) { 
      global.logError(e); 
     }; 
    }, 

    on_applet_clicked: function(event) 
    {    
     //path to your applet directory; hardcoded for now! 
     let path="~/.local/share/cinnamon/applets/[email protected]"; 
     //create in your applet directory a file "yourgtkfile.js" and 
     //make it executable "chmod +x yourgtkfile.js" 
     Util.spawnCommandLine(path + "/yourgtkfile.js"); 
    } 
    }; 

    function main(metadata, orientation) 
    { 
     let myApplet = new MyApplet(orientation); 
     return myApplet; 
    } 

可以在yourgtkfile.js複製/粘貼this。 (用#!/ usr/bin/cjs更改#!/ usr/bin/gjs)

或者,這個(取自here)(用#!/ usr/bin更改#!/ usr/bin/gjs/CJS):

#!/usr/bin/cjs 

const Lang = imports.lang; 
const Gtk = imports.gi.Gtk; 

const Application = new Lang.Class({ 
    //A Class requires an explicit Name parameter. This is the Class Name. 
    Name: 'Application', 

    //create the application 
    _init: function() { 
     this.application = new Gtk.Application(); 

     //connect to 'activate' and 'startup' signals to handlers. 
     this.application.connect('activate', Lang.bind(this, this._onActivate)); 
     this.application.connect('startup', Lang.bind(this, this._onStartup)); 
    }, 

    //create the UI 
    _buildUI: function() { 
     this._window = new Gtk.ApplicationWindow({ application: this.application, 
                title: "Hello World!" }); 
     this._window.set_default_size(200, 200); 
     this.label = new Gtk.Label({ label: "Hello World" }); 
     this._window.add(this.label); 
    }, 

    //handler for 'activate' signal 
    _onActivate: function() { 
     //show the window and all child widgets 
     this._window.show_all(); 
    }, 

    //handler for 'startup' signal 
    _onStartup: function() { 
     this._buildUI(); 
    } 
}); 

//run the application 
let app = new Application(); 
app.application.run(ARGV); 

我假設你不需要與剛剛推出的應用程序:)