0

当我尝试从肉桂小程序打开 GtkWindow 时,整个桌面冻结。
文件中没有错误~/.cinnamon/glass.log

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

代码一直执行,直到Gtk.main()没有窗口显示并且桌面被冻结。
任何人都知道如何使它正常工作?

4

2 回答 2

0

Javascript 不能做多线程,这就是调用Gtk.main();会破坏 Cinnamon 的原因。
Cinnamon 小程序已经运行了一个主循环,并且调用Gtk.main();尝试创建另一个。
所以不可能直接在 Javascript 中从肉桂小程序打开 GtkWindow。
解决方案可能是通过 Python 脚本打开 GtkWindow,并使用 DBus 在 Cinnamon 小程序和 Python/GTK 窗口之间进行通信。

在 Cinnamon GitHub 中打开的问题

于 2013-03-20T12:01:42.523 回答
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/your_applet@you.org";
        //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;
    }

您可以将其复制/粘贴您的gtkfile.js 中。(将 #!/usr/bin/gjs 更改为 #!/usr/bin/cjs)

或者,这个(取自此处)(将 #!/usr/bin/gjs 更改为 #!/usr/bin/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);

我认为您不需要与刚刚启动的应用程序进行通信 :)

于 2014-06-21T09:13:33.807 回答