GTK4 application activates and opens on macOS Big Sur Finder "Open with" resulting in two windows

Hi I am a hobbyist new to Vala and GTK4, hoping to get some advice.

The Valadoc states:

What happens next depends on the flags: if g_application_handles_command_line was specified then the remaining commandline arguments are sent to the primary instance, where a command_line signal is emitted. Otherwise, the remaining commandline arguments are assumed to be a list of files. If there are no files listed, the application is activated via the activate signal. If there are one or more files, and g_application_handles_open was specified then the files are opened via the open signal.

I made a test app that creates a window under application.activate callback (“activate” window), and a window under application.open callback (“open” window); it uses the HANDLES_OPEN flag, and it sets register-session to true.

The test app presents the “activate” window when ran without arguments from the terminal. It presents the “open” window when ran with an argument from the terminal.

The problem in the topic heading happened after I bundled it into a macOS app and put it inside the macos Applications folder. When I click the test.app, it presents the “activate” window as expected. However, when I right-click on a file, and “open with” the test.app, it shows both the “activate” window and the “open” window.

This was done in Vala and I have not used any classes or subclassing (I do not know how). I wonder whether this is a macOS issue, or a problem with the vala binding, or problem with my code.

using Gtk;
using Poppler;

int 
main (string[] argv)
{
    GLib.File gfile = null;
    
    Gtk.Application application =
        new Gtk.Application (
            "org.test.www",
            GLib.ApplicationFlags.HANDLES_OPEN);

    // register with OSX to open file from Finder
    // must be done here and
    // cannot be done under startup
    Value val = Value (GLib.Type.BOOLEAN);
    val.set_boolean (true);
    application.set_property ("register-session", val);
    val.unset ();

    application.activate.connect (()=>{
        Gtk.ApplicationWindow window =
            new Gtk.ApplicationWindow (application);
        window.set_default_size (480, 360);

        Gtk.Label label =
            new Gtk.Label ("This is activate");
        window.set_child (label);

        window.present ();

    });
    application.open.connect ((files, hint)=>{
        gfile = files[0];

        Gtk.ApplicationWindow window =
            new Gtk.ApplicationWindow (application);
        window.set_default_size (480, 360);

        string formatted = "%s is Open.".printf (gfile.get_path ());
        Gtk.Label label =
            new Gtk.Label (formatted);
        window.set_child (label);

        window.present ();

    });

    var status = application.run (argv);
    return status;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.