hugopl / gtk4.cr

GTK4 bindings for Crystal
https://hugopl.github.io/gtk4.cr/
MIT License
102 stars 8 forks source link

Glib seems to expect the first parameter of ARGV to be the app name #21

Closed BlobCodes closed 2 years ago

BlobCodes commented 2 years ago

Using the following code:

require "gtk4"

app = Gtk::Application.new("test.test", Gio::ApplicationFlags::None)
exit(app.run(ARGV))

In the command line (app built as test2 in this example):

[blobcodes@toolbox mangaba]$ ./test2

(test2:23128): GLib-GIO-WARNING **: 16:10:17.211: Your application does not implement g_application_activate() and has no handlers connected to the 'activate' signal.  It should do one of these.
⬢[blobcodes@toolbox mangaba]$ ./test2 my_app_name

(my_app_name:23157): GLib-GIO-WARNING **: 16:10:35.782: Your application does not implement g_application_activate() and has no handlers connected to the 'activate' signal.  It should do one of these.
⬢[blobcodes@toolbox mangaba]$ ./test2 --help

(--help:23191): GLib-GIO-WARNING **: 16:10:52.199: Your application does not implement g_application_activate() and has no handlers connected to the 'activate' signal.  It should do one of these.
⬢[blobcodes@toolbox mangaba]$ ./test2 my_app_name --help
Usage:
  my_app_name [OPTION…]

Help Options:
  -h, --help                 Show help options
  --help-all                 Show all help options
  --help-gapplication        Show GApplication options

Using test2 --help did not open the help dialog but instead showed in the error message that our app is called --help.

To fix this, we would need to pass ARGV_UNSAFE to app.run, which we cannot because Pointer is not Enumerable.

We would need something like this:

require "gtk4"

app = Gtk::Application.new("test.test", Gio::ApplicationFlags::None)

argv_unsafe_as_enumerable = Array.new(ARGC_UNSAFE) do |i|
  String.new(ARGV_UNSAFE[i])
end
exit(app.run(argv_unsafe_as_enumerable))

..or:

require "gtk4"

app = Gtk::Application.new("test.test", Gio::ApplicationFlags::None)
exit(app.run([PROGRAM_NAME, *ARGV]))

So the examples need to be changed and it would be useful to be able to pass ARGV_UNSAFE directly to app.run. But I don't really know what's the best way to deal with this issue.

hugopl commented 2 years ago

Fixed in the patch above.

I added a version of #run that receives no parameters and pass ARGC_UNSAFE and ARGV_UNSAFE.

And for the overload that accepts Enumarable(String)? I append the PROGRAM_NAME before calling the C function.