当前位置: 代码迷 >> JavaScript >> 替基于GTK/CLUTTER的库增加GJS支持
  详细解决方案

替基于GTK/CLUTTER的库增加GJS支持

热度:226   发布时间:2012-07-05 07:59:18.0
为基于GTK/CLUTTER的库增加GJS支持

参考GObject Introspection官方网址:

http://live.gnome.org/GObjectIntrospection

gtk gjs教程:

http://zetcode.com/gui/javascriptgtktutorial/

?

? 在gnome shell中可以使用GJS来调用常用的GTK/CLUTTER/DBUS等库。这些库在编译的过程中通过生成.gir .typelib文件,使得gjs可以直接进行import。

这些文件的具体路径为:

/usr/share/gir-1.0/Gtk-3.0.gir

/usr/lib/girepository-1.0/Gtk-3.0.typelib

?

直接在gjs文件中的使用方法testgtk.js:

imports.gi.versions.Gtk = '2.0';
// GObject Introspection APIs are available from imports.gi.Modulename
const Gtk = imports.gi.Gtk

// JavaScript imports happen via imports.filename, where filename.js
// is assumed to be available in module/ folder

Gtk.init (0, null);

let win = new Gtk.Window({type: Gtk.WindowType.TOPLEVEL});
win.set_border_width(10);
win.connect('destroy', function (){
	Gtk.main_quit();
});

let button = new Gtk.Button({label: "Hello, world"});
button.connect('clicked', function (){
	button.set_label("Bar");
});

win.add(button);
button.show();
win.show();

Gtk.main();
??

运行:gjs testgtk.js


?

?

gir的层次结构:

BUILD TIME:

          +-----------------------+
          |   foo.c                                                   |
          |   foo.h                                                   |
          |                                                           |
          | Library sources, with type annotations                    |
          +-----------------------+
              |                                      |
             gcc                                 g-ir-scanner
              |                                      |
              |                                      V
              |                              +------------------------+
              |                              |    Foo.gir             |
              |                              |                        |
              |                              |    <GI-name>.gir       |
              |                              |                        |
              |                              | XML file               |
              |                              |                        |
              |                              | Invocation information |
              |                              | Required .gir files    |
              |                              | API docs               |
              |                              |                        |
              |                              +------------------------+
              |                                          |
              |                                     g-ir-compiler
              |                                          |
  DEPLOYMENT TIME:                                       |
              |                                          |
              V                                          V
        +-----------------------------+      +---------------------------+
        |   libfoo.so                 |      |    Foo.typelib            |
        |                             |      |                           |
        |                             |      | Binary version of the     |
        |  ELF file                   |      | invocation info and       |
        |                             |      | required .typelib files   |
        |  Machine code, plus         |      +---------------------------+
        | dynamic linkage information |                  A
        | (DWARF debug data, etc)     |                  |
        +-----------------------------+                  |
                     A                                   |
                     |                       +---------------------------+
                     |                       |  libgirepository.so       |
                +-----------+                |                           |
                | libffi.so |                |  Can read typelibs and    |
                |           |<-------+------>|  present them in a        |
                +-----------+        |       |  libffi-based way         |
                                     |       |                           |
                                     |       +---------------------------+
                                     |
                                     |
                           +----------------------------+
                           | Specific language bindings |
                           +----------------------------+