在 Gjs Gtk.Application 的命令行中处理作为参数传递的文件
Handle files passed as arguments on the command line in a Gjs Gtk.Application
我正在尝试使用 Gjs which handles files passed as command line arguments 编写 GNOME GTK3 应用程序。为此,我连接 Gtk.Application
的 open
信号并设置 Gio.ApplicationFlags.HANDLES_OPEN
标志:
#!/usr/bin/gjs
const Gio = imports.gi.Gio
const Gtk = imports.gi.Gtk
const Lang = imports.lang
const MyApplication = new Lang.Class({
Name: 'MyApplication',
_init: function() {
this.application = new Gtk.Application({
application_id: 'com.example.my-application',
flags: Gio.ApplicationFlags.HANDLES_OPEN
})
this.application.connect('startup', this._onStartup.bind(this))
this.application.connect('open', this._onOpen.bind(this))
this.application.connect('activate', this._onActivate.bind(this))
},
_onStartup: function() {
log('starting application')
},
_onOpen: function(application, files) {
log('opening ' + files.length + ' files')
this._onStartup()
},
_onActivate: function() {
log('activating application')
}
})
let app = new MyApplication()
app.application.run(ARGV)
当我 运行 带有文件参数的程序时,我希望 _onOpen
被调用并传入 GFile
。但是 _onActivate
被调用,就像如果我 运行 它没有任何文件参数:
$ ./open-files.js open-files.js
Gjs-Message: JS LOG: starting application
Gjs-Message: JS LOG: activating application
我是运行ning gjs@1.44.
GJS ARGV
的定义方式与其他语言中的约定存在差异。例如,在 C 中,argv[0]
是程序的名称,第一个参数从 argv[1]
开始。在 GJS 中,程序的名称是 System.programInvocationName
,第一个参数是 ARGV[0]
.
不幸的是,作为 C 库的一部分,Gtk.Application
希望您根据 C 约定传入参数。你可以这样做:
ARGV.unshift(System.programInvocationName);
发生的事情是 ./open-files.js open-files.js
将 ['open-files.js']
设为 ARGV
,Gtk.Application
将其解释为程序名称,没有其他参数。如果你 运行 程序有两个文件参数,你会看到它 "opened" 只有第二个文件。
PS. 不幸的是,GJS 1.44 中似乎有一个错误阻止 open
信号正常工作。现在,我建议通过子类化 Gtk.Application
而不是代理它来解决这个问题。您的程序将如下所示:
const Gio = imports.gi.Gio
const Gtk = imports.gi.Gtk
const Lang = imports.lang
const System = imports.system
const MyApplication = new Lang.Class({
Name: 'MyApplication',
Extends: Gtk.Application,
_init: function(props={}) {
this.parent(props)
},
vfunc_startup: function() {
log('starting application')
this.parent()
},
vfunc_open: function(files, hint) {
log('opening ' + files.length + ' files')
},
vfunc_activate: function() {
log('activating application')
this.parent()
}
})
let app = new MyApplication({
application_id: 'com.example.my-application',
flags: Gio.ApplicationFlags.HANDLES_OPEN
})
ARGV.unshift(System.programInvocationName)
app.run(ARGV)
我正在尝试使用 Gjs which handles files passed as command line arguments 编写 GNOME GTK3 应用程序。为此,我连接 Gtk.Application
的 open
信号并设置 Gio.ApplicationFlags.HANDLES_OPEN
标志:
#!/usr/bin/gjs
const Gio = imports.gi.Gio
const Gtk = imports.gi.Gtk
const Lang = imports.lang
const MyApplication = new Lang.Class({
Name: 'MyApplication',
_init: function() {
this.application = new Gtk.Application({
application_id: 'com.example.my-application',
flags: Gio.ApplicationFlags.HANDLES_OPEN
})
this.application.connect('startup', this._onStartup.bind(this))
this.application.connect('open', this._onOpen.bind(this))
this.application.connect('activate', this._onActivate.bind(this))
},
_onStartup: function() {
log('starting application')
},
_onOpen: function(application, files) {
log('opening ' + files.length + ' files')
this._onStartup()
},
_onActivate: function() {
log('activating application')
}
})
let app = new MyApplication()
app.application.run(ARGV)
当我 运行 带有文件参数的程序时,我希望 _onOpen
被调用并传入 GFile
。但是 _onActivate
被调用,就像如果我 运行 它没有任何文件参数:
$ ./open-files.js open-files.js
Gjs-Message: JS LOG: starting application
Gjs-Message: JS LOG: activating application
我是运行ning gjs@1.44.
GJS ARGV
的定义方式与其他语言中的约定存在差异。例如,在 C 中,argv[0]
是程序的名称,第一个参数从 argv[1]
开始。在 GJS 中,程序的名称是 System.programInvocationName
,第一个参数是 ARGV[0]
.
不幸的是,作为 C 库的一部分,Gtk.Application
希望您根据 C 约定传入参数。你可以这样做:
ARGV.unshift(System.programInvocationName);
发生的事情是 ./open-files.js open-files.js
将 ['open-files.js']
设为 ARGV
,Gtk.Application
将其解释为程序名称,没有其他参数。如果你 运行 程序有两个文件参数,你会看到它 "opened" 只有第二个文件。
PS. 不幸的是,GJS 1.44 中似乎有一个错误阻止 open
信号正常工作。现在,我建议通过子类化 Gtk.Application
而不是代理它来解决这个问题。您的程序将如下所示:
const Gio = imports.gi.Gio
const Gtk = imports.gi.Gtk
const Lang = imports.lang
const System = imports.system
const MyApplication = new Lang.Class({
Name: 'MyApplication',
Extends: Gtk.Application,
_init: function(props={}) {
this.parent(props)
},
vfunc_startup: function() {
log('starting application')
this.parent()
},
vfunc_open: function(files, hint) {
log('opening ' + files.length + ' files')
},
vfunc_activate: function() {
log('activating application')
this.parent()
}
})
let app = new MyApplication({
application_id: 'com.example.my-application',
flags: Gio.ApplicationFlags.HANDLES_OPEN
})
ARGV.unshift(System.programInvocationName)
app.run(ARGV)