• 8
  • Nov

Wow!GTK+真是灵活过头了,C、C++、C#、Java、Python、Perl……上述的语言都可以用来写GTK+应用程序,现在,JavaScript(WebKit JavaScriptCore)也可以了!

这个项目叫做:Seed

------

简单的说,Seed是基于GObject Introspection(GObject自省)的一个工程,通过它,可以用JavaScript来写GTK+应用程序。一般用于在WebKit(GTK)中用JS进行操作界面吧。

下面是例子代码示例:

一、构造器

Clutter这个较新的工程也有绑定,不错!

timeline = new Clutter.Timeline({fps:60, num_frames:30});

二、方法和属性

将控件的事件设置成Gdk所有事件

widget.events = Gdk.EventMask.all_events_mask;
description = widget.create_pango_context();

三、信号连接

这个信号的作用是利用JS的get_uri方法设置地址

var update_url = function (web_view, web_frame, url_entry)
{
    url_entry.text = web_frame.get_uri();
}

browser_view.signal.load_commited.connect(update_url, entry);

四、定义新对象

喔!干净利落,比用C语言定义一个对象要方便多了!

HelloLabel = new GType({
    parent: Gtk.Label,
    name: "HelloLabel",
    instance_init: function(klass)
    {
        this.text = "Hello" // Hello labels always say hello.
    },
    class_init: function(klass, prototype)
    {
        // Add a say goodbye function to all instances.
        prototype.say_goodbye = function(){this.text = "Goodbye"};
        klass.install_signal({name: "say-hello"});
    }
});
/* Later */
label1 = new HelloLabel();
label1.signal.say_hello.emit();

5、错误处理

异常就是GError

try
{
    var file = Gio.file_new_for_path("/tmp");
    var fstream = file.replace();
}
catch (e)
{
    // e.name is equal to GIoError.
    // e.message is equal to "Error opening file '/tmp/': Is a directory
    // Also has source file/line number, as line and sourceURL properties
}

真是很强大!不知道能用这个搞出些什么应用来,好好动动脑袋,可惜JS还不怎么会。

» You can leave a comment.

10 Comments

  1. 那它是给Webkits打patch实现?er...

  2. 啥时候给blog加一个邮件通知的插件?

  3. 嗯。为了方便与读者交互哈。

    • 山猫

      其实先前也有个 gjs ,
      不过老 gjs 是使用 firefox 的 spidermonkey 干的,

      比现在 JavaScriptCore 的要差

Leave a Comment