GitHub の gjs サンプルコードが更新されていることに気が付いた。
gjs/gtk-application.js at master ? GNOME/gjs ? GitHub
あれ、_init というメソッドが定義されていたの?
constructor を無理に使わなくてもいいってことみたい。
_init なら以前書いたみたく super() 以降を別関数に分けなくてもイケるのかな。
GtkApplication を使うなら Gtk.init() は不要だった。
実は Y901x を作っていた時に気がついていた、コッチに書くのを忘れていた。
constructor の引数は JSON しか受け付けなかったけど _init だとどうだ?
わかんない時はとっととサンプルコードを書いて動かすほうが速いので早速。
単なる Window ではつまらないので Evince のバインドで。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #!/usr/bin/gjs imports .gi.versions.Gtk = "3.0" ; imports .gi.versions.EvinceDocument = "3.0" ; imports .gi.versions.EvinceView = "3.0" ; const Gtk = imports .gi.Gtk; const Gdk = imports .gi.Gdk; const GObject = imports .gi.GObject; const EvinceDocument = imports .gi.EvinceDocument; const EvinceView = imports .gi.EvinceView; var EvinceWindow = GObject.registerClass({ GTypeName: "EvinceWindow" }, class EvinceWindow extends Gtk.Window { /*constructor(props={}) { super(props); } create() {*/ _init(text) { super ._init({title: text}); this .model = new EvinceView.DocumentModel(); let view = new EvinceView.View(); view.set_model( this .model); // Scroll let scroll = new Gtk.ScrolledWindow(); scroll.add(view); this .add(scroll); /* No need this.drag_dest_set( Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP, [Gtk.TargetEntry.new("text/uri-list", 0, 0)], Gdk.DragAction.MOVE );*/ this .drag_dest_add_uri_targets(); this .connect( "drag-data-received" , (widget, drag_context, x, y, data, info, time)=> { let uri = data.get_uris()[0]; let doc = EvinceDocument.Document.factory_get_document(uri); this .model.set_document(doc); }); //this.connect("hide", Gtk.main_quit); //arguments Error this .connect( "hide" , ()=>Gtk.main_quit()); this .show_all(); } }); // When GtkApplication is not used. Gtk.init( null ); EvinceDocument.init(); let ev = new EvinceWindow( "Drop the PDF File!" ); //ev.create(); Gtk.main(); |
なるほど、_init を使うほうが圧倒的に簡単になるやん。
ちなみに gir で Evince 等を使うドキュメントは以下に。
GNOME JavaScript Docs
それより drag_dest_set 指定はいつのまに不要になったのだ?
以前は drag_dest_add_uri_targets だけでは動作しなかった気がするんだが。