2013-03-15 1 views
0

Lorsque j'essaie d'ouvrir une GtkWindow à partir d'une applet de cannelle, le bureau entier se fige. Aucune erreur dans le fichier ~/.cinnamon/glass.log.Impossible d'ouvrir une GtkWindow à partir d'une applet de cannelle

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

Le code est exécuté jusqu'à Gtk.main() alors aucune fenêtre est affichée et le bureau se congelés.
Quelqu'un sait comment le faire fonctionner correctement?

+0

Je ne sais pas, mais êtes-vous sûr l'appel à 'Gtk.init' est vraiment nécessaire? Et de toute façon je pense que vous avez les paramètres échangés, il devrait être 'Gtk.init (0, null)'. – rodrigo

+0

Selon la documentation, il semble que vous ayez raison, mais l'utilisation de 'Gtk.init (0, null)' rend le type attendu utf8 pour Argument 'argv' mais a obtenu le type 'number' (nil) 'while' Gtk. init (null, 0) 'fonctionne bien dans un simple script Gjs (en dehors des applets de cannelle). En outre, il semble que je peux supprimer 'Gtk.init' de l'applet de cannelle sans que cela ne change. – Nicolas

Répondre

0

Javascript ne peut pas faire le multithreading, c'est pourquoi appeler Gtk.main(); casse la cannelle.
L'applet cannelle exécute déjà une boucle principale et l'appel de Gtk.main(); essaie d'en créer un autre.
Il n'est donc pas possible d'ouvrir une GtkWindow à partir d'une applet de cannelle directement en Javascript.
La solution pourrait être d'ouvrir un GtkWindow via un script Python et d'utiliser DBus pour communiquer entre l'applet Cinnamon et la fenêtre Python/GTK.

Opened issue in Cinnamon GitHub

0

Voici comment vous pouvez le faire:

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

Vous pouvez copier/coller dans this yourgtkfile.js. (Changer #!/Usr/bin/gjs aveC#!/Usr/bin/cjs)

Ou, celui-ci (tiré de here) (Changer #!/Usr/bin/gjs aveC#!/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); 

Je suppose que vous n'avez pas besoin de communiquer avec l'application vient de lancer :)

Questions connexes