Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gtk.Application in Genie

Tags:

gtk

gtk3

genie

I can not find information or examples about "Gtk.Application" in Genie.

What is the correct way of using the Gtk.Application class in Genie?

Good day and thank you

EDIT: I do not know if this is the best way to do it, but my code is this:

// compila con valac --pkg gtk+-3.0 nombre_archivo.gs
uses Gtk

class MyApplication : Gtk.Application   
    def override activate ()        
        var window = new Gtk.ApplicationWindow (this)
        window.title = "Welcome to GNOME"
        window.set_default_size (400, 400)
        window.show ()

init
    new MyApplication ().run (args)
like image 812
Webierta Avatar asked Jan 31 '17 21:01

Webierta


1 Answers

You're example looks like a pretty good start to me, but I think you should add an application ID and some application flags.

Three good resources are the GTK+3 Reference Manual's documentation for GtkApplication, the GNOME Wiki "HowDoI" section's page called "Using GtkApplication" and the GIO Reference Manual's documentation for GApplication. GApplication, or GLib.Application in the Vala binding, is the parent class for GtkApplication.

The "HowDoI" page advises:

GtkApplication does not implement main() for you. You must do so yourself. Your main() function should be as small as possible and do almost nothing except creating your GtkApplication and running it. The "real work" should always be done in response to the signals fired by GtkApplication.

Your main() function in Genie is:

init
    new MyApplication().run( args )

and that's about as simple as you can get.

The "HowDoI" page also advises:

When your application starts, the startup signal will be fired. This gives you a chance to perform initialisation tasks that are not directly related to showing a new window. After this, depending on how the application is started, either activate or open will be called next.

You're not doing any start up tasks with your example, which is fine. So there is no need to use the startup signal, but you are using the activate signal by overriding a virtual function with def override activate (). activate is effectively the default signal when the Gtk.Application runs, but alternative signals can be emitted when the appropriate ApplicatonFlags are set. For example if the HANDLES_OPEN flag is set then the open signal will be sent if there are unparsed command line arguments. The unparsed arguments are taken to be filenames or URIs. The default flags are FLAGS_NONE and that will be made explicit in the example code later.

The GTK+3 Reference Manual's section on GtkApplication states:

Currently, GtkApplication handles GTK+ initialization, application uniqueness, session management, provides some basic scriptability and desktop shell integration by exporting actions and menus and manages a list of toplevel windows whose life-cycle is automatically tied to the life-cycle of your application...If no application ID is given then some features (most notably application uniqueness) will be disabled. A null application ID is only allowed with GTK+ 3.6 or later.

The application ID should be made up of at least two names separated by a dot. If the application is run a second time then the second instance's window becomes part of the first application, but the second application instance is then closed. This is the application uniqueness feature and can be disabled using ApplicationFlags.NON_UNIQUE. The application is registered on the session bus using the application ID. If you are using Linux you can use a tool like D-Feet to see the application appear on the session bus and also what happens when you run the application again (you need to refresh the view).

Time for some code:

// compila con valac --pkg gtk+-3.0 nombre_archivo.gs
[indent=4]
uses Gtk

init
    new MyApplication( "org.genie.Example.SimpleGtkApplication",
                     ApplicationFlags.FLAGS_NONE
                     ).run( args )

class MyApplication:Gtk.Application
    construct( application_id:string, flags:ApplicationFlags )
        if !id_is_valid( application_id )
            error( "application id %s is not valid", application_id )
        this.application_id = application_id
        this.flags = flags

    def override activate ()
        var window = new Gtk.ApplicationWindow( this )
        window.title = "Welcome to GNOME"
        window.set_default_size( 400, 400 )
        window.show_all()

This adds an application ID and makes the ApplicationFlags explicit.

like image 105
AlThomas Avatar answered Oct 04 '22 04:10

AlThomas