GTK tray example in Vala

If you are starting to implement your own GUI application I highly recommend to look at the Vala language. This is a killer language for GTK.

As a vala example I decided to implement a small application that shows how to work with Tray. The example shows how to handle left/right click, has a menu with “about” window and exit item. It is almost a real application 🙂

Ok, here is the promised code:

using Gtk;

public class Main {

  class AppStatusIcon : Window {
    private StatusIcon trayicon;
    private Menu menuSystem;

    public AppStatusIcon() {
      /* Create tray icon */
      trayicon = new StatusIcon.from_stock(Stock.HOME);
      trayicon.set_tooltip_text ("Tray");



    /* Create menu for right button */
    public void create_menuSystem() {
      menuSystem = new Menu();
      var menuAbout = new ImageMenuItem.from_stock(Stock.ABOUT, null);
      var menuQuit = new ImageMenuItem.from_stock(Stock.QUIT, null);

    /* Show popup menu on right button */
    private void menuSystem_popup(uint button, uint time) {
      menuSystem.popup(null, null, null, button, time);

    private void about_clicked() {
      var about = new AboutDialog();
      about.set_comments("Tray utility");

  public static int main (string[] args) {
    Gtk.init(ref args);
    var App = new AppStatusIcon();
    return 0;

To compile it run

valac --pkg=gtk+-2.0 tray.vala

The output is a small highly-optimized binary, very fast and requires a tiny amount of resources. It is what users expect from your application.


8 responses to this post.

  1. Posted by dan on November 2, 2011 at 21:49

    Anatol, thanks for posting this example.

    It certainly compiles easily enough on my system (Ubuntu 11.10).

    It did have some run time errors, of the form:
    Unable to locate theme engine in module_path: “pixmap”

    I did a google search, and was able to fix this by
    sudo apt-get install gtk2-engines-pixbuf

    But on my system, even though it is presumably running
    if i execute ./tray, there’s no visible feedback (no window,
    not even a flash).

    Probably just some thoughtless blunder on my part, but would
    appreciate any clues anybody has to offer.

    My vala is version 0.14.0.



  2. Hi, yeah I see this issue on Ubuntu 11.10 as well. I think this is related to gnome3/unity somehow I’ll try to find some time and figure out what is going on there. Also it makes sense to use gtk3 now (this is default in 11.10).


    • Posted by dan on November 3, 2011 at 08:03

      Thanks Anatol. I hope to try it out on an older ubuntu tonight, and use gtk3 on 11.10. I appreciate your help as i am a total newbie at this.


      • > if i execute ./tray, there’s no visible feedback

        This is because we intentionally hide the windows, check the line “App.hide();”, if you want to see the window then replace this line with “App.show_all();” and add “this.destroy.connect(Gtk.main_quit);” to the constructor to handle close button correctly.

        But I do not see the tray icon. Honestly I don’t know why. I think it is better to contact vala maillist and politely ask them to update page in their wiki to gtk3.

      • Posted by dan on November 3, 2011 at 21:16

        Thanks Anatol. The modifications you suggest do make the window visible and the close button functional.

        I also tried it out on an earlier ubuntu system (10.10), but for that system, the valac compiler’s version was probably too low (0.10.0), so there were compilation errors.
        (For reference, they were of the form: The name `Stock’ does not exist in the context of `’.)

        If i can get to the point that i know more what i’m doing i could bring up the question to the vala mailing list (i’m far too much of a super-neophyte now :)).

        If i may ask, what system (distro and version number) are you using (or did you use)?

        Thanks for fielding all these questions and investigating the matter.

        I’m trying to settle in on a graphics/gui system to program in for gnu/linux, and vala seems very promising, but still doing so due diligence.

      • Hi, I tested it on Ubuntu 11.10.

      • Posted by dan on November 3, 2011 at 21:56

        Thanks for the info, so that it sounds like we have the same results — but did you originally develop on ubuntu? (Which i guess if you did, it would have been 11.04?)

      • AFAIK originally it was written for 10.04 LTS. As of “Stock” – this is code specific for vala 0.12. If you compile it with vala older than 0.10 then Stock.HOME should be replaced with STOCK_HOME

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: