From: Luc Chante <luc.cha...@gmail.com>
 Sent: Monday, 27 June 2016, 11:36
 Subject: Re: [Vala] getting started with vala and glade
   
I think it would be a good start to use gtk templates.
So you can use your ui file and create the ApplicationWindow by it's
constructor (which takes an Application instance).

Join to this mail a really simple example using ui file and gtk templates.
Tested with msys 2.


This is a great start for a code sample on the wiki, 
maybe:https://wiki.gnome.org/Projects/Vala/GTKTemplateSample
As a code sample I have a couple of doubts about the current coding style:
 - I think the main() method should be on its own, not buried in an object
 - For setting properties on the parent in a constructor what is wrong with   
this.property_name = xyz;   instead of using Vala specific GObject plumbing 
like Object( property_name: value );  Although these low level GObject features 
exist they add additional complexity when 
  initially learning the language and are unnecessary in most cases
So a revised main.vala:
/**
 * Application
 */
public class MyApplication : Gtk.Application
{
    public MyApplication(string? app_id = null, ApplicationFlags flags = 
ApplicationFlags.FLAGS_NONE)
    {
    if (app_id != null && Application.id_is_valid (app_id) == false) {
        error( "Application ID is not valid");
    }
    this.application_id = app_id;
    this.flags = flags;
    }
 
    public override void activate () {
        var window = new Template.ApplicationWindow (this);
        window.present ();
    }
}
 
/**
 * Application window
 */
[GtkTemplate(ui = "/template/window.ui")]
public class Template.ApplicationWindow : Gtk.ApplicationWindow
{
    [GtkChild]
    Gtk.Button test_button;
 
    public ApplicationWindow(Gtk.Application application)
    {
        this.application = application;
    }
 
    [GtkCallback]
    public void on_test_button_clicked(Gtk.Button button)
    {
        GLib.message("Button clicked !!!");
    }
}
 
/** Application entry point */
public static int main(string[] args)
{
    var app = new MyApplication ("my.application");
    return app.run (args);
}

Finally as a more generic way to compile use:
glib-compile-resources --sourcedir ./ --generate-source --target resources.c 
template.gresource.xmlvalac --pkg gtk+-3.0 --target-glib 2.38 --gresources 
template.gresource.xml resources.c main.vala --output myapp
The Makefile is nice though.
Regards,
Al

   
_______________________________________________
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to